1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket GUI-specific MSW code
7 * -------------------------------------------------------------------------
11 * PLEASE don't put C++ comments here - this is a C source file.
14 /* including rasasync.h (included from windows.h itself included from
15 * wx/setup.h and/or winsock.h results in this warning for
16 * RPCNOTIFICATION_ROUTINE
19 # pragma warning(disable:4115) /* named type definition in parentheses */
22 #ifndef __GSOCKET_STANDALONE__
27 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
29 #ifndef __GSOCKET_STANDALONE__
31 #include "wx/msw/gsockmsw.h"
32 #include "wx/gsocket.h"
34 #define INSTANCE wxGetInstance()
41 /* If not using wxWindows, a global var called hInst must
42 * be available and it must contain the app's instance
45 #define INSTANCE hInst
47 #endif /* __GSOCKET_STANDALONE__ */
58 /* don't use C++ TRUE/FALSE definition which we get from wx/defs.h */
65 # pragma warning(default:4115) /* named type definition in parentheses */
68 #define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
69 #define WINDOWNAME TEXT("_GSocket_Internal_Window_Name")
71 /* Maximum number of different GSocket objects at a given time.
72 * This value can be modified at will, but it CANNOT be greater
73 * than (0x7FFF - WM_USER + 1)
75 #define MAXSOCKETS 1024
77 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
78 #error "MAXSOCKETS is too big!"
82 /* Global variables */
84 extern HINSTANCE INSTANCE
;
86 static CRITICAL_SECTION critical
;
87 static GSocket
* socketList
[MAXSOCKETS
];
88 static int firstAvailable
;
90 /* Global initializers */
92 int GSocket_Init(void)
98 /* Create internal window for event notifications */
100 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
101 winClass
.cbClsExtra
= 0;
102 winClass
.cbWndExtra
= 0;
103 winClass
.hInstance
= INSTANCE
;
104 winClass
.hIcon
= (HICON
) NULL
;
105 winClass
.hCursor
= (HCURSOR
) NULL
;
106 winClass
.hbrBackground
= (HBRUSH
) NULL
;
107 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
108 winClass
.lpszClassName
= CLASSNAME
;
110 RegisterClass(&winClass
);
111 hWin
= CreateWindow(CLASSNAME
,
114 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
116 if (!hWin
) return FALSE
;
118 /* Initialize socket list */
119 InitializeCriticalSection(&critical
);
121 for (i
= 0; i
< MAXSOCKETS
; i
++)
123 socketList
[i
] = NULL
;
127 /* Initialize WinSocket */
128 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
131 void GSocket_Cleanup(void)
133 /* Destroy internal window */
135 UnregisterClass(CLASSNAME
, INSTANCE
);
137 /* Delete critical section */
138 DeleteCriticalSection(&critical
);
140 /* Cleanup WinSocket */
144 /* Per-socket GUI initialization / cleanup */
146 int _GSocket_GUI_Init(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(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
)];
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__) */