GNU CommonC++
socketport.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef CCXX_SOCKETPORT_H_
45 #define CCXX_SOCKETPORT_H_
46 
47 #ifndef CCXX_ADDRESS_H_
48 #include <cc++/address.h>
49 #endif
50 
51 #ifndef CCXX_SOCKET_H_
52 #include <cc++/socket.h>
53 #endif
54 
55 #ifdef CCXX_NAMESPACES
56 namespace ost {
57 #endif
58 
59 class __EXPORT SocketPort;
61 
81 class __EXPORT SocketPort : public Socket, public TimerPort
82 {
83 private:
84  SocketPort *next, *prev;
85  SocketService *service;
86 #ifndef WIN32
87  struct timeval porttimer;
88 #ifdef USE_POLL
89  struct pollfd * ufd;
90 #endif
91 #else
92  HANDLE event;
93 #endif
94  bool detect_pending;
95  bool detect_output;
96  bool detect_disconnect;
97 
98  friend class SocketService;
99 
100 protected:
110 #ifdef CCXX_IPV6
112 #endif
113 
123 #ifdef CCXX_IPV6
125 #endif
126 
140  SocketPort(SocketService *svc, const IPV4Host &ih, tpport_t port);
141 #ifdef CCXX_IPV6
142  SocketPort(SocketService *svc, const IPV6Host &ih, tpport_t port);
143 #endif
144 
150  void attach( SocketService* svc );
151 
152 
157  virtual ~SocketPort();
158 
163  void setDetectPending( bool );
164 
168  bool getDetectPending( void ) const
169  { return detect_pending; }
170 
175  void setDetectOutput( bool );
176 
180  bool getDetectOutput( void ) const
181  { return detect_output; }
182 
187  virtual void expired(void);
188 
193  virtual void pending(void);
194 
199  virtual void output(void);
200 
205  virtual void disconnect(void);
206 
217  Error connect(const IPV4Address &ia, tpport_t port);
218 #ifdef CCXX_IPV6
219  Error connect(const IPV6Address &ia, tpport_t port);
220 #endif
221 
231  inline ssize_t send(const void *buf, size_t len)
232  {return _IORET64 ::send(so, (const char *)buf, _IOLEN64 len, 0);};
233 
242  inline ssize_t receive(void *buf, size_t len)
243  {return _IORET64 ::recv(so, (char *)buf, _IOLEN64 len, 0);};
244 
253  inline ssize_t peek(void *buf, size_t len)
254  {return _IORET64 ::recv(so, (char *)buf, _IOLEN64 len, MSG_PEEK);};
255 
256 public:
264  void setTimer(timeout_t timeout = 0);
265 
273  void incTimer(timeout_t timeout);
274 };
275 
288 class __EXPORT SocketService : public Thread, private Mutex
289 {
290 private:
291 #ifndef WIN32
292  fd_set connect;
293  int iosync[2];
294  int hiwater;
295 #else
296  // private syncronization class
297  class Sync;
298  Sync* sync;
299 #endif
300  int volatile count;
301  SocketPort* volatile first, *last;
302 
308  void attach(SocketPort *port);
314  void detach(SocketPort *port);
315 
319  void run(void);
320 
321  friend class SocketPort;
322 
323 protected:
329  virtual void onUpdate(unsigned char buf);
330 
336  virtual void onEvent(void);
337 
345  virtual void onCallback(SocketPort *port);
346 
347 public:
358  void update(unsigned char flag = 0xff);
359 
368  SocketService(int pri = 0, size_t stack = 0, const char *id = NULL);
369 
374  virtual ~SocketService();
375 
382  inline int getCount(void) const
383  {return count;};
384 };
385 
386 #ifdef CCXX_NAMESPACES
387 }
388 #endif
389 
390 #endif
391 
ost::tpport_t
unsigned short tpport_t
Transport Protocol Ports.
Definition: address.h:86
ost::SocketPort::setDetectPending
void setDetectPending(bool)
Used to indicate if the service thread should monitor pending data for us.
ost::SocketPort::~SocketPort
virtual ~SocketPort()
Disconnect the socket from the service thread pool and the remote connection.
ost::SocketPort::incTimer
void incTimer(timeout_t timeout)
Derived incTimer to notify the service thread pool of a change in expected timeout.
ost::SocketService::SocketService
SocketService(int pri=0, size_t stack=0, const char *id=NULL)
Create a service thread for attaching socket ports.
ost::SocketPort::send
ssize_t send(const void *buf, size_t len)
Transmit "send" data to a connected peer host.
Definition: socketport.h:231
ost::SocketPort::attach
void attach(SocketService *svc)
Attach yourself to the service pool thread object.
ost::IPV4Host
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:562
ost::Thread
Every thread of execution in an application is created by instantiating an object of a class derived ...
Definition: thread.h:1094
ost::TCPSocket
TCP sockets are used for stream based connected sessions between two sockets.
Definition: socket.h:1396
ost::SocketService::~SocketService
virtual ~SocketService()
Terminate the thread pool and eliminate any attached socket ports.
timeout_t
unsigned long timeout_t
Definition: thread.h:74
ost::SocketPort::peek
ssize_t peek(void *buf, size_t len)
Examine the content of the next packet.
Definition: socketport.h:253
ost::SocketPort::output
virtual void output(void)
Called by the service thread pool when output data is pending for this socket.
ost::IPV6Host
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:949
HANDLE
int HANDLE
Definition: serial.h:60
ost::Mutex
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
Definition: thread.h:187
ost::SocketPort::SocketPort
SocketPort(SocketService *svc, const IPV4Host &ih, tpport_t port)
A non-blocking constructor for outbound tcp connections.
ost::Socket::Error
Error
Definition: socket.h:131
address.h
Network addresses and sockets related classes.
ost::SocketPort::getDetectPending
bool getDetectPending(void) const
Get the current state of the DetectPending flag.
Definition: socketport.h:168
ost::SocketService::onUpdate
virtual void onUpdate(unsigned char buf)
Handles all requests other than "termination".
ost::SocketPort::receive
ssize_t receive(void *buf, size_t len)
Receive a message from any host.
Definition: socketport.h:242
__EXPORT
#define __EXPORT
Definition: config.h:979
ost::IPV6Address
The network name and address objects are all derived from a common IPV6Address base class.
Definition: address.h:754
ost::Socket
The Socket is used as the base for all Internet protocol services under Common C++.
Definition: socket.h:120
ost::IPV4Address
The network name and address objects are all derived from a common IPV4Address base class.
Definition: address.h:351
ost::SocketService::onCallback
virtual void onCallback(SocketPort *port)
Called for each port that is being processed in response to an event.
ost::SocketPort::SocketPort
SocketPort(SocketService *svc, const IPV6Address &ia, tpport_t port)
ost::SocketPort::SocketPort
SocketPort(SocketService *svc, const IPV6Host &ih, tpport_t port)
ost::SocketPort::SocketPort
SocketPort(SocketService *svc, TCPV6Socket &tcp)
ost::SocketPort::connect
Error connect(const IPV4Address &ia, tpport_t port)
Connect a Socket Port to a known peer host.
ost::SocketPort::expired
virtual void expired(void)
Called by the service thread pool when the objects timer has expired.
ost::SocketPort::pending
virtual void pending(void)
Called by the service thread pool when input data is pending for this socket.
ost::SocketPort::connect
Error connect(const IPV6Address &ia, tpport_t port)
ost::SocketPort
The socket port is an internal class which is attached to and then serviced by a specific SocketServi...
Definition: socketport.h:82
ost::SocketService::getCount
int getCount(void) const
Get current reference count.
Definition: socketport.h:382
ost::SocketPort::SocketPort
SocketPort(SocketService *svc, TCPSocket &tcp)
Construct an accepted TCP socket connection from a specific bound TCP server.
ost
Definition: address.h:64
ost::TCPV6Socket
TCPV6 sockets are used for stream based connected sessions between two ipv6 sockets.
Definition: socket.h:1515
ost::SocketPort::setTimer
void setTimer(timeout_t timeout=0)
Derived setTimer to notify the service thread pool of change in expected timeout.
ost::SocketPort::getDetectOutput
bool getDetectOutput(void) const
Get the current state of the DetectOutput flag.
Definition: socketport.h:180
ost::SocketPort::SocketPort
SocketPort(SocketService *svc, const IPV4Address &ia, tpport_t port)
Construct a bound UDP socket for use in deriving realtime UDP streaming protocols handled by thread p...
socket.h
Network addresses and sockets related classes.
_IOLEN64
#define _IOLEN64
Definition: socket.h:64
ost::SocketService::update
void update(unsigned char flag=0xff)
Notify service thread that a port has been added or removed, or a timer changed, so that a new schedu...
ost::SocketService
The SocketService is a thread pool object that is meant to service attached socket ports.
Definition: socketport.h:289
ost::SocketPort::disconnect
virtual void disconnect(void)
Called by the service thread pool when a disconnect has occured.
ost::SocketService::onEvent
virtual void onEvent(void)
Called once each time the service thread is rescheduled.
ost::TimerPort
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
Definition: thread.h:1760
ost::SocketPort::setDetectOutput
void setDetectOutput(bool)
Used to indicate if output ready monitoring should be performed by the service thread.