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_socket.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_socket.h"
9 #include "vsnet_sockettcp.h"
10 #include "vsnet_sockethttp.h"
11 #include "vsnet_socketudp.h"
12 #include "vsnet_socketset.h"
13 
14 LOCALCONST_DEF( SOCKETALT, bool, TCP, 1 )
15 LOCALCONST_DEF( SOCKETALT, bool, UDP, 0 )
16 /***********************************************************************
17 * SOCKETALT
18 ***********************************************************************/
19 
20 SOCKETALT::SOCKETALT()
21 {}
22 
23 SOCKETALT::SOCKETALT( int sock, bool mode, const AddressIP &remote_ip, SocketSet &sets )
24 {
25  if (mode == TCP)
26  _sock = ptr( new VsnetTCPSocket( sock, remote_ip, sets ) );
27  else
28  _sock = ptr( new VsnetUDPSocket( sock, remote_ip, sets ) );
29 }
30 
32 {
33  _sock = orig._sock;
34 }
35 
37 {
38  _sock = ptr( s );
39 }
40 
42 {
43  _sock = orig._sock;
44  return *this;
45 }
46 
47 int SOCKETALT::get_fd() const
48 {
49  return !_sock ? -1 : _sock->get_fd();
50 }
51 
52 bool SOCKETALT::valid() const
53 {
54  if (!_sock) return false;
55  return _sock->valid();
56 }
57 
58 bool SOCKETALT::isTcp() const
59 {
60  return !_sock ? false : _sock->isTcp();
61 }
62 
63 int SOCKETALT::queueLen( int pri )
64 {
65  return !_sock ? -1 : _sock->queueLen( pri );
66 }
67 
69 {
70  return !_sock ? -1 : _sock->optPayloadSize();
71 }
72 
73 std::ostream&operator<<( std::ostream &ostr, const SOCKETALT &s )
74 {
75  if (s._sock) s._sock->dump( ostr );
76  else ostr<<"NULL";
77  return ostr;
78 }
79 
80 bool operator==( const SOCKETALT &l, const SOCKETALT &r )
81 {
82  if (!l._sock)
83  return (!r._sock) ? true : false;
84  else if (!r._sock)
85  return false;
86  else
87  return l._sock->eq( *r._sock );
88 }
89 
90 bool SOCKETALT::CompareLt::operator()( const SOCKETALT &l, const SOCKETALT &r ) const
91 {
92  return l.lowerAddress( r );
93 }
94 
96 {
97  if (!_sock) {
98  return false;
99  } else {
100  bool r = _sock->setLocalAddress( inp );
101  return r;
102  }
103 }
104 
106 {
107  if (!_sock) {
108  return false;
109  } else {
110  bool r = _sock->setRemoteAddress( inp );
111  return r;
112  }
113 }
114 
116 {
117  if (!_sock) {
118  return false;
119  } else {
120  bool r = _sock->isActive();
121  return r;
122  }
123 }
124 
125 int SOCKETALT::sendbuf( Packet *packet, const AddressIP *to, int pcktflags )
126 {
127  if (!_sock || !packet)
128  return -1;
129  else
130  return _sock->sendbuf( packet, to, pcktflags );
131 }
132 
133 //int SOCKETALT::sendbuf( PacketMem& packet, const AddressIP* to, int pcktflags )
134 //{
135 //return ( !_sock ? -1 : _sock->sendbuf( packet, to, pcktflags ) );
136 //}
137 
139 {
140  return !_sock ? false : _sock->set_nonblock();
141 }
142 
144 {
145  return !_sock ? false : _sock->set_block();
146 }
147 
149 {
150  if (!_sock || !p) {
151  return -1;
152  } else {
153  int retval = _sock->recvbuf( p, ipadr );
154  return retval;
155  }
156 }
157 
158 void SOCKETALT::disconnect( const char *s )
159 {
160  if (_sock) _sock->disconnect( s );
161 }
162 
163 bool SOCKETALT::sameAddress( const SOCKETALT &l ) const
164 {
165  if (!l._sock)
166  return (!_sock) ? true : false;
167  else if (!_sock)
168  return false;
169  else
170  return _sock->eq( *l._sock );
171 }
172 
173 bool SOCKETALT::lowerAddress( const SOCKETALT &right ) const
174 {
175  if (!_sock) {
176  if (right._sock) return true;
177  return false;
178  } else if (!right._sock) {
179  return false;
180  } else {
181  return this->_sock->lt( *right._sock );
182  }
183 }
184 
186 {
187  if (_sock) sockset.set( &(*this->_sock) );
188 }
189 
191 {
192  if (_sock) _sock->setSet( set );
193 }
194 
196 {
197  static AddressIP nullAdr;
198  if (_sock)
199  return _sock->getRemoteAddress();
200  else
201  return nullAdr; //just in case.
202 }
203 
205 {
206  static AddressIP nullAdr;
207  if (_sock)
208  return _sock->getLocalAddress();
209  else
210  return nullAdr; //just in case.
211 }
212