GNU CommonC++
|
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation. 00002 // 00003 // This program is free software; you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation; either version 2 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00016 // 00017 // As a special exception, you may use this file as part of a free software 00018 // library without restriction. Specifically, if other files instantiate 00019 // templates or use macros or inline functions from this file, or you compile 00020 // this file and link it with other files to produce an executable, this 00021 // file does not by itself cause the resulting executable to be covered by 00022 // the GNU General Public License. This exception does not however 00023 // invalidate any other reasons why the executable file might be covered by 00024 // the GNU General Public License. 00025 // 00026 // This exception applies only to the code released under the name GNU 00027 // Common C++. If you copy code from other releases into a copy of GNU 00028 // Common C++, as the General Public License permits, the exception does 00029 // not apply to the code that you add in this way. To avoid misleading 00030 // anyone as to the status of such modified files, you must delete 00031 // this exception notice from them. 00032 // 00033 // If you write modifications of your own for GNU Common C++, it is your choice 00034 // whether to permit this exception to apply to your modifications. 00035 // If you do not wish that, delete this exception notice. 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 // private syncronization class 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