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__
26 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
28 #ifndef __GSOCKET_STANDALONE__
30 #include "wx/msw/gsockmsw.h"
31 #include "wx/gsocket.h"
33 #define INSTANCE wxGetInstance()
40 /* If not using wxWindows, a global var called hInst must
41 * be available and it must contain the app's instance
44 #define INSTANCE hInst
46 #endif /* __GSOCKET_STANDALONE__ */
58 # pragma warning(default:4115) /* named type definition in parentheses */
61 #define CLASSNAME "_GSocket_Internal_Window_Class"
62 #define WINDOWNAME "_GSocket_Internal_Window_Name"
64 /* Maximum number of different GSocket objects at a given time.
65 * This value can be modified at will, but it CANNOT be greater
66 * than (0x7FFF - WM_USER + 1)
68 #define MAXSOCKETS 1024
70 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
71 #error "MAXSOCKETS is too big!"
75 /* Global variables */
77 extern HINSTANCE INSTANCE
;
79 static CRITICAL_SECTION critical
;
80 static GSocket
* socketList
[MAXSOCKETS
];
81 static int firstAvailable
;
83 /* Global initializers */
85 int GSocket_Init(void)
91 /* Create internal window for event notifications */
93 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
94 winClass
.cbClsExtra
= 0;
95 winClass
.cbWndExtra
= 0;
96 winClass
.hInstance
= INSTANCE
;
97 winClass
.hIcon
= (HICON
) NULL
;
98 winClass
.hCursor
= (HCURSOR
) NULL
;
99 winClass
.hbrBackground
= (HBRUSH
) NULL
;
100 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
101 winClass
.lpszClassName
= CLASSNAME
;
103 RegisterClass(&winClass
);
104 hWin
= CreateWindow(CLASSNAME
,
107 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
109 if (!hWin
) return FALSE
;
111 /* Initialize socket list */
112 InitializeCriticalSection(&critical
);
114 for (i
= 0; i
< MAXSOCKETS
; i
++)
116 socketList
[i
] = NULL
;
120 /* Initialize WinSocket */
121 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
124 void GSocket_Cleanup(void)
126 /* Destroy internal window */
128 UnregisterClass(CLASSNAME
, INSTANCE
);
130 /* Delete critical section */
131 DeleteCriticalSection(&critical
);
133 /* Cleanup WinSocket */
137 /* Per-socket GUI initialization / cleanup */
139 bool _GSocket_GUI_Init(GSocket
*socket
)
143 /* Allocate a new message number for this socket */
144 EnterCriticalSection(&critical
);
147 while (socketList
[i
] != NULL
)
149 i
= (i
+ 1) % MAXSOCKETS
;
151 if (i
== firstAvailable
) /* abort! */
153 LeaveCriticalSection(&critical
);
157 socketList
[i
] = socket
;
158 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
159 socket
->m_msgnumber
= (i
+ WM_USER
);
161 LeaveCriticalSection(&critical
);
166 void _GSocket_GUI_Destroy(GSocket
*socket
)
168 /* Remove the socket from the list */
169 EnterCriticalSection(&critical
);
170 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
171 LeaveCriticalSection(&critical
);
174 /* Windows proc for asynchronous event handling */
176 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
183 GSocketCallback cback
;
186 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
188 EnterCriticalSection(&critical
);
189 socket
= socketList
[(uMsg
- WM_USER
)];
194 /* Check that the socket still exists (it has not been
195 * destroyed) and for safety, check that the m_fd field
196 * is what we expect it to be.
198 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
200 switch WSAGETSELECTEVENT(lParam
)
202 case FD_READ
: event
= GSOCK_INPUT
; break;
203 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
204 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
207 if (WSAGETSELECTERROR(lParam
) != 0)
210 event
= GSOCK_CONNECTION
;
213 case FD_CLOSE
: event
= GSOCK_LOST
; break;
218 cback
= socket
->m_cbacks
[event
];
219 data
= socket
->m_data
[event
];
221 if (event
== GSOCK_LOST
)
222 socket
->m_detected
= GSOCK_LOST_FLAG
;
224 socket
->m_detected
|= (1 << event
);
228 /* OK, we can now leave the critical section because we have
229 * already obtained the callback address (we make no further
230 * accesses to socket->whatever). However, the app should
231 * be prepared to handle events from a socket that has just
234 LeaveCriticalSection(&critical
);
237 (cback
)(socket
, event
, data
);
242 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
245 /* _GSocket_Enable_Events:
246 * Enable all event notifications; we need to be notified of all
247 * events for internal processing, but we will only notify users
248 * when an appropiate callback function has been installed.
250 void _GSocket_Enable_Events(GSocket
*socket
)
252 assert (socket
!= NULL
);
254 if (socket
->m_fd
!= INVALID_SOCKET
)
256 /* We could probably just subscribe to all events regardless
257 * of the socket type, but MS recommends to do it this way.
259 long lEvent
= socket
->m_server
?
260 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
262 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
266 /* _GSocket_Disable_Events:
267 * Disable event notifications (when shutdowning the socket)
269 void _GSocket_Disable_Events(GSocket
*socket
)
271 assert (socket
!= NULL
);
273 if (socket
->m_fd
!= INVALID_SOCKET
)
275 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
279 #else /* !wxUSE_SOCKETS */
282 * Translation unit shouldn't be empty, so include this typedef to make the
283 * compiler (VC++ 6.0, for example) happy
285 typedef (*wxDummy
)();
287 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */