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
00038
00044 #ifndef CCXX_SOCKETPORT_H_
00045 #define CCXX_SOCKETPORT_H_
00046
00047 #ifndef CCXX_ADDRESS_H_
00048 #include <cc++/address.h>
00049 #endif
00050
00051 #ifndef CCXX_SOCKET_H_
00052 #include <cc++/socket.h>
00053 #endif
00054
00055 #ifdef CCXX_NAMESPACES
00056 namespace ost {
00057 #endif
00058
00059 class __EXPORT SocketPort;
00060 class __EXPORT SocketService;
00061
00081 class __EXPORT SocketPort : public Socket, public TimerPort
00082 {
00083 private:
00084 SocketPort *next, *prev;
00085 SocketService *service;
00086 #ifndef WIN32
00087 struct timeval porttimer;
00088 #ifdef USE_POLL
00089 struct pollfd * ufd;
00090 #endif
00091 #else
00092 HANDLE event;
00093 #endif
00094 bool detect_pending;
00095 bool detect_output;
00096 bool detect_disconnect;
00097
00098 friend class SocketService;
00099
00100 protected:
00109 SocketPort(SocketService *svc, TCPSocket &tcp);
00110 #ifdef CCXX_IPV6
00111 SocketPort(SocketService *svc, TCPV6Socket &tcp);
00112 #endif
00113
00122 SocketPort(SocketService *svc, const IPV4Address &ia, tpport_t port);
00123 #ifdef CCXX_IPV6
00124 SocketPort(SocketService *svc, const IPV6Address &ia, tpport_t port);
00125 #endif
00126
00140 SocketPort(SocketService *svc, const IPV4Host &ih, tpport_t port);
00141 #ifdef CCXX_IPV6
00142 SocketPort(SocketService *svc, const IPV6Host &ih, tpport_t port);
00143 #endif
00144
00150 void attach( SocketService* svc );
00151
00152
00157 virtual ~SocketPort();
00158
00163 void setDetectPending( bool );
00164
00168 bool getDetectPending( void ) const
00169 { return detect_pending; }
00170
00175 void setDetectOutput( bool );
00176
00180 bool getDetectOutput( void ) const
00181 { return detect_output; }
00182
00187 virtual void expired(void);
00188
00193 virtual void pending(void);
00194
00199 virtual void output(void);
00200
00205 virtual void disconnect(void);
00206
00217 Error connect(const IPV4Address &ia, tpport_t port);
00218 #ifdef CCXX_IPV6
00219 Error connect(const IPV6Address &ia, tpport_t port);
00220 #endif
00221
00231 inline ssize_t send(const void *buf, size_t len)
00232 {return _IORET64 ::send(so, (const char *)buf, _IOLEN64 len, 0);};
00233
00242 inline ssize_t receive(void *buf, size_t len)
00243 {return _IORET64 ::recv(so, (char *)buf, _IOLEN64 len, 0);};
00244
00253 inline ssize_t peek(void *buf, size_t len)
00254 {return _IORET64 ::recv(so, (char *)buf, _IOLEN64 len, MSG_PEEK);};
00255
00256 public:
00264 void setTimer(timeout_t timeout = 0);
00265
00273 void incTimer(timeout_t timeout);
00274 };
00275
00288 class __EXPORT SocketService : public Thread, private Mutex
00289 {
00290 private:
00291 #ifndef WIN32
00292 fd_set connect;
00293 int iosync[2];
00294 int hiwater;
00295 #else
00296
00297 class Sync;
00298 Sync* sync;
00299 #endif
00300 int volatile count;
00301 SocketPort* volatile first, *last;
00302
00308 void attach(SocketPort *port);
00314 void detach(SocketPort *port);
00315
00319 void run(void);
00320
00321 friend class SocketPort;
00322
00323 protected:
00329 virtual void onUpdate(unsigned char buf);
00330
00336 virtual void onEvent(void);
00337
00345 virtual void onCallback(SocketPort *port);
00346
00347 public:
00358 void update(unsigned char flag = 0xff);
00359
00368 SocketService(int pri = 0, size_t stack = 0, const char *id = NULL);
00369
00374 virtual ~SocketService();
00375
00382 inline int getCount(void) const
00383 {return count;};
00384 };
00385
00386 #ifdef CCXX_NAMESPACES
00387 }
00388 #endif
00389
00390 #endif
00391