ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/init.C
(Generate patch)

Comparing deliantra/server/socket/init.C (file contents):
Revision 1.8 by root, Thu Sep 14 22:34:05 2006 UTC vs.
Revision 1.10 by pippijn, Mon Dec 11 19:46:47 2006 UTC

35 35
36#include <global.h> 36#include <global.h>
37#ifndef __CEXTRACT__ 37#ifndef __CEXTRACT__
38# include <sproto.h> 38# include <sproto.h>
39#endif 39#endif
40#ifndef WIN32 /* ---win32 exclude include files */
41# include <sys/types.h> 40#include <sys/types.h>
42# include <sys/time.h> 41#include <sys/time.h>
43# include <sys/socket.h> 42#include <sys/socket.h>
44# include <netinet/in.h> 43#include <netinet/in.h>
45# include <netdb.h> 44#include <netdb.h>
46#endif /* win32 */
47 45
48#ifdef HAVE_UNISTD_H 46#ifdef HAVE_UNISTD_H
49# include <unistd.h> 47# include <unistd.h>
50#endif 48#endif
51 49
69 char buf[256]; 67 char buf[256];
70 int bufsize = 65535; /*Supposed absolute upper limit */ 68 int bufsize = 65535; /*Supposed absolute upper limit */
71 int oldbufsize; 69 int oldbufsize;
72 int buflen = sizeof (int); 70 int buflen = sizeof (int);
73 71
74#ifdef WIN32 /* ***WIN32 SOCKET: init win32 non blocking socket */
75 int temp = 1;
76
77 if (ioctlsocket (ns->fd, FIONBIO, &temp) == -1)
78 LOG (llevError, "InitConnection: Error on ioctlsocket.\n");
79#else
80 if (fcntl (ns->fd, F_SETFL, O_NONBLOCK) == -1) 72 if (fcntl (ns->fd, F_SETFL, O_NONBLOCK) == -1)
81 { 73 {
82 LOG (llevError, "InitConnection: Error on fcntl.\n"); 74 LOG (llevError, "InitConnection: Error on fcntl.\n");
83 } 75 }
84#endif /* end win32 */
85 76
86 if (getsockopt (ns->fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen) == -1) 77 if (getsockopt (ns->fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen) == -1)
87 oldbufsize = 0; 78 oldbufsize = 0;
88 if (oldbufsize < bufsize) 79 if (oldbufsize < bufsize)
89 { 80 {
108 ns->exp64 = 0; 99 ns->exp64 = 0;
109 ns->monitor_spells = 0; 100 ns->monitor_spells = 0;
110 ns->mapmode = Map0Cmd; 101 ns->mapmode = Map0Cmd;
111 ns->darkness = 1; 102 ns->darkness = 1;
112 ns->status = Ns_Add; 103 ns->status = Ns_Add;
113 ns->comment = NULL;
114 ns->old_mode = (Old_Mode) 0;
115 ns->mapx = 11; 104 ns->mapx = 11;
116 ns->mapy = 11; 105 ns->mapy = 11;
117 ns->newmapcmd = 0; 106 ns->newmapcmd = 0;
118 ns->itemcmd = 1; /* Default is version item1 command */ 107 ns->itemcmd = 1; /* Default is version item1 command */
119 ns->ext_mapinfos = 0; /*extendedmapinfo datas */ 108 ns->ext_mapinfos = 0; /*extendedmapinfo datas */
181{ 170{
182 struct sockaddr_in insock; 171 struct sockaddr_in insock;
183 struct protoent *protox; 172 struct protoent *protox;
184 struct linger linger_opt; 173 struct linger linger_opt;
185 174
186#ifdef WIN32 /* ***win32 - we init a windows socket */
187 WSADATA w;
188
189 socket_info.max_filedescriptor = 1; /* used in select, ignored in winsockets */
190 WSAStartup (0x0101, &w); /* this setup all socket stuff */
191 /* ill include no error tests here, winsocket 1.1 should always work */
192 /* except some old win95 versions without tcp/ip stack */
193#else /* non windows */
194
195# ifdef HAVE_SYSCONF 175#ifdef HAVE_SYSCONF
196 socket_info.max_filedescriptor = sysconf (_SC_OPEN_MAX); 176 socket_info.max_filedescriptor = sysconf (_SC_OPEN_MAX);
177#else
178# ifdef HAVE_GETDTABLESIZE
179 socket_info.max_filedescriptor = getdtablesize ();
197# else 180# else
198# ifdef HAVE_GETDTABLESIZE
199 socket_info.max_filedescriptor = getdtablesize ();
200# else
201 "Unable to find usable function to get max filedescriptors"; 181 "Unable to find usable function to get max filedescriptors";
202# endif
203# endif 182# endif
204#endif /* win32 */ 183#endif
205 184
206 socket_info.timeout.tv_sec = 0; 185 socket_info.timeout.tv_sec = 0;
207 socket_info.timeout.tv_usec = 0; 186 socket_info.timeout.tv_usec = 0;
208 socket_info.nconns = 0; 187 socket_info.nconns = 0;
209 188
251 defined(__sun__) || defined(__linux__) || defined(SVR4) || \ 230 defined(__sun__) || defined(__linux__) || defined(SVR4) || \
252 defined(__FreeBSD__) || defined(__OpenBSD__) || \ 231 defined(__FreeBSD__) || defined(__OpenBSD__) || \
253 defined(WIN32) /* ---win32 add this here */ || \ 232 defined(WIN32) /* ---win32 add this here */ || \
254 defined(__GNU__) /* HURD */ 233 defined(__GNU__) /* HURD */
255 { 234 {
256# ifdef WIN32
257 char tmp = 1;
258# else
259 int tmp = 1; 235 int tmp = 1;
260# endif
261 236
262 if (setsockopt (init_sockets[0].fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp))) 237 if (setsockopt (init_sockets[0].fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp)))
263 { 238 {
264 LOG (llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror (errno)); 239 LOG (llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror (errno));
265 } 240 }
272#endif 247#endif
273 248
274 if (bind (init_sockets[0].fd, (struct sockaddr *) &insock, sizeof (insock)) == (-1)) 249 if (bind (init_sockets[0].fd, (struct sockaddr *) &insock, sizeof (insock)) == (-1))
275 { 250 {
276 LOG (llevError, "Cannot bind socket to port %d: %s\n", ntohs (insock.sin_port), strerror (errno)); 251 LOG (llevError, "Cannot bind socket to port %d: %s\n", ntohs (insock.sin_port), strerror (errno));
277#ifdef WIN32 /* ***win32: close() -> closesocket() */
278 shutdown (init_sockets[0].fd, SD_BOTH);
279 closesocket (init_sockets[0].fd);
280#else
281 close (init_sockets[0].fd); 252 close (init_sockets[0].fd);
282#endif /* win32 */
283 exit (-1); 253 exit (-1);
284 } 254 }
285 if (listen (init_sockets[0].fd, 5) == (-1)) 255 if (listen (init_sockets[0].fd, 5) == (-1))
286 { 256 {
287 LOG (llevError, "Cannot listen on socket: %s\n", strerror (errno)); 257 LOG (llevError, "Cannot listen on socket: %s\n", strerror (errno));
288#ifdef WIN32 /* ***win32: close() -> closesocket() */
289 shutdown (init_sockets[0].fd, SD_BOTH);
290 closesocket (init_sockets[0].fd);
291#else
292 close (init_sockets[0].fd); 258 close (init_sockets[0].fd);
293#endif /* win32 */
294 exit (-1); 259 exit (-1);
295 } 260 }
296 init_sockets[0].status = Ns_Add; 261 init_sockets[0].status = Ns_Add;
297 read_client_images (); 262 read_client_images ();
298} 263}
321 */ 286 */
322 287
323void 288void
324free_newsocket (NewSocket * ns) 289free_newsocket (NewSocket * ns)
325{ 290{
326#ifdef WIN32 /* ***win32: closesocket in windows style */
327 shutdown (ns->fd, SD_BOTH);
328 if (closesocket (ns->fd))
329 {
330#else
331 if (close (ns->fd)) 291 if (close (ns->fd))
332 { 292 {
333#endif /* win32 */
334 293
335#ifdef ESRV_DEBUG 294#ifdef ESRV_DEBUG
336 LOG (llevDebug, "Error closing socket %d\n", ns->fd); 295 LOG (llevDebug, "Error closing socket %d\n", ns->fd);
337#endif 296#endif
338 } 297 }
298
339 if (ns->stats.range) 299 if (ns->stats.range)
340 free (ns->stats.range); 300 free (ns->stats.range);
301
341 if (ns->stats.title) 302 if (ns->stats.title)
342 free (ns->stats.title); 303 free (ns->stats.title);
343 if (ns->comment) 304
344 free (ns->comment);
345 free (ns->host); 305 free (ns->host);
346 free (ns->inbuf.buf); 306 free (ns->inbuf.buf);
347} 307}
348 308
349/** Sends the 'goodbye' command to the player, and closes connection. */ 309/** Sends the 'goodbye' command to the player, and closes connection. */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines