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 HINSTANCE
wxGetInstance(void);
51 #define INSTANCE wxGetInstance()
58 /* If not using wxWindows, a global var called hInst must
59 * be available and it must contain the app's instance
62 #define INSTANCE hInst
64 #endif /* __GSOCKET_STANDALONE__ */
71 #include "wx/msw/wince/net.h"
83 # pragma warning(default:4115) /* named type definition in parentheses */
86 #define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
87 #define WINDOWNAME TEXT("_GSocket_Internal_Window_Name")
89 /* Maximum number of different GSocket objects at a given time.
90 * This value can be modified at will, but it CANNOT be greater
91 * than (0x7FFF - WM_USER + 1)
93 #define MAXSOCKETS 1024
95 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
96 #error "MAXSOCKETS is too big!"
100 /* Global variables */
102 extern HINSTANCE INSTANCE
;
104 static CRITICAL_SECTION critical
;
105 static GSocket
* socketList
[MAXSOCKETS
];
106 static int firstAvailable
;
108 /* Global initializers */
110 int _GSocket_GUI_Init(void)
115 /* Create internal window for event notifications */
117 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
118 winClass
.cbClsExtra
= 0;
119 winClass
.cbWndExtra
= 0;
120 winClass
.hInstance
= INSTANCE
;
121 winClass
.hIcon
= (HICON
) NULL
;
122 winClass
.hCursor
= (HCURSOR
) NULL
;
123 winClass
.hbrBackground
= (HBRUSH
) NULL
;
124 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
125 winClass
.lpszClassName
= CLASSNAME
;
127 RegisterClass(&winClass
);
128 hWin
= CreateWindow(CLASSNAME
,
131 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
133 if (!hWin
) return FALSE
;
135 /* Initialize socket list */
136 InitializeCriticalSection(&critical
);
138 for (i
= 0; i
< MAXSOCKETS
; i
++)
140 socketList
[i
] = NULL
;
147 void _GSocket_GUI_Cleanup(void)
149 /* Destroy internal window */
151 UnregisterClass(CLASSNAME
, INSTANCE
);
153 /* Delete critical section */
154 DeleteCriticalSection(&critical
);
157 /* Per-socket GUI initialization / cleanup */
159 int _GSocket_GUI_Init_Socket(GSocket
*socket
)
163 /* Allocate a new message number for this socket */
164 EnterCriticalSection(&critical
);
167 while (socketList
[i
] != NULL
)
169 i
= (i
+ 1) % MAXSOCKETS
;
171 if (i
== firstAvailable
) /* abort! */
173 LeaveCriticalSection(&critical
);
177 socketList
[i
] = socket
;
178 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
179 socket
->m_msgnumber
= (i
+ WM_USER
);
181 LeaveCriticalSection(&critical
);
186 void _GSocket_GUI_Destroy_Socket(GSocket
*socket
)
188 /* Remove the socket from the list */
189 EnterCriticalSection(&critical
);
190 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
191 LeaveCriticalSection(&critical
);
194 /* Windows proc for asynchronous event handling */
196 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
203 GSocketCallback cback
;
206 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
208 EnterCriticalSection(&critical
);
209 socket
= socketList
[(uMsg
- WM_USER
)];
210 event
= (GSocketEvent
) -1;
214 /* Check that the socket still exists (it has not been
215 * destroyed) and for safety, check that the m_fd field
216 * is what we expect it to be.
218 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
220 switch WSAGETSELECTEVENT(lParam
)
222 case FD_READ
: event
= GSOCK_INPUT
; break;
223 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
224 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
227 if (WSAGETSELECTERROR(lParam
) != 0)
230 event
= GSOCK_CONNECTION
;
233 case FD_CLOSE
: event
= GSOCK_LOST
; break;
238 cback
= socket
->m_cbacks
[event
];
239 data
= socket
->m_data
[event
];
241 if (event
== GSOCK_LOST
)
242 socket
->m_detected
= GSOCK_LOST_FLAG
;
244 socket
->m_detected
|= (1 << event
);
248 /* OK, we can now leave the critical section because we have
249 * already obtained the callback address (we make no further
250 * accesses to socket->whatever). However, the app should
251 * be prepared to handle events from a socket that has just
254 LeaveCriticalSection(&critical
);
257 (cback
)(socket
, event
, data
);
262 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
265 /* _GSocket_Enable_Events:
266 * Enable all event notifications; we need to be notified of all
267 * events for internal processing, but we will only notify users
268 * when an appropiate callback function has been installed.
270 void _GSocket_Enable_Events(GSocket
*socket
)
272 assert (socket
!= NULL
);
274 if (socket
->m_fd
!= INVALID_SOCKET
)
276 /* We could probably just subscribe to all events regardless
277 * of the socket type, but MS recommends to do it this way.
279 long lEvent
= socket
->m_server
?
280 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
282 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
286 /* _GSocket_Disable_Events:
287 * Disable event notifications (when shutdowning the socket)
289 void _GSocket_Disable_Events(GSocket
*socket
)
291 assert (socket
!= NULL
);
293 if (socket
->m_fd
!= INVALID_SOCKET
)
295 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
299 #else /* !wxUSE_SOCKETS */
302 * Translation unit shouldn't be empty, so include this typedef to make the
303 * compiler (VC++ 6.0, for example) happy
305 typedef void (*wxDummy
)();
307 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */