1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Purpose: GSocket main MSW file
6 * -------------------------------------------------------------------------
9 #ifndef __GSOCKET_STANDALONE__
13 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
16 #ifndef __GSOCKET_STANDALONE__
18 #include "wx/msw/gsockmsw.h"
19 #include "wx/gsocket.h"
21 #define INSTANCE wxGetInstance()
28 /* If not using wxWindows, a global var called hInst must
29 * be available and it must containt the app's instance
32 #define INSTANCE hInst
34 #endif /* __GSOCKET_STANDALONE__ */
46 #define CLASSNAME "_GSocket_Internal_Window_Class"
47 #define WINDOWNAME "_GSocket_Internal_Window_Name"
49 /* Maximum number of different GSocket objects at a given time.
50 * This value can be modified at will, but it CANNOT be greater
51 * than (0x7FFF - WM_USER + 1)
53 #define MAXSOCKETS 1024
55 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
56 #error "MAXSOCKETS is too big!"
60 /* Global variables */
62 extern HINSTANCE INSTANCE
;
64 static CRITICAL_SECTION critical
;
65 static GSocket
* socketList
[MAXSOCKETS
];
66 static int firstAvailable
;
68 /* Global initializers */
76 /* Create internal window for event notifications */
78 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
79 winClass
.cbClsExtra
= 0;
80 winClass
.cbWndExtra
= 0;
81 winClass
.hInstance
= INSTANCE
;
82 winClass
.hIcon
= (HICON
) NULL
;
83 winClass
.hCursor
= (HCURSOR
) NULL
;
84 winClass
.hbrBackground
= (HBRUSH
) NULL
;
85 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
86 winClass
.lpszClassName
= CLASSNAME
;
88 RegisterClass(&winClass
);
89 hWin
= CreateWindow(CLASSNAME
,
92 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
94 if (!hWin
) return FALSE
;
96 /* Initialize socket list */
97 InitializeCriticalSection(&critical
);
99 for (i
= 0; i
< MAXSOCKETS
; i
++)
101 socketList
[i
] = NULL
;
105 /* Initialize WinSocket */
106 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
109 void GSocket_Cleanup()
111 /* Destroy internal window */
113 UnregisterClass(CLASSNAME
, INSTANCE
);
115 /* Delete critical section */
116 DeleteCriticalSection(&critical
);
118 /* Cleanup WinSocket */
122 /* Constructors / Destructors */
124 GSocket
*GSocket_new()
129 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
132 socket
->m_fd
= INVALID_SOCKET
;
133 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
135 socket
->m_cbacks
[i
] = NULL
;
137 socket
->m_local
= NULL
;
138 socket
->m_peer
= NULL
;
139 socket
->m_error
= GSOCK_NOERROR
;
140 socket
->m_server
= FALSE
;
141 socket
->m_stream
= TRUE
;
142 socket
->m_non_blocking
= FALSE
;
143 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
144 socket
->m_timeout
.tv_usec
= 0;
145 socket
->m_detected
= 0;
147 /* Allocate a new message number for this socket */
148 EnterCriticalSection(&critical
);
151 while (socketList
[i
] != NULL
)
153 i
= (i
+ 1) % MAXSOCKETS
;
155 if (i
== firstAvailable
) /* abort! */
158 LeaveCriticalSection(&critical
);
162 socketList
[i
] = socket
;
163 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
164 socket
->m_msgnumber
= (i
+ WM_USER
);
166 LeaveCriticalSection(&critical
);
171 void GSocket_destroy(GSocket
*socket
)
173 assert(socket
!= NULL
);
175 /* Remove the socket from the list */
176 EnterCriticalSection(&critical
);
177 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
178 LeaveCriticalSection(&critical
);
180 /* Check that the socket is really shutdowned */
181 if (socket
->m_fd
!= INVALID_SOCKET
)
182 GSocket_Shutdown(socket
);
184 /* Destroy private addresses */
186 GAddress_destroy(socket
->m_local
);
189 GAddress_destroy(socket
->m_peer
);
191 /* Destroy socket itself */
195 void GSocket_Shutdown(GSocket
*socket
)
199 assert(socket
!= NULL
);
201 /* If socket has been created, shutdown it */
202 if (socket
->m_fd
!= INVALID_SOCKET
)
204 shutdown(socket
->m_fd
, 2);
205 closesocket(socket
->m_fd
);
206 socket
->m_fd
= INVALID_SOCKET
;
209 /* Disable GUI callbacks */
210 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
211 socket
->m_cbacks
[evt
] = NULL
;
213 socket
->m_detected
= 0;
214 _GSocket_Disable_Events(socket
);
217 /* Address handling */
219 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
221 assert(socket
!= NULL
);
223 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
225 socket
->m_error
= GSOCK_INVSOCK
;
226 return GSOCK_INVSOCK
;
229 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
231 socket
->m_error
= GSOCK_INVADDR
;
232 return GSOCK_INVADDR
;
236 GAddress_destroy(socket
->m_local
);
238 socket
->m_local
= GAddress_copy(address
);
240 return GSOCK_NOERROR
;
243 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
245 assert(socket
!= NULL
);
247 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
249 socket
->m_error
= GSOCK_INVADDR
;
250 return GSOCK_INVADDR
;
254 GAddress_destroy(socket
->m_peer
);
256 socket
->m_peer
= GAddress_copy(address
);
258 return GSOCK_NOERROR
;
261 GAddress
*GSocket_GetLocal(GSocket
*socket
)
264 struct sockaddr addr
;
265 SOCKLEN_T size
= sizeof(addr
);
268 assert(socket
!= NULL
);
271 return GAddress_copy(socket
->m_local
);
273 if (socket
->m_fd
== INVALID_SOCKET
)
275 socket
->m_error
= GSOCK_INVSOCK
;
281 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
283 socket
->m_error
= GSOCK_IOERR
;
287 address
= GAddress_new();
290 socket
->m_error
= GSOCK_MEMERR
;
293 err
= _GAddress_translate_from(address
, &addr
, size
);
294 if (err
!= GSOCK_NOERROR
)
296 GAddress_destroy(address
);
297 socket
->m_error
= err
;
304 GAddress
*GSocket_GetPeer(GSocket
*socket
)
306 assert(socket
!= NULL
);
309 return GAddress_copy(socket
->m_peer
);
314 /* Server specific parts */
316 /* GSocket_SetServer:
317 * Sets up the socket as a server. It uses the "Local" field of GSocket.
318 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
319 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
320 * been initialized, GSOCK_INVADDR if the local address has not been
321 * defined and GSOCK_IOERR for other internal errors.
323 GSocketError
GSocket_SetServer(GSocket
*sck
)
329 if (sck
->m_fd
!= INVALID_SOCKET
)
331 sck
->m_error
= GSOCK_INVSOCK
;
332 return GSOCK_INVSOCK
;
337 sck
->m_error
= GSOCK_INVADDR
;
338 return GSOCK_INVADDR
;
341 /* Initialize all fields */
342 sck
->m_server
= TRUE
;
343 sck
->m_stream
= TRUE
;
344 sck
->m_oriented
= TRUE
;
346 /* Create the socket */
347 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
349 if (sck
->m_fd
== INVALID_SOCKET
)
351 sck
->m_error
= GSOCK_IOERR
;
355 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
356 _GSocket_Enable_Events(sck
);
358 /* Bind the socket to the LOCAL address */
359 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
361 closesocket(sck
->m_fd
);
362 sck
->m_fd
= INVALID_SOCKET
;
363 sck
->m_error
= GSOCK_IOERR
;
367 /* Enable listening up to 5 connections */
368 if (listen(sck
->m_fd
, 5) != 0)
370 closesocket(sck
->m_fd
);
371 sck
->m_fd
= INVALID_SOCKET
;
372 sck
->m_error
= GSOCK_IOERR
;
376 return GSOCK_NOERROR
;
379 /* GSocket_WaitConnection:
380 * Waits for an incoming client connection.
382 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
385 struct sockaddr from
;
386 SOCKLEN_T fromlen
= sizeof(from
);
392 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
394 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
396 sck
->m_error
= GSOCK_INVSOCK
;
400 /* Create a GSocket object for the new connection */
401 connection
= GSocket_new();
405 sck
->m_error
= GSOCK_MEMERR
;
409 /* Wait for a connection (with timeout) */
410 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
412 GSocket_destroy(connection
);
413 /* sck->m_error set by _GSocket_Input_Timeout */
417 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
419 if (connection
->m_fd
== INVALID_SOCKET
)
421 if (WSAGetLastError() == WSAEWOULDBLOCK
)
422 sck
->m_error
= GSOCK_WOULDBLOCK
;
424 sck
->m_error
= GSOCK_IOERR
;
426 GSocket_destroy(connection
);
430 /* Initialize all fields */
431 connection
->m_server
= FALSE
;
432 connection
->m_stream
= TRUE
;
433 connection
->m_oriented
= TRUE
;
435 /* Setup the peer address field */
436 connection
->m_peer
= GAddress_new();
437 if (!connection
->m_peer
)
439 GSocket_destroy(connection
);
440 sck
->m_error
= GSOCK_MEMERR
;
443 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
444 if (err
!= GSOCK_NOERROR
)
446 GAddress_destroy(connection
->m_peer
);
447 GSocket_destroy(connection
);
452 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
453 _GSocket_Enable_Events(connection
);
458 /* Non oriented connections */
460 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
466 if (sck
->m_fd
!= INVALID_SOCKET
)
468 sck
->m_error
= GSOCK_INVSOCK
;
469 return GSOCK_INVSOCK
;
474 sck
->m_error
= GSOCK_INVADDR
;
475 return GSOCK_INVADDR
;
478 /* Initialize all fields */
479 sck
->m_stream
= FALSE
;
480 sck
->m_server
= FALSE
;
481 sck
->m_oriented
= FALSE
;
483 /* Create the socket */
484 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
486 if (sck
->m_fd
== INVALID_SOCKET
)
488 sck
->m_error
= GSOCK_IOERR
;
492 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
493 _GSocket_Enable_Events(sck
);
495 /* Bind it to the LOCAL address */
496 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
498 closesocket(sck
->m_fd
);
499 sck
->m_fd
= INVALID_SOCKET
;
500 sck
->m_error
= GSOCK_IOERR
;
504 return GSOCK_NOERROR
;
507 GSocketError
GSocket_SetBroadcast(GSocket
*sck
)
513 if (GSocket_SetNonOriented(sck
) != GSOCK_NOERROR
)
516 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_BROADCAST
,
517 (const char FAR
*) &b
, sizeof(b
));
519 return GSOCK_NOERROR
;
522 /* Client specific parts */
525 * Establishes a client connection to a server using the "Peer"
526 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
527 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
528 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
529 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
530 * the connection request can be completed later. Use GSocket_Select()
531 * to check or wait for a GSOCK_CONNECTION event.
533 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
540 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
542 if (sck
->m_fd
!= INVALID_SOCKET
)
544 sck
->m_error
= GSOCK_INVSOCK
;
545 return GSOCK_INVSOCK
;
550 sck
->m_error
= GSOCK_INVADDR
;
551 return GSOCK_INVADDR
;
554 /* Test whether we want the socket to be a stream (e.g. TCP) */
555 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
556 sck
->m_oriented
= TRUE
;
557 sck
->m_server
= FALSE
;
564 /* Create the socket */
565 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
567 if (sck
->m_fd
== INVALID_SOCKET
)
569 sck
->m_error
= GSOCK_IOERR
;
573 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
574 _GSocket_Enable_Events(sck
);
576 /* Connect it to the PEER address, with a timeout (see below) */
577 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
579 if (ret
== SOCKET_ERROR
)
581 err
= WSAGetLastError();
583 /* If connect failed with EWOULDBLOCK and the GSocket object
584 * is in blocking mode, we select() for the specified timeout
585 * checking for writability to see if the connection request
588 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
590 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
592 closesocket(sck
->m_fd
);
593 sck
->m_fd
= INVALID_SOCKET
;
594 /* sck->m_error is set in _GSocket_Output_Timeout */
595 return GSOCK_TIMEDOUT
;
598 return GSOCK_NOERROR
;
601 /* If connect failed with EWOULDBLOCK and the GSocket object
602 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
603 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
604 * this way if the connection completes, a GSOCK_CONNECTION
605 * event will be generated, if enabled.
607 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
609 sck
->m_error
= GSOCK_WOULDBLOCK
;
610 return GSOCK_WOULDBLOCK
;
613 /* If connect failed with an error other than EWOULDBLOCK,
614 * then the call to GSocket_Connect has failed.
616 closesocket(sck
->m_fd
);
617 sck
->m_fd
= INVALID_SOCKET
;
618 sck
->m_error
= GSOCK_IOERR
;
622 return GSOCK_NOERROR
;
627 /* Like recv(), send(), ... */
628 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
632 assert(socket
!= NULL
);
634 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
636 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
638 socket
->m_error
= GSOCK_INVSOCK
;
642 /* If the socket is blocking, wait for data (with a timeout) */
643 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
647 if (socket
->m_stream
)
648 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
650 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
652 if (ret
== SOCKET_ERROR
)
654 /* NOTE: Window sockets exhibit a very strange property;
655 * if the socket is in non-blocking mode (which is always
656 * the case here, no matter the setting of GSocket itself)
657 * a call to send() can fail with EWOULDBLOCK even when
658 * select() says that the socket is writable.
660 * This can break several things because, usually, if
661 * select() says that the socket is writable, it is
662 * assumed that send() won't fail. To avoid this, we
663 * return 0 instead of -1 for this special case.
665 * XXX - this comment seems not to belong here, and also
666 * the code is not consistent with the unix version of
667 * gsocket... what to do? (GRG)
669 if (WSAGetLastError() != WSAEWOULDBLOCK
)
671 socket
->m_error
= GSOCK_IOERR
;
676 socket
->m_error
= GSOCK_WOULDBLOCK
;
684 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
688 assert(socket
!= NULL
);
690 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
692 socket
->m_error
= GSOCK_INVSOCK
;
696 /* If the socket is blocking, wait for writability (with a timeout) */
697 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
701 if (socket
->m_stream
)
702 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
704 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
706 if (ret
== SOCKET_ERROR
)
708 if (WSAGetLastError() != WSAEWOULDBLOCK
)
709 socket
->m_error
= GSOCK_IOERR
;
711 socket
->m_error
= GSOCK_WOULDBLOCK
;
713 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
721 * Polls the socket to determine its status. This function will
722 * check for the events specified in the 'flags' parameter, and
723 * it will return a mask indicating which operations can be
724 * performed. This function won't block, regardless of the
725 * mode (blocking|nonblocking) of the socket.
727 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
729 assert(socket
!= NULL
);
731 return (flags
& socket
->m_detected
);
736 /* GSocket_SetNonBlocking:
737 * Sets the socket to non-blocking mode. This is useful if
738 * we don't want to wait.
740 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
742 assert(socket
!= NULL
);
744 socket
->m_non_blocking
= non_block
;
747 /* GSocket_SetTimeout:
748 * Sets the timeout for blocking calls. Time is
749 * expressed in milliseconds.
751 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
753 assert(socket
!= NULL
);
755 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
756 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
760 * Returns the last error occured for this socket.
762 GSocketError
GSocket_GetError(GSocket
*socket
)
764 assert(socket
!= NULL
);
766 return socket
->m_error
;
771 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
772 * and LOST). The callbacks are called in the following situations:
774 * INPUT: There is at least one byte in the input buffer
775 * OUTPUT: The system is sure that the next write call will not block
776 * CONNECTION: Two cases are possible:
777 * Client socket -> the connection is established
778 * Server socket -> a client requests a connection
779 * LOST: The connection is lost
781 * An event is generated only once and its state is reseted when the
782 * relative IO call is requested.
783 * For example: INPUT -> GSocket_Read()
784 * CONNECTION -> GSocket_Accept()
787 /* GSocket_SetCallback:
788 * Enables the callbacks specified by 'flags'. Note that 'flags'
789 * may be a combination of flags OR'ed toghether, so the same
790 * callback function can be made to accept different events.
791 * The callback function must have the following prototype:
793 * void function(GSocket *socket, GSocketEvent event, char *cdata)
795 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
796 GSocketCallback callback
, char *cdata
)
800 assert (socket
!= NULL
);
802 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
804 if ((flags
& (1 << count
)) != 0)
806 socket
->m_cbacks
[count
] = callback
;
807 socket
->m_data
[count
] = cdata
;
812 /* GSocket_UnsetCallback:
813 * Disables all callbacks specified by 'flags', which may be a
814 * combination of flags OR'ed toghether.
816 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
820 assert(socket
!= NULL
);
822 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
824 if ((flags
& (1 << count
)) != 0)
826 socket
->m_cbacks
[count
] = NULL
;
827 socket
->m_data
[count
] = NULL
;
835 /* _GSocket_Enable_Events:
836 * We enable all event notifications here (we need to be notified
837 * of all events for internal processing) but we will only notify
838 * users when an appropiate callback function has been installed.
840 void _GSocket_Enable_Events(GSocket
*socket
)
842 assert (socket
!= NULL
);
844 if (socket
->m_fd
!= INVALID_SOCKET
)
846 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
,
847 FD_READ
| FD_WRITE
| FD_ACCEPT
| FD_CONNECT
| FD_CLOSE
);
851 /* _GSocket_Disable_Events:
852 * Disable event notifications (when shutdowning the socket)
854 void _GSocket_Disable_Events(GSocket
*socket
)
856 assert (socket
!= NULL
);
858 if (socket
->m_fd
!= INVALID_SOCKET
)
860 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
865 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
872 GSocketCallback cback
;
875 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
877 EnterCriticalSection(&critical
);
878 socket
= socketList
[(uMsg
- WM_USER
)];
883 /* Check that the socket still exists (it has not been
884 * destroyed) and for safety, check that the m_fd field
885 * is what we expect it to be.
887 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
889 switch WSAGETSELECTEVENT(lParam
)
891 case FD_READ
: event
= GSOCK_INPUT
; break;
892 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
893 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
896 if (WSAGETSELECTERROR(lParam
) != 0)
899 event
= GSOCK_CONNECTION
;
902 case FD_CLOSE
: event
= GSOCK_LOST
; break;
907 cback
= socket
->m_cbacks
[event
];
908 data
= socket
->m_data
[event
];
910 if (event
== GSOCK_LOST
)
911 socket
->m_detected
= GSOCK_LOST_FLAG
;
913 socket
->m_detected
|= (1 << event
);
917 /* OK, we can now leave the critical section because we have
918 * already obtained the callback address (we make no further
919 * accesses to socket->whatever)
921 LeaveCriticalSection(&critical
);
924 (cback
)(socket
, event
, data
);
929 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
933 /* _GSocket_Input_Timeout:
934 * For blocking sockets, wait until data is available or
935 * until timeout ellapses.
937 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
941 if (!socket
->m_non_blocking
)
944 FD_SET(socket
->m_fd
, &readfds
);
945 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
947 socket
->m_error
= GSOCK_TIMEDOUT
;
948 return GSOCK_TIMEDOUT
;
951 return GSOCK_NOERROR
;
954 /* _GSocket_Output_Timeout:
955 * For blocking sockets, wait until data can be sent without
956 * blocking or until timeout ellapses.
958 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
962 if (!socket
->m_non_blocking
)
965 FD_SET(socket
->m_fd
, &writefds
);
966 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
968 socket
->m_error
= GSOCK_TIMEDOUT
;
969 return GSOCK_TIMEDOUT
;
972 return GSOCK_NOERROR
;
975 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
977 return recv(socket
->m_fd
, buffer
, size
, 0);
980 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
982 struct sockaddr from
;
983 SOCKLEN_T fromlen
= sizeof(from
);
987 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
989 if (ret
== SOCKET_ERROR
)
992 /* Translate a system address into a GSocket address */
995 socket
->m_peer
= GAddress_new();
998 socket
->m_error
= GSOCK_MEMERR
;
1002 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1003 if (err
!= GSOCK_NOERROR
)
1005 GAddress_destroy(socket
->m_peer
);
1006 socket
->m_peer
= NULL
;
1007 socket
->m_error
= err
;
1014 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1016 return send(socket
->m_fd
, buffer
, size
, 0);
1019 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1021 struct sockaddr
*addr
;
1025 if (!socket
->m_peer
)
1027 socket
->m_error
= GSOCK_INVADDR
;
1031 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1032 if (err
!= GSOCK_NOERROR
)
1034 socket
->m_error
= err
;
1038 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1040 /* Frees memory allocated by _GAddress_translate_to */
1048 * -------------------------------------------------------------------------
1050 * -------------------------------------------------------------------------
1053 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1054 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1055 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1057 #define CHECK_ADDRESS(address, family, retval) \
1059 if (address->m_family == GSOCK_NOFAMILY) \
1060 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1061 return address->m_error; \
1062 if (address->m_family != GSOCK_##family) \
1064 address->m_error = GSOCK_INVADDR; \
1069 GAddress
*GAddress_new()
1073 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1076 address
->m_family
= GSOCK_NOFAMILY
;
1077 address
->m_addr
= NULL
;
1083 GAddress
*GAddress_copy(GAddress
*address
)
1087 assert(address
!= NULL
);
1089 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1092 memcpy(addr2
, address
, sizeof(GAddress
));
1094 if (address
->m_addr
)
1096 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1097 if (addr2
->m_addr
== NULL
)
1102 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1108 void GAddress_destroy(GAddress
*address
)
1110 assert(address
!= NULL
);
1115 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1117 assert(address
!= NULL
);
1119 address
->m_family
= type
;
1122 GAddressType
GAddress_GetFamily(GAddress
*address
)
1124 assert(address
!= NULL
);
1126 return address
->m_family
;
1129 GSocketError
_GAddress_translate_from(GAddress
*address
,
1130 struct sockaddr
*addr
, int len
)
1132 address
->m_realfamily
= addr
->sa_family
;
1133 switch (addr
->sa_family
)
1136 address
->m_family
= GSOCK_INET
;
1139 address
->m_family
= GSOCK_UNIX
;
1143 address
->m_family
= GSOCK_INET6
;
1148 address
->m_error
= GSOCK_INVOP
;
1153 if (address
->m_addr
)
1154 free(address
->m_addr
);
1156 address
->m_len
= len
;
1157 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1159 if (address
->m_addr
== NULL
)
1161 address
->m_error
= GSOCK_MEMERR
;
1162 return GSOCK_MEMERR
;
1164 memcpy(address
->m_addr
, addr
, len
);
1166 return GSOCK_NOERROR
;
1169 GSocketError
_GAddress_translate_to(GAddress
*address
,
1170 struct sockaddr
**addr
, int *len
)
1172 if (!address
->m_addr
)
1174 address
->m_error
= GSOCK_INVADDR
;
1175 return GSOCK_INVADDR
;
1178 *len
= address
->m_len
;
1179 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1182 address
->m_error
= GSOCK_MEMERR
;
1183 return GSOCK_MEMERR
;
1186 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1187 return GSOCK_NOERROR
;
1191 * -------------------------------------------------------------------------
1192 * Internet address family
1193 * -------------------------------------------------------------------------
1196 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1198 address
->m_len
= sizeof(struct sockaddr_in
);
1199 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1200 if (address
->m_addr
== NULL
)
1202 address
->m_error
= GSOCK_MEMERR
;
1203 return GSOCK_MEMERR
;
1206 address
->m_family
= GSOCK_INET
;
1207 address
->m_realfamily
= PF_INET
;
1208 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1209 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_NONE
;
1211 return GSOCK_NOERROR
;
1214 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1217 struct in_addr
*addr
;
1219 assert(address
!= NULL
);
1221 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1223 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1225 addr
->s_addr
= inet_addr(hostname
);
1227 /* If it is a numeric host name, convert it now */
1228 if (addr
->s_addr
== INADDR_NONE
)
1230 struct in_addr
*array_addr
;
1232 /* It is a real name, we solve it */
1233 if ((he
= gethostbyname(hostname
)) == NULL
)
1235 address
->m_error
= GSOCK_NOHOST
;
1236 return GSOCK_NOHOST
;
1238 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1239 addr
->s_addr
= array_addr
[0].s_addr
;
1241 return GSOCK_NOERROR
;
1244 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1245 unsigned long hostaddr
)
1247 struct in_addr
*addr
;
1249 assert(address
!= NULL
);
1251 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1253 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1254 addr
->s_addr
= hostaddr
;
1256 return GSOCK_NOERROR
;
1259 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1260 const char *protocol
)
1263 struct sockaddr_in
*addr
;
1265 assert(address
!= NULL
);
1266 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1270 address
->m_error
= GSOCK_INVPORT
;
1274 se
= getservbyname(port
, protocol
);
1277 if (isdigit(port
[0]))
1281 port_int
= atoi(port
);
1282 addr
= (struct sockaddr_in
*)address
->m_addr
;
1283 addr
->sin_port
= htons((u_short
) port_int
);
1284 return GSOCK_NOERROR
;
1287 address
->m_error
= GSOCK_INVPORT
;
1288 return GSOCK_INVPORT
;
1291 addr
= (struct sockaddr_in
*)address
->m_addr
;
1292 addr
->sin_port
= se
->s_port
;
1294 return GSOCK_NOERROR
;
1297 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1299 struct sockaddr_in
*addr
;
1301 assert(address
!= NULL
);
1302 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1304 addr
= (struct sockaddr_in
*)address
->m_addr
;
1305 addr
->sin_port
= htons(port
);
1307 return GSOCK_NOERROR
;
1310 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1314 struct sockaddr_in
*addr
;
1316 assert(address
!= NULL
);
1317 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1319 addr
= (struct sockaddr_in
*)address
->m_addr
;
1320 addr_buf
= (char *)&(addr
->sin_addr
);
1322 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1325 address
->m_error
= GSOCK_NOHOST
;
1326 return GSOCK_NOHOST
;
1329 strncpy(hostname
, he
->h_name
, sbuf
);
1331 return GSOCK_NOERROR
;
1334 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1336 struct sockaddr_in
*addr
;
1338 assert(address
!= NULL
);
1339 CHECK_ADDRESS(address
, INET
, 0);
1341 addr
= (struct sockaddr_in
*)address
->m_addr
;
1343 return addr
->sin_addr
.s_addr
;
1346 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1348 struct sockaddr_in
*addr
;
1350 assert(address
!= NULL
);
1351 CHECK_ADDRESS(address
, INET
, 0);
1353 addr
= (struct sockaddr_in
*)address
->m_addr
;
1354 return ntohs(addr
->sin_port
);
1358 * -------------------------------------------------------------------------
1359 * Unix address family
1360 * -------------------------------------------------------------------------
1363 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1365 assert (address
!= NULL
);
1366 address
->m_error
= GSOCK_INVADDR
;
1367 return GSOCK_INVADDR
;
1370 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1372 assert (address
!= NULL
);
1373 address
->m_error
= GSOCK_INVADDR
;
1374 return GSOCK_INVADDR
;
1377 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1379 assert (address
!= NULL
);
1380 address
->m_error
= GSOCK_INVADDR
;
1381 return GSOCK_INVADDR
;
1384 #else /* !wxUSE_SOCKETS */
1387 * translation unit shouldn't be empty, so include this typedef to make the
1388 * compiler (VC++ 6.0, for example) happy
1390 typedef (*wxDummy
)();
1392 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
1394 /* Diferencias con la version Unix:
1395 * - El descriptor es SOCKET y no int
1396 * - Constantes -1 pasan a INVALID_SOCKET
1397 * - Errores en muchas funciones pasan de -1 o <0 a SOCKET_ERROR
1398 * - ioctl y close pasan a ioctlsocket y closesocket
1399 * - inet_addr en lugar de inet_aton
1400 * - Codigo de inicializacion y terminacion para inicializar y
1401 * terminar WinSocket y para la ventana interna.
1402 * - SetTimeout, SetNonBlocking y la implementacion de los
1403 * timeouts eran bastante diferentes, pero ahora se han
1404 * hecho en la version Unix igual que en esta.