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
inet.h File Reference

Go to the source code of this file.

Functions

bool INET_BytesToRead (int socket)
 
bool INET_Read (int socket, char *data, int bytestoread)
 
int INET_Recv (int socket, char *data, int bytestoread)
 
int INET_Write (int socket, int bytestowrite, const char *data)
 
int INET_listen (unsigned short port, const char *addr=NULL)
 
int INET_Accept (int hServerSocket)
 
int INET_AcceptFrom (unsigned short port, const char *addr=NULL)
 
int INET_ConnectTo (const char *hostname, unsigned short port)
 
char INET_fgetc (int socket)
 
void INET_close (int socket)
 
void INET_startup ()
 
void INET_cleanup ()
 

Function Documentation

int INET_Accept ( int  hServerSocket)

Definition at line 164 of file inet.cpp.

Referenced by INET_AcceptFrom().

165 {
166  sockaddr_in Address;
167 #if defined (_WIN32) || defined (__CYGWIN__) || defined (MAC_OS_X_VERSION_10_3) || defined (MAC_OS_X_VERSION_10_2) \
168  || defined (MAC_OS_X_VERSION_10_1)
169  int
170 #else
171  socklen_t
172 #endif
173  nAddressSize = sizeof (Address);
174  return accept( hServerSocket, (struct sockaddr*) &Address, &nAddressSize );
175 }
int INET_AcceptFrom ( unsigned short  port,
const char *  addr = NULL 
)

Definition at line 177 of file inet.cpp.

References INET_Accept(), INET_close(), and INET_listen().

Referenced by main().

178 {
179  int hServerSocket = INET_listen( port, hostname );
180  int hSocket = INET_Accept( hServerSocket );
181  INET_close( hServerSocket );
182  return hSocket;
183 }
bool INET_BytesToRead ( int  socket)

Definition at line 26 of file inet.cpp.

Referenced by ForkedProcess(), ForwardBytes(), Music::Listen(), main(), and my_bytestoread().

27 {
28 #ifdef _WIN32
29  unsigned long datato;
30  if ( 0 == ioctlsocket( socket, FIONREAD, &datato ) )
31  return datato > 0;
32 #else
33  struct timeval tv;
34  tv.tv_sec = 0;
35  tv.tv_usec = 00000;
36  int selres = 0;
37  fd_set rfds;
38  FD_ZERO( &rfds );
39  FD_SET( socket, &rfds );
40  if ( ( selres = select( socket+1, &rfds, NULL, NULL, &tv ) ) ) {
41  if (selres == -1)
42  return false;
43  return true;
44  }
45 #endif
46  return false;
47 }
void INET_cleanup ( )

Definition at line 56 of file inet.cpp.

Referenced by main(), and Music::~Music().

57 {
58 #ifdef _WIN32
59  WSACleanup();
60 #endif
61 }
void INET_close ( int  socket)

Definition at line 48 of file inet.cpp.

Referenced by FinishOlde(), INET_AcceptFrom(), main(), and Music::~Music().

49 {
50 #ifdef _WIN32
51  closesocket( socket );
52 #else
53  close( socket );
54 #endif
55 }
int INET_ConnectTo ( const char *  hostname,
unsigned short  port 
)

Definition at line 184 of file inet.cpp.

References fprintf, INET_getHostByName(), and VsnetOSS::socket().

Referenced by mmoc::connectTo(), main(), and Music::Music().

185 {
186  sockaddr_in connexto;
187  int aftype = AF_INET;
188 #ifdef _WIN32
189  aftype = PF_INET;
190 #endif
191  int retval = -1;
192  int my_socket = -1;
193  if ( INET_getHostByName( hostname, port, connexto ) ) {
194  my_socket = socket( aftype, SOCK_STREAM, 0 );
195  retval = true;
196  if (connect( my_socket, (struct sockaddr*) &connexto, sizeof (connexto) ) != -1) {
197  return my_socket;
198  } else {
199  retval = -1;
200 #ifdef _WIN32
201  fprintf( stderr, "Socket Error %d", WSAGetLastError() );
202 #endif
203  }
204  }
205  return retval;
206 }
char INET_fgetc ( int  socket)

Definition at line 91 of file inet.cpp.

References INET_Read().

Referenced by Music::Listen(), main(), my_getchar(), and my_getstring().

92 {
93  char myc = '\0';
94  INET_Read( socket, &myc, sizeof (char) );
95  return myc;
96 }
int INET_listen ( unsigned short  port,
const char *  addr = NULL 
)

Definition at line 122 of file inet.cpp.

References INET_getHostByName(), VsnetOSS::socket(), and SOCKET_ERROR.

Referenced by INET_AcceptFrom().

123 {
124  int listenqueue = 5;
125  int hServerSocket; //so signal can be caught;
126  struct sockaddr_in Address; //Internet socket address stuct
127 #if defined (_WIN32) || defined (__CYGWIN__) || defined (MAC_OS_X_VERSION_10_3) || defined (MAC_OS_X_VERSION_10_2) \
128  || defined (MAC_OS_X_VERSION_10_1)
129  int
130 #else
131  socklen_t
132 #endif
133  nAddressSize = sizeof (struct sockaddr_in);
134  hServerSocket = socket( AF_INET, SOCK_STREAM, 0 );
135  int sockerr = SOCKET_ERROR;
136 #ifdef _WIN32
137  sockerr = INVALID_SOCKET;
138 #endif
139  if (hServerSocket == sockerr) {
140 #ifdef _WIN32
141  int err = WSAGetLastError();
142  printf( "\nCould not make a socket %d\n", err );
143 #endif
144 
145  return -1;
146  }
147  INET_getHostByName( hostname, port, Address );
148  Address.sin_addr.s_addr = INADDR_ANY;
149  Address.sin_family = AF_INET;
150  if (bind( hServerSocket, (struct sockaddr*) &Address, sizeof (Address) )
151  == SOCKET_ERROR) {
152  printf( "\nCould not connect to host\n" );
153  printf( "%d Error ", errno );
154  return -1;
155  }
156  getsockname( hServerSocket, (struct sockaddr*) &Address, &nAddressSize );
157  if (listen( hServerSocket, listenqueue ) == SOCKET_ERROR) {
158  printf( "\nCould not listen\n" );
159  return -1;
160  }
161  return hServerSocket;
162  //get the connected socket
163 }
bool INET_Read ( int  socket,
char *  data,
int  bytestoread 
)

Definition at line 66 of file inet.cpp.

References VsnetOSS::recv(), and SOCKET_ERROR.

Referenced by INET_fgetc(), and mmoc::listenThread().

67 {
68  int bytes_read = 0;
69  int ret;
70  while (bytes_read < bytestoread) {
71  ret = recv( socket, data+bytes_read, bytestoread-bytes_read, 0 );
72  if (ret == 0 || SOCKET_ERROR == ret)
73  return false;
74  bytes_read += ret;
75  }
76  return true;
77 }
int INET_Recv ( int  socket,
char *  data,
int  bytestoread 
)

Definition at line 62 of file inet.cpp.

References VsnetOSS::recv().

Referenced by ForwardBytes(), and mmoc::listenThread().

63 {
64  return recv( socket, data, bytestoread, 0 );
65 }
void INET_startup ( )

Definition at line 83 of file inet.cpp.

Referenced by main(), mmoc::mmoc(), and Music::Music().

84 {
85 #ifdef _WIN32
86  WORD wVersionRequested = MAKEWORD( 1, 1 );
87  WSADATA wsaData;
88  WSAStartup( wVersionRequested, &wsaData );
89 #endif
90 }
int INET_Write ( int  socket,
int  bytestowrite,
const char *  data 
)

Definition at line 79 of file inet.cpp.

Referenced by FinishOlde(), ForwardBytes(), ForwardFileBytes(), main(), Music::Music(), music_finished(), mmoc::send(), and Music::~Music().

80 {
81  return send( socket, data, bytestowrite, 0 );
82 }