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_oss.cpp
Go to the documentation of this file.
1 #ifndef VSNET_OSS_CPP
2 #define VSNET_OSS_CPP
3 #include <stdio.h>
4 #include <config.h>
5 
6 #include "vsnet_headers.h"
7 
8 #if !defined (_WIN32) || defined (__CYGWIN__)
9 #include <sys/ioctl.h>
10 #endif
11 
12 #if defined (__SVR4) && defined (__sun)
13 #include <sys/filio.h>
14 #endif
15 
16 #include <iostream>
17 
18 #include "vsnet_oss.h"
19 
20 namespace VsnetOSS
21 {
22 INLINE int close_socket( int fd )
23 {
24 #if defined (_WIN32) && !defined (__CYGWIN__)
25  return ::closesocket( fd );
26 
27 #else
28  return ::close( fd );
29 #endif
30 }
31 
32 INLINE int inet_aton( const char *host, struct in_addr *inp )
33 {
34 #if defined (_WIN32) && !defined (__CYGWIN__)
35  inp->s_addr = ::inet_addr( host );
36  if (inp->s_addr == INADDR_NONE) return 0;
37  return 1;
38 
39 #else
40  return ::inet_aton( host, inp );
41 #endif
42 }
43 
44 INLINE int socket( int domain, int type, int protocol )
45 {
46  int ret = ::socket( domain, type, protocol );
47 #if defined (_WIN32) && !defined (__CYGWIN__)
48  if (ret == INVALID_SOCKET) return -1;
49 #else
50  if (ret < 0) return -1;
51 #endif
52  return ret;
53 }
54 
55 INLINE int recv( int fd, void *buf, unsigned int len, int flags )
56 {
57 #if defined (_WIN32) && !defined (__CYGWIN__)
58  int ret = ::recv( fd, (char*) buf, len, flags );
59 #else
60  int ret = ::recv( fd, buf, len, flags );
61 #endif
62  return ret;
63 }
64 
65 INLINE void memcpy( void *dest, const void *src, int bytesize )
66 {
67  /* If your memcpy needs a (char* src), make ifdefs and a typecast
68  * here.
69  */
70  ::memcpy( dest, src, bytesize );
71 }
72 
73 bool set_blocking( int _fd, bool isBlocking )
74 {
75  if (_fd == -1)
76  return false;
77 #if !defined (_WIN32) || defined (__CYGWIN__)
78  int datato = isBlocking ? 0 : 1;
79  if (::ioctl( _fd, FIONBIO, &datato ) == -1) {
80 #if defined (_WIN32) || __GNUC__ != 2
81  ::perror( "Error fcntl : " );
82 #else
83  fprintf( stderr, "Error fcntl : " );
84 #endif
85  return false;
86  }
87 #else
88  unsigned long datato = isBlocking ? 0 : 1;
89  if (::ioctlsocket( _fd, FIONBIO, &datato ) != 0) {
90 #if defined (_WIN32) || __GNUC__ != 2
91  ::perror( "Error fcntl : " );
92 #else
93  fprintf( stderr, "Error fcntl : " );
94 #endif
95  return false;
96  }
97 #endif
98  return true;
99 }
100 };
101 
102 #endif /* VSNET_OSS_CPP */
103