]> git.saurik.com Git - wxWidgets.git/blob - src/msw/gsocket.c
removed mpthread and added thread.cpp and tglbutton.cpp
[wxWidgets.git] / src / msw / gsocket.c
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
3 * Name: gsocket.c
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket main MSW file
6 * Licence: The wxWidgets licence
7 * CVSID: $Id$
8 * -------------------------------------------------------------------------
9 */
10
11 /*
12 * PLEASE don't put C++ comments here - this is a C source file.
13 */
14
15 #ifdef _MSC_VER
16 /* RPCNOTIFICATION_ROUTINE in rasasync.h (included from winsock.h),
17 * warning: conditional expression is constant.
18 */
19 # pragma warning(disable:4115)
20 /* FD_SET,
21 * warning: named type definition in parentheses.
22 */
23 # pragma warning(disable:4127)
24 /* GAddress_UNIX_GetPath,
25 * warning: unreferenced formal parameter.
26 */
27 # pragma warning(disable:4100)
28
29 #ifdef __WXWINCE__
30 /* windows.h results in tons of warnings at max warning level */
31 # ifdef _MSC_VER
32 # pragma warning(push, 1)
33 # endif
34 # include <windows.h>
35 # ifdef _MSC_VER
36 # pragma warning(pop)
37 # pragma warning(disable:4514)
38 # endif
39 #endif
40
41 #endif /* _MSC_VER */
42
43 #include <winsock.h>
44
45 #ifndef __GSOCKET_STANDALONE__
46 # include "wx/platform.h"
47 # include "wx/setup.h"
48 #endif
49
50 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
51
52 #ifndef __GSOCKET_STANDALONE__
53 # include "wx/msw/gsockmsw.h"
54 # include "wx/gsocket.h"
55 #else
56 # include "gsockmsw.h"
57 # include "gsocket.h"
58 #endif /* __GSOCKET_STANDALONE__ */
59
60 #ifndef __WXWINCE__
61 #include <assert.h>
62 #else
63 #define assert(x)
64 #ifndef isdigit
65 #define isdigit(x) (x > 47 && x < 58)
66 #endif
67 #include "wx/msw/wince/net.h"
68 #endif
69
70 #include <string.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <stddef.h>
74 #include <ctype.h>
75
76 /* if we use configure for MSW SOCKLEN_T will be already defined */
77 #ifndef SOCKLEN_T
78 # define SOCKLEN_T int
79 #endif
80
81 /* Table of GUI-related functions. We must call them indirectly because
82 * of wxBase and GUI separation: */
83
84 static struct GSocketGUIFunctionsTable *gs_gui_functions;
85
86 #define USE_GUI() (gs_gui_functions != NULL)
87
88 /* Define macros to simplify indirection: */
89 #define _GSocket_GUI_Init() \
90 if (gs_gui_functions) gs_gui_functions->GUI_Init()
91 #define _GSocket_GUI_Cleanup() \
92 if (gs_gui_functions) gs_gui_functions->GUI_Cleanup()
93 #define _GSocket_GUI_Init_Socket(socket) \
94 (gs_gui_functions ? gs_gui_functions->GUI_Init_Socket(socket) : 1)
95 #define _GSocket_GUI_Destroy_Socket(socket) \
96 if (gs_gui_functions) gs_gui_functions->GUI_Destroy_Socket(socket)
97 #define _GSocket_Enable_Events(socket) \
98 if (gs_gui_functions) gs_gui_functions->Enable_Events(socket)
99 #define _GSocket_Disable_Events(socket) \
100 if (gs_gui_functions) gs_gui_functions->Disable_Events(socket)
101 #define _GSocket_Install_Callback(socket, event) \
102 if (gs_gui_functions) gs_gui_functions->Install_Callback(socket, event)
103 #define _GSocket_Uninstall_Callback(socket, event) \
104 if (gs_gui_functions) gs_gui_functions->Uninstall_Callback(socket, event)
105
106 /* Global initialisers */
107
108 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable *guifunc)
109 {
110 gs_gui_functions = guifunc;
111 }
112
113 int GSocket_Init(void)
114 {
115 WSADATA wsaData;
116
117 if (gs_gui_functions)
118 {
119 if ( !gs_gui_functions->GUI_Init() )
120 return 0;
121 }
122
123 /* Initialize WinSocket */
124 return (WSAStartup((1 << 8) | 1, &wsaData) == 0);
125 }
126
127 void GSocket_Cleanup(void)
128 {
129 if (gs_gui_functions)
130 {
131 gs_gui_functions->GUI_Cleanup();
132 }
133
134 /* Cleanup WinSocket */
135 WSACleanup();
136 }
137
138 /* Constructors / Destructors for GSocket */
139
140 GSocket *GSocket_new(void)
141 {
142 int i, success;
143 GSocket *socket;
144
145 if ((socket = (GSocket *) malloc(sizeof(GSocket))) == NULL)
146 return NULL;
147
148 socket->m_fd = INVALID_SOCKET;
149 for (i = 0; i < GSOCK_MAX_EVENT; i++)
150 {
151 socket->m_cbacks[i] = NULL;
152 }
153 socket->m_detected = 0;
154 socket->m_local = NULL;
155 socket->m_peer = NULL;
156 socket->m_error = GSOCK_NOERROR;
157 socket->m_server = FALSE;
158 socket->m_stream = TRUE;
159 socket->m_non_blocking = FALSE;
160 socket->m_timeout.tv_sec = 10 * 60; /* 10 minutes */
161 socket->m_timeout.tv_usec = 0;
162 socket->m_establishing = FALSE;
163
164 /* Per-socket GUI-specific initialization */
165 success = _GSocket_GUI_Init_Socket(socket);
166 if (!success)
167 {
168 free(socket);
169 socket = NULL;
170 }
171
172 return socket;
173 }
174
175 void GSocket_close(GSocket *socket)
176 {
177 _GSocket_Disable_Events(socket);
178 closesocket(socket->m_fd);
179 socket->m_fd = INVALID_SOCKET;
180 }
181
182 void GSocket_destroy(GSocket *socket)
183 {
184 assert(socket != NULL);
185
186 /* Per-socket GUI-specific cleanup */
187 _GSocket_GUI_Destroy_Socket(socket);
188
189 /* Check that the socket is really shutdowned */
190 if (socket->m_fd != INVALID_SOCKET)
191 GSocket_Shutdown(socket);
192
193 /* Destroy private addresses */
194 if (socket->m_local)
195 GAddress_destroy(socket->m_local);
196
197 if (socket->m_peer)
198 GAddress_destroy(socket->m_peer);
199
200 /* Destroy the socket itself */
201 free(socket);
202 }
203
204 /* GSocket_Shutdown:
205 * Disallow further read/write operations on this socket, close
206 * the fd and disable all callbacks.
207 */
208 void GSocket_Shutdown(GSocket *socket)
209 {
210 int evt;
211
212 assert(socket != NULL);
213
214 /* If socket has been created, shutdown it */
215 if (socket->m_fd != INVALID_SOCKET)
216 {
217 shutdown(socket->m_fd, 2);
218 GSocket_close(socket);
219 }
220
221 /* Disable GUI callbacks */
222 for (evt = 0; evt < GSOCK_MAX_EVENT; evt++)
223 socket->m_cbacks[evt] = NULL;
224
225 socket->m_detected = GSOCK_LOST_FLAG;
226 }
227
228 /* Address handling */
229
230 /* GSocket_SetLocal:
231 * GSocket_GetLocal:
232 * GSocket_SetPeer:
233 * GSocket_GetPeer:
234 * Set or get the local or peer address for this socket. The 'set'
235 * functions return GSOCK_NOERROR on success, an error code otherwise.
236 * The 'get' functions return a pointer to a GAddress object on success,
237 * or NULL otherwise, in which case they set the error code of the
238 * corresponding GSocket.
239 *
240 * Error codes:
241 * GSOCK_INVSOCK - the socket is not valid.
242 * GSOCK_INVADDR - the address is not valid.
243 */
244 GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address)
245 {
246 assert(socket != NULL);
247
248 /* the socket must be initialized, or it must be a server */
249 if (socket->m_fd != INVALID_SOCKET && !socket->m_server)
250 {
251 socket->m_error = GSOCK_INVSOCK;
252 return GSOCK_INVSOCK;
253 }
254
255 /* check address */
256 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
257 {
258 socket->m_error = GSOCK_INVADDR;
259 return GSOCK_INVADDR;
260 }
261
262 if (socket->m_local)
263 GAddress_destroy(socket->m_local);
264
265 socket->m_local = GAddress_copy(address);
266
267 return GSOCK_NOERROR;
268 }
269
270 GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address)
271 {
272 assert(socket != NULL);
273
274 /* check address */
275 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
276 {
277 socket->m_error = GSOCK_INVADDR;
278 return GSOCK_INVADDR;
279 }
280
281 if (socket->m_peer)
282 GAddress_destroy(socket->m_peer);
283
284 socket->m_peer = GAddress_copy(address);
285
286 return GSOCK_NOERROR;
287 }
288
289 GAddress *GSocket_GetLocal(GSocket *socket)
290 {
291 GAddress *address;
292 struct sockaddr addr;
293 SOCKLEN_T size = sizeof(addr);
294 GSocketError err;
295
296 assert(socket != NULL);
297
298 /* try to get it from the m_local var first */
299 if (socket->m_local)
300 return GAddress_copy(socket->m_local);
301
302 /* else, if the socket is initialized, try getsockname */
303 if (socket->m_fd == INVALID_SOCKET)
304 {
305 socket->m_error = GSOCK_INVSOCK;
306 return NULL;
307 }
308
309 if (getsockname(socket->m_fd, &addr, &size) == SOCKET_ERROR)
310 {
311 socket->m_error = GSOCK_IOERR;
312 return NULL;
313 }
314
315 /* got a valid address from getsockname, create a GAddress object */
316 if ((address = GAddress_new()) == NULL)
317 {
318 socket->m_error = GSOCK_MEMERR;
319 return NULL;
320 }
321
322 if ((err = _GAddress_translate_from(address, &addr, size)) != GSOCK_NOERROR)
323 {
324 GAddress_destroy(address);
325 socket->m_error = err;
326 return NULL;
327 }
328
329 return address;
330 }
331
332 GAddress *GSocket_GetPeer(GSocket *socket)
333 {
334 assert(socket != NULL);
335
336 /* try to get it from the m_peer var */
337 if (socket->m_peer)
338 return GAddress_copy(socket->m_peer);
339
340 return NULL;
341 }
342
343 /* Server specific parts */
344
345 /* GSocket_SetServer:
346 * Sets up this socket as a server. The local address must have been
347 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
348 * Returns GSOCK_NOERROR on success, one of the following otherwise:
349 *
350 * Error codes:
351 * GSOCK_INVSOCK - the socket is in use.
352 * GSOCK_INVADDR - the local address has not been set.
353 * GSOCK_IOERR - low-level error.
354 */
355 GSocketError GSocket_SetServer(GSocket *sck)
356 {
357 u_long arg = 1;
358
359 assert(sck != NULL);
360
361 /* must not be in use */
362 if (sck->m_fd != INVALID_SOCKET)
363 {
364 sck->m_error = GSOCK_INVSOCK;
365 return GSOCK_INVSOCK;
366 }
367
368 /* the local addr must have been set */
369 if (!sck->m_local)
370 {
371 sck->m_error = GSOCK_INVADDR;
372 return GSOCK_INVADDR;
373 }
374
375 /* Initialize all fields */
376 sck->m_server = TRUE;
377 sck->m_stream = TRUE;
378 sck->m_oriented = TRUE;
379
380 /* Create the socket */
381 sck->m_fd = socket(sck->m_local->m_realfamily, SOCK_STREAM, 0);
382
383 if (sck->m_fd == INVALID_SOCKET)
384 {
385 sck->m_error = GSOCK_IOERR;
386 return GSOCK_IOERR;
387 }
388
389 ioctlsocket(sck->m_fd, FIONBIO, (u_long FAR *) &arg);
390 _GSocket_Enable_Events(sck);
391
392 /* allow a socket to re-bind if the socket is in the TIME_WAIT
393 state after being previously closed.
394 */
395 setsockopt(sck->m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(u_long));
396
397 /* Bind to the local address,
398 * retrieve the actual address bound,
399 * and listen up to 5 connections.
400 */
401 if ((bind(sck->m_fd, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||
402 (getsockname(sck->m_fd,
403 sck->m_local->m_addr,
404 (SOCKLEN_T *)&sck->m_local->m_len) != 0) ||
405 (listen(sck->m_fd, 5) != 0))
406 {
407 GSocket_close(sck);
408 sck->m_error = GSOCK_IOERR;
409 return GSOCK_IOERR;
410 }
411
412 return GSOCK_NOERROR;
413 }
414
415 /* GSocket_WaitConnection:
416 * Waits for an incoming client connection. Returns a pointer to
417 * a GSocket object, or NULL if there was an error, in which case
418 * the last error field will be updated for the calling GSocket.
419 *
420 * Error codes (set in the calling GSocket)
421 * GSOCK_INVSOCK - the socket is not valid or not a server.
422 * GSOCK_TIMEDOUT - timeout, no incoming connections.
423 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
424 * GSOCK_MEMERR - couldn't allocate memory.
425 * GSOCK_IOERR - low-level error.
426 */
427 GSocket *GSocket_WaitConnection(GSocket *sck)
428 {
429 GSocket *connection;
430 struct sockaddr from;
431 SOCKLEN_T fromlen = sizeof(from);
432 GSocketError err;
433 u_long arg = 1;
434
435 assert(sck != NULL);
436
437 /* Reenable CONNECTION events */
438 sck->m_detected &= ~GSOCK_CONNECTION_FLAG;
439
440 /* If the socket has already been created, we exit immediately */
441 if (sck->m_fd == INVALID_SOCKET || !sck->m_server)
442 {
443 sck->m_error = GSOCK_INVSOCK;
444 return NULL;
445 }
446
447 /* Create a GSocket object for the new connection */
448 connection = GSocket_new();
449
450 if (!connection)
451 {
452 sck->m_error = GSOCK_MEMERR;
453 return NULL;
454 }
455
456 /* Wait for a connection (with timeout) */
457 if (_GSocket_Input_Timeout(sck) == GSOCK_TIMEDOUT)
458 {
459 GSocket_destroy(connection);
460 /* sck->m_error set by _GSocket_Input_Timeout */
461 return NULL;
462 }
463
464 connection->m_fd = accept(sck->m_fd, &from, &fromlen);
465
466 if (connection->m_fd == INVALID_SOCKET)
467 {
468 if (WSAGetLastError() == WSAEWOULDBLOCK)
469 sck->m_error = GSOCK_WOULDBLOCK;
470 else
471 sck->m_error = GSOCK_IOERR;
472
473 GSocket_destroy(connection);
474 return NULL;
475 }
476
477 /* Initialize all fields */
478 connection->m_server = FALSE;
479 connection->m_stream = TRUE;
480 connection->m_oriented = TRUE;
481
482 /* Setup the peer address field */
483 connection->m_peer = GAddress_new();
484 if (!connection->m_peer)
485 {
486 GSocket_destroy(connection);
487 sck->m_error = GSOCK_MEMERR;
488 return NULL;
489 }
490 err = _GAddress_translate_from(connection->m_peer, &from, fromlen);
491 if (err != GSOCK_NOERROR)
492 {
493 GAddress_destroy(connection->m_peer);
494 GSocket_destroy(connection);
495 sck->m_error = err;
496 return NULL;
497 }
498
499 ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg);
500 _GSocket_Enable_Events(connection);
501
502 return connection;
503 }
504
505 /* Client specific parts */
506
507 /* GSocket_Connect:
508 * For stream (connection oriented) sockets, GSocket_Connect() tries
509 * to establish a client connection to a server using the peer address
510 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
511 * connection has been succesfully established, or one of the error
512 * codes listed below. Note that for nonblocking sockets, a return
513 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
514 * request can be completed later; you should use GSocket_Select()
515 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
516 * corresponding asynchronous events.
517 *
518 * For datagram (non connection oriented) sockets, GSocket_Connect()
519 * just sets the peer address established with GSocket_SetPeer() as
520 * default destination.
521 *
522 * Error codes:
523 * GSOCK_INVSOCK - the socket is in use or not valid.
524 * GSOCK_INVADDR - the peer address has not been established.
525 * GSOCK_TIMEDOUT - timeout, the connection failed.
526 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
527 * GSOCK_MEMERR - couldn't allocate memory.
528 * GSOCK_IOERR - low-level error.
529 */
530 GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
531 {
532 int ret, err;
533 u_long arg = 1;
534
535 assert(sck != NULL);
536
537 /* Enable CONNECTION events (needed for nonblocking connections) */
538 sck->m_detected &= ~GSOCK_CONNECTION_FLAG;
539
540 if (sck->m_fd != INVALID_SOCKET)
541 {
542 sck->m_error = GSOCK_INVSOCK;
543 return GSOCK_INVSOCK;
544 }
545
546 if (!sck->m_peer)
547 {
548 sck->m_error = GSOCK_INVADDR;
549 return GSOCK_INVADDR;
550 }
551
552 /* Streamed or dgram socket? */
553 sck->m_stream = (stream == GSOCK_STREAMED);
554 sck->m_oriented = TRUE;
555 sck->m_server = FALSE;
556 sck->m_establishing = FALSE;
557
558 /* Create the socket */
559 sck->m_fd = socket(sck->m_peer->m_realfamily,
560 sck->m_stream? SOCK_STREAM : SOCK_DGRAM, 0);
561
562 if (sck->m_fd == INVALID_SOCKET)
563 {
564 sck->m_error = GSOCK_IOERR;
565 return GSOCK_IOERR;
566 }
567
568 ioctlsocket(sck->m_fd, FIONBIO, (u_long FAR *) &arg);
569 _GSocket_Enable_Events(sck);
570
571 /* Connect it to the peer address, with a timeout (see below) */
572 ret = connect(sck->m_fd, sck->m_peer->m_addr, sck->m_peer->m_len);
573
574 if (ret == SOCKET_ERROR)
575 {
576 err = WSAGetLastError();
577
578 /* If connect failed with EWOULDBLOCK and the GSocket object
579 * is in blocking mode, we select() for the specified timeout
580 * checking for writability to see if the connection request
581 * completes.
582 */
583 if ((err == WSAEWOULDBLOCK) && (!sck->m_non_blocking))
584 {
585 err = _GSocket_Connect_Timeout(sck);
586
587 if (err != GSOCK_NOERROR)
588 {
589 GSocket_close(sck);
590 /* sck->m_error is set in _GSocket_Connect_Timeout */
591 }
592
593 return (GSocketError) err;
594 }
595
596 /* If connect failed with EWOULDBLOCK and the GSocket object
597 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
598 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
599 * this way if the connection completes, a GSOCK_CONNECTION
600 * event will be generated, if enabled.
601 */
602 if ((err == WSAEWOULDBLOCK) && (sck->m_non_blocking))
603 {
604 sck->m_establishing = TRUE;
605 sck->m_error = GSOCK_WOULDBLOCK;
606 return GSOCK_WOULDBLOCK;
607 }
608
609 /* If connect failed with an error other than EWOULDBLOCK,
610 * then the call to GSocket_Connect() has failed.
611 */
612 GSocket_close(sck);
613 sck->m_error = GSOCK_IOERR;
614 return GSOCK_IOERR;
615 }
616
617 return GSOCK_NOERROR;
618 }
619
620 /* Datagram sockets */
621
622 /* GSocket_SetNonOriented:
623 * Sets up this socket as a non-connection oriented (datagram) socket.
624 * Before using this function, the local address must have been set
625 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
626 * on success, or one of the following otherwise.
627 *
628 * Error codes:
629 * GSOCK_INVSOCK - the socket is in use.
630 * GSOCK_INVADDR - the local address has not been set.
631 * GSOCK_IOERR - low-level error.
632 */
633 GSocketError GSocket_SetNonOriented(GSocket *sck)
634 {
635 u_long arg = 1;
636
637 assert(sck != NULL);
638
639 if (sck->m_fd != INVALID_SOCKET)
640 {
641 sck->m_error = GSOCK_INVSOCK;
642 return GSOCK_INVSOCK;
643 }
644
645 if (!sck->m_local)
646 {
647 sck->m_error = GSOCK_INVADDR;
648 return GSOCK_INVADDR;
649 }
650
651 /* Initialize all fields */
652 sck->m_stream = FALSE;
653 sck->m_server = FALSE;
654 sck->m_oriented = FALSE;
655
656 /* Create the socket */
657 sck->m_fd = socket(sck->m_local->m_realfamily, SOCK_DGRAM, 0);
658
659 if (sck->m_fd == INVALID_SOCKET)
660 {
661 sck->m_error = GSOCK_IOERR;
662 return GSOCK_IOERR;
663 }
664
665 ioctlsocket(sck->m_fd, FIONBIO, (u_long FAR *) &arg);
666 _GSocket_Enable_Events(sck);
667
668 /* Bind to the local address,
669 * and retrieve the actual address bound.
670 */
671 if ((bind(sck->m_fd, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||
672 (getsockname(sck->m_fd,
673 sck->m_local->m_addr,
674 (SOCKLEN_T *)&sck->m_local->m_len) != 0))
675 {
676 GSocket_close(sck);
677 sck->m_error = GSOCK_IOERR;
678 return GSOCK_IOERR;
679 }
680
681 return GSOCK_NOERROR;
682 }
683
684 /* Generic IO */
685
686 /* Like recv(), send(), ... */
687 int GSocket_Read(GSocket *socket, char *buffer, int size)
688 {
689 int ret;
690
691 assert(socket != NULL);
692
693 /* Reenable INPUT events */
694 socket->m_detected &= ~GSOCK_INPUT_FLAG;
695
696 if (socket->m_fd == INVALID_SOCKET || socket->m_server)
697 {
698 socket->m_error = GSOCK_INVSOCK;
699 return -1;
700 }
701
702 /* If the socket is blocking, wait for data (with a timeout) */
703 if (_GSocket_Input_Timeout(socket) == GSOCK_TIMEDOUT)
704 return -1;
705
706 /* Read the data */
707 if (socket->m_stream)
708 ret = _GSocket_Recv_Stream(socket, buffer, size);
709 else
710 ret = _GSocket_Recv_Dgram(socket, buffer, size);
711
712 if (ret == SOCKET_ERROR)
713 {
714 if (WSAGetLastError() != WSAEWOULDBLOCK)
715 socket->m_error = GSOCK_IOERR;
716 else
717 socket->m_error = GSOCK_WOULDBLOCK;
718 return -1;
719 }
720
721 return ret;
722 }
723
724 int GSocket_Write(GSocket *socket, const char *buffer, int size)
725 {
726 int ret;
727
728 assert(socket != NULL);
729
730 if (socket->m_fd == INVALID_SOCKET || socket->m_server)
731 {
732 socket->m_error = GSOCK_INVSOCK;
733 return -1;
734 }
735
736 /* If the socket is blocking, wait for writability (with a timeout) */
737 if (_GSocket_Output_Timeout(socket) == GSOCK_TIMEDOUT)
738 return -1;
739
740 /* Write the data */
741 if (socket->m_stream)
742 ret = _GSocket_Send_Stream(socket, buffer, size);
743 else
744 ret = _GSocket_Send_Dgram(socket, buffer, size);
745
746 if (ret == SOCKET_ERROR)
747 {
748 if (WSAGetLastError() != WSAEWOULDBLOCK)
749 socket->m_error = GSOCK_IOERR;
750 else
751 socket->m_error = GSOCK_WOULDBLOCK;
752
753 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
754 * does). Once the first OUTPUT event is received, users can assume
755 * that the socket is writable until a read operation fails. Only then
756 * will further OUTPUT events be posted.
757 */
758 socket->m_detected &= ~GSOCK_OUTPUT_FLAG;
759 return -1;
760 }
761
762 return ret;
763 }
764
765 /* GSocket_Select:
766 * Polls the socket to determine its status. This function will
767 * check for the events specified in the 'flags' parameter, and
768 * it will return a mask indicating which operations can be
769 * performed. This function won't block, regardless of the
770 * mode (blocking | nonblocking) of the socket.
771 */
772 GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags)
773 {
774 if (!USE_GUI())
775 {
776 GSocketEventFlags result = 0;
777 fd_set readfds;
778 fd_set writefds;
779 fd_set exceptfds;
780
781 assert(socket != NULL);
782
783 FD_ZERO(&readfds);
784 FD_ZERO(&writefds);
785 FD_ZERO(&exceptfds);
786 FD_SET(socket->m_fd, &readfds);
787 if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG)
788 FD_SET(socket->m_fd, &writefds);
789 FD_SET(socket->m_fd, &exceptfds);
790
791 /* Check 'sticky' CONNECTION flag first */
792 result |= (GSOCK_CONNECTION_FLAG & socket->m_detected);
793
794 /* If we have already detected a LOST event, then don't try
795 * to do any further processing.
796 */
797 if ((socket->m_detected & GSOCK_LOST_FLAG) != 0)
798 {
799 socket->m_establishing = FALSE;
800
801 return (GSOCK_LOST_FLAG & flags);
802 }
803
804 /* Try select now */
805 if (select(socket->m_fd + 1, &readfds, &writefds, &exceptfds,
806 &socket->m_timeout) <= 0)
807 {
808 /* What to do here? */
809 return (result & flags);
810 }
811
812 /* Check for readability */
813 if (FD_ISSET(socket->m_fd, &readfds))
814 {
815 char c;
816
817 if (!socket->m_stream || recv(socket->m_fd, &c, 1, MSG_PEEK) > 0)
818 {
819 result |= GSOCK_INPUT_FLAG;
820 }
821 else
822 {
823 if (socket->m_server && socket->m_stream)
824 {
825 result |= GSOCK_CONNECTION_FLAG;
826 socket->m_detected |= GSOCK_CONNECTION_FLAG;
827 }
828 else
829 {
830 socket->m_detected = GSOCK_LOST_FLAG;
831 socket->m_establishing = FALSE;
832
833 /* LOST event: Abort any further processing */
834 return (GSOCK_LOST_FLAG & flags);
835 }
836 }
837 }
838
839 /* Check for writability */
840 if (FD_ISSET(socket->m_fd, &writefds))
841 {
842 if (socket->m_establishing && !socket->m_server)
843 {
844 int error;
845 SOCKLEN_T len = sizeof(error);
846
847 socket->m_establishing = FALSE;
848
849 getsockopt(socket->m_fd, SOL_SOCKET, SO_ERROR, (void*)&error, &len);
850
851 if (error)
852 {
853 socket->m_detected = GSOCK_LOST_FLAG;
854
855 /* LOST event: Abort any further processing */
856 return (GSOCK_LOST_FLAG & flags);
857 }
858 else
859 {
860 result |= GSOCK_CONNECTION_FLAG;
861 socket->m_detected |= GSOCK_CONNECTION_FLAG;
862 }
863 }
864 else
865 {
866 result |= GSOCK_OUTPUT_FLAG;
867 }
868 }
869
870 /* Check for exceptions and errors (is this useful in Unices?) */
871 if (FD_ISSET(socket->m_fd, &exceptfds))
872 {
873 socket->m_establishing = FALSE;
874 socket->m_detected = GSOCK_LOST_FLAG;
875
876 /* LOST event: Abort any further processing */
877 return (GSOCK_LOST_FLAG & flags);
878 }
879
880 return (result & flags);
881 }
882 else /* USE_GUI() */
883 {
884 assert(socket != NULL);
885 return flags & socket->m_detected;
886 }
887 }
888
889 /* Attributes */
890
891 /* GSocket_SetNonBlocking:
892 * Sets the socket to non-blocking mode. All IO calls will return
893 * immediately.
894 */
895 void GSocket_SetNonBlocking(GSocket *socket, int non_block)
896 {
897 assert(socket != NULL);
898
899 socket->m_non_blocking = non_block;
900 }
901
902 /* GSocket_SetTimeout:
903 * Sets the timeout for blocking calls. Time is expressed in
904 * milliseconds.
905 */
906 void GSocket_SetTimeout(GSocket *socket, unsigned long millis)
907 {
908 assert(socket != NULL);
909
910 socket->m_timeout.tv_sec = (millis / 1000);
911 socket->m_timeout.tv_usec = (millis % 1000) * 1000;
912 }
913
914 /* GSocket_GetError:
915 * Returns the last error occured for this socket. Note that successful
916 * operations do not clear this back to GSOCK_NOERROR, so use it only
917 * after an error.
918 */
919 GSocketError GSocket_GetError(GSocket *socket)
920 {
921 assert(socket != NULL);
922
923 return socket->m_error;
924 }
925
926 /* Callbacks */
927
928 /* GSOCK_INPUT:
929 * There is data to be read in the input buffer. If, after a read
930 * operation, there is still data available, the callback function will
931 * be called again.
932 * GSOCK_OUTPUT:
933 * The socket is available for writing. That is, the next write call
934 * won't block. This event is generated only once, when the connection is
935 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
936 * when the output buffer empties again. This means that the app should
937 * assume that it can write since the first OUTPUT event, and no more
938 * OUTPUT events will be generated unless an error occurs.
939 * GSOCK_CONNECTION:
940 * Connection succesfully established, for client sockets, or incoming
941 * client connection, for server sockets. Wait for this event (also watch
942 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
943 * GSOCK_LOST:
944 * The connection is lost (or a connection request failed); this could
945 * be due to a failure, or due to the peer closing it gracefully.
946 */
947
948 /* GSocket_SetCallback:
949 * Enables the callbacks specified by 'flags'. Note that 'flags'
950 * may be a combination of flags OR'ed toghether, so the same
951 * callback function can be made to accept different events.
952 * The callback function must have the following prototype:
953 *
954 * void function(GSocket *socket, GSocketEvent event, char *cdata)
955 */
956 void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags,
957 GSocketCallback callback, char *cdata)
958 {
959 int count;
960
961 assert(socket != NULL);
962
963 for (count = 0; count < GSOCK_MAX_EVENT; count++)
964 {
965 if ((flags & (1 << count)) != 0)
966 {
967 socket->m_cbacks[count] = callback;
968 socket->m_data[count] = cdata;
969 }
970 }
971 }
972
973 /* GSocket_UnsetCallback:
974 * Disables all callbacks specified by 'flags', which may be a
975 * combination of flags OR'ed toghether.
976 */
977 void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags)
978 {
979 int count;
980
981 assert(socket != NULL);
982
983 for (count = 0; count < GSOCK_MAX_EVENT; count++)
984 {
985 if ((flags & (1 << count)) != 0)
986 {
987 socket->m_cbacks[count] = NULL;
988 socket->m_data[count] = NULL;
989 }
990 }
991 }
992
993 /* Internals (IO) */
994
995 /* _GSocket_Input_Timeout:
996 * For blocking sockets, wait until data is available or
997 * until timeout ellapses.
998 */
999 GSocketError _GSocket_Input_Timeout(GSocket *socket)
1000 {
1001 fd_set readfds;
1002
1003 if (!socket->m_non_blocking)
1004 {
1005 FD_ZERO(&readfds);
1006 FD_SET(socket->m_fd, &readfds);
1007 if (select(0, &readfds, NULL, NULL, &socket->m_timeout) == 0)
1008 {
1009 socket->m_error = GSOCK_TIMEDOUT;
1010 return GSOCK_TIMEDOUT;
1011 }
1012 }
1013 return GSOCK_NOERROR;
1014 }
1015
1016 /* _GSocket_Output_Timeout:
1017 * For blocking sockets, wait until data can be sent without
1018 * blocking or until timeout ellapses.
1019 */
1020 GSocketError _GSocket_Output_Timeout(GSocket *socket)
1021 {
1022 fd_set writefds;
1023
1024 if (!socket->m_non_blocking)
1025 {
1026 FD_ZERO(&writefds);
1027 FD_SET(socket->m_fd, &writefds);
1028 if (select(0, NULL, &writefds, NULL, &socket->m_timeout) == 0)
1029 {
1030 socket->m_error = GSOCK_TIMEDOUT;
1031 return GSOCK_TIMEDOUT;
1032 }
1033 }
1034 return GSOCK_NOERROR;
1035 }
1036
1037 /* _GSocket_Connect_Timeout:
1038 * For blocking sockets, wait until the connection is
1039 * established or fails, or until timeout ellapses.
1040 */
1041 GSocketError _GSocket_Connect_Timeout(GSocket *socket)
1042 {
1043 fd_set writefds;
1044 fd_set exceptfds;
1045
1046 FD_ZERO(&writefds);
1047 FD_ZERO(&exceptfds);
1048 FD_SET(socket->m_fd, &writefds);
1049 FD_SET(socket->m_fd, &exceptfds);
1050 if (select(0, NULL, &writefds, &exceptfds, &socket->m_timeout) == 0)
1051 {
1052 socket->m_error = GSOCK_TIMEDOUT;
1053 return GSOCK_TIMEDOUT;
1054 }
1055 if (!FD_ISSET(socket->m_fd, &writefds))
1056 {
1057 socket->m_error = GSOCK_IOERR;
1058 return GSOCK_IOERR;
1059 }
1060
1061 return GSOCK_NOERROR;
1062 }
1063
1064 int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size)
1065 {
1066 return recv(socket->m_fd, buffer, size, 0);
1067 }
1068
1069 int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size)
1070 {
1071 struct sockaddr from;
1072 SOCKLEN_T fromlen = sizeof(from);
1073 int ret;
1074 GSocketError err;
1075
1076 ret = recvfrom(socket->m_fd, buffer, size, 0, &from, &fromlen);
1077
1078 if (ret == SOCKET_ERROR)
1079 return SOCKET_ERROR;
1080
1081 /* Translate a system address into a GSocket address */
1082 if (!socket->m_peer)
1083 {
1084 socket->m_peer = GAddress_new();
1085 if (!socket->m_peer)
1086 {
1087 socket->m_error = GSOCK_MEMERR;
1088 return -1;
1089 }
1090 }
1091 err = _GAddress_translate_from(socket->m_peer, &from, fromlen);
1092 if (err != GSOCK_NOERROR)
1093 {
1094 GAddress_destroy(socket->m_peer);
1095 socket->m_peer = NULL;
1096 socket->m_error = err;
1097 return -1;
1098 }
1099
1100 return ret;
1101 }
1102
1103 int _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size)
1104 {
1105 return send(socket->m_fd, buffer, size, 0);
1106 }
1107
1108 int _GSocket_Send_Dgram(GSocket *socket, const char *buffer, int size)
1109 {
1110 struct sockaddr *addr;
1111 int len, ret;
1112 GSocketError err;
1113
1114 if (!socket->m_peer)
1115 {
1116 socket->m_error = GSOCK_INVADDR;
1117 return -1;
1118 }
1119
1120 err = _GAddress_translate_to(socket->m_peer, &addr, &len);
1121 if (err != GSOCK_NOERROR)
1122 {
1123 socket->m_error = err;
1124 return -1;
1125 }
1126
1127 ret = sendto(socket->m_fd, buffer, size, 0, addr, len);
1128
1129 /* Frees memory allocated by _GAddress_translate_to */
1130 free(addr);
1131
1132 return ret;
1133 }
1134
1135
1136 /*
1137 * -------------------------------------------------------------------------
1138 * GAddress
1139 * -------------------------------------------------------------------------
1140 */
1141
1142 /* CHECK_ADDRESS verifies that the current address family is either
1143 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1144 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1145 * an appropiate error code.
1146 *
1147 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1148 */
1149 #define CHECK_ADDRESS(address, family) \
1150 { \
1151 if (address->m_family == GSOCK_NOFAMILY) \
1152 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1153 return address->m_error; \
1154 if (address->m_family != GSOCK_##family) \
1155 { \
1156 address->m_error = GSOCK_INVADDR; \
1157 return GSOCK_INVADDR; \
1158 } \
1159 }
1160
1161 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1162 { \
1163 if (address->m_family == GSOCK_NOFAMILY) \
1164 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1165 return retval; \
1166 if (address->m_family != GSOCK_##family) \
1167 { \
1168 address->m_error = GSOCK_INVADDR; \
1169 return retval; \
1170 } \
1171 }
1172
1173
1174 GAddress *GAddress_new(void)
1175 {
1176 GAddress *address;
1177
1178 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1179 return NULL;
1180
1181 address->m_family = GSOCK_NOFAMILY;
1182 address->m_addr = NULL;
1183 address->m_len = 0;
1184
1185 return address;
1186 }
1187
1188 GAddress *GAddress_copy(GAddress *address)
1189 {
1190 GAddress *addr2;
1191
1192 assert(address != NULL);
1193
1194 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1195 return NULL;
1196
1197 memcpy(addr2, address, sizeof(GAddress));
1198
1199 if (address->m_addr)
1200 {
1201 addr2->m_addr = (struct sockaddr *) malloc(addr2->m_len);
1202 if (addr2->m_addr == NULL)
1203 {
1204 free(addr2);
1205 return NULL;
1206 }
1207 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
1208 }
1209
1210 return addr2;
1211 }
1212
1213 void GAddress_destroy(GAddress *address)
1214 {
1215 assert(address != NULL);
1216
1217 if (address->m_addr)
1218 free(address->m_addr);
1219
1220 free(address);
1221 }
1222
1223 void GAddress_SetFamily(GAddress *address, GAddressType type)
1224 {
1225 assert(address != NULL);
1226
1227 address->m_family = type;
1228 }
1229
1230 GAddressType GAddress_GetFamily(GAddress *address)
1231 {
1232 assert(address != NULL);
1233
1234 return address->m_family;
1235 }
1236
1237 GSocketError _GAddress_translate_from(GAddress *address,
1238 struct sockaddr *addr, int len)
1239 {
1240 address->m_realfamily = addr->sa_family;
1241 switch (addr->sa_family)
1242 {
1243 case AF_INET:
1244 address->m_family = GSOCK_INET;
1245 break;
1246 case AF_UNIX:
1247 address->m_family = GSOCK_UNIX;
1248 break;
1249 #ifdef AF_INET6
1250 case AF_INET6:
1251 address->m_family = GSOCK_INET6;
1252 break;
1253 #endif
1254 default:
1255 {
1256 address->m_error = GSOCK_INVOP;
1257 return GSOCK_INVOP;
1258 }
1259 }
1260
1261 if (address->m_addr)
1262 free(address->m_addr);
1263
1264 address->m_len = len;
1265 address->m_addr = (struct sockaddr *) malloc(len);
1266
1267 if (address->m_addr == NULL)
1268 {
1269 address->m_error = GSOCK_MEMERR;
1270 return GSOCK_MEMERR;
1271 }
1272 memcpy(address->m_addr, addr, len);
1273
1274 return GSOCK_NOERROR;
1275 }
1276
1277 GSocketError _GAddress_translate_to(GAddress *address,
1278 struct sockaddr **addr, int *len)
1279 {
1280 if (!address->m_addr)
1281 {
1282 address->m_error = GSOCK_INVADDR;
1283 return GSOCK_INVADDR;
1284 }
1285
1286 *len = address->m_len;
1287 *addr = (struct sockaddr *) malloc(address->m_len);
1288 if (*addr == NULL)
1289 {
1290 address->m_error = GSOCK_MEMERR;
1291 return GSOCK_MEMERR;
1292 }
1293
1294 memcpy(*addr, address->m_addr, address->m_len);
1295 return GSOCK_NOERROR;
1296 }
1297
1298 /*
1299 * -------------------------------------------------------------------------
1300 * Internet address family
1301 * -------------------------------------------------------------------------
1302 */
1303
1304 GSocketError _GAddress_Init_INET(GAddress *address)
1305 {
1306 address->m_len = sizeof(struct sockaddr_in);
1307 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1308 if (address->m_addr == NULL)
1309 {
1310 address->m_error = GSOCK_MEMERR;
1311 return GSOCK_MEMERR;
1312 }
1313
1314 address->m_family = GSOCK_INET;
1315 address->m_realfamily = PF_INET;
1316 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
1317 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
1318
1319 return GSOCK_NOERROR;
1320 }
1321
1322 GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
1323 {
1324 struct hostent *he;
1325 struct in_addr *addr;
1326
1327 assert(address != NULL);
1328
1329 CHECK_ADDRESS(address, INET);
1330
1331 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1332
1333 addr->s_addr = inet_addr(hostname);
1334
1335 /* If it is a numeric host name, convert it now */
1336 if (addr->s_addr == INADDR_NONE)
1337 {
1338 struct in_addr *array_addr;
1339
1340 /* It is a real name, we solve it */
1341 if ((he = gethostbyname(hostname)) == NULL)
1342 {
1343 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1344 address->m_error = GSOCK_NOHOST;
1345 return GSOCK_NOHOST;
1346 }
1347 array_addr = (struct in_addr *) *(he->h_addr_list);
1348 addr->s_addr = array_addr[0].s_addr;
1349 }
1350 return GSOCK_NOERROR;
1351 }
1352
1353 GSocketError GAddress_INET_SetAnyAddress(GAddress *address)
1354 {
1355 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
1356 }
1357
1358 GSocketError GAddress_INET_SetHostAddress(GAddress *address,
1359 unsigned long hostaddr)
1360 {
1361 struct in_addr *addr;
1362
1363 assert(address != NULL);
1364
1365 CHECK_ADDRESS(address, INET);
1366
1367 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1368 addr->s_addr = htonl(hostaddr);;
1369
1370 return GSOCK_NOERROR;
1371 }
1372
1373 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
1374 const char *protocol)
1375 {
1376 struct servent *se;
1377 struct sockaddr_in *addr;
1378
1379 assert(address != NULL);
1380 CHECK_ADDRESS(address, INET);
1381
1382 if (!port)
1383 {
1384 address->m_error = GSOCK_INVPORT;
1385 return GSOCK_INVPORT;
1386 }
1387
1388 se = getservbyname(port, protocol);
1389 if (!se)
1390 {
1391 if (isdigit(port[0]))
1392 {
1393 int port_int;
1394
1395 port_int = atoi(port);
1396 addr = (struct sockaddr_in *)address->m_addr;
1397 addr->sin_port = htons((u_short) port_int);
1398 return GSOCK_NOERROR;
1399 }
1400
1401 address->m_error = GSOCK_INVPORT;
1402 return GSOCK_INVPORT;
1403 }
1404
1405 addr = (struct sockaddr_in *)address->m_addr;
1406 addr->sin_port = se->s_port;
1407
1408 return GSOCK_NOERROR;
1409 }
1410
1411 GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
1412 {
1413 struct sockaddr_in *addr;
1414
1415 assert(address != NULL);
1416 CHECK_ADDRESS(address, INET);
1417
1418 addr = (struct sockaddr_in *)address->m_addr;
1419 addr->sin_port = htons(port);
1420
1421 return GSOCK_NOERROR;
1422 }
1423
1424 GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1425 {
1426 struct hostent *he;
1427 char *addr_buf;
1428 struct sockaddr_in *addr;
1429
1430 assert(address != NULL);
1431 CHECK_ADDRESS(address, INET);
1432
1433 addr = (struct sockaddr_in *)address->m_addr;
1434 addr_buf = (char *)&(addr->sin_addr);
1435
1436 he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET);
1437 if (he == NULL)
1438 {
1439 address->m_error = GSOCK_NOHOST;
1440 return GSOCK_NOHOST;
1441 }
1442
1443 strncpy(hostname, he->h_name, sbuf);
1444
1445 return GSOCK_NOERROR;
1446 }
1447
1448 unsigned long GAddress_INET_GetHostAddress(GAddress *address)
1449 {
1450 struct sockaddr_in *addr;
1451
1452 assert(address != NULL);
1453 CHECK_ADDRESS_RETVAL(address, INET, 0);
1454
1455 addr = (struct sockaddr_in *)address->m_addr;
1456
1457 return ntohl(addr->sin_addr.s_addr);
1458 }
1459
1460 unsigned short GAddress_INET_GetPort(GAddress *address)
1461 {
1462 struct sockaddr_in *addr;
1463
1464 assert(address != NULL);
1465 CHECK_ADDRESS_RETVAL(address, INET, 0);
1466
1467 addr = (struct sockaddr_in *)address->m_addr;
1468 return ntohs(addr->sin_port);
1469 }
1470
1471 /*
1472 * -------------------------------------------------------------------------
1473 * Unix address family
1474 * -------------------------------------------------------------------------
1475 */
1476
1477 GSocketError _GAddress_Init_UNIX(GAddress *address)
1478 {
1479 assert (address != NULL);
1480 address->m_error = GSOCK_INVADDR;
1481 return GSOCK_INVADDR;
1482 }
1483
1484 GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
1485 {
1486 #if defined(__BORLANDC__)
1487 /* prevents unused variable message in Borland */
1488 (void)path;
1489 #endif
1490 assert (address != NULL);
1491 address->m_error = GSOCK_INVADDR;
1492 return GSOCK_INVADDR;
1493 }
1494
1495 GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
1496 {
1497 #if defined(__BORLANDC__)
1498 /* prevents unused variable message in Borland */
1499 (void)path;
1500 (void)sbuf;
1501 #endif
1502 assert (address != NULL);
1503 address->m_error = GSOCK_INVADDR;
1504 return GSOCK_INVADDR;
1505 }
1506
1507 #else /* !wxUSE_SOCKETS */
1508
1509 /*
1510 * Translation unit shouldn't be empty, so include this typedef to make the
1511 * compiler (VC++ 6.0, for example) happy
1512 */
1513 typedef void (*wxDummy)();
1514
1515 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
1516