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