00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00043 #ifndef CCXX_SOCKETPORT_H_
00044 #define CCXX_SOCKETPORT_H_
00045
00046 #ifndef CCXX_ADDRESS_H_
00047 #include <cc++/address.h>
00048 #endif
00049
00050 #ifndef CCXX_SOCKET_H_
00051 #include <cc++/socket.h>
00052 #endif
00053
00054 #ifdef CCXX_NAMESPACES
00055 namespace ost {
00056 #endif
00057
00058 class __EXPORT SocketPort;
00059 class __EXPORT SocketService;
00060
00080 class __EXPORT SocketPort : public Socket, public TimerPort
00081 {
00082 private:
00083 SocketPort *next, *prev;
00084 SocketService *service;
00085 #ifndef WIN32
00086 struct timeval porttimer;
00087 #ifdef USE_POLL
00088 struct pollfd * ufd;
00089 #endif
00090 #else
00091 HANDLE event;
00092 #endif
00093 bool detect_pending;
00094 bool detect_output;
00095 bool detect_disconnect;
00096
00097 friend class SocketService;
00098
00099 protected:
00108 SocketPort(SocketService *svc, TCPSocket &tcp);
00109 #ifdef CCXX_IPV6
00110 SocketPort(SocketService *svc, TCPV6Socket &tcp);
00111 #endif
00112
00121 SocketPort(SocketService *svc, const IPV4Address &ia, tpport_t port);
00122 #ifdef CCXX_IPV6
00123 SocketPort(SocketService *svc, const IPV6Address &ia, tpport_t port);
00124 #endif
00125
00139 SocketPort(SocketService *svc, const IPV4Host &ih, tpport_t port);
00140 #ifdef CCXX_IPV6
00141 SocketPort(SocketService *svc, const IPV6Host &ih, tpport_t port);
00142 #endif
00143
00149 void attach( SocketService* svc );
00150
00151
00156 virtual ~SocketPort();
00157
00162 void setDetectPending( bool );
00163
00167 bool getDetectPending( void ) const
00168 { return detect_pending; }
00169
00174 void setDetectOutput( bool );
00175
00179 bool getDetectOutput( void ) const
00180 { return detect_output; }
00181
00186 virtual void expired(void);
00187
00192 virtual void pending(void);
00193
00198 virtual void output(void);
00199
00204 virtual void disconnect(void);
00205
00216 Error connect(const IPV4Address &ia, tpport_t port);
00217 #ifdef CCXX_IPV6
00218 Error connect(const IPV6Address &ia, tpport_t port);
00219 #endif
00220
00230 inline ssize_t send(const void *buf, size_t len)
00231 {return _IORET64 ::send(so, (const char *)buf, _IOLEN64 len, 0);};
00232
00241 inline ssize_t receive(void *buf, size_t len)
00242 {return _IORET64 ::recv(so, (char *)buf, _IOLEN64 len, 0);};
00243
00252 inline ssize_t peek(void *buf, size_t len)
00253 {return _IORET64 ::recv(so, (char *)buf, _IOLEN64 len, MSG_PEEK);};
00254
00255 public:
00263 void setTimer(timeout_t timeout = 0);
00264
00272 void incTimer(timeout_t timeout);
00273 };
00274
00287 class __EXPORT SocketService : public Thread, private Mutex
00288 {
00289 private:
00290 #ifndef WIN32
00291 fd_set connect;
00292 int iosync[2];
00293 int hiwater;
00294 #else
00295
00296 class Sync;
00297 Sync* sync;
00298 #endif
00299 int volatile count;
00300 SocketPort* volatile first, *last;
00301
00307 void attach(SocketPort *port);
00313 void detach(SocketPort *port);
00314
00318 void run(void);
00319
00320 friend class SocketPort;
00321
00322 protected:
00328 virtual void onUpdate(unsigned char buf);
00329
00335 virtual void onEvent(void);
00336
00344 virtual void onCallback(SocketPort *port);
00345
00346 public:
00357 void update(unsigned char flag = 0xff);
00358
00367 SocketService(int pri = 0, size_t stack = 0, const char *id = NULL);
00368
00373 virtual ~SocketService();
00374
00381 inline int getCount(void) const
00382 {return count;};
00383 };
00384
00385 #ifdef CCXX_NAMESPACES
00386 }
00387 #endif
00388
00389 #endif
00390