NCBI C++ ToolKit
psgs_io_callbacks.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef PSGS_IO_CALLBACKS__HPP
2 #define PSGS_IO_CALLBACKS__HPP
3 
4 /* $Id: psgs_io_callbacks.hpp 98308 2022-10-26 17:48:52Z satskyse $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Sergey Satskiy
30  *
31  * File Description: PSG server processor callbacks for async I/O
32  *
33  */
34 
35 #include <uv.h>
36 
37 #include <functional>
38 #include <string>
39 using namespace std;
40 
41 
42 // Asynchronous callback on a socket event.
43 // The instance must live while the user is still interested in the events.
44 // The processor instance must live longer than the callback class instance.
46 {
47  public:
48  // The socket events on which asyncronous callbacks are supported
49  enum EPSGS_Event {
53  ePSGS_Prioritized
54  };
55 
56  // All callbacks must tell what to do with the polled socket:
57  // - to continue: the timer will be restarted
58  // - to stop polling: the timer will be stopped and it is safe to
59  // destroy the callback object
62  ePSGS_ContinuePolling
63  };
64 
65  using TEventCB = function<EPSGS_PollContinue(void * user_data)>;
66  using TTimeoutCB = function<EPSGS_PollContinue(void * user_data)>;
67  using TErrorCB = function<EPSGS_PollContinue(const string & message,
68  void * user_data)>;
69 
70  // Starts polling the given fd for the given event
71  CPSGS_SocketIOCallback(uv_loop_t * loop,
72  size_t request_id,
73  int fd,
74  EPSGS_Event event,
75  uint64_t timeout_millisec,
76  void * user_data,
77  TEventCB event_cb,
78  TTimeoutCB timeout_cb,
79  TErrorCB error_cb);
80 
82 
86 
87  void StartPolling(void);
88  void StopPolling(void);
89  bool IsSafeToDelete(void);
90 
91  public:
92  void x_UvOnTimer(void);
93  void x_UvOnSocketEvent(int status);
94  void x_UvTimerHandleClosedEvent(void);
95  void x_UvSocketPollHandleClosedEvent(void);
96 
97  private:
98  void x_StartPolling(void);
99  void x_StopPolling(void);
100  void x_StartTimer(void);
101  void x_StopTimer(void);
102  void x_RestartTimer(void);
103 
104  private:
105  uv_loop_t * m_Loop;
106  size_t m_RequestId;
107  int m_FD;
109  uv_timer_t m_TimerReq;
112  uv_poll_t m_PollReq;
116 
117  void * m_UserData;
121 };
122 
123 
124 #endif // PSGS_IO_CALLBACKS__HPP
125 
CPSGS_SocketIOCallback(const CPSGS_SocketIOCallback &)=delete
CPSGS_SocketIOCallback & operator=(const CPSGS_SocketIOCallback &)=delete
function< EPSGS_PollContinue(void *user_data)> TEventCB
function< EPSGS_PollContinue(void *user_data)> TTimeoutCB
CPSGS_SocketIOCallback(CPSGS_SocketIOCallback &&)=delete
function< EPSGS_PollContinue(const string &message, void *user_data)> TErrorCB
Uint8 uint64_t
Modified on Fri Sep 20 14:58:25 2024 by modify_doxy.py rev. 669887