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 readable.
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 if (WSAGetLastError() != WSAEWOULDBLOCK
)
667 socket
->m_error
= GSOCK_IOERR
;
672 socket
->m_error
= GSOCK_WOULDBLOCK
;
680 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
684 assert(socket
!= NULL
);
686 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
688 socket
->m_error
= GSOCK_INVSOCK
;
692 /* If the socket is blocking, wait for writability (with a timeout) */
693 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
697 if (socket
->m_stream
)
698 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
700 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
702 if (ret
== SOCKET_ERROR
)
704 if (WSAGetLastError() != WSAEWOULDBLOCK
)
705 socket
->m_error
= GSOCK_IOERR
;
707 socket
->m_error
= GSOCK_WOULDBLOCK
;
709 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
717 * Polls the socket to determine its status. This function will
718 * check for the events specified in the 'flags' parameter, and
719 * it will return a mask indicating which operations can be
720 * performed. This function won't block, regardless of the
721 * mode (blocking|nonblocking) of the socket.
723 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
725 assert(socket
!= NULL
);
727 return (flags
& socket
->m_detected
);
732 /* GSocket_SetNonBlocking:
733 * Sets the socket to non-blocking mode. This is useful if
734 * we don't want to wait.
736 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
738 assert(socket
!= NULL
);
740 socket
->m_non_blocking
= non_block
;
743 /* GSocket_SetTimeout:
744 * Sets the timeout for blocking calls. Time is
745 * expressed in milliseconds.
747 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
749 assert(socket
!= NULL
);
751 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
752 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
756 * Returns the last error occured for this socket.
758 GSocketError
GSocket_GetError(GSocket
*socket
)
760 assert(socket
!= NULL
);
762 return socket
->m_error
;
767 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
768 * and LOST). The callbacks are called in the following situations:
770 * INPUT: There is at least one byte in the input buffer
771 * OUTPUT: The system is sure that the next write call will not block
772 * CONNECTION: Two cases are possible:
773 * Client socket -> the connection is established
774 * Server socket -> a client requests a connection
775 * LOST: The connection is lost
777 * An event is generated only once and its state is reseted when the
778 * relative IO call is requested.
779 * For example: INPUT -> GSocket_Read()
780 * CONNECTION -> GSocket_Accept()
783 /* GSocket_SetCallback:
784 * Enables the callbacks specified by 'flags'. Note that 'flags'
785 * may be a combination of flags OR'ed toghether, so the same
786 * callback function can be made to accept different events.
787 * The callback function must have the following prototype:
789 * void function(GSocket *socket, GSocketEvent event, char *cdata)
791 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
792 GSocketCallback callback
, char *cdata
)
796 assert (socket
!= NULL
);
798 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
800 if ((flags
& (1 << count
)) != 0)
802 socket
->m_cbacks
[count
] = callback
;
803 socket
->m_data
[count
] = cdata
;
808 /* GSocket_UnsetCallback:
809 * Disables all callbacks specified by 'flags', which may be a
810 * combination of flags OR'ed toghether.
812 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
816 assert(socket
!= NULL
);
818 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
820 if ((flags
& (1 << count
)) != 0)
822 socket
->m_cbacks
[count
] = NULL
;
823 socket
->m_data
[count
] = NULL
;
831 /* _GSocket_Enable_Events:
832 * We enable all event notifications here (we need to be notified
833 * of all events for internal processing) but we will only notify
834 * users when an appropiate callback function has been installed.
836 void _GSocket_Enable_Events(GSocket
*socket
)
838 assert (socket
!= NULL
);
840 if (socket
->m_fd
!= INVALID_SOCKET
)
842 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
,
843 FD_READ
| FD_WRITE
| FD_ACCEPT
| FD_CONNECT
| FD_CLOSE
);
847 /* _GSocket_Disable_Events:
848 * Disable event notifications (when shutdowning the socket)
850 void _GSocket_Disable_Events(GSocket
*socket
)
852 assert (socket
!= NULL
);
854 if (socket
->m_fd
!= INVALID_SOCKET
)
856 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
861 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
868 GSocketCallback cback
;
871 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
873 EnterCriticalSection(&critical
);
874 socket
= socketList
[(uMsg
- WM_USER
)];
879 /* Check that the socket still exists (it has not been
880 * destroyed) and for safety, check that the m_fd field
881 * is what we expect it to be.
883 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
885 switch WSAGETSELECTEVENT(lParam
)
887 case FD_READ
: event
= GSOCK_INPUT
; break;
888 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
889 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
892 if (WSAGETSELECTERROR(lParam
) != 0)
895 event
= GSOCK_CONNECTION
;
898 case FD_CLOSE
: event
= GSOCK_LOST
; break;
903 cback
= socket
->m_cbacks
[event
];
904 data
= socket
->m_data
[event
];
906 if (event
== GSOCK_LOST
)
907 socket
->m_detected
= GSOCK_LOST_FLAG
;
909 socket
->m_detected
|= (1 << event
);
913 /* OK, we can now leave the critical section because we have
914 * already obtained the callback address (we make no further
915 * accesses to socket->whatever)
917 LeaveCriticalSection(&critical
);
920 (cback
)(socket
, event
, data
);
925 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
929 /* _GSocket_Input_Timeout:
930 * For blocking sockets, wait until data is available or
931 * until timeout ellapses.
933 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
937 if (!socket
->m_non_blocking
)
940 FD_SET(socket
->m_fd
, &readfds
);
941 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
943 socket
->m_error
= GSOCK_TIMEDOUT
;
944 return GSOCK_TIMEDOUT
;
947 return GSOCK_NOERROR
;
950 /* _GSocket_Output_Timeout:
951 * For blocking sockets, wait until data can be sent without
952 * blocking or until timeout ellapses.
954 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
958 if (!socket
->m_non_blocking
)
961 FD_SET(socket
->m_fd
, &writefds
);
962 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
964 socket
->m_error
= GSOCK_TIMEDOUT
;
965 return GSOCK_TIMEDOUT
;
968 return GSOCK_NOERROR
;
971 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
973 return recv(socket
->m_fd
, buffer
, size
, 0);
976 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
978 struct sockaddr from
;
979 SOCKLEN_T fromlen
= sizeof(from
);
983 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
985 if (ret
== SOCKET_ERROR
)
988 /* Translate a system address into a GSocket address */
991 socket
->m_peer
= GAddress_new();
994 socket
->m_error
= GSOCK_MEMERR
;
998 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
999 if (err
!= GSOCK_NOERROR
)
1001 GAddress_destroy(socket
->m_peer
);
1002 socket
->m_peer
= NULL
;
1003 socket
->m_error
= err
;
1010 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1012 return send(socket
->m_fd
, buffer
, size
, 0);
1015 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1017 struct sockaddr
*addr
;
1021 if (!socket
->m_peer
)
1023 socket
->m_error
= GSOCK_INVADDR
;
1027 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1028 if (err
!= GSOCK_NOERROR
)
1030 socket
->m_error
= err
;
1034 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1036 /* Frees memory allocated by _GAddress_translate_to */
1044 * -------------------------------------------------------------------------
1046 * -------------------------------------------------------------------------
1049 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1050 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1051 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1053 #define CHECK_ADDRESS(address, family, retval) \
1055 if (address->m_family == GSOCK_NOFAMILY) \
1056 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1057 return address->m_error; \
1058 if (address->m_family != GSOCK_##family) \
1060 address->m_error = GSOCK_INVADDR; \
1065 GAddress
*GAddress_new()
1069 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1072 address
->m_family
= GSOCK_NOFAMILY
;
1073 address
->m_addr
= NULL
;
1079 GAddress
*GAddress_copy(GAddress
*address
)
1083 assert(address
!= NULL
);
1085 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1088 memcpy(addr2
, address
, sizeof(GAddress
));
1090 if (address
->m_addr
)
1092 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1093 if (addr2
->m_addr
== NULL
)
1098 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1104 void GAddress_destroy(GAddress
*address
)
1106 assert(address
!= NULL
);
1111 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1113 assert(address
!= NULL
);
1115 address
->m_family
= type
;
1118 GAddressType
GAddress_GetFamily(GAddress
*address
)
1120 assert(address
!= NULL
);
1122 return address
->m_family
;
1125 GSocketError
_GAddress_translate_from(GAddress
*address
,
1126 struct sockaddr
*addr
, int len
)
1128 address
->m_realfamily
= addr
->sa_family
;
1129 switch (addr
->sa_family
)
1132 address
->m_family
= GSOCK_INET
;
1135 address
->m_family
= GSOCK_UNIX
;
1139 address
->m_family
= GSOCK_INET6
;
1144 address
->m_error
= GSOCK_INVOP
;
1149 if (address
->m_addr
)
1150 free(address
->m_addr
);
1152 address
->m_len
= len
;
1153 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1155 if (address
->m_addr
== NULL
)
1157 address
->m_error
= GSOCK_MEMERR
;
1158 return GSOCK_MEMERR
;
1160 memcpy(address
->m_addr
, addr
, len
);
1162 return GSOCK_NOERROR
;
1165 GSocketError
_GAddress_translate_to(GAddress
*address
,
1166 struct sockaddr
**addr
, int *len
)
1168 if (!address
->m_addr
)
1170 address
->m_error
= GSOCK_INVADDR
;
1171 return GSOCK_INVADDR
;
1174 *len
= address
->m_len
;
1175 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1178 address
->m_error
= GSOCK_MEMERR
;
1179 return GSOCK_MEMERR
;
1182 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1183 return GSOCK_NOERROR
;
1187 * -------------------------------------------------------------------------
1188 * Internet address family
1189 * -------------------------------------------------------------------------
1192 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1194 address
->m_len
= sizeof(struct sockaddr_in
);
1195 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1196 if (address
->m_addr
== NULL
)
1198 address
->m_error
= GSOCK_MEMERR
;
1199 return GSOCK_MEMERR
;
1202 address
->m_family
= GSOCK_INET
;
1203 address
->m_realfamily
= PF_INET
;
1204 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1205 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_NONE
;
1207 return GSOCK_NOERROR
;
1210 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1213 struct in_addr
*addr
;
1215 assert(address
!= NULL
);
1217 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1219 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1221 addr
->s_addr
= inet_addr(hostname
);
1223 /* If it is a numeric host name, convert it now */
1224 if (addr
->s_addr
== INADDR_NONE
)
1226 struct in_addr
*array_addr
;
1228 /* It is a real name, we solve it */
1229 if ((he
= gethostbyname(hostname
)) == NULL
)
1231 address
->m_error
= GSOCK_NOHOST
;
1232 return GSOCK_NOHOST
;
1234 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1235 addr
->s_addr
= array_addr
[0].s_addr
;
1237 return GSOCK_NOERROR
;
1240 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1241 unsigned long hostaddr
)
1243 struct in_addr
*addr
;
1245 assert(address
!= NULL
);
1247 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1249 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1250 addr
->s_addr
= hostaddr
;
1252 return GSOCK_NOERROR
;
1255 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1256 const char *protocol
)
1259 struct sockaddr_in
*addr
;
1261 assert(address
!= NULL
);
1262 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1266 address
->m_error
= GSOCK_INVPORT
;
1270 se
= getservbyname(port
, protocol
);
1273 if (isdigit(port
[0]))
1277 port_int
= atoi(port
);
1278 addr
= (struct sockaddr_in
*)address
->m_addr
;
1279 addr
->sin_port
= htons((u_short
) port_int
);
1280 return GSOCK_NOERROR
;
1283 address
->m_error
= GSOCK_INVPORT
;
1284 return GSOCK_INVPORT
;
1287 addr
= (struct sockaddr_in
*)address
->m_addr
;
1288 addr
->sin_port
= se
->s_port
;
1290 return GSOCK_NOERROR
;
1293 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1295 struct sockaddr_in
*addr
;
1297 assert(address
!= NULL
);
1298 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1300 addr
= (struct sockaddr_in
*)address
->m_addr
;
1301 addr
->sin_port
= htons(port
);
1303 return GSOCK_NOERROR
;
1306 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1310 struct sockaddr_in
*addr
;
1312 assert(address
!= NULL
);
1313 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1315 addr
= (struct sockaddr_in
*)address
->m_addr
;
1316 addr_buf
= (char *)&(addr
->sin_addr
);
1318 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1321 address
->m_error
= GSOCK_NOHOST
;
1322 return GSOCK_NOHOST
;
1325 strncpy(hostname
, he
->h_name
, sbuf
);
1327 return GSOCK_NOERROR
;
1330 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1332 struct sockaddr_in
*addr
;
1334 assert(address
!= NULL
);
1335 CHECK_ADDRESS(address
, INET
, 0);
1337 addr
= (struct sockaddr_in
*)address
->m_addr
;
1339 return addr
->sin_addr
.s_addr
;
1342 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1344 struct sockaddr_in
*addr
;
1346 assert(address
!= NULL
);
1347 CHECK_ADDRESS(address
, INET
, 0);
1349 addr
= (struct sockaddr_in
*)address
->m_addr
;
1350 return ntohs(addr
->sin_port
);
1354 * -------------------------------------------------------------------------
1355 * Unix address family
1356 * -------------------------------------------------------------------------
1359 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1361 assert (address
!= NULL
);
1362 address
->m_error
= GSOCK_INVADDR
;
1363 return GSOCK_INVADDR
;
1366 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1368 assert (address
!= NULL
);
1369 address
->m_error
= GSOCK_INVADDR
;
1370 return GSOCK_INVADDR
;
1373 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1375 assert (address
!= NULL
);
1376 address
->m_error
= GSOCK_INVADDR
;
1377 return GSOCK_INVADDR
;
1380 #else /* !wxUSE_SOCKETS */
1383 * translation unit shouldn't be empty, so include this typedef to make the
1384 * compiler (VC++ 6.0, for example) happy
1386 typedef (*wxDummy
)();
1388 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
1390 /* Diferencias con la version Unix:
1391 * - El descriptor es SOCKET y no int
1392 * - Constantes -1 pasan a INVALID_SOCKET
1393 * - Errores en muchas funciones pasan de -1 o <0 a SOCKET_ERROR
1394 * - ioctl y close pasan a ioctlsocket y closesocket
1395 * - inet_addr en lugar de inet_aton
1396 * - Codigo de inicializacion y terminacion para inicializar y
1397 * terminar WinSocket y para la ventana interna.
1398 * - SetTimeout, SetNonBlocking y la implementacion de los
1399 * timeouts eran bastante diferentes, pero ahora se han
1400 * hecho en la version Unix igual que en esta.