1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket main MSW file
7 * -------------------------------------------------------------------------
11 * PLEASE don't put C++ comments here - this is a C source file.
14 #ifndef __GSOCKET_STANDALONE__
18 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
20 #ifndef __GSOCKET_STANDALONE__
22 #include "wx/msw/gsockmsw.h"
23 #include "wx/gsocket.h"
25 #define INSTANCE wxGetInstance()
32 /* If not using wxWindows, a global var called hInst must
33 * be available and it must containt the app's instance
36 #define INSTANCE hInst
38 #endif /* __GSOCKET_STANDALONE__ */
50 #define CLASSNAME "_GSocket_Internal_Window_Class"
51 #define WINDOWNAME "_GSocket_Internal_Window_Name"
53 /* Maximum number of different GSocket objects at a given time.
54 * This value can be modified at will, but it CANNOT be greater
55 * than (0x7FFF - WM_USER + 1)
57 #define MAXSOCKETS 1024
59 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
60 #error "MAXSOCKETS is too big!"
64 /* Global variables */
66 extern HINSTANCE INSTANCE
;
68 static CRITICAL_SECTION critical
;
69 static GSocket
* socketList
[MAXSOCKETS
];
70 static int firstAvailable
;
72 /* Global initializers */
80 /* Create internal window for event notifications */
82 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
83 winClass
.cbClsExtra
= 0;
84 winClass
.cbWndExtra
= 0;
85 winClass
.hInstance
= INSTANCE
;
86 winClass
.hIcon
= (HICON
) NULL
;
87 winClass
.hCursor
= (HCURSOR
) NULL
;
88 winClass
.hbrBackground
= (HBRUSH
) NULL
;
89 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
90 winClass
.lpszClassName
= CLASSNAME
;
92 RegisterClass(&winClass
);
93 hWin
= CreateWindow(CLASSNAME
,
96 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
98 if (!hWin
) return FALSE
;
100 /* Initialize socket list */
101 InitializeCriticalSection(&critical
);
103 for (i
= 0; i
< MAXSOCKETS
; i
++)
105 socketList
[i
] = NULL
;
109 /* Initialize WinSocket */
110 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
113 void GSocket_Cleanup()
115 /* Destroy internal window */
117 UnregisterClass(CLASSNAME
, INSTANCE
);
119 /* Delete critical section */
120 DeleteCriticalSection(&critical
);
122 /* Cleanup WinSocket */
126 /* Constructors / Destructors for GSocket */
128 GSocket
*GSocket_new()
133 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
136 socket
->m_fd
= INVALID_SOCKET
;
137 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
139 socket
->m_cbacks
[i
] = NULL
;
141 socket
->m_local
= NULL
;
142 socket
->m_peer
= NULL
;
143 socket
->m_error
= GSOCK_NOERROR
;
144 socket
->m_server
= FALSE
;
145 socket
->m_stream
= TRUE
;
146 socket
->m_non_blocking
= FALSE
;
147 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
148 socket
->m_timeout
.tv_usec
= 0;
149 socket
->m_detected
= 0;
151 /* Allocate a new message number for this socket */
152 EnterCriticalSection(&critical
);
155 while (socketList
[i
] != NULL
)
157 i
= (i
+ 1) % MAXSOCKETS
;
159 if (i
== firstAvailable
) /* abort! */
162 LeaveCriticalSection(&critical
);
166 socketList
[i
] = socket
;
167 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
168 socket
->m_msgnumber
= (i
+ WM_USER
);
170 LeaveCriticalSection(&critical
);
175 void GSocket_destroy(GSocket
*socket
)
177 assert(socket
!= NULL
);
179 /* Remove the socket from the list */
180 EnterCriticalSection(&critical
);
181 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
182 LeaveCriticalSection(&critical
);
184 /* Check that the socket is really shutdowned */
185 if (socket
->m_fd
!= INVALID_SOCKET
)
186 GSocket_Shutdown(socket
);
188 /* Destroy private addresses */
190 GAddress_destroy(socket
->m_local
);
193 GAddress_destroy(socket
->m_peer
);
195 /* Destroy the socket itself */
200 * Disallow further read/write operations on this socket, close
201 * the fd and disable all callbacks.
203 void GSocket_Shutdown(GSocket
*socket
)
207 assert(socket
!= NULL
);
209 /* If socket has been created, shutdown it */
210 if (socket
->m_fd
!= INVALID_SOCKET
)
212 shutdown(socket
->m_fd
, 2);
213 closesocket(socket
->m_fd
);
214 socket
->m_fd
= INVALID_SOCKET
;
217 /* Disable GUI callbacks */
218 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
219 socket
->m_cbacks
[evt
] = NULL
;
221 socket
->m_detected
= 0;
222 _GSocket_Disable_Events(socket
);
225 /* Address handling */
231 * Set or get the local or peer address for this socket. The 'set'
232 * functions return GSOCK_NOERROR on success, an error code otherwise.
233 * The 'get' functions return a pointer to a GAddress object on success,
234 * or NULL otherwise, in which case they set the error code of the
235 * corresponding GSocket.
238 * GSOCK_INVSOCK - the socket is not valid.
239 * GSOCK_INVADDR - the address is not valid.
241 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
243 assert(socket
!= NULL
);
245 /* the socket must be initialized, or it must be a server */
246 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
248 socket
->m_error
= GSOCK_INVSOCK
;
249 return GSOCK_INVSOCK
;
253 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
255 socket
->m_error
= GSOCK_INVADDR
;
256 return GSOCK_INVADDR
;
260 GAddress_destroy(socket
->m_local
);
262 socket
->m_local
= GAddress_copy(address
);
264 return GSOCK_NOERROR
;
267 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
269 assert(socket
!= NULL
);
272 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
274 socket
->m_error
= GSOCK_INVADDR
;
275 return GSOCK_INVADDR
;
279 GAddress_destroy(socket
->m_peer
);
281 socket
->m_peer
= GAddress_copy(address
);
283 return GSOCK_NOERROR
;
286 GAddress
*GSocket_GetLocal(GSocket
*socket
)
289 struct sockaddr addr
;
290 SOCKLEN_T size
= sizeof(addr
);
293 assert(socket
!= NULL
);
295 /* try to get it from the m_local var first */
297 return GAddress_copy(socket
->m_local
);
299 /* else, if the socket is initialized, try getsockname */
300 if (socket
->m_fd
== INVALID_SOCKET
)
302 socket
->m_error
= GSOCK_INVSOCK
;
306 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
308 socket
->m_error
= GSOCK_IOERR
;
312 /* got a valid address from getsockname, create a GAddress object */
313 if ((address
= GAddress_new()) == NULL
)
315 socket
->m_error
= GSOCK_MEMERR
;
319 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
321 GAddress_destroy(address
);
322 socket
->m_error
= err
;
329 GAddress
*GSocket_GetPeer(GSocket
*socket
)
331 assert(socket
!= NULL
);
333 /* try to get it from the m_peer var */
335 return GAddress_copy(socket
->m_peer
);
340 /* Server specific parts */
342 /* GSocket_SetServer:
343 * Sets up this socket as a server. The local address must have been
344 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
345 * Returns GSOCK_NOERROR on success, one of the following otherwise:
348 * GSOCK_INVSOCK - the socket is in use.
349 * GSOCK_INVADDR - the local address has not been set.
350 * GSOCK_IOERR - low-level error.
352 GSocketError
GSocket_SetServer(GSocket
*sck
)
358 /* must not be in use */
359 if (sck
->m_fd
!= INVALID_SOCKET
)
361 sck
->m_error
= GSOCK_INVSOCK
;
362 return GSOCK_INVSOCK
;
365 /* the local addr must have been set */
368 sck
->m_error
= GSOCK_INVADDR
;
369 return GSOCK_INVADDR
;
372 /* Initialize all fields */
373 sck
->m_server
= TRUE
;
374 sck
->m_stream
= TRUE
;
375 sck
->m_oriented
= TRUE
;
377 /* Create the socket */
378 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
380 if (sck
->m_fd
== INVALID_SOCKET
)
382 sck
->m_error
= GSOCK_IOERR
;
386 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
387 _GSocket_Enable_Events(sck
);
389 /* Bind to the local address,
390 * retrieve the actual address bound,
391 * and listen up to 5 connections.
393 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
394 (getsockname(sck
->m_fd
,
395 sck
->m_local
->m_addr
,
396 &sck
->m_local
->m_len
) != 0) ||
397 (listen(sck
->m_fd
, 5) != 0))
399 closesocket(sck
->m_fd
);
400 sck
->m_fd
= INVALID_SOCKET
;
401 sck
->m_error
= GSOCK_IOERR
;
405 return GSOCK_NOERROR
;
408 /* GSocket_WaitConnection:
409 * Waits for an incoming client connection. Returns a pointer to
410 * a GSocket object, or NULL if there was an error, in which case
411 * the last error field will be updated for the calling GSocket.
413 * Error codes (set in the calling GSocket)
414 * GSOCK_INVSOCK - the socket is not valid or not a server.
415 * GSOCK_TIMEDOUT - timeout, no incoming connections.
416 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
417 * GSOCK_MEMERR - couldn't allocate memory.
418 * GSOCK_IOERR - low-level error.
420 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
423 struct sockaddr from
;
424 SOCKLEN_T fromlen
= sizeof(from
);
430 /* Reenable CONNECTION events */
431 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
433 /* If the socket has already been created, we exit immediately */
434 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
436 sck
->m_error
= GSOCK_INVSOCK
;
440 /* Create a GSocket object for the new connection */
441 connection
= GSocket_new();
445 sck
->m_error
= GSOCK_MEMERR
;
449 /* Wait for a connection (with timeout) */
450 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
452 GSocket_destroy(connection
);
453 /* sck->m_error set by _GSocket_Input_Timeout */
457 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
459 if (connection
->m_fd
== INVALID_SOCKET
)
461 if (WSAGetLastError() == WSAEWOULDBLOCK
)
462 sck
->m_error
= GSOCK_WOULDBLOCK
;
464 sck
->m_error
= GSOCK_IOERR
;
466 GSocket_destroy(connection
);
470 /* Initialize all fields */
471 connection
->m_server
= FALSE
;
472 connection
->m_stream
= TRUE
;
473 connection
->m_oriented
= TRUE
;
475 /* Setup the peer address field */
476 connection
->m_peer
= GAddress_new();
477 if (!connection
->m_peer
)
479 GSocket_destroy(connection
);
480 sck
->m_error
= GSOCK_MEMERR
;
483 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
484 if (err
!= GSOCK_NOERROR
)
486 GAddress_destroy(connection
->m_peer
);
487 GSocket_destroy(connection
);
492 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
493 _GSocket_Enable_Events(connection
);
498 /* Client specific parts */
501 * For stream (connection oriented) sockets, GSocket_Connect() tries
502 * to establish a client connection to a server using the peer address
503 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
504 * connection has been succesfully established, or one of the error
505 * codes listed below. Note that for nonblocking sockets, a return
506 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
507 * request can be completed later; you should use GSocket_Select()
508 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
509 * corresponding asynchronous events.
511 * For datagram (non connection oriented) sockets, GSocket_Connect()
512 * just sets the peer address established with GSocket_SetPeer() as
513 * default destination.
516 * GSOCK_INVSOCK - the socket is in use or not valid.
517 * GSOCK_INVADDR - the peer address has not been established.
518 * GSOCK_TIMEDOUT - timeout, the connection failed.
519 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
520 * GSOCK_MEMERR - couldn't allocate memory.
521 * GSOCK_IOERR - low-level error.
523 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
530 /* Enable CONNECTION events (needed for nonblocking connections) */
531 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
533 if (sck
->m_fd
!= INVALID_SOCKET
)
535 sck
->m_error
= GSOCK_INVSOCK
;
536 return GSOCK_INVSOCK
;
541 sck
->m_error
= GSOCK_INVADDR
;
542 return GSOCK_INVADDR
;
545 /* Streamed or dgram socket? */
546 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
547 sck
->m_oriented
= TRUE
;
548 sck
->m_server
= FALSE
;
550 /* Create the socket */
551 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
552 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
554 if (sck
->m_fd
== INVALID_SOCKET
)
556 sck
->m_error
= GSOCK_IOERR
;
560 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
561 _GSocket_Enable_Events(sck
);
563 /* Connect it to the peer address, with a timeout (see below) */
564 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
566 if (ret
== SOCKET_ERROR
)
568 err
= WSAGetLastError();
570 /* If connect failed with EWOULDBLOCK and the GSocket object
571 * is in blocking mode, we select() for the specified timeout
572 * checking for writability to see if the connection request
575 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
577 err
= _GSocket_Connect_Timeout(sck
);
579 if (err
!= GSOCK_NOERROR
)
581 closesocket(sck
->m_fd
);
582 sck
->m_fd
= INVALID_SOCKET
;
583 /* sck->m_error is set in _GSocket_Connect_Timeout */
589 /* If connect failed with EWOULDBLOCK and the GSocket object
590 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
591 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
592 * this way if the connection completes, a GSOCK_CONNECTION
593 * event will be generated, if enabled.
595 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
597 sck
->m_error
= GSOCK_WOULDBLOCK
;
598 return GSOCK_WOULDBLOCK
;
601 /* If connect failed with an error other than EWOULDBLOCK,
602 * then the call to GSocket_Connect() has failed.
604 closesocket(sck
->m_fd
);
605 sck
->m_fd
= INVALID_SOCKET
;
606 sck
->m_error
= GSOCK_IOERR
;
610 return GSOCK_NOERROR
;
613 /* Datagram sockets */
615 /* GSocket_SetNonOriented:
616 * Sets up this socket as a non-connection oriented (datagram) socket.
617 * Before using this function, the local address must have been set
618 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
619 * on success, or one of the following otherwise.
622 * GSOCK_INVSOCK - the socket is in use.
623 * GSOCK_INVADDR - the local address has not been set.
624 * GSOCK_IOERR - low-level error.
626 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
632 if (sck
->m_fd
!= INVALID_SOCKET
)
634 sck
->m_error
= GSOCK_INVSOCK
;
635 return GSOCK_INVSOCK
;
640 sck
->m_error
= GSOCK_INVADDR
;
641 return GSOCK_INVADDR
;
644 /* Initialize all fields */
645 sck
->m_stream
= FALSE
;
646 sck
->m_server
= FALSE
;
647 sck
->m_oriented
= FALSE
;
649 /* Create the socket */
650 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
652 if (sck
->m_fd
== INVALID_SOCKET
)
654 sck
->m_error
= GSOCK_IOERR
;
658 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
659 _GSocket_Enable_Events(sck
);
661 /* Bind to the local address,
662 * and retrieve the actual address bound.
664 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
665 (getsockname(sck
->m_fd
,
666 sck
->m_local
->m_addr
,
667 &sck
->m_local
->m_len
) != 0))
669 closesocket(sck
->m_fd
);
670 sck
->m_fd
= INVALID_SOCKET
;
671 sck
->m_error
= GSOCK_IOERR
;
675 return GSOCK_NOERROR
;
681 /* Like recv(), send(), ... */
682 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
686 assert(socket
!= NULL
);
688 /* Reenable INPUT events */
689 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
691 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
693 socket
->m_error
= GSOCK_INVSOCK
;
697 /* If the socket is blocking, wait for data (with a timeout) */
698 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
702 if (socket
->m_stream
)
703 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
705 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
707 if (ret
== SOCKET_ERROR
)
709 if (WSAGetLastError() != WSAEWOULDBLOCK
)
710 socket
->m_error
= GSOCK_IOERR
;
712 socket
->m_error
= GSOCK_WOULDBLOCK
;
719 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
723 assert(socket
!= NULL
);
725 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
727 socket
->m_error
= GSOCK_INVSOCK
;
731 /* If the socket is blocking, wait for writability (with a timeout) */
732 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
736 if (socket
->m_stream
)
737 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
739 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
741 if (ret
== SOCKET_ERROR
)
743 if (WSAGetLastError() != WSAEWOULDBLOCK
)
744 socket
->m_error
= GSOCK_IOERR
;
746 socket
->m_error
= GSOCK_WOULDBLOCK
;
748 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
749 * does). Once the first OUTPUT event is received, users can assume
750 * that the socket is writable until a read operation fails. Only then
751 * will further OUTPUT events be posted.
753 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
761 * Polls the socket to determine its status. This function will
762 * check for the events specified in the 'flags' parameter, and
763 * it will return a mask indicating which operations can be
764 * performed. This function won't block, regardless of the
765 * mode (blocking | nonblocking) of the socket.
767 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
769 assert(socket
!= NULL
);
771 return (flags
& socket
->m_detected
);
776 /* GSocket_SetNonBlocking:
777 * Sets the socket to non-blocking mode. All IO calls will return
780 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
782 assert(socket
!= NULL
);
784 socket
->m_non_blocking
= non_block
;
787 /* GSocket_SetTimeout:
788 * Sets the timeout for blocking calls. Time is expressed in
791 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
793 assert(socket
!= NULL
);
795 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
796 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
800 * Returns the last error occured for this socket. Note that successful
801 * operations do not clear this back to GSOCK_NOERROR, so use it only
804 GSocketError
GSocket_GetError(GSocket
*socket
)
806 assert(socket
!= NULL
);
808 return socket
->m_error
;
815 * There is data to be read in the input buffer. If, after a read
816 * operation, there is still data available, the callback function will
819 * The socket is available for writing. That is, the next write call
820 * won't block. This event is generated only once, when the connection is
821 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
822 * when the output buffer empties again. This means that the app should
823 * assume that it can write since the first OUTPUT event, and no more
824 * OUTPUT events will be generated unless an error occurs.
826 * Connection succesfully established, for client sockets, or incoming
827 * client connection, for server sockets. Wait for this event (also watch
828 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
830 * The connection is lost (or a connection request failed); this could
831 * be due to a failure, or due to the peer closing it gracefully.
834 /* GSocket_SetCallback:
835 * Enables the callbacks specified by 'flags'. Note that 'flags'
836 * may be a combination of flags OR'ed toghether, so the same
837 * callback function can be made to accept different events.
838 * The callback function must have the following prototype:
840 * void function(GSocket *socket, GSocketEvent event, char *cdata)
842 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
843 GSocketCallback callback
, char *cdata
)
847 assert(socket
!= NULL
);
849 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
851 if ((flags
& (1 << count
)) != 0)
853 socket
->m_cbacks
[count
] = callback
;
854 socket
->m_data
[count
] = cdata
;
859 /* GSocket_UnsetCallback:
860 * Disables all callbacks specified by 'flags', which may be a
861 * combination of flags OR'ed toghether.
863 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
867 assert(socket
!= NULL
);
869 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
871 if ((flags
& (1 << count
)) != 0)
873 socket
->m_cbacks
[count
] = NULL
;
874 socket
->m_data
[count
] = NULL
;
882 /* _GSocket_Enable_Events:
883 * Enable all event notifications; we need to be notified of all
884 * events for internal processing, but we will only notify users
885 * when an appropiate callback function has been installed.
887 void _GSocket_Enable_Events(GSocket
*socket
)
889 assert (socket
!= NULL
);
891 if (socket
->m_fd
!= INVALID_SOCKET
)
893 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
,
894 FD_READ
| FD_WRITE
| FD_ACCEPT
| FD_CONNECT
| FD_CLOSE
);
898 /* _GSocket_Disable_Events:
899 * Disable event notifications (when shutdowning the socket)
901 void _GSocket_Disable_Events(GSocket
*socket
)
903 assert (socket
!= NULL
);
905 if (socket
->m_fd
!= INVALID_SOCKET
)
907 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
912 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
919 GSocketCallback cback
;
922 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
924 EnterCriticalSection(&critical
);
925 socket
= socketList
[(uMsg
- WM_USER
)];
930 /* Check that the socket still exists (it has not been
931 * destroyed) and for safety, check that the m_fd field
932 * is what we expect it to be.
934 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
936 switch WSAGETSELECTEVENT(lParam
)
938 case FD_READ
: event
= GSOCK_INPUT
; break;
939 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
940 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
943 if (WSAGETSELECTERROR(lParam
) != 0)
946 event
= GSOCK_CONNECTION
;
949 case FD_CLOSE
: event
= GSOCK_LOST
; break;
954 cback
= socket
->m_cbacks
[event
];
955 data
= socket
->m_data
[event
];
957 if (event
== GSOCK_LOST
)
958 socket
->m_detected
= GSOCK_LOST_FLAG
;
960 socket
->m_detected
|= (1 << event
);
964 /* OK, we can now leave the critical section because we have
965 * already obtained the callback address (we make no further
966 * accesses to socket->whatever). However, the app should
967 * be prepared to handle events from a socket that has just
970 LeaveCriticalSection(&critical
);
973 (cback
)(socket
, event
, data
);
978 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
982 /* _GSocket_Input_Timeout:
983 * For blocking sockets, wait until data is available or
984 * until timeout ellapses.
986 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
990 if (!socket
->m_non_blocking
)
993 FD_SET(socket
->m_fd
, &readfds
);
994 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
996 socket
->m_error
= GSOCK_TIMEDOUT
;
997 return GSOCK_TIMEDOUT
;
1000 return GSOCK_NOERROR
;
1003 /* _GSocket_Output_Timeout:
1004 * For blocking sockets, wait until data can be sent without
1005 * blocking or until timeout ellapses.
1007 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1011 if (!socket
->m_non_blocking
)
1014 FD_SET(socket
->m_fd
, &writefds
);
1015 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
1017 socket
->m_error
= GSOCK_TIMEDOUT
;
1018 return GSOCK_TIMEDOUT
;
1021 return GSOCK_NOERROR
;
1024 /* _GSocket_Connect_Timeout:
1025 * For blocking sockets, wait until the connection is
1026 * established or fails, or until timeout ellapses.
1028 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
1033 if (!socket
->m_non_blocking
)
1036 FD_ZERO(&exceptfds
);
1037 FD_SET(socket
->m_fd
, &writefds
);
1038 FD_SET(socket
->m_fd
, &exceptfds
);
1039 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
1041 socket
->m_error
= GSOCK_TIMEDOUT
;
1042 return GSOCK_TIMEDOUT
;
1045 if (!FD_ISSET(socket
->m_fd
, &writefds
))
1047 socket
->m_error
= GSOCK_IOERR
;
1051 return GSOCK_NOERROR
;
1054 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1056 return recv(socket
->m_fd
, buffer
, size
, 0);
1059 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1061 struct sockaddr from
;
1062 SOCKLEN_T fromlen
= sizeof(from
);
1066 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1068 if (ret
== SOCKET_ERROR
)
1069 return SOCKET_ERROR
;
1071 /* Translate a system address into a GSocket address */
1072 if (!socket
->m_peer
)
1074 socket
->m_peer
= GAddress_new();
1075 if (!socket
->m_peer
)
1077 socket
->m_error
= GSOCK_MEMERR
;
1081 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1082 if (err
!= GSOCK_NOERROR
)
1084 GAddress_destroy(socket
->m_peer
);
1085 socket
->m_peer
= NULL
;
1086 socket
->m_error
= err
;
1093 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1095 return send(socket
->m_fd
, buffer
, size
, 0);
1098 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1100 struct sockaddr
*addr
;
1104 if (!socket
->m_peer
)
1106 socket
->m_error
= GSOCK_INVADDR
;
1110 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1111 if (err
!= GSOCK_NOERROR
)
1113 socket
->m_error
= err
;
1117 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1119 /* Frees memory allocated by _GAddress_translate_to */
1127 * -------------------------------------------------------------------------
1129 * -------------------------------------------------------------------------
1132 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1133 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1134 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1136 #define CHECK_ADDRESS(address, family, retval) \
1138 if (address->m_family == GSOCK_NOFAMILY) \
1139 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1140 return address->m_error; \
1141 if (address->m_family != GSOCK_##family) \
1143 address->m_error = GSOCK_INVADDR; \
1148 GAddress
*GAddress_new()
1152 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1155 address
->m_family
= GSOCK_NOFAMILY
;
1156 address
->m_addr
= NULL
;
1162 GAddress
*GAddress_copy(GAddress
*address
)
1166 assert(address
!= NULL
);
1168 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1171 memcpy(addr2
, address
, sizeof(GAddress
));
1173 if (address
->m_addr
)
1175 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1176 if (addr2
->m_addr
== NULL
)
1181 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1187 void GAddress_destroy(GAddress
*address
)
1189 assert(address
!= NULL
);
1194 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1196 assert(address
!= NULL
);
1198 address
->m_family
= type
;
1201 GAddressType
GAddress_GetFamily(GAddress
*address
)
1203 assert(address
!= NULL
);
1205 return address
->m_family
;
1208 GSocketError
_GAddress_translate_from(GAddress
*address
,
1209 struct sockaddr
*addr
, int len
)
1211 address
->m_realfamily
= addr
->sa_family
;
1212 switch (addr
->sa_family
)
1215 address
->m_family
= GSOCK_INET
;
1218 address
->m_family
= GSOCK_UNIX
;
1222 address
->m_family
= GSOCK_INET6
;
1227 address
->m_error
= GSOCK_INVOP
;
1232 if (address
->m_addr
)
1233 free(address
->m_addr
);
1235 address
->m_len
= len
;
1236 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1238 if (address
->m_addr
== NULL
)
1240 address
->m_error
= GSOCK_MEMERR
;
1241 return GSOCK_MEMERR
;
1243 memcpy(address
->m_addr
, addr
, len
);
1245 return GSOCK_NOERROR
;
1248 GSocketError
_GAddress_translate_to(GAddress
*address
,
1249 struct sockaddr
**addr
, int *len
)
1251 if (!address
->m_addr
)
1253 address
->m_error
= GSOCK_INVADDR
;
1254 return GSOCK_INVADDR
;
1257 *len
= address
->m_len
;
1258 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1261 address
->m_error
= GSOCK_MEMERR
;
1262 return GSOCK_MEMERR
;
1265 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1266 return GSOCK_NOERROR
;
1270 * -------------------------------------------------------------------------
1271 * Internet address family
1272 * -------------------------------------------------------------------------
1275 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1277 address
->m_len
= sizeof(struct sockaddr_in
);
1278 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1279 if (address
->m_addr
== NULL
)
1281 address
->m_error
= GSOCK_MEMERR
;
1282 return GSOCK_MEMERR
;
1285 address
->m_family
= GSOCK_INET
;
1286 address
->m_realfamily
= PF_INET
;
1287 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1288 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1290 return GSOCK_NOERROR
;
1293 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1296 struct in_addr
*addr
;
1298 assert(address
!= NULL
);
1300 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1302 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1304 addr
->s_addr
= inet_addr(hostname
);
1306 /* If it is a numeric host name, convert it now */
1307 if (addr
->s_addr
== INADDR_NONE
)
1309 struct in_addr
*array_addr
;
1311 /* It is a real name, we solve it */
1312 if ((he
= gethostbyname(hostname
)) == NULL
)
1314 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1315 address
->m_error
= GSOCK_NOHOST
;
1316 return GSOCK_NOHOST
;
1318 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1319 addr
->s_addr
= array_addr
[0].s_addr
;
1321 return GSOCK_NOERROR
;
1324 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1326 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1329 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1330 unsigned long hostaddr
)
1332 struct in_addr
*addr
;
1334 assert(address
!= NULL
);
1336 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1338 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1339 addr
->s_addr
= hostaddr
;
1341 return GSOCK_NOERROR
;
1344 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1345 const char *protocol
)
1348 struct sockaddr_in
*addr
;
1350 assert(address
!= NULL
);
1351 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1355 address
->m_error
= GSOCK_INVPORT
;
1356 return GSOCK_INVPORT
;
1359 se
= getservbyname(port
, protocol
);
1362 if (isdigit(port
[0]))
1366 port_int
= atoi(port
);
1367 addr
= (struct sockaddr_in
*)address
->m_addr
;
1368 addr
->sin_port
= htons((u_short
) port_int
);
1369 return GSOCK_NOERROR
;
1372 address
->m_error
= GSOCK_INVPORT
;
1373 return GSOCK_INVPORT
;
1376 addr
= (struct sockaddr_in
*)address
->m_addr
;
1377 addr
->sin_port
= se
->s_port
;
1379 return GSOCK_NOERROR
;
1382 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1384 struct sockaddr_in
*addr
;
1386 assert(address
!= NULL
);
1387 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1389 addr
= (struct sockaddr_in
*)address
->m_addr
;
1390 addr
->sin_port
= htons(port
);
1392 return GSOCK_NOERROR
;
1395 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1399 struct sockaddr_in
*addr
;
1401 assert(address
!= NULL
);
1402 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1404 addr
= (struct sockaddr_in
*)address
->m_addr
;
1405 addr_buf
= (char *)&(addr
->sin_addr
);
1407 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1410 address
->m_error
= GSOCK_NOHOST
;
1411 return GSOCK_NOHOST
;
1414 strncpy(hostname
, he
->h_name
, sbuf
);
1416 return GSOCK_NOERROR
;
1419 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1421 struct sockaddr_in
*addr
;
1423 assert(address
!= NULL
);
1424 CHECK_ADDRESS(address
, INET
, 0);
1426 addr
= (struct sockaddr_in
*)address
->m_addr
;
1428 return addr
->sin_addr
.s_addr
;
1431 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1433 struct sockaddr_in
*addr
;
1435 assert(address
!= NULL
);
1436 CHECK_ADDRESS(address
, INET
, 0);
1438 addr
= (struct sockaddr_in
*)address
->m_addr
;
1439 return ntohs(addr
->sin_port
);
1443 * -------------------------------------------------------------------------
1444 * Unix address family
1445 * -------------------------------------------------------------------------
1448 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1450 assert (address
!= NULL
);
1451 address
->m_error
= GSOCK_INVADDR
;
1452 return GSOCK_INVADDR
;
1455 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1457 assert (address
!= NULL
);
1458 address
->m_error
= GSOCK_INVADDR
;
1459 return GSOCK_INVADDR
;
1462 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1464 assert (address
!= NULL
);
1465 address
->m_error
= GSOCK_INVADDR
;
1466 return GSOCK_INVADDR
;
1469 #else /* !wxUSE_SOCKETS */
1472 * Translation unit shouldn't be empty, so include this typedef to make the
1473 * compiler (VC++ 6.0, for example) happy
1475 typedef (*wxDummy
)();
1477 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */