Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vsnet_socketbase.cpp
Go to the documentation of this file.
1 #include <config.h>
2 
3 #include "vsnet_headers.h"
4 #if !defined (_WIN32) || defined (__CYGWIN__)
5 #include <sys/ioctl.h>
6 #endif
7 
8 #include "vsnet_oss.h"
9 #include "vsnet_socketbase.h"
10 #include "vsnet_socketset.h"
11 
12 /***********************************************************************
13 * VsnetSocket - definition
14 ***********************************************************************/
15 
16 VsnetSocket::VsnetSocket( int sock, const AddressIP &remote_ip, const char *socktype, SocketSet &sets ) :
17  VsnetSocketBase( sock, socktype, sets )
18  , _remote_ip( remote_ip )
19 {}
20 
22 {}
23 
24 //VsnetSocket& VsnetSocket::operator=( const VsnetSocket& orig )
25 //{
26 //VsnetSocketBase::operator=( orig );
27 //_remote_ip = orig._remote_ip;
28 //return *this;
29 //}
30 
31 bool VsnetSocket::eq( const VsnetSocket &r ) const
32 {
33  return ( isTcp() == r.isTcp() ) && ( get_fd() == r.get_fd() ) && (_remote_ip == r._remote_ip);
34 }
35 
36 bool VsnetSocket::lt( const VsnetSocket &r ) const
37 {
38  if ( !isTcp() && r.isTcp() ) return true;
39  if ( isTcp() == r.isTcp() ) {
40  if ( get_fd() < r.get_fd() ) return true;
41  if ( get_fd() == r.get_fd() )
42  if (_remote_ip < r._remote_ip) return true;
43  }
44  return false;
45 }
46 
47 bool VsnetSocket::sameAddress( const VsnetSocket &r ) const
48 {
49  const VsnetSocket *r2 = (const VsnetSocket*) &r;
50  return ( isTcp() == r2->isTcp() ) && (_remote_ip == r2->_remote_ip);
51 }
52 
53 /***********************************************************************
54 * VsnetSocketBase - definition
55 ***********************************************************************/
56 
57 VsnetSocketBase::VsnetSocketBase( int fd, const char *socktype, SocketSet &sets ) :
58  _fd( fd )
59  , _set( &sets )
60  , _noblock( -1 )
61 {
63 
64  char buf[100];
65 
66  sprintf( buf, "%d", fd );
67  unsigned int len = strlen( socktype )+strlen( buf )+4;
68  _socktype = new char[len];
69 
70  sprintf( _socktype, "%s (%s)", buf, socktype );
71 
72  assert( strlen( _socktype ) == len-1 );
73  if (fd >= 0)
74  set_block();
75  sets.set( this );
76 }
77 bool VsnetSocketBase::isReadyToSend( fd_set *write_set_select )
78 {
79  return get_write_fd() >= 0 && FD_ISSET( get_write_fd(), write_set_select );
80 }
82 {
84  if (_set) _set->unset( this );
85  delete[] _socktype;
87 }
88 
90 {
91  if (this->_set) this->_set->unset( this );
92  this->_set = set;
93  if (this->_set) this->_set->set( this );
94 }
95 
96 const char* VsnetSocketBase::get_socktype() const
97 {
98  return _socktype;
99 }
100 
102 {
104  return _fd >= 0;
105 }
106 
108 {
110  return _fd;
111 }
112 
114 {
116  int ret = 0;
117  if (_fd >= 0) {
118  ret = VsnetOSS::close_socket( _fd );
119  _fd = -1;
120  }
121  return ret;
122 }
123 
125 {
127  if ( !valid() ) {
128  COUT<<"Failed to set blocking socket "<<_socktype<<std::endl;
129  return false;
130  }
131  bool ret = VsnetOSS::set_blocking( _fd, false );
132  if (ret)
133  _noblock = 1;
134  return ret;
135 }
136 
138 {
140  if ( !valid() ) {
141  COUT<<"Failed to set blocking socket "<<_socktype<<std::endl;
142  return false;
143  }
144  bool ret = VsnetOSS::set_blocking( _fd, true );
145  if (ret)
146  _noblock = 0;
147  return ret;
148 }
149 
151 {
153  return _noblock == 1;
154 }
155 
156 void VsnetSocketBase::disconnect( const char *s )
157 {
159  if (_fd > 0)
160  if (_set) _set->unset( this );
161  child_disconnect( s );
162 }
163