GNU CommonC++
socketport.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef CCXX_SOCKETPORT_H_
44 #define CCXX_SOCKETPORT_H_
45 
46 #ifndef CCXX_ADDRESS_H_
47 #include <cc++/address.h>
48 #endif
49 
50 #ifndef CCXX_SOCKET_H_
51 #include <cc++/socket.h>
52 #endif
53 
54 #ifdef CCXX_NAMESPACES
55 namespace ost {
56 #endif
57 
60 
80 class __EXPORT SocketPort : public Socket, public TimerPort
81 {
82 private:
83  SocketPort *next, *prev;
84  SocketService *service;
85 #ifndef WIN32
86  struct timeval porttimer;
87 #ifdef USE_POLL
88  struct pollfd * ufd;
89 #endif
90 #else
91  HANDLE event;
92 #endif
93  bool detect_pending;
94  bool detect_output;
95  bool detect_disconnect;
96 
97  friend class SocketService;
98 
99 protected:
108  SocketPort(SocketService *svc, TCPSocket &tcp);
109 #ifdef CCXX_IPV6
111 #endif
112 
121  SocketPort(SocketService *svc, const IPV4Address &ia, tpport_t port);
122 #ifdef CCXX_IPV6
123  SocketPort(SocketService *svc, const IPV6Address &ia, tpport_t port);
124 #endif
125 
139  SocketPort(SocketService *svc, const IPV4Host &ih, tpport_t port);
140 #ifdef CCXX_IPV6
141  SocketPort(SocketService *svc, const IPV6Host &ih, tpport_t port);
142 #endif
143 
149  void attach( SocketService* svc );
150 
151 
156  virtual ~SocketPort();
157 
162  void setDetectPending( bool );
163 
167  bool getDetectPending( void ) const
168  { return detect_pending; }
169 
174  void setDetectOutput( bool );
175 
179  bool getDetectOutput( void ) const
180  { return detect_output; }
181 
186  virtual void expired(void);
187 
192  virtual void pending(void);
193 
198  virtual void output(void);
199 
204  virtual void disconnect(void);
205 
216  Error connect(const IPV4Address &ia, tpport_t port);
217 #ifdef CCXX_IPV6
218  Error connect(const IPV6Address &ia, tpport_t port);
219 #endif
220 
230  inline ssize_t send(const void *buf, size_t len)
231  {return _IORET64 ::send(so, (const char *)buf, _IOLEN64 len, 0);};
232 
241  inline ssize_t receive(void *buf, size_t len)
242  {return _IORET64 ::recv(so, (char *)buf, _IOLEN64 len, 0);};
243 
252  inline ssize_t peek(void *buf, size_t len)
253  {return _IORET64 ::recv(so, (char *)buf, _IOLEN64 len, MSG_PEEK);};
254 
255 public:
263  void setTimer(timeout_t timeout = 0);
264 
272  void incTimer(timeout_t timeout);
273 };
274 
287 class __EXPORT SocketService : public Thread, private Mutex
288 {
289 private:
290 #ifndef WIN32
291  fd_set connect;
292  int iosync[2];
293  int hiwater;
294 #else
295  // private syncronization class
296  class Sync;
297  Sync* sync;
298 #endif
299  int volatile count;
300  SocketPort* volatile first, *last;
301 
307  void attach(SocketPort *port);
313  void detach(SocketPort *port);
314 
318  void run(void);
319 
320  friend class SocketPort;
321 
322 protected:
328  virtual void onUpdate(unsigned char buf);
329 
335  virtual void onEvent(void);
336 
344  virtual void onCallback(SocketPort *port);
345 
346 public:
357  void update(unsigned char flag = 0xff);
358 
367  SocketService(int pri = 0, size_t stack = 0, const char *id = NULL);
368 
373  virtual ~SocketService();
374 
381  inline int getCount(void) const
382  {return count;};
383 };
384 
385 #ifdef CCXX_NAMESPACES
386 }
387 #endif
388 
389 #endif
390