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_serversocket.cpp
Go to the documentation of this file.
1 #include <config.h>
2 #include "vsnet_socket.h"
3 #include "vsnet_serversocket.h"
4 #include "vsnet_oss.h"
5 #include "vsnet_debug.h"
6 #include "vsnet_err.h"
7 using std::cout;
8 using std::cerr;
9 using std::endl;
10 using std::hex;
11 
12 #include "vsnet_headers.h"
13 
14 std::ostream&operator<<( std::ostream &ostr, const ServerSocket &s )
15 {
16  ostr<<"( s="<<s.get_fd()<<" l="<<s._srv_ip<<" )";
17  return ostr;
18 }
19 
20 bool operator==( const ServerSocket &l, const ServerSocket &r )
21 {
22  return l.get_fd() == r.get_fd();
23 }
24 
25 void ServerSocket::child_disconnect( const char *s )
26 {
27  if (get_fd() > 0)
29  COUT<<s<<" :\tWarning: disconnected"<<strerror( errno )<<endl;
30 }
31 
33 {
34  COUT<<endl
35  <<endl
36  <<"------------------------------------------"<<endl
37  <<"ServerSocketTCP for "<<get_fd()<<" selected"<<endl
38  <<"------------------------------------------"<<endl
39  <<endl
40  <<endl;
41 
42  struct sockaddr_in remote_ip;
43 #if defined (_WIN32) || defined (MAC_OS_X_VERSION_10_3) || defined (MAC_OS_X_VERSION_10_2) || defined (MAC_OS_X_VERSION_10_1)
44  int
45 #else
46  socklen_t
47 #endif
48  len = sizeof (struct sockaddr_in);
49  int sock = ::accept( get_fd(), (sockaddr*) &remote_ip, &len );
50  if (sock > 0) {
51  COUT<<"accepted new sock "<<sock<<endl;
52  SOCKETALT newsock( sock, SOCKETALT::TCP, remote_ip, *_set );
53  _ac_mx.lock();
54  _accepted_connections.push( newsock );
55  _ac_mx.unlock();
56  if (_set) _set->add_pending( get_fd() );
57  return true;
58  } else {
59  COUT<<"accept failed"<<endl;
60  COUT<<"Error accepting new conn: "<<vsnetLastError()<<endl;
61  return false;
62  }
63 }
64 
66  ServerSocket( fd, adr, "ServerSocketTCP", set )
67 {}
68 
70 {
71  _ac_mx.lock();
72  bool ret = (_accepted_connections.empty() == false);
73  _ac_mx.unlock();
74  return ret;
75 }
76 
78 {
79  _ac_mx.lock();
80  if ( !_accepted_connections.empty() ) {
81  COUT<<"A connection has been accepted"<<endl;
82  SOCKETALT ret( _accepted_connections.front() );
83  _accepted_connections.pop();
84  _ac_mx.unlock();
85  return ret;
86  } else {
87  COUT<<"No accepted TCP connection"<<endl;
88  _ac_mx.unlock();
89  if (_set) _set->rem_pending( get_fd() );
90  SOCKETALT ret;
91  return ret;
92  }
93 }
94 
96  ServerSocket( fd, adr, "ServerSocketUDP", set )
97 {}
98 
100 {
101  return false;
102 }
103 
105 {
106  SOCKETALT ret( get_fd(), SOCKETALT::UDP, _srv_ip, *_set );
107  return ret;
108 }
109