- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gsocket_unix.cpp
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 // ===
17 //
18 // gsocket_unix.cpp
19 //
20
21 #include "gdef.h"
22 #include "gnet.h"
23 #include "gsocket.h"
24 #include "gdebug.h"
25 #include "gassert.h"
26 #include "glog.h"
27 #include <cstring>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32
33 bool GNet::Socket::valid( Descriptor s )
34 {
35 return s.valid() ;
36 }
37
38 bool GNet::Socket::setNonBlock()
39 {
40 G_ASSERT( valid() ) ;
41
42 int mode = ::fcntl( m_socket.fd() , F_GETFL ) ;
43 if( mode < 0 )
44 return false ;
45
46 int rc = ::fcntl( m_socket.fd() , F_SETFL , mode | O_NONBLOCK ) ;
47 bool ok = rc >= 0 ;
48 if( ok )
49 {
50 G_ASSERT( ::fcntl(m_socket.fd(),F_GETFL) & O_NONBLOCK ) ;
51 }
52
53 return ok ;
54 }
55
56 int GNet::Socket::reason()
57 {
58 int r = errno ;
59 G_DEBUG( "GNet::Socket::reason: " << (r==EINPROGRESS?"":"error ") << r << ": " << std::strerror(r) ) ;
60 return r ;
61 }
62
63 void GNet::Socket::doClose()
64 {
65 G_ASSERT( valid() ) ;
66 ::close( m_socket.fd() ) ;
67 m_socket = Descriptor::invalid() ;
68 }
69
70 bool GNet::Socket::error( int rc )
71 {
72 return rc < 0 ;
73 }
74
75 bool GNet::Socket::sizeError( ssize_t size )
76 {
77 return size < 0 ;
78 }
79
80 bool GNet::Socket::eWouldBlock()
81 {
82 return m_reason == EWOULDBLOCK || m_reason == EAGAIN ;
83 }
84
85 bool GNet::Socket::eInProgress()
86 {
87 return m_reason == EINPROGRESS ;
88 }
89
90 bool GNet::Socket::eMsgSize()
91 {
92 return m_reason == EMSGSIZE ;
93 }
94
95 void GNet::Socket::setFault()
96 {
97 m_reason = EFAULT ;
98 }
99
100 bool GNet::Socket::canBindHint( const Address & address )
101 {
102 return bind( address ) ;
103 }
104
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::Descriptor::invalid
static Descriptor invalid()
Returns an invalid descriptor.
Definition: gdescriptor.cpp:30
glog.h
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::Address
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
GNet::Descriptor
A network file descriptor.
Definition: gdescriptor.h:37
GNet::Socket::sizeError
static bool sizeError(ssize_type size)
Definition: gsocket_unix.cpp:75
GNet::Socket::eMsgSize
bool eMsgSize()
Returns true if the previous socket operation failed with the EMSGSIZE error status.
Definition: gsocket_unix.cpp:90
G_ASSERT
#define G_ASSERT(test)
Definition: gassert.h:30
GNet::Socket::setFault
void setFault()
Definition: gsocket_unix.cpp:95
gassert.h
GNet::Descriptor::valid
bool valid() const
Returns true if the descriptor is valid.
Definition: gdescriptor_unix.cpp:30
GNet::Socket::eInProgress
bool eInProgress()
Returns true if the previous socket operation failed with the EINPROGRESS error status.
Definition: gsocket_unix.cpp:85
G_DEBUG
#define G_DEBUG(expr)
Definition: glog.h:95
gsocket.h
GNet::Socket::valid
bool valid() const
Returns true if the socket handle is valid (open).
Definition: gsocket.cpp:92
GNet::Socket::error
static bool error(int rc)
Definition: gsocket_unix.cpp:70
gdebug.h