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__
43 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
45 #ifndef __GSOCKET_STANDALONE__
47 #include "wx/msw/gsockmsw.h"
48 #include "wx/gsocket.h"
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")
86 #define WINDOWNAME TEXT("_GSocket_Internal_Window_Name")
88 /* Maximum number of different GSocket objects at a given time.
89 * This value can be modified at will, but it CANNOT be greater
90 * than (0x7FFF - WM_USER + 1)
92 #define MAXSOCKETS 1024
94 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
95 #error "MAXSOCKETS is too big!"
99 /* Global variables */
101 extern HINSTANCE INSTANCE
;
103 static CRITICAL_SECTION critical
;
104 static GSocket
* socketList
[MAXSOCKETS
];
105 static int firstAvailable
;
107 /* Global initializers */
109 int _GSocket_GUI_Init(void)
114 /* Create internal window for event notifications */
116 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
117 winClass
.cbClsExtra
= 0;
118 winClass
.cbWndExtra
= 0;
119 winClass
.hInstance
= INSTANCE
;
120 winClass
.hIcon
= (HICON
) NULL
;
121 winClass
.hCursor
= (HCURSOR
) NULL
;
122 winClass
.hbrBackground
= (HBRUSH
) NULL
;
123 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
124 winClass
.lpszClassName
= CLASSNAME
;
126 RegisterClass(&winClass
);
127 hWin
= CreateWindow(CLASSNAME
,
130 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
132 if (!hWin
) return FALSE
;
134 /* Initialize socket list */
135 InitializeCriticalSection(&critical
);
137 for (i
= 0; i
< MAXSOCKETS
; i
++)
139 socketList
[i
] = NULL
;
146 void _GSocket_GUI_Cleanup(void)
148 /* Destroy internal window */
150 UnregisterClass(CLASSNAME
, INSTANCE
);
152 /* Delete critical section */
153 DeleteCriticalSection(&critical
);
156 /* Per-socket GUI initialization / cleanup */
158 int _GSocket_GUI_Init_Socket(GSocket
*socket
)
162 /* Allocate a new message number for this socket */
163 EnterCriticalSection(&critical
);
166 while (socketList
[i
] != NULL
)
168 i
= (i
+ 1) % MAXSOCKETS
;
170 if (i
== firstAvailable
) /* abort! */
172 LeaveCriticalSection(&critical
);
176 socketList
[i
] = socket
;
177 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
178 socket
->m_msgnumber
= (i
+ WM_USER
);
180 LeaveCriticalSection(&critical
);
185 void _GSocket_GUI_Destroy_Socket(GSocket
*socket
)
187 /* Remove the socket from the list */
188 EnterCriticalSection(&critical
);
189 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
190 LeaveCriticalSection(&critical
);
193 /* Windows proc for asynchronous event handling */
195 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
202 GSocketCallback cback
;
205 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
207 EnterCriticalSection(&critical
);
208 socket
= socketList
[(uMsg
- WM_USER
)];
209 event
= (GSocketEvent
) -1;
213 /* Check that the socket still exists (it has not been
214 * destroyed) and for safety, check that the m_fd field
215 * is what we expect it to be.
217 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
219 switch WSAGETSELECTEVENT(lParam
)
221 case FD_READ
: event
= GSOCK_INPUT
; break;
222 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
223 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
226 if (WSAGETSELECTERROR(lParam
) != 0)
229 event
= GSOCK_CONNECTION
;
232 case FD_CLOSE
: event
= GSOCK_LOST
; break;
237 cback
= socket
->m_cbacks
[event
];
238 data
= socket
->m_data
[event
];
240 if (event
== GSOCK_LOST
)
241 socket
->m_detected
= GSOCK_LOST_FLAG
;
243 socket
->m_detected
|= (1 << event
);
247 /* OK, we can now leave the critical section because we have
248 * already obtained the callback address (we make no further
249 * accesses to socket->whatever). However, the app should
250 * be prepared to handle events from a socket that has just
253 LeaveCriticalSection(&critical
);
256 (cback
)(socket
, event
, data
);
261 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
264 /* _GSocket_Enable_Events:
265 * Enable all event notifications; we need to be notified of all
266 * events for internal processing, but we will only notify users
267 * when an appropiate callback function has been installed.
269 void _GSocket_Enable_Events(GSocket
*socket
)
271 assert (socket
!= NULL
);
273 if (socket
->m_fd
!= INVALID_SOCKET
)
275 /* We could probably just subscribe to all events regardless
276 * of the socket type, but MS recommends to do it this way.
278 long lEvent
= socket
->m_server
?
279 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
281 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
285 /* _GSocket_Disable_Events:
286 * Disable event notifications (when shutdowning the socket)
288 void _GSocket_Disable_Events(GSocket
*socket
)
290 assert (socket
!= NULL
);
292 if (socket
->m_fd
!= INVALID_SOCKET
)
294 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
298 #else /* !wxUSE_SOCKETS */
301 * Translation unit shouldn't be empty, so include this typedef to make the
302 * compiler (VC++ 6.0, for example) happy
304 typedef void (*wxDummy
)();
306 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */