- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
60 class GNet::Socket
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 ;
231 Descriptor m_socket ;
232
233 private:
234 Socket( const Socket & ) ;
235 void operator=( const Socket & ) ;
236 void drop() ;
237 } ;
238
240
246 class GNet::AcceptPair
247 {
248 public:
249 typedef std::auto_ptr<StreamSocket> first_type ;
250 typedef Address second_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
270 class GNet::StreamSocket : public GNet::Socket
271 {
272 public:
273 typedef Socket::size_type size_type ;
274 typedef Socket::ssize_type ssize_type ;
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
311 class GNet::DatagramSocket : public GNet::Socket
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
351 GNet::Socket::ssize_type GNet::DatagramSocket::write( const char *buf, size_type len )
352 {
353 return Socket::write(buf,len) ;
354 }
355
357
358 inline
359 GNet::AcceptPair::AcceptPair( StreamSocket * p , Address a ) :
360 first(p) ,
361 second(a)
362 {
363 }
364
365 inline
366 GNet::AcceptPair::AcceptPair( const AcceptPair & other ) :
367 first(const_cast<first_type&>(other.first)) ,
368 second(other.second)
369 {
370 }
371
372 inline
373 GNet::AcceptPair & GNet::AcceptPair::operator=( const AcceptPair & rhs )
374 {
376 first = const_cast<first_type&>(rhs.first) ;
377 second = rhs.second ;
378 return *this ;
379 }
380
381 #endif
382
GNet::DatagramSocket::write
ssize_type write(const char *buffer, size_type len, const Address &dst)
Sends a datagram to the given address.
Definition: gsocket.cpp:414
GNet::Socket::canBindHint
bool canBindHint(const Address &address)
Returns true if the socket can probably be bound with the given address.
Definition: gsocket_unix.cpp:100
GNet::Socket::reason
static int reason()
Definition: gsocket_unix.cpp:56
GNet::DatagramSocket::DatagramSocket
DatagramSocket()
Default constructor.
Definition: gsocket.cpp:378
GNet::Socket::listen
bool listen(int backlog=1)
Starts the socket listening on the bound address for incoming connections or incoming datagrams...
Definition: gsocket.cpp:203
GNet::Socket::getLocalAddress
std::pair< bool, Address > getLocalAddress() const
Retrieves local address of the socket.
Definition: gsocket.cpp:236
GNet
Network classes.
GNet::Socket::dropReadHandler
void dropReadHandler()
Reverses addReadHandler().
Definition: gsocket.cpp:258
GNet::Socket::addExceptionHandler
void addExceptionHandler(EventHandler &handler)
Adds this socket to the event source list so that the given handler receives exception events...
Definition: gsocket.cpp:270
GNet::Socket::connect
bool connect(const Address &addr, bool *done=NULL)
Initiates a connection to (or association with) the given address.
Definition: gsocket.cpp:130
GNet::Socket::write
virtual ssize_type write(const char *buf, size_type len)
Sends data.
Definition: gsocket.cpp:159
GNet::Socket::eWouldBlock
bool eWouldBlock()
Returns true if the previous socket operation failed with the EWOULDBLOCK or EGAIN error status...
Definition: gsocket_unix.cpp:80
GNet::Socket::addWriteHandler
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
GNet::DatagramSocket::~DatagramSocket
virtual ~DatagramSocket()
Destructor.
Definition: gsocket.cpp:388
GNet::Socket::getAddress
std::pair< bool, Address > getAddress(bool) const
Definition: gsocket.cpp:214
GNet::Address
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
GNet::Socket
The Socket class encapsulates a non-blocking Unix socket file descriptor or a Windows 'SOCKET' handle...
Definition: gsocket.h:60
GNet::DatagramSocket
A derivation of Socket for a connectionless datagram socket.
Definition: gsocket.h:311
GNet::Socket::addReadHandler
void addReadHandler(EventHandler &handler)
Adds this socket to the event source list so that the given handler receives read events...
Definition: gsocket.cpp:251
GNet::StreamSocket::~StreamSocket
virtual ~StreamSocket()
Destructor.
Definition: gsocket.cpp:332
GNet::Socket::m_reason
int m_reason
Definition: gsocket.h:230
GNet::Descriptor
A network file descriptor.
Definition: gdescriptor.h:37
GNet::StreamSocket::StreamSocket
StreamSocket()
Default constructor. Check with valid().
Definition: gsocket.cpp:313
GNet::Socket::sizeError
static bool sizeError(ssize_type size)
Definition: gsocket_unix.cpp:75
GNet::AcceptPair::first_type
std::auto_ptr< StreamSocket > first_type
Definition: gsocket.h:249
GNet::Socket::bind
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
GNet::Socket::fd
int fd(Credentials) const
Returns the socket descriptor as an integer.
Definition: gsocket.cpp:306
GNet::Socket::dropExceptionHandler
void dropExceptionHandler()
Reverses addExceptionHandler().
Definition: gsocket.cpp:282
GNet::Socket::eMsgSize
bool eMsgSize()
Returns true if the previous socket operation failed with the EMSGSIZE error status.
Definition: gsocket_unix.cpp:90
GNet::Socket::dropWriteHandler
void dropWriteHandler()
Reverses addWriteHandler().
Definition: gsocket.cpp:277
GNet::AcceptPair::AcceptPair
AcceptPair(StreamSocket *new_p, Address a)
Constructor.
Definition: gsocket.h:359
GNet::Socket::reasonString
std::string reasonString() const
Returns the failure reason as a string.
Definition: gsocket.cpp:294
GNet::DatagramSocket::disconnect
void disconnect()
Releases the association between two datagram endpoints reversing the effect of the previous Socket::...
Definition: gsocket.cpp:392
GNet::Socket::getPeerAddress
std::pair< bool, Address > getPeerAddress() const
Retrieves address of socket's peer.
Definition: gsocket.cpp:241
GNet::StreamSocket
A derivation of Socket for a stream socket.
Definition: gsocket.h:270
GNet::Socket::setFault
void setFault()
Definition: gsocket_unix.cpp:95
GNet::Socket::size_type
size_t size_type
Definition: gsocket.h:63
GNet::AcceptPair::first
first_type first
Definition: gsocket.h:252
GNet::DatagramSocket::read
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
GNet::StreamSocket::ssize_type
Socket::ssize_type ssize_type
Definition: gsocket.h:274
GNet::Socket::~Socket
virtual ~Socket()
Destructor.
Definition: gsocket.cpp:65
GNet::Socket::Socket
Socket(int domain, int type, int protocol)
Constructor used by derived classes.
Definition: gsocket.cpp:31
GNet::AcceptPair
A class which is used to return a new()ed socket to calling code, together with associated informatio...
Definition: gsocket.h:246
gevent.h
GNet::EventHandler
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
GNet::Socket::setReuse
void setReuse()
Definition: gsocket.cpp:196
GNet::Socket::eInProgress
bool eInProgress()
Returns true if the previous socket operation failed with the EINPROGRESS error status.
Definition: gsocket_unix.cpp:85
GNet::Socket::hasPeer
bool hasPeer() const
Returns true if the socket has a valid peer.
Definition: gsocket.cpp:246
GNet::StreamSocket::read
ssize_type read(char *buffer, size_type buffer_length)
Reads data from the socket stream.
Definition: gsocket.cpp:336
GNet::AcceptPair::second
second_type second
Definition: gsocket.h:253
gdescriptor.h
GNet::AcceptPair::second_type
Address second_type
Definition: gsocket.h:250
GNet::Socket::setNoLinger
void setNoLinger()
Definition: gsocket.cpp:179
GNet::Socket::valid
bool valid() const
Returns true if the socket handle is valid (open).
Definition: gsocket.cpp:92
GNet::Socket::setKeepAlive
void setKeepAlive()
Definition: gsocket.cpp:189
GNet::Socket::Credentials::SocketProtocolTest
friend class SocketProtocolTest
Definition: gsocket.h:69
GNet::Socket::Credentials
A credentials class that allows SocketProtocol to call Socket::fd().
Definition: gsocket.h:66
GNet::StreamSocket::size_type
Socket::size_type size_type
Definition: gsocket.h:273
GNet::Socket::shutdown
void shutdown(bool for_writing=true)
Shuts the socket for writing (or reading).
Definition: gsocket.cpp:301
gaddress.h
GNet::Socket::m_socket
Descriptor m_socket
Definition: gsocket.h:231
GNet::AcceptPair::operator=
AcceptPair & operator=(const AcceptPair &rhs)
Assignment operator.
Definition: gsocket.h:373
GNet::StreamSocket::accept
AcceptPair accept()
Accepts an incoming connection, returning a new()ed socket and the peer address.
Definition: gsocket.cpp:354
GNet::SocketProtocol
An interface for implementing a low-level protocol layer by means of calling read() and write() on a ...
Definition: gsocketprotocol.h:54
GNet::Socket::ssize_type
ssize_t ssize_type
Definition: gsocket.h:64
GNet::Socket::error
static bool error(int rc)
Definition: gsocket_unix.cpp:70
GNet::Socket::prepare
void prepare()
Definition: gsocket.cpp:53
GNet::Socket::asString
std::string asString() const
Returns the socket handle as a string.
Definition: gsocket.cpp:287