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.9 by root, Wed Sep 20 21:53:50 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 {
179{ 170{
180 struct sockaddr_in insock; 171 struct sockaddr_in insock;
181 struct protoent *protox; 172 struct protoent *protox;
182 struct linger linger_opt; 173 struct linger linger_opt;
183 174
184#ifdef WIN32 /* ***win32 - we init a windows socket */
185 WSADATA w;
186
187 socket_info.max_filedescriptor = 1; /* used in select, ignored in winsockets */
188 WSAStartup (0x0101, &w); /* this setup all socket stuff */
189 /* ill include no error tests here, winsocket 1.1 should always work */
190 /* except some old win95 versions without tcp/ip stack */
191#else /* non windows */
192
193# ifdef HAVE_SYSCONF 175#ifdef HAVE_SYSCONF
194 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 ();
195# else 180# else
196# ifdef HAVE_GETDTABLESIZE
197 socket_info.max_filedescriptor = getdtablesize ();
198# else
199 "Unable to find usable function to get max filedescriptors"; 181 "Unable to find usable function to get max filedescriptors";
200# endif
201# endif 182# endif
202#endif /* win32 */ 183#endif
203 184
204 socket_info.timeout.tv_sec = 0; 185 socket_info.timeout.tv_sec = 0;
205 socket_info.timeout.tv_usec = 0; 186 socket_info.timeout.tv_usec = 0;
206 socket_info.nconns = 0; 187 socket_info.nconns = 0;
207 188
249 defined(__sun__) || defined(__linux__) || defined(SVR4) || \ 230 defined(__sun__) || defined(__linux__) || defined(SVR4) || \
250 defined(__FreeBSD__) || defined(__OpenBSD__) || \ 231 defined(__FreeBSD__) || defined(__OpenBSD__) || \
251 defined(WIN32) /* ---win32 add this here */ || \ 232 defined(WIN32) /* ---win32 add this here */ || \
252 defined(__GNU__) /* HURD */ 233 defined(__GNU__) /* HURD */
253 { 234 {
254# ifdef WIN32
255 char tmp = 1;
256# else
257 int tmp = 1; 235 int tmp = 1;
258# endif
259 236
260 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)))
261 { 238 {
262 LOG (llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror (errno)); 239 LOG (llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror (errno));
263 } 240 }
270#endif 247#endif
271 248
272 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))
273 { 250 {
274 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));
275#ifdef WIN32 /* ***win32: close() -> closesocket() */
276 shutdown (init_sockets[0].fd, SD_BOTH);
277 closesocket (init_sockets[0].fd);
278#else
279 close (init_sockets[0].fd); 252 close (init_sockets[0].fd);
280#endif /* win32 */
281 exit (-1); 253 exit (-1);
282 } 254 }
283 if (listen (init_sockets[0].fd, 5) == (-1)) 255 if (listen (init_sockets[0].fd, 5) == (-1))
284 { 256 {
285 LOG (llevError, "Cannot listen on socket: %s\n", strerror (errno)); 257 LOG (llevError, "Cannot listen on socket: %s\n", strerror (errno));
286#ifdef WIN32 /* ***win32: close() -> closesocket() */
287 shutdown (init_sockets[0].fd, SD_BOTH);
288 closesocket (init_sockets[0].fd);
289#else
290 close (init_sockets[0].fd); 258 close (init_sockets[0].fd);
291#endif /* win32 */
292 exit (-1); 259 exit (-1);
293 } 260 }
294 init_sockets[0].status = Ns_Add; 261 init_sockets[0].status = Ns_Add;
295 read_client_images (); 262 read_client_images ();
296} 263}
319 */ 286 */
320 287
321void 288void
322free_newsocket (NewSocket * ns) 289free_newsocket (NewSocket * ns)
323{ 290{
324#ifdef WIN32 /* ***win32: closesocket in windows style */
325 shutdown (ns->fd, SD_BOTH);
326 if (closesocket (ns->fd))
327 {
328#else
329 if (close (ns->fd)) 291 if (close (ns->fd))
330 { 292 {
331#endif /* win32 */
332 293
333#ifdef ESRV_DEBUG 294#ifdef ESRV_DEBUG
334 LOG (llevDebug, "Error closing socket %d\n", ns->fd); 295 LOG (llevDebug, "Error closing socket %d\n", ns->fd);
335#endif 296#endif
336 } 297 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines