gsocket.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
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 3 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, see <http://www.gnu.org/licenses/>.
16 // ===
20 
21 #ifndef G_SOCKET_H
22 #define G_SOCKET_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gaddress.h"
27 #include "gevent.h"
28 #include "gdescriptor.h"
29 #include <string>
30 
32 namespace GNet
33 {
34  class Socket ;
35  class SocketProtocol ;
36  class StreamSocket ;
37  class DatagramSocket ;
38  class AcceptPair ;
39 }
40 
61 {
62 public:
63  typedef size_t size_type ;
64  typedef ssize_t ssize_type ;
66  class Credentials
67  {
68  friend class SocketProtocol ;
69  friend class SocketProtocolTest ;
70  Credentials( const char * ) {}
71  } ;
72 
73  virtual ~Socket() ;
75 
76  bool valid() const ;
79 
80  std::pair<bool,Address> getLocalAddress() const ;
83 
84  std::pair<bool,Address> getPeerAddress() const ;
87 
88  bool hasPeer() const ;
92 
93  bool bind( const Address & address ) ;
98 
99  bool canBindHint( const Address & address ) ;
105 
106  bool connect( const Address & addr , bool *done = NULL ) ;
120 
121  bool listen( int backlog = 1 ) ;
125 
126  virtual ssize_type write( const char * buf , size_type len ) ;
142 
143  bool eWouldBlock() ;
149 
150  bool eInProgress() ;
155 
156  bool eMsgSize() ;
161 
162  void addReadHandler( EventHandler & handler ) ;
165 
166  void dropReadHandler();
168 
169  void addWriteHandler( EventHandler & handler ) ;
174 
175  void dropWriteHandler() ;
177 
178  void addExceptionHandler( EventHandler & handler );
184 
185  void dropExceptionHandler() ;
187 
188  std::string asString() const ;
191 
192  std::string reasonString() const ;
195 
196  void shutdown( bool for_writing = true ) ;
198 
199  int fd( Credentials ) const ;
202 
203 protected:
204  Socket( int domain , int type , int protocol ) ;
207 
208  explicit Socket( Descriptor s ) ;
212 
213 protected:
214  static bool valid( Descriptor s ) ;
215  static int reason() ;
216  static bool error( int rc ) ;
217  static bool sizeError( ssize_type size ) ;
218  void prepare() ;
219  void setFault() ;
220  void setNoLinger() ;
221  void setReuse() ;
222  void setKeepAlive() ;
223  std::pair<bool,Address> getAddress( bool ) const ;
224 
225 private:
226  void doClose() ;
227  bool setNonBlock() ;
228 
229 protected:
230  int m_reason ;
232 
233 private:
234  Socket( const Socket & ) ;
235  void operator=( const Socket & ) ;
236  void drop() ;
237 } ;
238 
240 
247 {
248 public:
249  typedef std::auto_ptr<StreamSocket> first_type ;
251 
252  first_type first ;
253  second_type second ;
254 
255  AcceptPair( StreamSocket * new_p , Address a ) ;
257 
258  AcceptPair( const AcceptPair & other ) ;
260 
261  AcceptPair & operator=( const AcceptPair & rhs ) ;
263 } ;
264 
266 
271 {
272 public:
275 
276  StreamSocket() ;
278 
279  explicit StreamSocket( const Address & address_hint ) ;
282 
283  virtual ~StreamSocket() ;
285 
286  ssize_type read( char * buffer , size_type buffer_length ) ;
294 
295  AcceptPair accept() ;
298 
299 private:
300  StreamSocket( const StreamSocket & ) ; // not implemented
301  void operator=( const StreamSocket & ) ; // not implemented
302  StreamSocket( Descriptor s ) ; // A private constructor used in accept().
303 } ;
304 
306 
312 {
313 public:
314  DatagramSocket() ;
316 
317  explicit DatagramSocket( const Address & address_hint ) ;
319 
320  virtual ~DatagramSocket() ;
322 
323  ssize_type read( void * buffer , size_type len , Address & src ) ;
328 
329  ssize_type write( const char * buffer , size_type len , const Address & dst ) ;
334 
335  ssize_type write( const char * buffer , size_type len ) ;
337 
338  void disconnect() ;
342 
343 private:
344  DatagramSocket( const DatagramSocket & ) ; // not implemented
345  void operator=( const DatagramSocket & ) ; // not implemented
346 } ;
347 
349 
350 inline
352 {
353  return Socket::write(buf,len) ;
354 }
355 
357 
358 inline
360  first(p) ,
361  second(a)
362 {
363 }
364 
365 inline
367  first(const_cast<first_type&>(other.first)) ,
368  second(other.second)
369 {
370 }
371 
372 inline
374 {
376  first = const_cast<first_type&>(rhs.first) ;
377  second = rhs.second ;
378  return *this ;
379 }
380 
381 #endif
382 
ssize_type write(const char *buffer, size_type len, const Address &dst)
Sends a datagram to the given address.
Definition: gsocket.cpp:414
bool canBindHint(const Address &address)
Returns true if the socket can probably be bound with the given address.
static int reason()
DatagramSocket()
Default constructor.
Definition: gsocket.cpp:378
bool listen(int backlog=1)
Starts the socket listening on the bound address for incoming connections or incoming datagrams...
Definition: gsocket.cpp:203
std::pair< bool, Address > getLocalAddress() const
Retrieves local address of the socket.
Definition: gsocket.cpp:236
Network classes.
void dropReadHandler()
Reverses addReadHandler().
Definition: gsocket.cpp:258
void addExceptionHandler(EventHandler &handler)
Adds this socket to the event source list so that the given handler receives exception events...
Definition: gsocket.cpp:270
bool connect(const Address &addr, bool *done=NULL)
Initiates a connection to (or association with) the given address.
Definition: gsocket.cpp:130
virtual ssize_type write(const char *buf, size_type len)
Sends data.
Definition: gsocket.cpp:159
bool eWouldBlock()
Returns true if the previous socket operation failed with the EWOULDBLOCK or EGAIN error status...
void addWriteHandler(EventHandler &handler)
Adds this socket to the event source list so that the given handler receives write events when flow c...
Definition: gsocket.cpp:263
virtual ~DatagramSocket()
Destructor.
Definition: gsocket.cpp:388
std::pair< bool, Address > getAddress(bool) const
Definition: gsocket.cpp:214
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
The Socket class encapsulates a non-blocking Unix socket file descriptor or a Windows 'SOCKET' handle...
Definition: gsocket.h:60
A derivation of Socket for a connectionless datagram socket.
Definition: gsocket.h:311
void addReadHandler(EventHandler &handler)
Adds this socket to the event source list so that the given handler receives read events...
Definition: gsocket.cpp:251
virtual ~StreamSocket()
Destructor.
Definition: gsocket.cpp:332
int m_reason
Definition: gsocket.h:230
A network file descriptor.
Definition: gdescriptor.h:37
StreamSocket()
Default constructor. Check with valid().
Definition: gsocket.cpp:313
static bool sizeError(ssize_type size)
std::auto_ptr< StreamSocket > first_type
Definition: gsocket.h:249
bool bind(const Address &address)
Binds the socket with an INADDR_ANY network address and the port number taken from the given address...
Definition: gsocket.cpp:97
int fd(Credentials) const
Returns the socket descriptor as an integer.
Definition: gsocket.cpp:306
void dropExceptionHandler()
Reverses addExceptionHandler().
Definition: gsocket.cpp:282
bool eMsgSize()
Returns true if the previous socket operation failed with the EMSGSIZE error status.
void dropWriteHandler()
Reverses addWriteHandler().
Definition: gsocket.cpp:277
AcceptPair(StreamSocket *new_p, Address a)
Constructor.
Definition: gsocket.h:359
std::string reasonString() const
Returns the failure reason as a string.
Definition: gsocket.cpp:294
void disconnect()
Releases the association between two datagram endpoints reversing the effect of the previous Socket::...
Definition: gsocket.cpp:392
std::pair< bool, Address > getPeerAddress() const
Retrieves address of socket's peer.
Definition: gsocket.cpp:241
A derivation of Socket for a stream socket.
Definition: gsocket.h:270
size_t size_type
Definition: gsocket.h:63
first_type first
Definition: gsocket.h:252
ssize_type read(void *buffer, size_type len, Address &src)
Reads a datagram and returns the sender's address by reference.
Definition: gsocket.cpp:399
Socket::ssize_type ssize_type
Definition: gsocket.h:274
virtual ~Socket()
Destructor.
Definition: gsocket.cpp:65
Socket(int domain, int type, int protocol)
Constructor used by derived classes.
Definition: gsocket.cpp:31
A class which is used to return a new()ed socket to calling code, together with associated informatio...
Definition: gsocket.h:246
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
void setReuse()
Definition: gsocket.cpp:196
bool eInProgress()
Returns true if the previous socket operation failed with the EINPROGRESS error status.
bool hasPeer() const
Returns true if the socket has a valid peer.
Definition: gsocket.cpp:246
ssize_type read(char *buffer, size_type buffer_length)
Reads data from the socket stream.
Definition: gsocket.cpp:336
second_type second
Definition: gsocket.h:253
Address second_type
Definition: gsocket.h:250
void setNoLinger()
Definition: gsocket.cpp:179
bool valid() const
Returns true if the socket handle is valid (open).
Definition: gsocket.cpp:92
void setKeepAlive()
Definition: gsocket.cpp:189
friend class SocketProtocolTest
Definition: gsocket.h:69
A credentials class that allows SocketProtocol to call Socket::fd().
Definition: gsocket.h:66
Socket::size_type size_type
Definition: gsocket.h:273
void shutdown(bool for_writing=true)
Shuts the socket for writing (or reading).
Definition: gsocket.cpp:301
Descriptor m_socket
Definition: gsocket.h:231
AcceptPair & operator=(const AcceptPair &rhs)
Assignment operator.
Definition: gsocket.h:373
AcceptPair accept()
Accepts an incoming connection, returning a new()ed socket and the peer address.
Definition: gsocket.cpp:354
An interface for implementing a low-level protocol layer by means of calling read() and write() on a ...
ssize_t ssize_type
Definition: gsocket.h:64
static bool error(int rc)
void prepare()
Definition: gsocket.cpp:53
std::string asString() const
Returns the socket handle as a string.
Definition: gsocket.cpp:287