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_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
;
144 /* Initialize WinSocket */
145 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
148 void GSocket_Cleanup(void)
150 /* Destroy internal window */
152 UnregisterClass(CLASSNAME
, INSTANCE
);
154 /* Delete critical section */
155 DeleteCriticalSection(&critical
);
157 /* Cleanup WinSocket */
161 /* Per-socket GUI initialization / cleanup */
163 int _GSocket_GUI_Init(GSocket
*socket
)
167 /* Allocate a new message number for this socket */
168 EnterCriticalSection(&critical
);
171 while (socketList
[i
] != NULL
)
173 i
= (i
+ 1) % MAXSOCKETS
;
175 if (i
== firstAvailable
) /* abort! */
177 LeaveCriticalSection(&critical
);
181 socketList
[i
] = socket
;
182 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
183 socket
->m_msgnumber
= (i
+ WM_USER
);
185 LeaveCriticalSection(&critical
);
190 void _GSocket_GUI_Destroy(GSocket
*socket
)
192 /* Remove the socket from the list */
193 EnterCriticalSection(&critical
);
194 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
195 LeaveCriticalSection(&critical
);
198 /* Windows proc for asynchronous event handling */
200 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
207 GSocketCallback cback
;
210 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
212 EnterCriticalSection(&critical
);
213 socket
= socketList
[(uMsg
- WM_USER
)];
214 event
= (GSocketEvent
) -1;
218 /* Check that the socket still exists (it has not been
219 * destroyed) and for safety, check that the m_fd field
220 * is what we expect it to be.
222 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
224 switch WSAGETSELECTEVENT(lParam
)
226 case FD_READ
: event
= GSOCK_INPUT
; break;
227 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
228 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
231 if (WSAGETSELECTERROR(lParam
) != 0)
234 event
= GSOCK_CONNECTION
;
237 case FD_CLOSE
: event
= GSOCK_LOST
; break;
242 cback
= socket
->m_cbacks
[event
];
243 data
= socket
->m_data
[event
];
245 if (event
== GSOCK_LOST
)
246 socket
->m_detected
= GSOCK_LOST_FLAG
;
248 socket
->m_detected
|= (1 << event
);
252 /* OK, we can now leave the critical section because we have
253 * already obtained the callback address (we make no further
254 * accesses to socket->whatever). However, the app should
255 * be prepared to handle events from a socket that has just
258 LeaveCriticalSection(&critical
);
261 (cback
)(socket
, event
, data
);
266 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
269 /* _GSocket_Enable_Events:
270 * Enable all event notifications; we need to be notified of all
271 * events for internal processing, but we will only notify users
272 * when an appropiate callback function has been installed.
274 void _GSocket_Enable_Events(GSocket
*socket
)
276 assert (socket
!= NULL
);
278 if (socket
->m_fd
!= INVALID_SOCKET
)
280 /* We could probably just subscribe to all events regardless
281 * of the socket type, but MS recommends to do it this way.
283 long lEvent
= socket
->m_server
?
284 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
286 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
290 /* _GSocket_Disable_Events:
291 * Disable event notifications (when shutdowning the socket)
293 void _GSocket_Disable_Events(GSocket
*socket
)
295 assert (socket
!= NULL
);
297 if (socket
->m_fd
!= INVALID_SOCKET
)
299 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
303 #else /* !wxUSE_SOCKETS */
306 * Translation unit shouldn't be empty, so include this typedef to make the
307 * compiler (VC++ 6.0, for example) happy
309 typedef void (*wxDummy
)();
311 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */