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 
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 
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 
81 {
82  return m_reason == EWOULDBLOCK || m_reason == EAGAIN ;
83 }
84 
86 {
87  return m_reason == EINPROGRESS ;
88 }
89 
91 {
92  return m_reason == EMSGSIZE ;
93 }
94 
96 {
97  m_reason = EFAULT ;
98 }
99 
100 bool GNet::Socket::canBindHint( const Address & address )
101 {
102  return bind( address ) ;
103 }
104 
bool canBindHint(const Address &address)
Returns true if the socket can probably be bound with the given address.
static int reason()
static Descriptor invalid()
Returns an invalid descriptor.
Definition: gdescriptor.cpp:30
bool eWouldBlock()
Returns true if the previous socket operation failed with the EWOULDBLOCK or EGAIN error status...
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
A network file descriptor.
Definition: gdescriptor.h:37
static bool sizeError(ssize_type size)
bool eMsgSize()
Returns true if the previous socket operation failed with the EMSGSIZE error status.
#define G_ASSERT(test)
Definition: gassert.h:30
bool valid() const
Returns true if the descriptor is valid.
bool eInProgress()
Returns true if the previous socket operation failed with the EINPROGRESS error status.
#define G_DEBUG(expr)
Definition: glog.h:95
bool valid() const
Returns true if the socket handle is valid (open).
Definition: gsocket.cpp:92
static bool error(int rc)