1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket GUI-specific MSW code
7 * -------------------------------------------------------------------------
11 * TODO: for WinCE we need to replace WSAAsyncSelect
12 * (Windows message-based notification of network events for a socket)
13 * with another mechanism.
14 * We may need to have a separate thread that polls for socket events
15 * using select() and sends a message to the main thread.
19 * PLEASE don't put C++ comments here - this is a C source file.
22 /* including rasasync.h (included from windows.h itself included from
23 * wx/setup.h and/or winsock.h results in this warning for
24 * RPCNOTIFICATION_ROUTINE
27 # pragma warning(disable:4115) /* named type definition in parentheses */
30 /* This needs to be before the wx/defs/h inclusion
38 #ifndef __GSOCKET_STANDALONE__
42 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
44 #ifndef __GSOCKET_STANDALONE__
46 #include "wx/msw/gsockmsw.h"
47 #include "wx/gsocket.h"
49 HINSTANCE
wxGetInstance(void);
50 #define INSTANCE wxGetInstance()
57 /* If not using wxWindows, a global var called hInst must
58 * be available and it must contain the app's instance
61 #define INSTANCE hInst
63 #endif /* __GSOCKET_STANDALONE__ */
70 #include "wx/msw/wince/net.h"
82 # pragma warning(default:4115) /* named type definition in parentheses */
85 #define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
87 /* implemented in utils.cpp */
88 extern WXDLLIMPEXP_BASE HWND
89 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
91 /* Maximum number of different GSocket objects at a given time.
92 * This value can be modified at will, but it CANNOT be greater
93 * than (0x7FFF - WM_USER + 1)
95 #define MAXSOCKETS 1024
97 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
98 #error "MAXSOCKETS is too big!"
102 /* Global variables */
104 extern HINSTANCE INSTANCE
;
106 static CRITICAL_SECTION critical
;
107 static GSocket
* socketList
[MAXSOCKETS
];
108 static int firstAvailable
;
110 /* Global initializers */
112 int _GSocket_GUI_Init(void)
114 static LPCTSTR pclassname
= NULL
;
117 /* Create internal window for event notifications */
118 hWin
= wxCreateHiddenWindow(&pclassname
, CLASSNAME
, _GSocket_Internal_WinProc
);
122 /* Initialize socket list */
123 InitializeCriticalSection(&critical
);
125 for (i
= 0; i
< MAXSOCKETS
; i
++)
127 socketList
[i
] = NULL
;
134 void _GSocket_GUI_Cleanup(void)
136 /* Destroy internal window */
138 UnregisterClass(CLASSNAME
, INSTANCE
);
140 /* Delete critical section */
141 DeleteCriticalSection(&critical
);
144 /* Per-socket GUI initialization / cleanup */
146 int _GSocket_GUI_Init_Socket(GSocket
*socket
)
150 /* Allocate a new message number for this socket */
151 EnterCriticalSection(&critical
);
154 while (socketList
[i
] != NULL
)
156 i
= (i
+ 1) % MAXSOCKETS
;
158 if (i
== firstAvailable
) /* abort! */
160 LeaveCriticalSection(&critical
);
164 socketList
[i
] = socket
;
165 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
166 socket
->m_msgnumber
= (i
+ WM_USER
);
168 LeaveCriticalSection(&critical
);
173 void _GSocket_GUI_Destroy_Socket(GSocket
*socket
)
175 /* Remove the socket from the list */
176 EnterCriticalSection(&critical
);
177 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
178 LeaveCriticalSection(&critical
);
181 /* Windows proc for asynchronous event handling */
183 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
190 GSocketCallback cback
;
193 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
195 EnterCriticalSection(&critical
);
196 socket
= socketList
[(uMsg
- WM_USER
)];
197 event
= (GSocketEvent
) -1;
201 /* Check that the socket still exists (it has not been
202 * destroyed) and for safety, check that the m_fd field
203 * is what we expect it to be.
205 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
207 switch WSAGETSELECTEVENT(lParam
)
209 case FD_READ
: event
= GSOCK_INPUT
; break;
210 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
211 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
214 if (WSAGETSELECTERROR(lParam
) != 0)
217 event
= GSOCK_CONNECTION
;
220 case FD_CLOSE
: event
= GSOCK_LOST
; break;
225 cback
= socket
->m_cbacks
[event
];
226 data
= socket
->m_data
[event
];
228 if (event
== GSOCK_LOST
)
229 socket
->m_detected
= GSOCK_LOST_FLAG
;
231 socket
->m_detected
|= (1 << event
);
235 /* OK, we can now leave the critical section because we have
236 * already obtained the callback address (we make no further
237 * accesses to socket->whatever). However, the app should
238 * be prepared to handle events from a socket that has just
241 LeaveCriticalSection(&critical
);
244 (cback
)(socket
, event
, data
);
249 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
252 /* _GSocket_Enable_Events:
253 * Enable all event notifications; we need to be notified of all
254 * events for internal processing, but we will only notify users
255 * when an appropiate callback function has been installed.
257 void _GSocket_Enable_Events(GSocket
*socket
)
259 assert (socket
!= NULL
);
261 if (socket
->m_fd
!= INVALID_SOCKET
)
263 /* We could probably just subscribe to all events regardless
264 * of the socket type, but MS recommends to do it this way.
266 long lEvent
= socket
->m_server
?
267 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
269 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
273 /* _GSocket_Disable_Events:
274 * Disable event notifications (when shutdowning the socket)
276 void _GSocket_Disable_Events(GSocket
*socket
)
278 assert (socket
!= NULL
);
280 if (socket
->m_fd
!= INVALID_SOCKET
)
282 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
286 #else /* !wxUSE_SOCKETS */
289 * Translation unit shouldn't be empty, so include this typedef to make the
290 * compiler (VC++ 6.0, for example) happy
292 typedef void (*wxDummy
)();
294 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */