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 #ifndef __GSOCKET_STANDALONE__
18 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
20 #ifndef __GSOCKET_STANDALONE__
22 #include "wx/msw/gsockmsw.h"
23 #include "wx/gsocket.h"
25 #define INSTANCE wxGetInstance()
32 /* If not using wxWindows, a global var called hInst must
33 * be available and it must containt the app's instance
36 #define INSTANCE hInst
38 #endif /* __GSOCKET_STANDALONE__ */
48 #define CLASSNAME "_GSocket_Internal_Window_Class"
49 #define WINDOWNAME "_GSocket_Internal_Window_Name"
51 /* Maximum number of different GSocket objects at a given time.
52 * This value can be modified at will, but it CANNOT be greater
53 * than (0x7FFF - WM_USER + 1)
55 #define MAXSOCKETS 1024
57 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
58 #error "MAXSOCKETS is too big!"
62 /* Global variables */
64 extern HINSTANCE INSTANCE
;
66 static CRITICAL_SECTION critical
;
67 static GSocket
* socketList
[MAXSOCKETS
];
68 static int firstAvailable
;
70 /* Global initializers */
72 bool GSocket_Init(void)
78 /* Create internal window for event notifications */
80 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
81 winClass
.cbClsExtra
= 0;
82 winClass
.cbWndExtra
= 0;
83 winClass
.hInstance
= INSTANCE
;
84 winClass
.hIcon
= (HICON
) NULL
;
85 winClass
.hCursor
= (HCURSOR
) NULL
;
86 winClass
.hbrBackground
= (HBRUSH
) NULL
;
87 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
88 winClass
.lpszClassName
= CLASSNAME
;
90 RegisterClass(&winClass
);
91 hWin
= CreateWindow(CLASSNAME
,
94 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
96 if (!hWin
) return FALSE
;
98 /* Initialize socket list */
99 InitializeCriticalSection(&critical
);
101 for (i
= 0; i
< MAXSOCKETS
; i
++)
103 socketList
[i
] = NULL
;
107 /* Initialize WinSocket */
108 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
111 void GSocket_Cleanup(void)
113 /* Destroy internal window */
115 UnregisterClass(CLASSNAME
, INSTANCE
);
117 /* Delete critical section */
118 DeleteCriticalSection(&critical
);
120 /* Cleanup WinSocket */
124 /* Per-socket GUI initialization / cleanup */
126 bool _GSocket_GUI_Init(GSocket
*socket
)
130 /* Allocate a new message number for this socket */
131 EnterCriticalSection(&critical
);
134 while (socketList
[i
] != NULL
)
136 i
= (i
+ 1) % MAXSOCKETS
;
138 if (i
== firstAvailable
) /* abort! */
140 LeaveCriticalSection(&critical
);
144 socketList
[i
] = socket
;
145 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
146 socket
->m_msgnumber
= (i
+ WM_USER
);
148 LeaveCriticalSection(&critical
);
153 void _GSocket_GUI_Destroy(GSocket
*socket
)
155 /* Remove the socket from the list */
156 EnterCriticalSection(&critical
);
157 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
158 LeaveCriticalSection(&critical
);
161 /* Windows proc for asynchronous event handling */
163 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
170 GSocketCallback cback
;
173 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
175 EnterCriticalSection(&critical
);
176 socket
= socketList
[(uMsg
- WM_USER
)];
181 /* Check that the socket still exists (it has not been
182 * destroyed) and for safety, check that the m_fd field
183 * is what we expect it to be.
185 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
187 switch WSAGETSELECTEVENT(lParam
)
189 case FD_READ
: event
= GSOCK_INPUT
; break;
190 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
191 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
194 if (WSAGETSELECTERROR(lParam
) != 0)
197 event
= GSOCK_CONNECTION
;
200 case FD_CLOSE
: event
= GSOCK_LOST
; break;
205 cback
= socket
->m_cbacks
[event
];
206 data
= socket
->m_data
[event
];
208 if (event
== GSOCK_LOST
)
209 socket
->m_detected
= GSOCK_LOST_FLAG
;
211 socket
->m_detected
|= (1 << event
);
215 /* OK, we can now leave the critical section because we have
216 * already obtained the callback address (we make no further
217 * accesses to socket->whatever). However, the app should
218 * be prepared to handle events from a socket that has just
221 LeaveCriticalSection(&critical
);
224 (cback
)(socket
, event
, data
);
229 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
232 /* _GSocket_Enable_Events:
233 * Enable all event notifications; we need to be notified of all
234 * events for internal processing, but we will only notify users
235 * when an appropiate callback function has been installed.
237 void _GSocket_Enable_Events(GSocket
*socket
)
239 assert (socket
!= NULL
);
241 if (socket
->m_fd
!= INVALID_SOCKET
)
243 /* We could probably just subscribe to all events regardless
244 * of the socket type, but MS recommends to do it this way.
246 long lEvent
= socket
->m_server
?
247 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
249 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
253 /* _GSocket_Disable_Events:
254 * Disable event notifications (when shutdowning the socket)
256 void _GSocket_Disable_Events(GSocket
*socket
)
258 assert (socket
!= NULL
);
260 if (socket
->m_fd
!= INVALID_SOCKET
)
262 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
266 #else /* !wxUSE_SOCKETS */
269 * Translation unit shouldn't be empty, so include this typedef to make the
270 * compiler (VC++ 6.0, for example) happy
272 typedef (*wxDummy
)();
274 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */