logog
logger optimized for games
socket.hpp
Go to the documentation of this file.
1 
9 #ifndef __LOGOG_SOCKET_HPP_
10 #define __LOGOG_SOCKET_HPP_
11 
12 #ifdef LOGOG_FLAVOR_WINDOWS
13 #pragma comment(lib, "wsock32.lib")
14 #endif
15 
16 #include <memory.h>
17 
18 namespace logog
19 {
21 class Socket : public Target
22 {
23 public:
24  static int Initialize()
25  {
26  static bool bInitialized = false;
27 
28  if ( !bInitialized )
29  {
30 #ifdef LOGOG_FLAVOR_WINDOWS
31  WORD wVersionRequested;
32  WSADATA wsaData;
33  int err;
34  wVersionRequested = MAKEWORD( 2, 2 );
35 
36  err = WSAStartup( wVersionRequested, &wsaData );
37 
38  if ( err != 0 )
39  {
40 #ifdef LOGOG_INTERNAL_DEBUGGING
41  LOGOG_COUT << _LG("WSAStartup failed with error: ") << err << endl;
42 #endif
43  return 1;
44  }
45 
46 #endif
47  }
48 
49  bInitialized = true;
50 
51  return 0;
52  }
53 
54  static void Shutdown()
55  {
56 #ifdef LOGOG_FLAVOR_WINDOWS
57  WSACleanup();
58 #endif
59  }
60 
61  static const int MAXHOSTNAME = 255;
62 
64  int type = SOCK_STREAM,
65  int port = LOGOG_DEFAULT_PORT
66  )
67  {
68  m_Socket = -1;
69  m_nType = type;
70  m_nPort = port;
71  }
72 
73  virtual void Close()
74  {
75 #ifdef LOGOG_FLAVOR_WINDOWS
76  closesocket( m_Socket );
77 #endif
78 #ifdef LOGOG_FLAVOR_POSIX
79  close( m_Socket );
80 #endif
81  }
82 
83 #ifdef NYI
84  virtual int Create( int type,
85  int port )
86  {
87  char myname[MAXHOSTNAME+1];
88  int err;
89  struct sockaddr_in sa;
90  struct hostent *hp;
91 
92  memset( &sa, 0, sizeof( struct sockaddr_in ) ); /* clear our address */
93  gethostname( myname, MAXHOSTNAME ); /* who are we? */
94  hp= gethostbyname( myname ); /* get our address info */
95 
96  if ( hp == NULL ) /* we don't exist !? */
97  return -1;
98 
99  sa.sin_family= hp->h_addrtype; /* this is our host address */
100  sa.sin_port= (u_short)htons( (u_short)port ); /* this is our port number */
101 
102  if (( m_Socket = socket( AF_INET, type, 0 ) ) < 0 ) /* create socket */
103  return -1;
104 
105  if ( bind( m_Socket, (struct sockaddr*) &sa, sizeof( struct sockaddr_in ) ) < 0 )
106  {
107  Close();
108  return -1;
109  }
110 
111  if (( err = SetNonBlocking() ) != 0 )
112  return err;
113  }
114 
115 #endif // NYI
116 
117  virtual int SetNonBlocking()
118  {
119  int err;
120 
121 #ifdef LOGOG_FLAVOR_POSIX
122  int flags;
123  flags = socket_fcntl( m_Socket, F_GETFL, 0 );
124  flags |= O_NONBLOCK;
125  err = socket_fcntl( m_Socket, F_SETFL, flags );
126 #endif
127 
128 #ifdef LOGOG_FLAVOR_WINDOWS
129  unsigned long parg;
130  parg = 1;
131  err = ioctlsocket( m_Socket, FIONBIO, &parg );
132 #endif
133  return err;
134  }
135 
136  virtual int Output( const LOGOG_STRING &output ) = 0;
137 
138 protected:
139  int m_Socket;
140  int m_nType;
141  int m_nPort;
142 };
143 
145 {
146 
147 };
148 }
149 
150 #endif // __LOGOG_CHECKPOINT_HPP_
[Thread]
Definition: api.hpp:8
Definition: target.hpp:16
#define LOGOG_STRING
Definition: string.hpp:113
int m_Socket
Definition: socket.hpp:139
int m_nType
Definition: socket.hpp:140
virtual int Output(const LOGOG_STRING &output)=0
Socket(int type=SOCK_STREAM, int port=LOGOG_DEFAULT_PORT)
Definition: socket.hpp:63
virtual int SetNonBlocking()
Definition: socket.hpp:117
virtual void Close()
Definition: socket.hpp:73
#define _LG(x)
Definition: string.hpp:53
Definition: socket.hpp:144
Definition: socket.hpp:21
static const int MAXHOSTNAME
Definition: socket.hpp:61
static void Shutdown()
Definition: socket.hpp:54
int m_nPort
Definition: socket.hpp:141
#define LOGOG_COUT
#define LOGOG_DEFAULT_PORT
[STLTypes]
Definition: platform.hpp:195
static int Initialize()
Definition: socket.hpp:24