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
NetUIBase Class Reference

#include <netui.h>

Static Public Member Functions

static AddressIP lookupHost (const char *host, unsigned short port)
 
static int createClientSocket (const AddressIP &host, bool isTCP, bool isHTTP)
 
static int createServerSocket (const AddressIP &host, bool isTCP)
 

Detailed Description

Definition at line 31 of file netui.h.

Member Function Documentation

int NetUIBase::createClientSocket ( const AddressIP host,
bool  isTCP,
bool  isHTTP 
)
static

Definition at line 65 of file netui.cpp.

References bindFd(), VsnetOSS::close_socket(), COUT, NONBLOCKING_CONNECT, VsnetOSS::set_blocking(), VsnetOSS::socket(), SOCKET_ERROR, and static_initNetwork().

Referenced by createServerSocket(), NetUITCP::createSocket(), NetUIUDP::createSocket(), and VsnetHTTPSocket::reopenConnection().

66 {
68 
69  int local_fd;
70  if ( ( local_fd = VsnetOSS::socket( PF_INET, isTCP ? SOCK_STREAM : SOCK_DGRAM, 0 ) ) == -1 ) {
71  COUT<<"Could not create socket"<<std::endl;
72  return -1;
73  }
74  if (isTCP) {
75  COUT<<"Connecting to "<<inet_ntoa( remote_ip.sin_addr )<<" on port "<<ntohs( remote_ip.sin_port )<<std::endl;
76  //NETFIXME: Always doing nonblocking because it's good for your health.
77  //Plus, we use select() for every read/write, so it should be safe.
78  if (NONBLOCKING_CONNECT) //&&isHTTP) {
79  VsnetOSS::set_blocking( local_fd, false ); //Connect may hang... we don't want that.
80  //No, it really is good for your health. You don't have to stare at
81  //the screen for 3 minutes until the OS times out the connection.
82 
83 #if defined (_WIN32) && !defined (__CYGWIN__)
84  if (::connect( local_fd, (sockaddr*) &remote_ip, sizeof (struct sockaddr) ) == SOCKET_ERROR)
85 #else
86  if (::connect( local_fd, (sockaddr*) &remote_ip, sizeof (struct sockaddr) ) < 0)
87 #endif
88  {
89 #if defined (_WIN32) && !defined (__CYGWIN__)
90  int lasterr = WSAGetLastError();
91  if (lasterr != WSAEINPROGRESS && lasterr != WSAEWOULDBLOCK)
92 #else
93  if (errno != EINPROGRESS && errno != EWOULDBLOCK)
94 #endif
95  {
96  perror( "Can't connect to server " );
97  VsnetOSS::close_socket( local_fd );
98  return -1;
99  }
100  }
101  } else
102  //binds socket
103  if ( !bindFd( local_fd, remote_ip ) ) {
104  perror( "Can't bind socket" );
105  VsnetOSS::close_socket( local_fd );
106  return -1;
107  }
108  return local_fd;
109 }
int NetUIBase::createServerSocket ( const AddressIP host,
bool  isTCP 
)
static

Definition at line 111 of file netui.cpp.

References VsnetOSS::close_socket(), COUT, createClientSocket(), VsnetOSS::socket(), SOCKET_ERROR, and static_initNetwork().

Referenced by NetUITCP::createServerSocket(), and NetUIUDP::createServerSocket().

112 {
113  if (!isTCP)
114  return createClientSocket( local_ip, false, false );
116 
117  int local_fd;
118  if ( ( local_fd = VsnetOSS::socket( PF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
119  COUT<<"Could not create socket"<<std::endl;
120  return -1;
121  }
122  int newval = 1;
123  char *buf = (char*) &newval;
124  setsockopt( local_fd, SOL_SOCKET, SO_REUSEADDR, buf, sizeof (int) );
125 
126  //binds socket
127  COUT<<"Bind on "<<ntohl( local_ip.sin_addr.s_addr )<<", port "
128  <<ntohs( local_ip.sin_port )<<std::endl;
129  if (bind( local_fd, (sockaddr*) &local_ip, sizeof (struct sockaddr_in) ) == SOCKET_ERROR) {
130  perror( "Problem binding socket" );
131  VsnetOSS::close_socket( local_fd );
132  return -1;
133  }
134  COUT<<"Accepting max : "<<SOMAXCONN<<std::endl;
135  if (listen( local_fd, SOMAXCONN ) == SOCKET_ERROR) {
136  perror( "Problem listening on socket" );
137  VsnetOSS::close_socket( local_fd );
138  return -1;
139  }
140  return local_fd;
141 }
AddressIP NetUIBase::lookupHost ( const char *  host,
unsigned short  port 
)
static

Definition at line 28 of file netui.cpp.

References COUT, VsnetOSS::inet_aton(), VsnetOSS::memcpy(), and static_initNetwork().

Referenced by NetUITCP::createServerSocket(), NetUIUDP::createServerSocket(), NetUITCP::createSocket(), NetUIUDP::createSocket(), remoteIPFromURI(), and NetClient::synchronizeTime().

29 {
31 
32  AddressIP remote_ip;
33  memset( &remote_ip, 0, sizeof (AddressIP) );
34 
35  struct hostent *he = NULL;
36  //Gets the host info for host
37  if (host[0] < 48 || host[0] > 57) {
38  COUT<<"Resolving host name... ";
39  if ( ( he = gethostbyname( host ) ) == NULL ) {
40  cerr<<"Could not resolve hostname"<<std::endl;
41  return AddressIP();
42  }
43  memcpy( &remote_ip.sin_addr.s_addr, he->h_addr_list[0], he->h_length );
44  cerr<<"found : "<<inet_ntoa( remote_ip.sin_addr )<<std::endl;
45  } else if (VsnetOSS::inet_aton( host, &remote_ip.sin_addr ) == 0) {
46  COUT<<"Error inet_aton"<<std::endl;
47  return AddressIP();
48  }
49  //Store it in srv_ip struct
50  remote_ip.sin_port = htons( port );
51  remote_ip.sin_family = AF_INET;
52  return remote_ip;
53 }

The documentation for this class was generated from the following files: