|
a/src/utils/netcon.h |
|
b/src/utils/netcon.h |
|
... |
|
... |
4 |
#include "refcntr.h"
|
4 |
#include "refcntr.h"
|
5 |
|
5 |
|
6 |
/// A set of classes to manage client-server communication over a
|
6 |
/// A set of classes to manage client-server communication over a
|
7 |
/// connection-oriented network, or a pipe.
|
7 |
/// connection-oriented network, or a pipe.
|
8 |
///
|
8 |
///
|
9 |
/// Currently only uses TCP. The classes include client-side and
|
9 |
/// The listening/connection-accepting code currently only uses
|
10 |
/// server-side (accepting) endpoints, and server-side code to handle
|
10 |
/// TCP. The classes include client-side and server-side (accepting)
|
11 |
/// a set of client connections in parallel.
|
11 |
/// endpoints. Netcon also has server-side static code to handle a set
|
|
|
12 |
/// of client connections in parallel. This should be moved to a
|
|
|
13 |
/// friend class.
|
12 |
///
|
14 |
///
|
13 |
/// The client data transfer class can also be used for
|
15 |
/// The client data transfer class can also be used for
|
14 |
/// timeout-protected/asynchronous io using a given fd.
|
16 |
/// timeout-protected/asynchronous io using a given fd (ie a pipe
|
|
|
17 |
/// descriptor)
|
15 |
|
18 |
|
16 |
/// Base class for all network endpoints:
|
19 |
/// Base class for all network endpoints:
|
17 |
class Netcon;
|
20 |
class Netcon;
|
18 |
typedef RefCntr<Netcon> NetconP;
|
21 |
typedef RefCntr<Netcon> NetconP;
|
19 |
|
22 |
|
20 |
class Netcon {
|
23 |
class Netcon {
|
21 |
public:
|
24 |
public:
|
22 |
enum Event {NETCONPOLL_READ = 0x1, NETCONPOLL_WRITE=0x2};
|
25 |
enum Event {NETCONPOLL_READ = 0x1, NETCONPOLL_WRITE=0x2};
|
23 |
Netcon()
|
26 |
Netcon()
|
24 |
: m_peer(0), m_fd(-1), m_didtimo(0), m_wantedEvents(0)
|
27 |
: m_peer(0), m_fd(-1), m_ownfd(true), m_didtimo(0), m_wantedEvents(0)
|
25 |
{}
|
28 |
{}
|
26 |
virtual ~Netcon();
|
29 |
virtual ~Netcon();
|
27 |
/// Remember whom we're talking to. We let external code do this because
|
30 |
/// Remember whom we're talking to. We let external code do this because
|
28 |
/// the application may have a non-dns method to find the peer name.
|
31 |
/// the application may have a non-dns method to find the peer name.
|
29 |
virtual void setpeer(const char *hostname);
|
32 |
virtual void setpeer(const char *hostname);
|
|
... |
|
... |
63 |
|
66 |
|
64 |
/// The selectloop interface is used to implement parallel servers.
|
67 |
/// The selectloop interface is used to implement parallel servers.
|
65 |
/// All the interface is static (independant of any given object).
|
68 |
/// All the interface is static (independant of any given object).
|
66 |
|
69 |
|
67 |
/// Loop waiting for events on the connections and call the
|
70 |
/// Loop waiting for events on the connections and call the
|
68 |
/// cando() method on the object where something happens (this will in
|
71 |
/// cando() method on the object when something happens (this will in
|
69 |
/// turn typically call the app callback set on the netcon). Possibly
|
72 |
/// turn typically call the app callback set on the netcon). Possibly
|
70 |
/// call the periodic handler (if set) at regular intervals.
|
73 |
/// call the periodic handler (if set) at regular intervals.
|
71 |
/// @return -1 for error. 0 if no descriptors left for i/o. 1 for periodic
|
74 |
/// @return -1 for error. 0 if no descriptors left for i/o. 1 for periodic
|
72 |
/// timeout (should call back in after processing)
|
75 |
/// timeout (should call back in after processing)
|
73 |
static int selectloop();
|
76 |
static int selectloop();
|
|
... |
|
... |
87 |
|
90 |
|
88 |
/// Set a function to be called periodically, or a time before return.
|
91 |
/// Set a function to be called periodically, or a time before return.
|
89 |
/// @param handler the function to be called.
|
92 |
/// @param handler the function to be called.
|
90 |
/// - if it is 0, selectloop() will return after ms mS (and can be called
|
93 |
/// - if it is 0, selectloop() will return after ms mS (and can be called
|
91 |
/// again
|
94 |
/// again
|
92 |
/// - if it is not 0, it will be called at ms mS intervals. If its return
|
95 |
/// - if it is not 0, it will be called at ms mS intervals. If its return
|
93 |
/// value is <= 0, selectloop will return.
|
96 |
/// value is <= 0, selectloop will return.
|
94 |
/// @param clp client data to be passed to handler at every call.
|
97 |
/// @param clp client data to be passed to handler at every call.
|
95 |
/// @param ms milliseconds interval between handler calls or before return.
|
98 |
/// @param ms milliseconds interval between handler calls or
|
96 |
/// set ms to 0 for no periodic handler
|
99 |
/// before return. Set to 0 for no periodic handler.
|
97 |
static void setperiodichandler(int (*handler)(void *), void *clp, int ms);
|
100 |
static void setperiodichandler(int (*handler)(void *), void *clp, int ms);
|
98 |
|
101 |
|
99 |
protected:
|
102 |
protected:
|
100 |
static bool o_selectloopDoReturn;
|
103 |
static bool o_selectloopDoReturn;
|
101 |
static int o_selectloopReturnValue;
|
104 |
static int o_selectloopReturnValue;
|
102 |
char *m_peer; // Name of the connected host
|
105 |
char *m_peer; // Name of the connected host
|
103 |
int m_fd;
|
106 |
int m_fd;
|
|
|
107 |
bool m_ownfd;
|
104 |
int m_didtimo;
|
108 |
int m_didtimo;
|
105 |
// Used when part of the selectloop map.
|
109 |
// Used when part of the selectloop map.
|
106 |
short m_wantedEvents;
|
110 |
short m_wantedEvents;
|
107 |
// Method called by the selectloop when something can be done with a netcon
|
111 |
// Method called by the selectloop when something can be done with a netcon
|
108 |
virtual int cando(Netcon::Event reason) = 0;
|
112 |
virtual int cando(Netcon::Event reason) = 0;
|
109 |
};
|
113 |
};
|
|
... |
|
... |
123 |
virtual ~NetconWorker() {}
|
127 |
virtual ~NetconWorker() {}
|
124 |
// NetconP holds a NetconData oeuf corse
|
128 |
// NetconP holds a NetconData oeuf corse
|
125 |
virtual int data(NetconData *con, Netcon::Event reason) = 0;
|
129 |
virtual int data(NetconData *con, Netcon::Event reason) = 0;
|
126 |
};
|
130 |
};
|
127 |
|
131 |
|
128 |
/// Base class for connections that actually transfer data.
|
132 |
/// Base class for connections that actually transfer data. T
|
129 |
class NetconData : public Netcon {
|
133 |
class NetconData : public Netcon {
|
130 |
public:
|
134 |
public:
|
131 |
NetconData() : m_buf(0), m_bufbase(0), m_bufbytes(0), m_bufsize(0)
|
135 |
NetconData() : m_buf(0), m_bufbase(0), m_bufbytes(0), m_bufsize(0)
|
132 |
{}
|
136 |
{}
|
133 |
virtual ~NetconData();
|
137 |
virtual ~NetconData();
|
|
... |
|
... |
140 |
/// error occurred.
|
144 |
/// error occurred.
|
141 |
virtual int send(const char *buf, int cnt, int expedited = 0);
|
145 |
virtual int send(const char *buf, int cnt, int expedited = 0);
|
142 |
|
146 |
|
143 |
/// Read from the connection
|
147 |
/// Read from the connection
|
144 |
/// @param buf the data buffer
|
148 |
/// @param buf the data buffer
|
145 |
/// @param cnt the number of bytes we should try to read
|
149 |
/// @param cnt the number of bytes we should try to read (but we return
|
|
|
150 |
/// as soon as we get data)
|
146 |
/// @param timeo maximum number of seconds we should be waiting for data.
|
151 |
/// @param timeo maximum number of seconds we should be waiting for data.
|
147 |
/// @return the count of bytes actually read. 0 for timeout (call
|
152 |
/// @return the count of bytes actually read. 0 for timeout (call
|
148 |
/// didtimo() to discriminate from EOF). -1 if an error occurred.
|
153 |
/// didtimo() to discriminate from EOF). -1 if an error occurred.
|
149 |
virtual int receive(char *buf, int cnt, int timeo = -1);
|
154 |
virtual int receive(char *buf, int cnt, int timeo = -1);
|
150 |
/// Loop on receive until cnt bytes are actually read or a timeout occurs
|
155 |
/// Loop on receive until cnt bytes are actually read or a timeout occurs
|
|
... |
|
... |
170 |
|
175 |
|
171 |
/// Network endpoint, client side.
|
176 |
/// Network endpoint, client side.
|
172 |
class NetconCli : public NetconData {
|
177 |
class NetconCli : public NetconData {
|
173 |
public:
|
178 |
public:
|
174 |
NetconCli(int silent = 0) {m_silentconnectfailure = silent;}
|
179 |
NetconCli(int silent = 0) {m_silentconnectfailure = silent;}
|
|
|
180 |
|
175 |
/// Open connection to specified host and named service.
|
181 |
/// Open connection to specified host and named service.
|
176 |
int openconn(const char *host, char *serv, int timeo = -1);
|
182 |
int openconn(const char *host, char *serv, int timeo = -1);
|
|
|
183 |
|
177 |
/// Open connection to specified host and numeric port. port is in
|
184 |
/// Open connection to specified host and numeric port. port is in
|
178 |
/// HOST byte order
|
185 |
/// HOST byte order
|
179 |
int openconn(const char *host, unsigned int port, int timeo = -1);
|
186 |
int openconn(const char *host, unsigned int port, int timeo = -1);
|
|
|
187 |
|
|
|
188 |
/// Reuse existing fd.
|
180 |
/// Reuse existing fd. We DONT take ownership of the fd, and do no closin'
|
189 |
/// We DONT take ownership of the fd, and do no closin' EVEN on an
|
181 |
/// EVEN on an explicit closeconn() (use getfd(), close, setconn(-1)).
|
190 |
/// explicit closeconn() or setconn() (use getfd(), close,
|
|
|
191 |
/// setconn(-1) if you need to really close the fd and have no
|
|
|
192 |
/// other copy).
|
182 |
int setconn(int fd);
|
193 |
int setconn(int fd);
|
|
|
194 |
|
183 |
/// Do not log message if openconn() fails.
|
195 |
/// Do not log message if openconn() fails.
|
184 |
void setSilentFail(int onoff) {m_silentconnectfailure = onoff;}
|
196 |
void setSilentFail(int onoff) {m_silentconnectfailure = onoff;}
|
|
|
197 |
|
185 |
private:
|
198 |
private:
|
186 |
int m_silentconnectfailure; // No logging of connection failures if set
|
199 |
int m_silentconnectfailure; // No logging of connection failures if set
|
187 |
};
|
200 |
};
|
188 |
|
201 |
|
189 |
class NetconServCon;
|
202 |
class NetconServCon;
|