1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Purpose: GSocket main MSW file
6 * -------------------------------------------------------------------------
10 #ifndef __GSOCKET_STANDALONE__
14 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
17 #ifndef __GSOCKET_STANDALONE__
19 #include "wx/msw/gsockmsw.h"
20 #include "wx/gsocket.h"
22 #define INSTANCE wxhInstance
29 /* If not using wxWindows, a global var called hInst must
30 * be available and it must containt the app's instance
33 #define INSTANCE hInst
35 #endif /* __GSOCKET_STANDALONE__ */
47 #define CLASSNAME "_GSocket_Internal_Window_Class"
48 #define WINDOWNAME "_GSocket_Internal_Window_Name"
50 /* Maximum number of different GSocket objects at a given time.
51 * This value can be modified at will, but it CANNOT be greater
52 * than (0x7FFF - WM_USER + 1)
54 #define MAXSOCKETS 1024
56 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
57 #error "MAXSOCKETS is too big!"
61 /* Global variables */
63 extern HINSTANCE INSTANCE
;
65 static CRITICAL_SECTION critical
;
66 static GSocket
* socketList
[MAXSOCKETS
];
67 static int firstAvailable
;
69 /* Global initializers */
77 /* Create internal window for event notifications */
79 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
80 winClass
.cbClsExtra
= 0;
81 winClass
.cbWndExtra
= 0;
82 winClass
.hInstance
= INSTANCE
;
83 winClass
.hIcon
= (HICON
) NULL
;
84 winClass
.hCursor
= (HCURSOR
) NULL
;
85 winClass
.hbrBackground
= (HBRUSH
) NULL
;
86 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
87 winClass
.lpszClassName
= CLASSNAME
;
89 RegisterClass(&winClass
);
90 hWin
= CreateWindow(CLASSNAME
,
93 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
95 if (!hWin
) return FALSE
;
97 /* Initialize socket list */
98 InitializeCriticalSection(&critical
);
100 for (i
= 0; i
< MAXSOCKETS
; i
++)
102 socketList
[i
] = NULL
;
106 /* Initialize WinSocket */
107 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
110 void GSocket_Cleanup()
112 /* Destroy internal window */
114 UnregisterClass(CLASSNAME
, INSTANCE
);
116 /* Delete critical section */
117 DeleteCriticalSection(&critical
);
119 /* Cleanup WinSocket */
123 /* Constructors / Destructors */
125 GSocket
*GSocket_new()
130 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
133 socket
->m_fd
= INVALID_SOCKET
;
134 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
136 socket
->m_cbacks
[i
] = NULL
;
138 socket
->m_local
= NULL
;
139 socket
->m_peer
= NULL
;
140 socket
->m_error
= GSOCK_NOERROR
;
141 socket
->m_server
= FALSE
;
142 socket
->m_stream
= TRUE
;
143 socket
->m_non_blocking
= FALSE
;
144 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
145 socket
->m_timeout
.tv_usec
= 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 /* We don't want more event notifications; so first of all we
176 * remove the socket from the list (NOTE: we won't get a CLOSE
177 * event for this socket if it wasn't closed before).
179 EnterCriticalSection(&critical
);
180 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
181 LeaveCriticalSection(&critical
);
183 /* We check that the socket is really shutdowned */
184 if (socket
->m_fd
!= INVALID_SOCKET
)
185 GSocket_Shutdown(socket
);
187 /* We destroy private addresses */
189 GAddress_destroy(socket
->m_local
);
192 GAddress_destroy(socket
->m_peer
);
194 /* We destroy socket itself */
198 void GSocket_Shutdown(GSocket
*socket
)
202 assert(socket
!= NULL
);
204 /* If socket has been created, we shutdown it */
205 if (socket
->m_fd
!= INVALID_SOCKET
)
207 shutdown(socket
->m_fd
, 2);
208 closesocket(socket
->m_fd
);
209 socket
->m_fd
= INVALID_SOCKET
;
212 /* We disable GUI callbacks */
213 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
215 socket
->m_cbacks
[evt
] = NULL
;
218 _GSocket_Configure_Callbacks(socket
);
221 /* Address handling */
223 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
225 assert(socket
!= NULL
);
227 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
229 socket
->m_error
= GSOCK_INVSOCK
;
230 return GSOCK_INVSOCK
;
233 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
235 socket
->m_error
= GSOCK_INVADDR
;
236 return GSOCK_INVADDR
;
240 GAddress_destroy(socket
->m_local
);
242 socket
->m_local
= GAddress_copy(address
);
244 return GSOCK_NOERROR
;
247 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
249 assert(socket
!= NULL
);
251 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
253 socket
->m_error
= GSOCK_INVADDR
;
254 return GSOCK_INVADDR
;
258 GAddress_destroy(socket
->m_peer
);
260 socket
->m_peer
= GAddress_copy(address
);
262 return GSOCK_NOERROR
;
265 GAddress
*GSocket_GetLocal(GSocket
*socket
)
268 struct sockaddr addr
;
271 assert(socket
!= NULL
);
274 return GAddress_copy(socket
->m_local
);
276 if (socket
->m_fd
== INVALID_SOCKET
)
278 socket
->m_error
= GSOCK_INVSOCK
;
284 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
286 socket
->m_error
= GSOCK_IOERR
;
290 address
= GAddress_new();
293 socket
->m_error
= GSOCK_MEMERR
;
296 if (_GAddress_translate_from(address
, &addr
, size
) != GSOCK_NOERROR
)
298 socket
->m_error
= GSOCK_MEMERR
;
299 GAddress_destroy(address
);
306 GAddress
*GSocket_GetPeer(GSocket
*socket
)
308 assert(socket
!= NULL
);
311 return GAddress_copy(socket
->m_peer
);
316 /* Server specific parts */
318 /* GSocket_SetServer:
319 * Sets up the socket as a server. It uses the "Local" field of GSocket.
320 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
321 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
322 * been initialized, GSOCK_INVADDR if the local address has not been
323 * defined and GSOCK_IOERR for other internal errors.
325 GSocketError
GSocket_SetServer(GSocket
*sck
)
331 if (sck
->m_fd
!= INVALID_SOCKET
)
333 sck
->m_error
= GSOCK_INVSOCK
;
334 return GSOCK_INVSOCK
;
339 sck
->m_error
= GSOCK_INVADDR
;
340 return GSOCK_INVADDR
;
343 /* Initialize all fields */
344 sck
->m_server
= TRUE
;
345 sck
->m_stream
= TRUE
;
346 sck
->m_oriented
= TRUE
;
348 /* Create the socket */
349 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
351 if (sck
->m_fd
== INVALID_SOCKET
)
353 sck
->m_error
= GSOCK_IOERR
;
357 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
358 _GSocket_Configure_Callbacks(sck
);
360 /* Bind the socket to the LOCAL address */
361 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
363 closesocket(sck
->m_fd
);
364 sck
->m_fd
= INVALID_SOCKET
;
365 sck
->m_error
= GSOCK_IOERR
;
369 /* Enable listening up to 5 connections */
370 if (listen(sck
->m_fd
, 5) != 0)
372 closesocket(sck
->m_fd
);
373 sck
->m_fd
= INVALID_SOCKET
;
374 sck
->m_error
= GSOCK_IOERR
;
378 return GSOCK_NOERROR
;
381 /* GSocket_WaitConnection:
382 * Waits for an incoming client connection.
384 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
391 /* If the socket has already been created, we exit immediately */
392 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
394 sck
->m_error
= GSOCK_INVSOCK
;
398 /* Create a GSocket object for the new connection */
399 connection
= GSocket_new();
403 sck
->m_error
= GSOCK_MEMERR
;
407 /* Wait for a connection (with timeout) */
408 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
410 GSocket_destroy(connection
);
411 /* sck->m_error set by _GSocket_Input_Timeout */
415 connection
->m_fd
= accept(sck
->m_fd
, NULL
, NULL
);
417 if (connection
->m_fd
== INVALID_SOCKET
)
419 if (WSAGetLastError() == WSAEWOULDBLOCK
)
420 sck
->m_error
= GSOCK_WOULDBLOCK
;
422 sck
->m_error
= GSOCK_IOERR
;
424 GSocket_destroy(connection
);
428 /* Initialize all fields */
429 connection
->m_server
= FALSE
;
430 connection
->m_stream
= TRUE
;
431 connection
->m_oriented
= TRUE
;
433 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
434 _GSocket_Configure_Callbacks(connection
);
439 /* Non oriented connections */
441 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
447 if (sck
->m_fd
!= INVALID_SOCKET
)
449 sck
->m_error
= GSOCK_INVSOCK
;
450 return GSOCK_INVSOCK
;
455 sck
->m_error
= GSOCK_INVADDR
;
456 return GSOCK_INVADDR
;
459 /* Initialize all fields */
460 sck
->m_stream
= FALSE
;
461 sck
->m_server
= FALSE
;
462 sck
->m_oriented
= FALSE
;
464 /* Create the socket */
465 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
467 if (sck
->m_fd
== INVALID_SOCKET
)
469 sck
->m_error
= GSOCK_IOERR
;
473 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
474 _GSocket_Configure_Callbacks(sck
);
476 /* Bind it to the LOCAL address */
477 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
479 closesocket(sck
->m_fd
);
480 sck
->m_fd
= INVALID_SOCKET
;
481 sck
->m_error
= GSOCK_IOERR
;
485 return GSOCK_NOERROR
;
488 GSocketError
GSocket_SetBroadcast(GSocket
*sck
)
494 if (GSocket_SetNonOriented(sck
) != GSOCK_NOERROR
)
498 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char FAR
*) &b
, sizeof(b
));
500 return GSOCK_NOERROR
;
503 /* Client specific parts */
506 * Establishes a client connection to a server using the "Peer"
507 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
508 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
509 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
510 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
511 * the connection request can be completed later. Use GSocket_Select()
512 * to check or wait for a GSOCK_CONNECTION event.
514 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
521 if (sck
->m_fd
!= INVALID_SOCKET
)
523 sck
->m_error
= GSOCK_INVSOCK
;
524 return GSOCK_INVSOCK
;
529 sck
->m_error
= GSOCK_INVADDR
;
530 return GSOCK_INVADDR
;
533 /* Test whether we want the socket to be a stream (e.g. TCP) */
534 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
535 sck
->m_oriented
= TRUE
;
536 sck
->m_server
= FALSE
;
543 /* Create the socket */
544 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
546 if (sck
->m_fd
== INVALID_SOCKET
)
548 sck
->m_error
= GSOCK_IOERR
;
552 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
553 _GSocket_Configure_Callbacks(sck
);
555 /* Connect it to the PEER address, with a timeout (see below) */
556 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
558 if (ret
== SOCKET_ERROR
)
560 err
= WSAGetLastError();
562 /* If connect failed with EWOULDBLOCK and the GSocket object
563 * is in blocking mode, we select() for the specified timeout
564 * checking for writability to see if the connection request
567 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
569 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
571 closesocket(sck
->m_fd
);
572 sck
->m_fd
= INVALID_SOCKET
;
573 /* sck->m_error is set in _GSocket_Output_Timeout */
574 return GSOCK_TIMEDOUT
;
577 return GSOCK_NOERROR
;
580 /* If connect failed with EWOULDBLOCK and the GSocket object
581 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
582 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
583 * this way if the connection completes, a GSOCK_CONNECTION
584 * event will be generated, if enabled.
586 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
588 sck
->m_error
= GSOCK_WOULDBLOCK
;
589 return GSOCK_WOULDBLOCK
;
592 /* If connect failed with an error other than EWOULDBLOCK,
593 * then the call to GSocket_Connect has failed.
595 closesocket(sck
->m_fd
);
596 sck
->m_fd
= INVALID_SOCKET
;
597 sck
->m_error
= GSOCK_IOERR
;
601 return GSOCK_NOERROR
;
606 /* Like recv(), send(), ... */
607 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
609 assert(socket
!= NULL
);
611 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
613 socket
->m_error
= GSOCK_INVSOCK
;
617 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
620 if (socket
->m_stream
)
621 return _GSocket_Recv_Stream(socket
, buffer
, size
);
623 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
626 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
628 assert(socket
!= NULL
);
630 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
632 socket
->m_error
= GSOCK_INVSOCK
;
636 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
639 if (socket
->m_stream
)
640 return _GSocket_Send_Stream(socket
, buffer
, size
);
642 return _GSocket_Send_Dgram(socket
, buffer
, size
);
646 * Polls the socket to determine its status. This function will
647 * check for the events specified in the 'flags' parameter, and
648 * it will return a mask indicating which operations can be
649 * performed. This function won't block, regardless of the
650 * mode (blocking|nonblocking) of the socket.
652 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
654 fd_set readfds
, writefds
, exceptfds
;
656 GSocketEventFlags mask
;
658 assert(socket
!= NULL
);
660 if (socket
->m_fd
== INVALID_SOCKET
)
662 socket
->m_error
= GSOCK_INVSOCK
;
669 FD_SET(socket
->m_fd
, &readfds
);
670 FD_SET(socket
->m_fd
, &writefds
);
671 FD_SET(socket
->m_fd
, &exceptfds
);
675 select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
);
679 /* If select() says that the socket is readable, then we have
680 * no way to distinguish if that means 'data available' (to
681 * recv) or 'incoming connection' (to accept). The same goes
682 * for writability: we cannot distinguish between 'you can
683 * send data' and 'connection request completed'. So we will
684 * assume the following: if the flag was set upon entry,
685 * that means that the event was possible.
687 if (FD_ISSET(socket
->m_fd
, &readfds
))
689 mask
|= (flags
& GSOCK_CONNECTION_FLAG
);
690 mask
|= (flags
& GSOCK_INPUT_FLAG
);
692 if (FD_ISSET(socket
->m_fd
, &writefds
))
694 mask
|= (flags
& GSOCK_CONNECTION_FLAG
);
695 mask
|= (flags
& GSOCK_OUTPUT_FLAG
);
697 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
698 mask
|= (flags
& GSOCK_LOST_FLAG
);
705 /* GSocket_SetNonBlocking:
706 * Sets the socket to non-blocking mode. This is useful if
707 * we don't want to wait.
709 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
711 assert(socket
!= NULL
);
713 socket
->m_non_blocking
= non_block
;
716 /* GSocket_SetTimeout:
717 * Sets the timeout for blocking calls. Time is
718 * expressed in milliseconds.
720 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisecs
)
722 assert(socket
!= NULL
);
724 socket
->m_timeout
.tv_sec
= (millisecs
/ 1000);
725 socket
->m_timeout
.tv_usec
= (millisecs
% 1000) * 1000;
729 * Returns the last error occured for this socket.
731 GSocketError
GSocket_GetError(GSocket
*socket
)
733 assert(socket
!= NULL
);
735 return socket
->m_error
;
740 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
741 * and LOST). The callbacks are called in the following situations:
743 * INPUT: There is at least one byte in the input buffer
744 * OUTPUT: The system is sure that the next write call will not block
745 * CONNECTION: Two cases are possible:
746 * Client socket -> the connection is established
747 * Server socket -> a client requests a connection
748 * LOST: The connection is lost
750 * An event is generated only once and its state is reseted when the
751 * relative IO call is requested.
752 * For example: INPUT -> GSocket_Read()
753 * CONNECTION -> GSocket_Accept()
756 /* GSocket_SetCallback:
757 * Enables the callbacks specified by 'flags'. Note that 'flags'
758 * may be a combination of flags OR'ed toghether, so the same
759 * callback function can be made to accept different events.
760 * The callback function must have the following prototype:
762 * void function(GSocket *socket, GSocketEvent event, char *cdata)
764 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
765 GSocketCallback callback
, char *cdata
)
769 assert (socket
!= NULL
);
771 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
773 /* We test each flag and enable the corresponding events */
774 if ((flags
& (1 << count
)) != 0)
776 socket
->m_cbacks
[count
] = callback
;
777 socket
->m_data
[count
] = cdata
;
781 _GSocket_Configure_Callbacks(socket
);
784 /* GSocket_UnsetCallback:
785 * Disables all callbacks specified by 'flags', which may be a
786 * combination of flags OR'ed toghether.
788 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
792 assert(socket
!= NULL
);
794 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
796 /* We test each flag and disable the corresponding events */
797 if ((flags
& (1 << count
)) != 0)
799 socket
->m_cbacks
[count
] = NULL
;
803 _GSocket_Configure_Callbacks(socket
);
809 void _GSocket_Configure_Callbacks(GSocket
*socket
)
814 if (socket
->m_fd
== INVALID_SOCKET
)
817 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
819 if (socket
->m_cbacks
[count
] != NULL
)
823 case GSOCK_INPUT
: mask
|= FD_READ
; break;
824 case GSOCK_OUTPUT
: mask
|= FD_WRITE
; break;
825 case GSOCK_CONNECTION
: mask
|= (FD_ACCEPT
| FD_CONNECT
); break;
826 case GSOCK_LOST
: mask
|= FD_CLOSE
; break;
831 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, mask
);
834 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
841 GSocketCallback cback
;
843 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
845 EnterCriticalSection(&critical
);
846 socket
= socketList
[(uMsg
- WM_USER
)];
850 /* Check that the socket still exists (it has not been
851 * destroyed) and for safety, check that the m_fd field
852 * is what we expect it to be.
854 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
856 switch WSAGETSELECTEVENT(lParam
)
858 case FD_READ
: event
= GSOCK_INPUT
; break;
859 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
860 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
863 if (WSAGETSELECTERROR(lParam
) != 0)
866 event
= GSOCK_CONNECTION
;
869 case FD_CLOSE
: event
= GSOCK_LOST
; break;
873 cback
= socket
->m_cbacks
[event
];
876 /* OK, we can now leave the critical section because we have
877 * already obtained the callback address (we make no further
878 * accesses to socket->whatever)
880 LeaveCriticalSection(&critical
);
883 (cback
)(socket
, event
, socket
->m_data
[event
]);
888 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
892 /* _GSocket_Input_Timeout:
893 * For blocking sockets, wait until data is available or
894 * until timeout ellapses.
896 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
900 if (!socket
->m_non_blocking
)
903 FD_SET(socket
->m_fd
, &readfds
);
904 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
906 socket
->m_error
= GSOCK_TIMEDOUT
;
907 return GSOCK_TIMEDOUT
;
910 return GSOCK_NOERROR
;
913 /* _GSocket_Output_Timeout:
914 * For blocking sockets, wait until data can be sent without
915 * blocking or until timeout ellapses.
917 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
921 if (!socket
->m_non_blocking
)
924 FD_SET(socket
->m_fd
, &writefds
);
925 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
927 socket
->m_error
= GSOCK_TIMEDOUT
;
928 return GSOCK_TIMEDOUT
;
931 return GSOCK_NOERROR
;
934 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
938 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
940 if (ret
== SOCKET_ERROR
)
942 if (WSAGetLastError() != WSAEWOULDBLOCK
)
943 socket
->m_error
= GSOCK_IOERR
;
945 socket
->m_error
= GSOCK_WOULDBLOCK
;
953 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
955 struct sockaddr from
;
959 fromlen
= sizeof(from
);
961 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
963 if (ret
== SOCKET_ERROR
)
965 if (WSAGetLastError() != WSAEWOULDBLOCK
)
966 socket
->m_error
= GSOCK_IOERR
;
968 socket
->m_error
= GSOCK_WOULDBLOCK
;
973 /* Translate a system address into a GSocket address */
976 socket
->m_peer
= GAddress_new();
979 socket
->m_error
= GSOCK_MEMERR
;
983 if (_GAddress_translate_from(socket
->m_peer
, &from
, fromlen
) != GSOCK_NOERROR
)
985 socket
->m_error
= GSOCK_MEMERR
;
986 GAddress_destroy(socket
->m_peer
);
993 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
997 ret
= send(socket
->m_fd
, buffer
, size
, 0);
999 if (ret
== SOCKET_ERROR
)
1001 if (WSAGetLastError() != WSAEWOULDBLOCK
)
1002 socket
->m_error
= GSOCK_IOERR
;
1004 socket
->m_error
= GSOCK_WOULDBLOCK
;
1011 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1013 struct sockaddr
*addr
;
1016 if (!socket
->m_peer
)
1018 socket
->m_error
= GSOCK_INVADDR
;
1022 if (!_GAddress_translate_to(socket
->m_peer
, &addr
, &len
))
1024 socket
->m_error
= GSOCK_MEMERR
;
1028 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1030 /* Frees memory allocated by _GAddress_translate_to */
1033 if (ret
== SOCKET_ERROR
)
1035 if (WSAGetLastError() != WSAEWOULDBLOCK
)
1036 socket
->m_error
= GSOCK_IOERR
;
1038 socket
->m_error
= GSOCK_WOULDBLOCK
;
1047 * -------------------------------------------------------------------------
1049 * -------------------------------------------------------------------------
1052 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1053 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1054 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1056 #define CHECK_ADDRESS(address, family, retval) \
1058 if (address->m_family == GSOCK_NOFAMILY) \
1059 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1060 return address->m_error; \
1061 if (address->m_family != GSOCK_##family) \
1063 address->m_error = GSOCK_INVADDR; \
1068 GAddress
*GAddress_new()
1072 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1075 address
->m_family
= GSOCK_NOFAMILY
;
1076 address
->m_addr
= NULL
;
1082 GAddress
*GAddress_copy(GAddress
*address
)
1086 assert(address
!= NULL
);
1088 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1091 memcpy(addr2
, address
, sizeof(GAddress
));
1093 if (address
->m_addr
)
1095 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1096 if (addr2
->m_addr
== NULL
)
1101 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1107 void GAddress_destroy(GAddress
*address
)
1109 assert(address
!= NULL
);
1114 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1116 assert(address
!= NULL
);
1118 address
->m_family
= type
;
1121 GAddressType
GAddress_GetFamily(GAddress
*address
)
1123 assert(address
!= NULL
);
1125 return address
->m_family
;
1128 GSocketError
_GAddress_translate_from(GAddress
*address
,
1129 struct sockaddr
*addr
, int len
)
1131 address
->m_realfamily
= addr
->sa_family
;
1132 switch (addr
->sa_family
)
1135 address
->m_family
= GSOCK_INET
;
1138 address
->m_family
= GSOCK_UNIX
;
1142 address
->m_family
= GSOCK_INET6
;
1147 address
->m_error
= GSOCK_INVOP
;
1152 if (address
->m_addr
)
1153 free(address
->m_addr
);
1155 address
->m_len
= len
;
1156 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1158 if (address
->m_addr
== NULL
)
1160 address
->m_error
= GSOCK_MEMERR
;
1161 return GSOCK_MEMERR
;
1163 memcpy(address
->m_addr
, addr
, len
);
1165 return GSOCK_NOERROR
;
1168 GSocketError
_GAddress_translate_to(GAddress
*address
,
1169 struct sockaddr
**addr
, int *len
)
1171 if (!address
->m_addr
)
1173 address
->m_error
= GSOCK_INVADDR
;
1174 return GSOCK_INVADDR
;
1177 *len
= address
->m_len
;
1178 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1181 address
->m_error
= GSOCK_MEMERR
;
1182 return GSOCK_MEMERR
;
1185 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1186 return GSOCK_NOERROR
;
1190 * -------------------------------------------------------------------------
1191 * Internet address family
1192 * -------------------------------------------------------------------------
1195 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1197 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1198 if (address
->m_addr
== NULL
)
1200 address
->m_error
= GSOCK_MEMERR
;
1201 return GSOCK_MEMERR
;
1204 address
->m_len
= sizeof(struct sockaddr_in
);
1205 address
->m_family
= GSOCK_INET
;
1206 address
->m_realfamily
= PF_INET
;
1207 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1208 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1210 return GSOCK_NOERROR
;
1213 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1216 struct in_addr
*addr
;
1218 assert(address
!= NULL
);
1220 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1222 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1224 addr
->s_addr
= inet_addr(hostname
);
1226 /* If it is a numeric host name, convert it now */
1227 if (addr
->s_addr
== INADDR_NONE
)
1229 struct in_addr
*array_addr
;
1231 /* It is a real name, we solve it */
1232 if ((he
= gethostbyname(hostname
)) == NULL
)
1234 address
->m_error
= GSOCK_NOHOST
;
1235 return GSOCK_NOHOST
;
1237 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1238 addr
->s_addr
= array_addr
[0].s_addr
;
1240 return GSOCK_NOERROR
;
1243 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1244 unsigned long hostaddr
)
1246 struct in_addr
*addr
;
1248 assert(address
!= NULL
);
1250 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1252 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1253 addr
->s_addr
= hostaddr
;
1255 return GSOCK_NOERROR
;
1258 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1259 const char *protocol
)
1262 struct sockaddr_in
*addr
;
1264 assert(address
!= NULL
);
1265 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1269 address
->m_error
= GSOCK_INVPORT
;
1273 se
= getservbyname(port
, protocol
);
1276 if (isdigit(port
[0]))
1280 port_int
= atoi(port
);
1281 addr
= (struct sockaddr_in
*)address
->m_addr
;
1282 addr
->sin_port
= htons((u_short
) port_int
);
1283 return GSOCK_NOERROR
;
1286 address
->m_error
= GSOCK_INVPORT
;
1287 return GSOCK_INVPORT
;
1290 addr
= (struct sockaddr_in
*)address
->m_addr
;
1291 addr
->sin_port
= se
->s_port
;
1293 return GSOCK_NOERROR
;
1296 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1298 struct sockaddr_in
*addr
;
1300 assert(address
!= NULL
);
1301 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1303 addr
= (struct sockaddr_in
*)address
->m_addr
;
1304 addr
->sin_port
= htons(port
);
1306 return GSOCK_NOERROR
;
1309 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1313 struct sockaddr_in
*addr
;
1315 assert(address
!= NULL
);
1316 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1318 addr
= (struct sockaddr_in
*)address
->m_addr
;
1319 addr_buf
= (char *)&(addr
->sin_addr
);
1321 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1324 address
->m_error
= GSOCK_NOHOST
;
1325 return GSOCK_NOHOST
;
1328 strncpy(hostname
, he
->h_name
, sbuf
);
1330 return GSOCK_NOERROR
;
1333 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1335 struct sockaddr_in
*addr
;
1337 assert(address
!= NULL
);
1338 CHECK_ADDRESS(address
, INET
, 0);
1340 addr
= (struct sockaddr_in
*)address
->m_addr
;
1342 return addr
->sin_addr
.s_addr
;
1345 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1347 struct sockaddr_in
*addr
;
1349 assert(address
!= NULL
);
1350 CHECK_ADDRESS(address
, INET
, 0);
1352 addr
= (struct sockaddr_in
*)address
->m_addr
;
1353 return ntohs(addr
->sin_port
);
1357 * -------------------------------------------------------------------------
1358 * Unix address family
1359 * -------------------------------------------------------------------------
1362 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1364 assert (address
!= NULL
);
1365 address
->m_error
= GSOCK_INVADDR
;
1366 return GSOCK_INVADDR
;
1369 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1371 assert (address
!= NULL
);
1372 address
->m_error
= GSOCK_INVADDR
;
1373 return GSOCK_INVADDR
;
1376 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1378 assert (address
!= NULL
);
1379 address
->m_error
= GSOCK_INVADDR
;
1380 return GSOCK_INVADDR
;
1384 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
1389 /* Diferencias con la version Unix:
1390 * - El descriptor es SOCKET y no int
1391 * - Constantes -1 pasan a INVALID_SOCKET
1392 * - Errores en muchas funciones pasan de -1 o <0 a SOCKET_ERROR
1393 * - ioctl y close pasan a ioctlsocket y closesocket
1394 * - inet_addr en lugar de inet_aton
1395 * - Codigo de inicializacion y terminacion para inicializar y
1396 * terminar WinSocket y para la ventana interna.
1397 * - SetTimeout en la version MSW simplemente guarda el valor en
1398 * socket.m_timeout, por tanto no hay necesidad de llamar a
1399 * SetTimeout cada vez que se crea realmente un socket (no
1401 * - Lo mismo para SetNonBlocking.