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_file.cpp
Go to the documentation of this file.
1 #if defined (_WIN32)
2 #define in_addr_t unsigned long
3 #include <wchar.h>
4 #ifndef NOMINMAX
5 #define NOMINMAX
6 #endif //tells VCC not to generate min/max macros
7 #include <windows.h>
8 #include <io.h>
9 #else
10 #define SOCKET_ERROR -1
11 #if defined (__CYGWIN__)
12 #define in_addr_t unsigned long
13 #endif
14 #include <sys/time.h>
15 #include <sys/types.h>
16 //#if !defined(__APPLE__) && !defined(__CYGWIN__) && !defined(BSD)
17 //#include <error.h>
18 //#endif
19 #include <netdb.h>
20 #include <string.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #define micro_sleep( n ) \
31  do { \
32  struct timeval tv; \
33  tv.tv_usec = n%1000000; \
34  tv.tv_sec = n/1000000; \
35  select( 0, NULL, NULL, NULL, &tv ); \
36  } \
37  while (0)
38 
40 {
41  /*
42  * char c;
43  * if (read (socket,&c,1)==1) {
44  * printf ("read %c",c);
45  * printf ("seekin %d",lseek (socket,SEEK_CUR,-1));
46  *
47  * return true;
48  * }
49  * return false;
50  */
51 
52  off_t curpos = lseek( socket, SEEK_CUR, 0 );
53  off_t endpos = lseek( socket, SEEK_END, 0 );
54  if (curpos+1 != endpos) {
55  lseek( socket, SEEK_SET, curpos );
56  return true;
57  }
58  return false;
59 }
60 void fNET_close( int socket )
61 {
62  close( socket );
63 }
64 int fNET_Recv( int socket, char *data, int bytestoread )
65 {
66  int retval = 0;
67  while (retval == 0) {
68  retval = read( socket, data, bytestoread );
69  if (!retval) micro_sleep( 50000 );
70  }
71  return retval;
72 }
73 bool fNET_Read( int socket, char *data, int bytestoread )
74 {
75  int bytes_read = 0;
76  int ret;
77  while (bytes_read < bytestoread) {
78  ret = read( socket, data+bytes_read, bytestoread-bytes_read );
79  if (-1 == ret)
80  return false;
81  bytes_read += ret;
82  if (!ret) micro_sleep( 50000 );
83  }
84  return true;
85 }
86 
87 int fNET_Write( int socket, int bytestowrite, const char *data )
88 {
89  return write( socket, data, bytestowrite );
90 }
91 char fNET_fgetc( int socket )
92 {
93  char myc = '\0';
94  fNET_Recv( socket, &myc, sizeof (char) );
95  return myc;
96 }
97 void fNET_cleanup() {}
98 void fNET_startup() {}
99 bool fNET_getHostByName( const char *hostname, unsigned short port, sockaddr_in &connexto )
100 {
101  return false;
102 }
103 
104 int fNET_listen( unsigned short port, const char *hostname )
105 {
106  return -1;
107 }
108 int fNET_ConnectTo( const char *hostname, unsigned short port )
109 {
110  return -1;
111 }
112 int fNET_Accept( int hServerSocket )
113 {
114  return -1;
115 }
116 int fNET_AcceptFrom( unsigned short port, const char *hostname )
117 {
118  return -1;
119 }
120