]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | //////////////////////////////////////////////////////////////////////////////// |
2 | // Name: socket.cpp | |
3 | // Purpose: Socket handler classes | |
4 | // Authors: Guilhem Lavaux (completely rewritten from a basic API of Andrew | |
5 | // Davidson(1995) in wxWeb) | |
6 | // Created: April 1997 | |
7 | // Updated: March 1998 | |
8 | // Copyright: (C) 1998, 1997, Guilhem Lavaux | |
9 | // RCS_ID: $Id$ | |
10 | // License: see wxWindows license | |
11 | //////////////////////////////////////////////////////////////////////////////// | |
384b4373 | 12 | #ifdef __GNUG__ |
f4ada568 | 13 | #pragma implementation "socket.h" |
0c32066b JS |
14 | // #pragma interface |
15 | // #pragma implementation "socket.cpp" | |
f4ada568 GL |
16 | #endif |
17 | ||
17dff81c SC |
18 | #ifdef __MWERKS__ |
19 | typedef int socklen_t ; | |
20 | #endif | |
21 | ||
fcc6dddd JS |
22 | // For compilers that support precompilation, includes "wx.h". |
23 | #include "wx/wxprec.h" | |
24 | ||
25 | #ifdef __BORLANDC__ | |
26 | #pragma hdrstop | |
27 | #endif | |
28 | ||
f4ada568 GL |
29 | ///////////////////////////////////////////////////////////////////////////// |
30 | // wxWindows headers | |
31 | ///////////////////////////////////////////////////////////////////////////// | |
32 | #include <wx/defs.h> | |
33 | #include <wx/object.h> | |
34 | #include <wx/string.h> | |
35 | #include <wx/timer.h> | |
36 | #include <wx/utils.h> | |
37 | ||
7f555861 JS |
38 | // Not enough OS behaviour defined for wxStubs |
39 | #ifndef __WXSTUBS__ | |
40 | ||
f4ada568 GL |
41 | #include <stdlib.h> |
42 | #include <string.h> | |
43 | #include <ctype.h> | |
44 | ||
45 | ///////////////////////////////////////////////////////////////////////////// | |
46 | // System specific headers | |
47 | ///////////////////////////////////////////////////////////////////////////// | |
17dff81c SC |
48 | #ifdef __WXMAC__ |
49 | // in order to avoid problems with our c library and double definitions | |
50 | #define close closesocket | |
51 | #define ioctl ioctlsocket | |
52 | ||
53 | #include <wx/mac/macsock.h> | |
54 | extern GUSISpinFn GUSISpin; | |
55 | #define PROCESS_EVENTS() wxMacProcessEvents() | |
56 | const short kwxMacNetEventsMax = 1000 ; | |
57 | short wxMacNetEventsTop = 0 ; | |
58 | short wxMacNetEventsBottom = 0 ; | |
59 | short wxMacNetEventsEvents[kwxMacNetEventsMax] ; | |
60 | void *wxMacNetEventsReferences[kwxMacNetEventsMax] ; | |
61 | ||
62 | #define FD_READ 1 | |
63 | #define FD_WRITE 2 | |
64 | #define FD_CLOSE 4 | |
65 | #define FD_ACCEPT 8 | |
66 | #define FD_CONNECT 16 | |
67 | #define FD_READY 32 | |
68 | ||
69 | extern "C" void wxMacSocketHandlerProc( void *refcon , short event ) ; // adds events | |
70 | extern "C" void wxMacSocketOnRequestProc( void *refcon , short event ) ; // consumes them | |
71 | extern "C" void GUSISetReference( short sock , short eventmask , void * data ) ; | |
72 | void wxMacProcessEvents() ; | |
73 | #endif | |
74 | ||
f4ada568 GL |
75 | #if defined(__WINDOWS__) |
76 | #include <winsock.h> | |
77 | #endif // __WINDOWS__ | |
78 | ||
79 | #if defined(__UNIX__) | |
80 | ||
81 | #ifdef VMS | |
82 | #include <socket.h> | |
83 | #else | |
84 | #include <sys/socket.h> | |
85 | #endif | |
86 | #include <sys/ioctl.h> | |
87 | ||
88 | #include <sys/time.h> | |
89 | #include <unistd.h> | |
90 | ||
384b4373 RD |
91 | #ifdef sun |
92 | #include <sys/filio.h> | |
93 | #endif | |
94 | ||
f4ada568 GL |
95 | #endif // __UNIX__ |
96 | ||
97 | #include <signal.h> | |
98 | #include <errno.h> | |
99 | ||
fcc6dddd JS |
100 | #ifdef _MSC_VER |
101 | #include <io.h> | |
102 | #endif | |
103 | ||
f4ada568 GL |
104 | #if defined(__WXMOTIF__) || defined(__WXXT__) |
105 | #include <X11/Intrinsic.h> | |
106 | ||
107 | ///////////////////////////// | |
108 | // Needs internal variables | |
109 | ///////////////////////////// | |
110 | #ifdef __WXXT__ | |
111 | #define Uses_XtIntrinsic | |
112 | #endif | |
113 | ||
114 | #endif | |
115 | ||
116 | #if defined(__WXGTK__) | |
117 | #include <gtk/gtk.h> | |
118 | #endif | |
119 | ||
120 | ///////////////////////////////////////////////////////////////////////////// | |
121 | // wxSocket headers | |
122 | ///////////////////////////////////////////////////////////////////////////// | |
3b4183d8 | 123 | #include "wx/module.h" |
f4ada568 GL |
124 | #define WXSOCK_INTERNAL |
125 | #include "wx/sckaddr.h" | |
126 | #include "wx/socket.h" | |
127 | ||
f4ada568 GL |
128 | ///////////////////////////////////////////////////////////////////////////// |
129 | // Some patch ///// BEGIN | |
130 | ///////////////////////////////////////////////////////////////////////////// | |
131 | #ifdef __WINDOWS__ | |
132 | #define close closesocket | |
133 | #define ioctl ioctlsocket | |
0c32066b JS |
134 | #ifdef errno |
135 | #undef errno | |
136 | #endif | |
f4ada568 GL |
137 | #define errno WSAGetLastError() |
138 | #ifdef EWOULDBLOCK | |
139 | #undef EWOULDBLOCK | |
140 | #endif | |
141 | #define EWOULDBLOCK WSAEWOULDBLOCK | |
142 | #define ETIMEDOUT WSAETIMEDOUT | |
143 | #undef EINTR | |
144 | #define EINTR WSAEINTR | |
145 | #endif | |
146 | ||
147 | #ifndef __WINDOWS__ | |
148 | #define INVALID_SOCKET -1 | |
149 | #endif | |
150 | ||
151 | #ifdef __WXMOTIF__ | |
46ccb510 | 152 | #define wxAPP_CONTEXT ((XtAppContext)wxTheApp->GetAppContext()) |
f4ada568 GL |
153 | #endif |
154 | ||
155 | #ifdef __WINDOWS__ | |
156 | // This is an MS TCP/IP routine and is not needed here. Some WinSock | |
157 | // implementations (such as PC-NFS) will require you to include this | |
158 | // or a similar routine (see appendix in WinSock doc or help file). | |
159 | ||
160 | #if defined( NEED_WSAFDIsSet ) || defined( _MSC_VER ) | |
161 | int PASCAL FAR __WSAFDIsSet(SOCKET fd, fd_set FAR *set) | |
162 | { | |
17dff81c | 163 | int i = set->fd_count; |
f4ada568 | 164 | |
17dff81c SC |
165 | while (i--) |
166 | { | |
167 | if (set->fd_array[i] == fd) | |
168 | return 1; | |
169 | } | |
f4ada568 | 170 | |
17dff81c | 171 | return 0; |
f4ada568 GL |
172 | } |
173 | #endif | |
174 | #endif | |
175 | ||
176 | #if defined(__WINDOWS__) | |
177 | #define PROCESS_EVENTS() wxYield() | |
178 | #elif defined(__WXXT__) || defined(__WXMOTIF__) | |
179 | #define PROCESS_EVENTS() XtAppProcessEvent(wxAPP_CONTEXT, XtIMAll) | |
180 | #elif defined(__WXGTK__) | |
8c14576d | 181 | #define PROCESS_EVENTS() gtk_main_iteration() |
f4ada568 GL |
182 | #endif |
183 | ||
184 | ///////////////////////////////////////////////////////////////////////////// | |
185 | // Some patch ///// END | |
186 | ///////////////////////////////////////////////////////////////////////////// | |
187 | ||
3b4183d8 GL |
188 | // -------------------------------------------------------------- |
189 | // Module | |
190 | // -------------------------------------------------------------- | |
db131261 RR |
191 | class wxSocketModule: public wxModule |
192 | { | |
3b4183d8 GL |
193 | DECLARE_DYNAMIC_CLASS(wxSocketModule) |
194 | public: | |
195 | wxSocketModule() {} | |
196 | bool OnInit(); | |
197 | void OnExit(); | |
198 | }; | |
199 | ||
f4ada568 GL |
200 | // -------------------------------------------------------------- |
201 | // ClassInfos | |
202 | // -------------------------------------------------------------- | |
203 | #if !USE_SHARED_LIBRARY | |
204 | IMPLEMENT_CLASS(wxSocketBase, wxObject) | |
205 | IMPLEMENT_CLASS(wxSocketServer, wxSocketBase) | |
206 | IMPLEMENT_CLASS(wxSocketClient, wxSocketBase) | |
207 | IMPLEMENT_CLASS(wxSocketHandler, wxObject) | |
208 | IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent) | |
3b4183d8 | 209 | IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule) |
f4ada568 GL |
210 | #endif |
211 | ||
db131261 RR |
212 | class wxSockWakeUp : public wxTimer |
213 | { | |
f4ada568 GL |
214 | public: |
215 | int *my_id; | |
216 | int n_val; | |
217 | wxSocketBase *sock; | |
218 | ||
db131261 RR |
219 | wxSockWakeUp(wxSocketBase *_sock, int *id, int new_val) |
220 | { | |
f4ada568 GL |
221 | my_id = id; n_val = new_val; |
222 | sock = _sock; | |
223 | } | |
db131261 RR |
224 | virtual void Notify() |
225 | { | |
f4ada568 GL |
226 | *my_id = n_val; |
227 | if (sock) sock->Notify(FALSE); | |
228 | } | |
229 | }; | |
230 | ||
231 | /// Socket request | |
db131261 RR |
232 | class SockRequest : public wxObject |
233 | { | |
f4ada568 GL |
234 | public: |
235 | char *buffer; | |
236 | size_t size, nbytes; | |
237 | bool done; | |
238 | int error; | |
239 | wxSockWakeUp *auto_wakeup; | |
240 | wxSocketBase::wxRequestNotify type; | |
241 | }; | |
384b4373 | 242 | |
f4ada568 GL |
243 | |
244 | ///////////////////////////////////////////////////////////////////////////// | |
245 | // Some internal define | |
246 | ///////////////////////////////////////////////////////////////////////////// | |
247 | ||
248 | // -------------------------------------------------------------- | |
249 | // --------- wxSocketBase CONSTRUCTOR --------------------------- | |
250 | // -------------------------------------------------------------- | |
251 | wxSocketBase::wxSocketBase(wxSocketBase::wxSockFlags _flags, | |
17dff81c | 252 | wxSocketBase::wxSockType _type) : |
f4ada568 GL |
253 | wxEvtHandler(), |
254 | m_flags(_flags), m_type(_type), m_connected(FALSE), m_connecting(FALSE), | |
255 | m_fd(INVALID_SOCKET), m_waitflags(0), m_cbk(0), m_cdata(0), m_id(-1), | |
256 | m_handler(0), | |
257 | m_neededreq((wxRequestNotify)(REQ_READ | REQ_LOST)), | |
258 | m_cbkon(FALSE), | |
259 | m_unread(NULL), m_unrd_size(0), | |
260 | m_processing(FALSE), | |
8c14576d | 261 | m_timeout(3600), m_wantbuf(0) |
f4ada568 GL |
262 | { |
263 | m_internal = new wxSockInternal; | |
264 | #if defined(__WXXT__) || defined(__WXMOTIF__) || defined(__WXGTK__) | |
265 | m_internal->sock_inputid = 0; | |
266 | m_internal->sock_outputid = 0; | |
267 | m_internal->sock_exceptid = 0; | |
268 | #endif | |
269 | #ifdef __WINDOWS__ | |
270 | m_internal->my_msg = 0; | |
271 | #endif | |
272 | } | |
273 | ||
274 | wxSocketBase::wxSocketBase() : | |
275 | wxEvtHandler(), | |
276 | m_flags(WAITALL), m_type(SOCK_UNINIT), m_connected(FALSE), | |
277 | m_connecting(FALSE), m_fd(INVALID_SOCKET), m_waitflags(0), | |
278 | m_cbk(0), m_cdata(0), | |
279 | m_id(-1), m_handler(0), | |
280 | m_neededreq((wxRequestNotify)(REQ_READ | REQ_LOST)), | |
281 | m_cbkon(FALSE), | |
282 | m_unread(NULL), m_unrd_size(0), | |
283 | m_processing(FALSE), | |
8c14576d | 284 | m_timeout(3600), m_wantbuf(0) |
f4ada568 GL |
285 | { |
286 | m_internal = new wxSockInternal; | |
287 | #if defined(__WXXT__) || defined(__WXMOTIF__) || defined(__WXGTK__) | |
288 | m_internal->sock_inputid = 0; | |
289 | m_internal->sock_outputid = 0; | |
290 | m_internal->sock_exceptid = 0; | |
291 | #endif | |
292 | #ifdef __WINDOWS__ | |
293 | m_internal->my_msg = 0; | |
294 | #endif | |
295 | } | |
296 | ||
297 | // -------------------------------------------------------------- | |
8c14576d | 298 | // wxSocketBase |
f4ada568 | 299 | // -------------------------------------------------------------- |
8c14576d | 300 | |
f4ada568 GL |
301 | wxSocketBase::~wxSocketBase() |
302 | { | |
303 | DestroyCallbacks(); | |
304 | Close(); | |
305 | ||
306 | if (m_unread) | |
307 | free(m_unread); | |
8c14576d RR |
308 | if (m_handler) |
309 | { | |
f4ada568 GL |
310 | #ifdef __WINDOWS__ |
311 | if (m_internal->my_msg) | |
312 | m_handler->DestroyMessage(m_internal->my_msg); | |
313 | #endif | |
314 | m_handler->UnRegister(this); | |
315 | } | |
316 | m_states.DeleteContents(TRUE); | |
317 | ||
318 | delete m_internal; | |
319 | } | |
320 | ||
321 | bool wxSocketBase::Close() | |
322 | { | |
8c14576d RR |
323 | if (m_fd != INVALID_SOCKET) |
324 | { | |
325 | for (int i=0;i<3;i++) | |
326 | { | |
f4ada568 GL |
327 | wxNode *n, *node = req_list[i].First(); |
328 | ||
8c14576d RR |
329 | while (node) |
330 | { | |
f4ada568 GL |
331 | SockRequest *req = (SockRequest *)node->Data(); |
332 | req->done = TRUE; | |
384b4373 | 333 | |
f4ada568 GL |
334 | n = node->Next(); |
335 | delete node; | |
336 | node = n; | |
337 | } | |
338 | } | |
339 | ||
340 | DestroyCallbacks(); | |
341 | shutdown(m_fd, 2); | |
342 | close(m_fd); | |
343 | m_fd = INVALID_SOCKET; | |
344 | m_connected = FALSE; | |
345 | } | |
346 | ||
347 | return TRUE; | |
348 | } | |
349 | ||
350 | // -------------------------------------------------------------- | |
8c14576d | 351 | // wxSocketBase base IO function |
f4ada568 | 352 | // -------------------------------------------------------------- |
8c14576d | 353 | |
f4ada568 GL |
354 | wxSocketBase& wxSocketBase::Read(char* buffer, size_t nbytes) |
355 | { | |
356 | m_lcount = GetPushback(buffer, nbytes, FALSE); | |
357 | ||
358 | // If we have got the whole needed buffer or if we don't want to | |
359 | // wait then it returns immediately. | |
360 | if (!nbytes || (m_lcount && !(m_flags & WAITALL)) ) | |
361 | return *this; | |
362 | ||
363 | WantBuffer(buffer, nbytes, EVT_READ); | |
364 | ||
365 | return *this; | |
366 | } | |
367 | ||
368 | wxSocketBase& wxSocketBase::Peek(char* buffer, size_t nbytes) | |
369 | { | |
370 | size_t nbytes_old = nbytes; | |
371 | ||
372 | nbytes -= GetPushback(buffer, nbytes, TRUE); | |
8c14576d RR |
373 | if (!nbytes) |
374 | { | |
f4ada568 GL |
375 | m_lcount = nbytes_old; |
376 | return *this; | |
377 | } | |
378 | ||
379 | WantBuffer(buffer, nbytes, EVT_PEEK); | |
380 | ||
381 | return *this; | |
382 | } | |
383 | ||
384 | wxSocketBase& wxSocketBase::Write(const char *buffer, size_t nbytes) | |
385 | { | |
386 | WantBuffer((char *)buffer, nbytes, EVT_WRITE); | |
387 | return *this; | |
388 | } | |
389 | ||
390 | wxSocketBase& wxSocketBase::ReadMsg(char* buffer, size_t nbytes) | |
391 | { | |
392 | SockMsg msg; | |
393 | size_t len, len2, sig; | |
384b4373 | 394 | |
f4ada568 GL |
395 | Read((char *)&msg, sizeof(msg)); |
396 | if (m_lcount != sizeof(msg)) | |
397 | return *this; | |
398 | ||
399 | sig = msg.sig[0] & 0xff; | |
400 | sig |= (size_t)(msg.sig[1] & 0xff) << 8; | |
401 | sig |= (size_t)(msg.sig[2] & 0xff) << 16; | |
402 | sig |= (size_t)(msg.sig[3] & 0xff) << 24; | |
403 | ||
404 | if (sig != 0xfeeddead) | |
405 | return *this; | |
406 | len = msg.len[0] & 0xff; | |
407 | len |= (size_t)(msg.len[1] & 0xff) << 8; | |
408 | len |= (size_t)(msg.len[2] & 0xff) << 16; | |
409 | len |= (size_t)(msg.len[3] & 0xff) << 24; | |
410 | len2 = len; | |
411 | if (len > nbytes) | |
412 | len = nbytes; | |
413 | else | |
414 | len2 = 0; | |
415 | ||
416 | if (Read(buffer, len).LastCount() != len) | |
417 | return *this; | |
418 | if (len2 && (Read(NULL, len2).LastCount() != len2)) | |
419 | return *this; | |
420 | if (Read((char *)&msg, sizeof(msg)).LastCount() != sizeof(msg)) | |
421 | return *this; | |
422 | ||
423 | sig = msg.sig[0] & 0xff; | |
424 | sig |= (size_t)(msg.sig[1] & 0xff) << 8; | |
425 | sig |= (size_t)(msg.sig[2] & 0xff) << 16; | |
426 | sig |= (size_t)(msg.sig[3] & 0xff) << 24; | |
427 | // ERROR | |
428 | if (sig != 0xdeadfeed) | |
429 | return *this; | |
430 | ||
431 | return *this; | |
432 | } | |
433 | ||
434 | wxSocketBase& wxSocketBase::WriteMsg(const char *buffer, size_t nbytes) | |
435 | { | |
436 | SockMsg msg; | |
384b4373 | 437 | |
0c32066b JS |
438 | msg.sig[0] = (char) 0xad; |
439 | msg.sig[1] = (char) 0xde; | |
440 | msg.sig[2] = (char) 0xed; | |
441 | msg.sig[3] = (char) 0xfe; | |
f4ada568 | 442 | |
0c32066b JS |
443 | msg.len[0] = (char) nbytes & 0xff; |
444 | msg.len[1] = (char) (nbytes >> 8) & 0xff; | |
445 | msg.len[2] = (char) (nbytes >> 16) & 0xff; | |
446 | msg.len[3] = (char) (nbytes >> 24) & 0xff; | |
f4ada568 GL |
447 | |
448 | if (Write((char *)&msg, sizeof(msg)).LastCount() < sizeof(msg)) | |
449 | return *this; | |
450 | if (Write(buffer, nbytes).LastCount() < nbytes) | |
384b4373 | 451 | return *this; |
f4ada568 | 452 | |
0c32066b JS |
453 | msg.sig[0] = (char) 0xed; |
454 | msg.sig[1] = (char) 0xfe; | |
455 | msg.sig[2] = (char) 0xad; | |
456 | msg.sig[3] = (char) 0xde; | |
457 | msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = (char) 0; | |
f4ada568 GL |
458 | Write((char *)&msg, sizeof(msg)); |
459 | ||
460 | return *this; | |
461 | } | |
462 | ||
463 | wxSocketBase& wxSocketBase::Unread(const char *buffer, size_t nbytes) | |
464 | { | |
465 | CreatePushbackAfter(buffer, nbytes); | |
466 | return *this; | |
467 | } | |
468 | ||
469 | bool wxSocketBase::IsData() const | |
470 | { | |
471 | struct timeval tv; | |
472 | fd_set sock_set; | |
473 | ||
474 | if (m_fd < 0) | |
475 | return FALSE; | |
476 | if (m_unrd_size > 0) | |
477 | return TRUE; | |
478 | ||
479 | tv.tv_sec = 0; | |
480 | tv.tv_usec = 0; | |
481 | FD_ZERO(&sock_set); | |
482 | FD_SET(m_fd, &sock_set); | |
483 | select(FD_SETSIZE, &sock_set, NULL, NULL, &tv); | |
0c32066b | 484 | return (FD_ISSET(m_fd, &sock_set) != 0); |
f4ada568 GL |
485 | } |
486 | ||
487 | // --------------------------------------------------------------------- | |
488 | // --------- wxSocketBase Discard(): deletes all byte in the input queue | |
489 | // --------------------------------------------------------------------- | |
490 | void wxSocketBase::Discard() | |
491 | { | |
492 | #define MAX_BUFSIZE (10*1024) | |
493 | char *my_data = new char[MAX_BUFSIZE]; | |
494 | size_t recv_size = MAX_BUFSIZE; | |
495 | ||
496 | SaveState(); | |
497 | SetFlags((wxSockFlags)(NOWAIT | SPEED)); | |
384b4373 | 498 | |
8c14576d RR |
499 | while (recv_size == MAX_BUFSIZE) |
500 | { | |
f4ada568 GL |
501 | recv_size = Read(my_data, MAX_BUFSIZE).LastCount(); |
502 | } | |
503 | ||
504 | RestoreState(); | |
505 | delete [] my_data; | |
506 | ||
507 | #undef MAX_BUFSIZE | |
508 | } | |
509 | ||
15ed4533 JS |
510 | // If what? Who seems to need unsigned int? |
511 | // BTW uint isn't even defined on wxMSW for VC++ for some reason. Even if it | |
512 | // were, getpeername/getsockname don't take unsigned int*, they take int*. | |
bbe0af5b RR |
513 | // |
514 | // Under glibc 2.0.7, socketbits.h declares socklen_t to be unsigned int | |
515 | // and it uses *socklen_t as the 3rd parameter. Robert. | |
516 | ||
ee4c6942 JS |
517 | // JACS - How can we detect this? |
518 | // Meanwhile, if your compiler complains about socklen_t, | |
519 | // switch lines below. | |
520 | ||
6d5977df KB |
521 | #if wxHAVE_GLIBC2 |
522 | # define wxSOCKET_INT socklen_t | |
15ed4533 | 523 | #else |
6d5977df | 524 | # define wxSOCKET_INT int |
15ed4533 JS |
525 | #endif |
526 | ||
f4ada568 | 527 | // -------------------------------------------------------------- |
8c14576d | 528 | // wxSocketBase socket info functions |
f4ada568 | 529 | // -------------------------------------------------------------- |
8c14576d | 530 | |
f4ada568 GL |
531 | bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const |
532 | { | |
533 | struct sockaddr my_addr; | |
15ed4533 | 534 | wxSOCKET_INT len_addr = sizeof(my_addr); |
f4ada568 GL |
535 | |
536 | if (m_fd < 0) | |
537 | return FALSE; | |
538 | ||
db131261 | 539 | if (getpeername(m_fd, (struct sockaddr *)&my_addr, &len_addr) < 0) |
384b4373 | 540 | return FALSE; |
f4ada568 GL |
541 | |
542 | addr_man.Disassemble(&my_addr, len_addr); | |
543 | return TRUE; | |
544 | } | |
545 | ||
546 | bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const | |
547 | { | |
548 | struct sockaddr my_addr; | |
15ed4533 | 549 | wxSOCKET_INT len_addr = sizeof(my_addr); |
f4ada568 GL |
550 | |
551 | if (m_fd < 0) | |
552 | return FALSE; | |
553 | ||
db131261 | 554 | if (getsockname(m_fd, (struct sockaddr *)&my_addr, &len_addr) < 0) |
62448488 | 555 | |
f4ada568 GL |
556 | return FALSE; |
557 | ||
558 | addr_man.Disassemble(&my_addr, len_addr); | |
559 | return TRUE; | |
560 | } | |
561 | ||
562 | // -------------------------------------------------------------- | |
8c14576d | 563 | // wxSocketBase wait functions |
f4ada568 | 564 | // -------------------------------------------------------------- |
db131261 | 565 | |
f4ada568 GL |
566 | void wxSocketBase::SaveState() |
567 | { | |
568 | wxSockState *state = new wxSockState; | |
569 | ||
570 | state->cbk_on = m_cbkon; | |
571 | state->cbk_set= m_neededreq; | |
572 | state->cbk = m_cbk; | |
573 | state->cdata = m_cdata; | |
574 | state->flags = m_flags; | |
575 | state->notif = m_notifyme; | |
576 | ||
577 | m_states.Append(state); | |
578 | } | |
579 | ||
580 | void wxSocketBase::RestoreState() | |
581 | { | |
582 | wxNode *node; | |
583 | ||
584 | node = m_states.Last(); | |
585 | if (!node) | |
586 | return; | |
587 | ||
588 | wxSockState *state = (wxSockState *)node->Data(); | |
384b4373 | 589 | |
f4ada568 GL |
590 | SetFlags(state->flags); |
591 | m_neededreq = state->cbk_set; | |
592 | m_cbk = state->cbk; | |
593 | m_cdata = state->cdata; | |
594 | m_notifyme = state->notif; | |
595 | if (state->cbk_on) | |
596 | SetupCallbacks(); | |
597 | else | |
598 | DestroyCallbacks(); | |
599 | ||
600 | delete node; | |
601 | delete state; | |
602 | } | |
603 | ||
604 | // -------------------------------------------------------------- | |
605 | // --------- wxSocketBase wait functions ------------------------ | |
606 | // -------------------------------------------------------------- | |
db131261 | 607 | |
f4ada568 GL |
608 | bool wxSocketBase::_Wait(long seconds, long microseconds, int type) |
609 | { | |
610 | if ((!m_connected && !m_connecting) || m_fd < 0) | |
611 | return FALSE; | |
612 | ||
613 | wxSockWakeUp wakeup(this, &m_waitflags, 0); | |
614 | ||
615 | SaveState(); | |
616 | SetNotify((wxRequestNotify)type); | |
617 | SetupCallbacks(); | |
618 | ||
619 | if (seconds != -1) | |
620 | wakeup.Start((int)(seconds*1000 + (microseconds / 1000)), TRUE); | |
384b4373 | 621 | |
f4ada568 GL |
622 | m_waitflags = 0x80 | type; |
623 | while (m_waitflags & 0x80) | |
624 | PROCESS_EVENTS(); | |
625 | ||
626 | RestoreState(); | |
627 | ||
8c14576d RR |
628 | if (m_waitflags & 0x40) |
629 | { | |
f4ada568 GL |
630 | m_waitflags = 0; |
631 | return TRUE; | |
632 | } | |
633 | m_waitflags = 0; | |
634 | ||
635 | return FALSE; | |
636 | } | |
637 | ||
638 | bool wxSocketBase::Wait(long seconds, long microseconds) | |
639 | { | |
640 | return _Wait(seconds, microseconds, REQ_ACCEPT | REQ_CONNECT | | |
641 | REQ_READ | REQ_WRITE | REQ_LOST); | |
642 | } | |
643 | ||
644 | bool wxSocketBase::WaitForRead(long seconds, long microseconds) | |
645 | { | |
646 | return _Wait(seconds, microseconds, REQ_READ | REQ_LOST); | |
647 | } | |
648 | ||
649 | bool wxSocketBase::WaitForWrite(long seconds, long microseconds) | |
650 | { | |
651 | return _Wait(seconds, microseconds, REQ_WRITE); | |
652 | } | |
653 | ||
654 | bool wxSocketBase::WaitForLost(long seconds, long microseconds) | |
655 | { | |
656 | return _Wait(seconds, microseconds, REQ_LOST); | |
657 | } | |
658 | ||
659 | // -------------------------------------------------------------- | |
660 | // --------- wxSocketBase callback management ------------------- | |
661 | // -------------------------------------------------------------- | |
662 | ||
663 | #if defined(__WXMOTIF__) || defined(__WXXT__) || defined(__WXGTK__) | |
664 | #if defined(__WXMOTIF__) || defined(__WXXT__) | |
665 | static void wx_socket_read(XtPointer client, int *fid, | |
17dff81c | 666 | XtInputId *WXUNUSED(id)) |
f4ada568 GL |
667 | #define fd *fid |
668 | #else | |
669 | static void wx_socket_read(gpointer client, gint fd, | |
670 | GdkInputCondition WXUNUSED(cond)) | |
671 | #define fd fd | |
672 | #endif | |
673 | { | |
674 | wxSocketBase *sock = (wxSocketBase *)client; | |
675 | char c; | |
676 | int i; | |
677 | ||
678 | i = recv(fd, &c, 1, MSG_PEEK); | |
679 | ||
8c14576d RR |
680 | if (i == -1 && (sock->NeededReq() & wxSocketBase::REQ_ACCEPT)) |
681 | { | |
f4ada568 GL |
682 | sock->OnRequest(wxSocketBase::EVT_ACCEPT); |
683 | return; | |
684 | } | |
685 | ||
8c14576d RR |
686 | if (i != 0) |
687 | { | |
f4ada568 | 688 | if (!(sock->NeededReq() & wxSocketBase::REQ_READ)) |
8c14576d | 689 | { |
f4ada568 | 690 | return; |
8c14576d | 691 | } |
f4ada568 GL |
692 | |
693 | sock->OnRequest(wxSocketBase::EVT_READ); | |
8c14576d RR |
694 | } |
695 | else | |
696 | { | |
697 | if (!(sock->NeededReq() & wxSocketBase::REQ_LOST)) | |
698 | { | |
f4ada568 GL |
699 | sock->Close(); |
700 | return; | |
701 | } | |
702 | ||
703 | sock->OnRequest(wxSocketBase::EVT_LOST); | |
704 | } | |
705 | } | |
706 | #undef fd | |
707 | ||
708 | #if defined(__WXMOTIF__) || defined(__WXXT__) | |
709 | static void wx_socket_write(XtPointer client, int *WXUNUSED(fid), | |
17dff81c | 710 | XtInputId *WXUNUSED(id)) |
f4ada568 GL |
711 | #else |
712 | static void wx_socket_write(gpointer client, gint WXUNUSED(fd), | |
17dff81c | 713 | GdkInputCondition WXUNUSED(cond)) |
f4ada568 GL |
714 | #endif |
715 | { | |
716 | wxSocketBase *sock = (wxSocketBase *)client; | |
717 | ||
718 | if (!sock->IsConnected()) | |
719 | sock->OnRequest(wxSocketBase::EVT_CONNECT); | |
720 | else | |
721 | sock->OnRequest(wxSocketBase::EVT_WRITE); | |
722 | } | |
723 | #endif | |
724 | ||
725 | #ifdef wx_xview | |
726 | Notify_value wx_sock_read_xview (Notify_client client, int fd) | |
727 | { | |
728 | wxSocketBase *sock = (wxSocketBase *)client; | |
729 | char c; | |
730 | int i; | |
731 | ||
732 | i = recv(fd, &c, 1, MSG_PEEK); | |
733 | ||
8c14576d RR |
734 | if (i == -1 && (sock->NeededReq() & wxSocketBase::REQ_ACCEPT)) |
735 | { | |
f4ada568 GL |
736 | sock->OnRequest(wxSocketBase::EVT_ACCEPT); |
737 | return; | |
738 | } | |
739 | ||
740 | /* Bytes arrived */ | |
8c14576d RR |
741 | if (i != 0) |
742 | { | |
f4ada568 GL |
743 | if (!(sock->NeededReq() & wxSocketBase::REQ_READ)) |
744 | return (Notify_value) FALSE; | |
745 | ||
746 | sock->OnRequest(wxSocketBase::EVT_READ); | |
8c14576d RR |
747 | } |
748 | else | |
749 | { | |
f4ada568 GL |
750 | if (!(sock->NeededReq() & wxSocketBase::REQ_LOST)) |
751 | return; | |
752 | ||
753 | sock->OnRequest(wxSocketBase::EVT_LOST); | |
754 | } | |
755 | ||
756 | return (Notify_value) FALSE; | |
757 | } | |
758 | ||
759 | Notify_value wx_sock_write_xview (Notify_client client, int fd) | |
760 | { | |
761 | wxSocketBase *sock = (wxSocketBase *)client; | |
762 | ||
763 | if (!sock->IsConnected()) | |
764 | sock->OnRequest(wxSocketBase::EVT_CONNECT); | |
765 | else | |
766 | sock->OnRequest(wxSocketBase::EVT_WRITE); | |
767 | ||
768 | return (Notify_value) TRUE; | |
769 | } | |
770 | #endif | |
771 | ||
772 | wxSocketBase::wxRequestNotify wxSocketBase::EventToNotify(wxRequestEvent evt) | |
773 | { | |
db131261 RR |
774 | switch (evt) |
775 | { | |
f4ada568 GL |
776 | case EVT_READ: |
777 | return REQ_READ; | |
778 | case EVT_PEEK: | |
779 | return REQ_PEEK; | |
780 | case EVT_WRITE: | |
781 | return REQ_WRITE; | |
782 | case EVT_LOST: | |
783 | return REQ_LOST; | |
784 | case EVT_ACCEPT: | |
785 | return REQ_ACCEPT; | |
786 | case EVT_CONNECT: | |
787 | return REQ_CONNECT; | |
788 | } | |
789 | return 0; | |
790 | } | |
791 | ||
792 | void wxSocketBase::SetFlags(wxSockFlags _flags) | |
793 | { | |
794 | m_flags = _flags; | |
db131261 RR |
795 | if (_flags & SPEED) |
796 | { | |
f4ada568 GL |
797 | unsigned long flag = 0; |
798 | ioctl(m_fd, FIONBIO, &flag); | |
799 | ||
800 | // SPEED and WAITALL are antagonists. | |
801 | m_flags = (wxSockFlags)(m_flags & ~WAITALL); | |
802 | ||
803 | Notify(FALSE); | |
db131261 RR |
804 | } |
805 | else | |
806 | { | |
f4ada568 GL |
807 | unsigned long flag = 1; |
808 | ioctl(m_fd, FIONBIO, &flag); | |
809 | } | |
810 | } | |
811 | ||
812 | void wxSocketBase::SetNotify(wxRequestNotify flags) | |
813 | { | |
814 | wxRequestNotify old_needed_req = m_neededreq; | |
db131261 RR |
815 | if (flags & REQ_ACCEPT) |
816 | { | |
f4ada568 GL |
817 | /* Check if server */ |
818 | if (!(GetClassInfo()->IsKindOf(CLASSINFO(wxSocketServer)))) | |
819 | flags &= ~REQ_ACCEPT; | |
820 | } | |
821 | m_neededreq = flags; | |
db131261 RR |
822 | |
823 | /* | |
824 | if (m_cbkon && old_needed_req != flags) seems to be wrong, Robert Roebling | |
825 | SetupCallbacks(); | |
826 | */ | |
827 | ||
8c14576d | 828 | if (old_needed_req != flags) |
f4ada568 GL |
829 | SetupCallbacks(); |
830 | } | |
831 | ||
832 | void wxSocketBase::SetupCallbacks() | |
833 | { | |
834 | if (m_fd == INVALID_SOCKET || !m_handler || (m_flags & SPEED)) | |
835 | return; | |
836 | ||
8c14576d RR |
837 | #if defined(__WXMOTIF__) || defined(__WXXT__) |
838 | if (m_neededreq & (REQ_ACCEPT | REQ_READ | REQ_LOST)) | |
839 | { | |
840 | if (m_internal->sock_inputid <= 0) | |
841 | { | |
842 | m_internal->sock_inputid = XtAppAddInput (wxAPP_CONTEXT, m_fd, | |
f4ada568 GL |
843 | (XtPointer *) XtInputReadMask, |
844 | (XtInputCallbackProc) wx_socket_read, | |
b412f9be | 845 | (XtPointer) this); |
8c14576d | 846 | } |
f4ada568 | 847 | } |
8c14576d RR |
848 | else |
849 | { | |
850 | if (m_internal->sock_inputid > 0) | |
851 | { | |
852 | XtRemoveInput(m_internal->sock_inputid); | |
853 | m_internal->sock_inputid = 0; | |
854 | } | |
855 | } | |
856 | ||
857 | if (m_neededreq & (REQ_CONNECT | REQ_WRITE)) | |
858 | { | |
859 | if (m_internal->sock_outputid <= 0) | |
860 | { | |
861 | m_internal->sock_outputid = XtAppAddInput (wxAPP_CONTEXT, m_fd, | |
f4ada568 GL |
862 | (XtPointer *) XtInputWriteMask, |
863 | (XtInputCallbackProc) wx_socket_write, | |
864 | (XtPointer) this); | |
8c14576d RR |
865 | } |
866 | } | |
867 | else | |
868 | { | |
869 | if (m_internal->sock_outputid > 0) | |
870 | { | |
871 | XtRemoveInput(m_internal->sock_outputid); | |
872 | m_internal->sock_outputid = 0; | |
873 | } | |
874 | } | |
f4ada568 | 875 | #endif |
8c14576d RR |
876 | |
877 | ||
878 | #ifdef __WXGTK__ | |
879 | if (m_neededreq & (REQ_ACCEPT | REQ_READ | REQ_LOST)) | |
880 | { | |
881 | if (m_internal->sock_inputid <= 0) | |
882 | { | |
883 | m_internal->sock_inputid = gdk_input_add(m_fd, GDK_INPUT_READ, | |
884 | wx_socket_read, (gpointer)this); | |
885 | } | |
886 | } | |
887 | else | |
888 | { | |
889 | if (m_internal->sock_inputid > 0) | |
890 | { | |
891 | /* | |
892 | gdk_input_remove(m_internal->sock_inputid); | |
893 | m_internal->sock_inputid = 0; | |
894 | */ | |
895 | } | |
896 | } | |
897 | ||
898 | if (m_neededreq & (REQ_CONNECT | REQ_WRITE)) | |
899 | { | |
900 | if (m_internal->sock_outputid <= 0) | |
901 | { | |
902 | m_internal->sock_outputid = gdk_input_add(m_fd, GDK_INPUT_WRITE, | |
903 | wx_socket_write, (gpointer)this); | |
904 | } | |
905 | } | |
906 | else | |
907 | { | |
908 | if (m_internal->sock_outputid > 0) | |
909 | { | |
910 | /* | |
911 | gdk_input_remove(m_internal->sock_outputid); | |
912 | m_internal->sock_outputid = 0; | |
913 | */ | |
914 | } | |
f4ada568 GL |
915 | } |
916 | #endif | |
8c14576d RR |
917 | |
918 | ||
919 | #ifdef __WXMSW__ | |
f4ada568 GL |
920 | WORD mask = 0; |
921 | ||
922 | if (m_neededreq & REQ_READ) | |
923 | mask |= FD_READ; | |
924 | if (m_neededreq & REQ_WRITE) | |
925 | mask |= FD_WRITE; | |
926 | if (m_neededreq & REQ_LOST) | |
927 | mask |= FD_CLOSE; | |
928 | if (m_neededreq & REQ_ACCEPT) | |
929 | mask |= FD_ACCEPT; | |
930 | if (m_neededreq & REQ_CONNECT) | |
931 | mask |= FD_CONNECT; | |
932 | ||
933 | if (!m_internal->my_msg) | |
934 | m_internal->my_msg = m_handler->NewMessage(this); | |
935 | WSAAsyncSelect(m_fd, m_handler->GetHWND(), m_internal->my_msg, mask); | |
17dff81c SC |
936 | #endif |
937 | #ifdef __WXMAC__ | |
938 | short mask = 0; | |
939 | ||
940 | if (m_neededreq & REQ_READ) | |
941 | mask |= FD_READ; | |
942 | if (m_neededreq & REQ_WRITE) | |
943 | mask |= FD_WRITE; | |
944 | if (m_neededreq & REQ_LOST) | |
945 | mask |= FD_CLOSE; | |
946 | if (m_neededreq & REQ_ACCEPT) | |
947 | mask |= FD_ACCEPT; | |
948 | if (m_neededreq & REQ_CONNECT) | |
949 | mask |= FD_CONNECT; | |
950 | ||
951 | GUSISetReference( m_fd ,mask, this ) ; | |
952 | unsigned long flag = 1; | |
953 | ioctl(m_fd, FIONBIO, &flag); | |
f4ada568 GL |
954 | #endif |
955 | m_cbkon = TRUE; | |
956 | m_processing = FALSE; | |
957 | } | |
958 | ||
959 | void wxSocketBase::DestroyCallbacks() | |
960 | { | |
961 | if (!m_cbkon || !m_handler) | |
962 | return; | |
db131261 | 963 | |
f4ada568 GL |
964 | m_cbkon = FALSE; |
965 | m_processing = FALSE; | |
966 | #if defined(__WXMOTIF__) || defined(__WXXT__) | |
967 | if (m_internal->sock_inputid > 0) | |
968 | XtRemoveInput(m_internal->sock_inputid); | |
969 | m_internal->sock_inputid = 0; | |
970 | if (m_internal->sock_outputid > 0) | |
971 | XtRemoveInput(m_internal->sock_outputid); | |
972 | m_internal->sock_outputid = 0; | |
973 | #endif | |
974 | #ifdef __WXGTK__ | |
975 | if (m_internal->sock_inputid > 0) | |
976 | gdk_input_remove(m_internal->sock_inputid); | |
977 | m_internal->sock_inputid = 0; | |
978 | if (m_internal->sock_outputid > 0) | |
979 | gdk_input_remove(m_internal->sock_outputid); | |
980 | m_internal->sock_outputid = 0; | |
981 | #endif | |
982 | #ifdef __WINDOWS__ | |
983 | WSAAsyncSelect(m_fd, m_handler->GetHWND(), 0, 0); | |
984 | #endif | |
17dff81c SC |
985 | #ifdef __WXMAC__ |
986 | GUSISetReference( m_fd , 0 , 0 ) ; | |
987 | int bottom = wxMacNetEventsBottom ; | |
988 | while ( wxMacNetEventsTop != bottom ) | |
989 | { | |
990 | // set all events that reference this socket to nil | |
991 | if ( wxMacNetEventsReferences[bottom] == (void*) this ) | |
992 | wxMacNetEventsReferences[bottom] = NULL ; | |
993 | bottom++ ; | |
994 | if ( bottom == kwxMacNetEventsMax ) | |
995 | bottom = 0 ; | |
996 | } | |
997 | SetFlags( m_flags ) ; | |
998 | #endif | |
f4ada568 GL |
999 | } |
1000 | ||
1001 | void wxSocketBase::Notify(bool notify) | |
1002 | { | |
1003 | if (m_notifyme == notify) | |
1004 | return; | |
1005 | if (notify) | |
1006 | SetupCallbacks(); | |
1007 | else | |
1008 | DestroyCallbacks(); | |
1009 | m_notifyme = notify; | |
1010 | } | |
1011 | ||
1012 | void wxSocketBase::OnRequest(wxRequestEvent req_evt) | |
1013 | { | |
1014 | wxRequestNotify req_notif = EventToNotify(req_evt); | |
1015 | ||
1016 | // Mask the current event | |
8c14576d RR |
1017 | SetNotify(m_neededreq & ~req_notif); |
1018 | ||
f4ada568 GL |
1019 | if (req_evt <= EVT_WRITE && DoRequests(req_evt)) |
1020 | return; | |
1021 | ||
db131261 RR |
1022 | if (m_waitflags & 0xF0) |
1023 | { | |
f4ada568 | 1024 | // Wake up |
db131261 RR |
1025 | if ((m_waitflags & 0x0F) == req_evt) |
1026 | { | |
f4ada568 GL |
1027 | m_waitflags = 0x80; |
1028 | #ifndef __WXGTK__ | |
8c14576d | 1029 | DestroyCallbacks(); |
f4ada568 GL |
1030 | #endif |
1031 | } | |
1032 | return; | |
1033 | } | |
1034 | ||
db131261 RR |
1035 | if (req_evt == EVT_LOST) |
1036 | { | |
f4ada568 GL |
1037 | m_connected = FALSE; |
1038 | Close(); | |
1039 | } | |
1040 | if (m_notifyme) | |
1041 | OldOnNotify(req_evt); | |
1042 | ||
1043 | // Unmask | |
1044 | SetNotify(m_neededreq | req_notif); | |
1045 | } | |
1046 | ||
1047 | wxSocketEvent::wxSocketEvent(int id) | |
1048 | : wxEvent(id) | |
1049 | { | |
1050 | wxEventType type = (wxEventType)wxEVT_SOCKET; | |
1051 | ||
1052 | SetEventType(type); | |
1053 | } | |
1054 | ||
1055 | void wxSocketBase::OldOnNotify(wxRequestEvent evt) | |
1056 | { | |
1057 | wxSocketEvent event(m_id); | |
1058 | ||
1059 | event.SetEventObject(this); | |
1060 | event.m_skevt = evt; | |
1061 | ProcessEvent(event); | |
1062 | ||
1063 | if (m_cbk) | |
1064 | m_cbk(*this, evt, m_cdata); | |
1065 | } | |
1066 | ||
1067 | // -------------------------------------------------------------- | |
1068 | // --------- wxSocketBase functions [Callback, CallbackData] ---- | |
1069 | // -------------------------------------------------------------- | |
db131261 | 1070 | |
f4ada568 GL |
1071 | wxSocketBase::wxSockCbk wxSocketBase::Callback(wxSocketBase::wxSockCbk _cbk) |
1072 | { | |
1073 | wxSockCbk old_cbk = m_cbk; | |
1074 | ||
1075 | m_cbk = _cbk; | |
1076 | return old_cbk; | |
1077 | } | |
1078 | ||
1079 | char *wxSocketBase::CallbackData(char *cdata_) | |
1080 | { | |
1081 | char *old_cdata = m_cdata; | |
1082 | ||
1083 | m_cdata = cdata_; | |
1084 | return old_cdata; | |
1085 | } | |
1086 | ||
1087 | void wxSocketBase::SetEventHandler(wxEvtHandler& h_evt, int id) | |
1088 | { | |
1089 | SetNextHandler(&h_evt); | |
1090 | m_id = id; | |
1091 | } | |
1092 | ||
1093 | // -------------------------------------------------------------- | |
1094 | // --------- wxSocketBase pushback library ---------------------- | |
1095 | // -------------------------------------------------------------- | |
db131261 | 1096 | |
f4ada568 GL |
1097 | void wxSocketBase::CreatePushbackAfter(const char *buffer, size_t size) |
1098 | { | |
1099 | char *curr_pos; | |
1100 | ||
1101 | m_unread = (char *) realloc(m_unread, m_unrd_size+size); | |
1102 | curr_pos = m_unread + m_unrd_size; | |
1103 | ||
1104 | memcpy(curr_pos, buffer, size); | |
1105 | m_unrd_size += size; | |
1106 | } | |
1107 | ||
1108 | void wxSocketBase::CreatePushbackBefore(const char *buffer, size_t size) | |
1109 | { | |
1110 | char *curr_pos, *new_buf; | |
1111 | ||
1112 | new_buf = (char *) malloc(m_unrd_size+size); | |
1113 | curr_pos = new_buf + size; | |
1114 | ||
1115 | memcpy(new_buf, buffer, size); | |
1116 | memcpy(curr_pos, m_unread, m_unrd_size); | |
1117 | ||
1118 | free(m_unread); | |
1119 | m_unread = new_buf; | |
1120 | m_unrd_size += size; | |
1121 | } | |
1122 | ||
1123 | size_t wxSocketBase::GetPushback(char *buffer, size_t size, bool peek) | |
1124 | { | |
1125 | if (!m_unrd_size) | |
1126 | return 0; | |
1127 | ||
1128 | if (size > m_unrd_size) | |
1129 | size = m_unrd_size; | |
1130 | memcpy(buffer, m_unread, size); | |
1131 | ||
1132 | if (!peek) { | |
1133 | m_unrd_size -= size; | |
1134 | if (!m_unrd_size) { | |
1135 | free(m_unread); | |
1136 | m_unread = NULL; | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | return size; | |
1141 | } | |
1142 | ||
1143 | // -------------------------------------------------------------- | |
1144 | // --------- wxSocketBase "multi-thread" core ------------------- | |
1145 | // -------------------------------------------------------------- | |
1146 | ||
1147 | bool wxSocketBase::DoRequests(wxRequestEvent req_flag) | |
1148 | { | |
1149 | wxNode *node = req_list[req_flag].First(); | |
1150 | size_t len; | |
1151 | int ret; | |
1152 | ||
1153 | if (!node) | |
1154 | return FALSE; | |
1155 | ||
1156 | SockRequest *req = (SockRequest *)node->Data(); | |
1157 | ||
1158 | delete node; | |
1159 | ||
db131261 RR |
1160 | switch (req->type) |
1161 | { | |
f4ada568 GL |
1162 | case EVT_READ: |
1163 | case EVT_PEEK: | |
1164 | ret = recv(m_fd, req->buffer, req->size, | |
1165 | (req->type == EVT_PEEK) ? MSG_PEEK : 0); | |
8c14576d RR |
1166 | if (ret < 0) |
1167 | { | |
f4ada568 GL |
1168 | req->error = errno; |
1169 | req->done = TRUE; | |
1170 | break; | |
1171 | } | |
1172 | len = ret; | |
8c14576d RR |
1173 | if ((len < req->size) && (m_flags & WAITALL)) |
1174 | { | |
f4ada568 GL |
1175 | req->size -= len; |
1176 | req->nbytes += len; | |
1177 | req->buffer += len; | |
1178 | req->auto_wakeup->Start(m_timeout*1000, TRUE); | |
1179 | req_list[req_flag].Insert(req); | |
1180 | break; | |
1181 | } | |
1182 | req->done = TRUE; | |
1183 | req->nbytes += len; | |
1184 | #ifndef __WXGTK__ | |
1185 | DestroyCallbacks(); | |
1186 | #endif | |
1187 | break; | |
1188 | case EVT_WRITE: | |
1189 | ret = send(m_fd, req->buffer, req->size, 0); | |
8c14576d RR |
1190 | if (ret < 0) |
1191 | { | |
f4ada568 GL |
1192 | req->error = errno; |
1193 | req->done = TRUE; | |
1194 | break; | |
1195 | } | |
1196 | len = ret; | |
8c14576d RR |
1197 | if ((len < req->size) && (m_flags & WAITALL)) |
1198 | { | |
f4ada568 GL |
1199 | req->size -= len; |
1200 | req->nbytes += len; | |
1201 | req->buffer += len; | |
1202 | req->auto_wakeup->Start(m_timeout*1000, TRUE); | |
1203 | req_list[req_flag].Insert(req); | |
1204 | break; | |
1205 | } | |
1206 | req->done = TRUE; | |
1207 | req->nbytes += len; | |
1208 | #ifndef __WXGTK__ | |
1209 | DestroyCallbacks(); | |
1210 | #endif | |
1211 | break; | |
1212 | default: | |
1213 | return FALSE; | |
1214 | } | |
1215 | return TRUE; | |
1216 | } | |
1217 | ||
1218 | void wxSocketBase::WantSpeedBuffer(char *buffer, size_t nbytes, | |
1219 | wxRequestEvent evt) | |
1220 | { | |
e22036dc | 1221 | int ret = 0; |
f4ada568 | 1222 | |
8c14576d RR |
1223 | switch (evt) |
1224 | { | |
f4ada568 GL |
1225 | case EVT_PEEK: |
1226 | case EVT_READ: | |
c740f496 GL |
1227 | ret = recv(m_fd, buffer, nbytes, |
1228 | (evt == EVT_PEEK) ? MSG_PEEK : 0); | |
f4ada568 GL |
1229 | break; |
1230 | case EVT_WRITE: | |
c740f496 | 1231 | ret = send(m_fd, buffer, nbytes, 0); |
f4ada568 GL |
1232 | break; |
1233 | } | |
8c14576d RR |
1234 | if (ret < 0) |
1235 | { | |
f4ada568 GL |
1236 | m_lcount = 0; |
1237 | m_error = errno; | |
8c14576d RR |
1238 | } |
1239 | else | |
1240 | { | |
f4ada568 | 1241 | m_lcount = ret; |
8ef6a930 GL |
1242 | m_error = 0; |
1243 | } | |
f4ada568 GL |
1244 | } |
1245 | ||
1246 | void wxSocketBase::WantBuffer(char *buffer, size_t nbytes, | |
17dff81c | 1247 | wxRequestEvent evt) |
f4ada568 GL |
1248 | { |
1249 | bool buf_timed_out; | |
1250 | ||
1251 | if (m_fd == INVALID_SOCKET || !m_handler || !m_connected) | |
1252 | return; | |
1253 | ||
8c14576d RR |
1254 | if (m_flags & SPEED) |
1255 | { | |
f4ada568 GL |
1256 | WantSpeedBuffer(buffer, nbytes, evt); |
1257 | return; | |
1258 | } | |
1259 | ||
1260 | SockRequest *buf = new SockRequest; | |
1261 | wxSockWakeUp s_wake(NULL, (int *)&buf_timed_out, (int)TRUE); | |
384b4373 | 1262 | |
f4ada568 GL |
1263 | m_wantbuf++; |
1264 | req_list[evt].Append(buf); | |
1265 | ||
1266 | SaveState(); | |
1267 | SetNotify(REQ_LOST | EventToNotify(evt)); | |
1268 | SetupCallbacks(); | |
1269 | buf->buffer = buffer; | |
1270 | buf->size = nbytes; | |
1271 | buf->done = FALSE; | |
1272 | buf->type = evt; | |
1273 | buf->nbytes = 0; | |
1274 | buf->auto_wakeup = &s_wake; | |
1275 | buf->error = 0; | |
1276 | buf_timed_out = FALSE; | |
1277 | ||
1278 | s_wake.Start(m_timeout*1000, TRUE); | |
db131261 RR |
1279 | if (m_flags & NOWAIT) |
1280 | { | |
f4ada568 | 1281 | DoRequests(evt); |
db131261 RR |
1282 | } |
1283 | else | |
1284 | { | |
f4ada568 GL |
1285 | while (!buf->done && !buf_timed_out) |
1286 | PROCESS_EVENTS(); | |
1287 | } | |
1288 | m_wantbuf--; | |
1289 | m_lcount = buf->nbytes; | |
1290 | if (buf_timed_out) | |
1291 | m_error = ETIMEDOUT; | |
1292 | else | |
1293 | m_error = buf->error; | |
1294 | ||
1295 | delete buf; | |
1296 | RestoreState(); | |
1297 | } | |
1298 | ||
1299 | // -------------------------------------------------------------- | |
8c14576d | 1300 | // wxSocketServer |
f4ada568 GL |
1301 | // -------------------------------------------------------------- |
1302 | ||
f4ada568 | 1303 | wxSocketServer::wxSocketServer(wxSockAddress& addr_man, |
17dff81c | 1304 | wxSockFlags flags) : |
f4ada568 GL |
1305 | wxSocketBase(flags, SOCK_SERVER) |
1306 | { | |
1307 | m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0); | |
1308 | ||
1309 | if (m_fd == INVALID_SOCKET) | |
1310 | return; | |
384b4373 | 1311 | |
f4ada568 GL |
1312 | int flag = 1; |
1313 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(int)); | |
384b4373 | 1314 | |
f4ada568 GL |
1315 | struct sockaddr *myaddr; |
1316 | size_t len; | |
384b4373 | 1317 | |
f4ada568 GL |
1318 | addr_man.Build(myaddr, len); |
1319 | if (bind(m_fd, myaddr, addr_man.SockAddrLen()) < 0) | |
1320 | return; | |
384b4373 | 1321 | |
f4ada568 GL |
1322 | if (listen(m_fd, 5) < 0) { |
1323 | m_fd = INVALID_SOCKET; | |
1324 | return; | |
1325 | } | |
1326 | } | |
1327 | ||
1328 | // -------------------------------------------------------------- | |
8c14576d | 1329 | // wxSocketServer Accept |
f4ada568 | 1330 | // -------------------------------------------------------------- |
8c14576d | 1331 | |
f4ada568 GL |
1332 | bool wxSocketServer::AcceptWith(wxSocketBase& sock) |
1333 | { | |
1334 | int fd2; | |
384b4373 | 1335 | |
f4ada568 GL |
1336 | if ((fd2 = accept(m_fd, 0, 0)) < 0) |
1337 | return FALSE; | |
1338 | ||
1339 | struct linger linger; | |
1340 | linger.l_onoff = 0; | |
1341 | linger.l_linger = 1; | |
384b4373 | 1342 | |
f4ada568 | 1343 | setsockopt(fd2, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger)); |
384b4373 | 1344 | |
f4ada568 GL |
1345 | int flag = 0; |
1346 | setsockopt(fd2, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int)); | |
384b4373 | 1347 | |
8c14576d RR |
1348 | if (!(sock.m_flags & SPEED)) |
1349 | { | |
f4ada568 GL |
1350 | unsigned long flag2 = 1; |
1351 | ioctl(fd2, FIONBIO, &flag2); | |
1352 | } | |
384b4373 | 1353 | |
f4ada568 GL |
1354 | sock.m_type = SOCK_INTERNAL; |
1355 | sock.m_fd = fd2; | |
1356 | sock.m_connected = TRUE; | |
1357 | ||
1358 | return TRUE; | |
1359 | } | |
1360 | ||
1361 | wxSocketBase *wxSocketServer::Accept() | |
1362 | { | |
1363 | wxSocketBase* sock = new wxSocketBase(); | |
1364 | ||
1365 | sock->SetFlags((wxSockFlags)m_flags); | |
1366 | ||
1367 | if (!AcceptWith(*sock)) | |
1368 | return NULL; | |
1369 | ||
1370 | if (m_handler) | |
1371 | m_handler->Register(sock); | |
1372 | ||
1373 | return sock; | |
1374 | } | |
1375 | ||
1376 | // -------------------------------------------------------------- | |
8c14576d | 1377 | // wxSocketServer callbacks |
f4ada568 | 1378 | // -------------------------------------------------------------- |
8c14576d | 1379 | |
f4ada568 GL |
1380 | void wxSocketServer::OnRequest(wxRequestEvent evt) |
1381 | { | |
8c14576d RR |
1382 | if (evt == EVT_ACCEPT) |
1383 | { | |
f4ada568 GL |
1384 | OldOnNotify(EVT_ACCEPT); |
1385 | } | |
1386 | } | |
1387 | ||
1388 | // -------------------------------------------------------------- | |
8c14576d | 1389 | // wxSocketClient |
f4ada568 GL |
1390 | // -------------------------------------------------------------- |
1391 | ||
1392 | // --------- wxSocketClient CONSTRUCTOR ------------------------- | |
1393 | // -------------------------------------------------------------- | |
1394 | wxSocketClient::wxSocketClient(wxSockFlags _flags) : | |
17dff81c | 1395 | wxSocketBase(_flags, SOCK_CLIENT) |
f4ada568 GL |
1396 | { |
1397 | } | |
1398 | ||
1399 | // -------------------------------------------------------------- | |
1400 | // --------- wxSocketClient DESTRUCTOR -------------------------- | |
1401 | // -------------------------------------------------------------- | |
1402 | wxSocketClient::~wxSocketClient() | |
1403 | { | |
1404 | } | |
1405 | ||
1406 | // -------------------------------------------------------------- | |
1407 | // --------- wxSocketClient Connect functions ------------------- | |
1408 | // -------------------------------------------------------------- | |
e22036dc | 1409 | bool wxSocketClient::Connect(wxSockAddress& addr_man, bool WXUNUSED(wait) ) |
f4ada568 GL |
1410 | { |
1411 | struct linger linger; | |
1412 | ||
1413 | if (IsConnected()) | |
1414 | Close(); | |
1415 | ||
1416 | m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0); | |
384b4373 | 1417 | |
f4ada568 GL |
1418 | if (m_fd < 0) |
1419 | return FALSE; | |
384b4373 | 1420 | |
f4ada568 GL |
1421 | m_connected = FALSE; |
1422 | ||
1423 | linger.l_onoff = 1; | |
384b4373 | 1424 | linger.l_linger = 5; |
f4ada568 | 1425 | setsockopt(m_fd, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger)); |
384b4373 | 1426 | |
f4ada568 | 1427 | // Stay in touch with the state of things... |
384b4373 | 1428 | |
f4ada568 GL |
1429 | unsigned long flag = 1; |
1430 | setsockopt(m_fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int)); | |
384b4373 | 1431 | |
f4ada568 GL |
1432 | // Disable the nagle algorithm, which delays sends till the |
1433 | // buffer is full (or a certain time period has passed?)... | |
1434 | ||
1435 | #if defined(__WINDOWS__) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY)) | |
1436 | flag = 1; | |
1437 | setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int)); | |
1438 | #endif | |
384b4373 | 1439 | |
f4ada568 GL |
1440 | struct sockaddr *remote; |
1441 | size_t len; | |
1442 | ||
1443 | addr_man.Build(remote, len); | |
1444 | ||
1445 | if (connect(m_fd, remote, len) != 0) | |
1446 | return FALSE; | |
1447 | ||
8c14576d RR |
1448 | if (!(m_flags & SPEED)) |
1449 | { | |
f4ada568 GL |
1450 | flag = 1; |
1451 | ioctl(m_fd, FIONBIO, &flag); | |
1452 | } | |
384b4373 | 1453 | |
f4ada568 GL |
1454 | Notify(TRUE); |
1455 | ||
1456 | m_connected = TRUE; | |
1457 | return TRUE; | |
1458 | } | |
1459 | ||
75ed1d15 | 1460 | bool wxSocketClient::WaitOnConnect(long seconds, long microseconds) |
f4ada568 | 1461 | { |
75ed1d15 | 1462 | int ret = _Wait(seconds, microseconds, REQ_CONNECT | REQ_LOST); |
384b4373 | 1463 | |
f4ada568 GL |
1464 | if (ret) |
1465 | m_connected = TRUE; | |
384b4373 RD |
1466 | |
1467 | return m_connected; | |
f4ada568 GL |
1468 | } |
1469 | ||
1470 | void wxSocketClient::OnRequest(wxRequestEvent evt) | |
1471 | { | |
8c14576d RR |
1472 | if (evt == EVT_CONNECT) |
1473 | { | |
1474 | if (m_connected) | |
1475 | { | |
1476 | #ifndef __WXGTK__ | |
f4ada568 | 1477 | SetNotify(m_neededreq & ~REQ_CONNECT); |
8c14576d | 1478 | #endif |
f4ada568 GL |
1479 | return; |
1480 | } | |
1481 | m_waitflags = 0x40; | |
384b4373 | 1482 | m_connected = TRUE; |
f4ada568 | 1483 | OldOnNotify(EVT_CONNECT); |
8c14576d | 1484 | #ifndef __WXGTK__ |
f4ada568 | 1485 | DestroyCallbacks(); |
8c14576d | 1486 | #endif |
f4ada568 GL |
1487 | return; |
1488 | } | |
1489 | wxSocketBase::OnRequest(evt); | |
1490 | } | |
1491 | ||
1492 | ///////////////////////////////////////////////////////////////// | |
1493 | // wxSocketHandler /////////////////////////////////////////////// | |
1494 | ///////////////////////////////////////////////////////////////// | |
1495 | ||
1496 | wxSocketHandler *wxSocketHandler::master = NULL; | |
1497 | #if defined(__WINDOWS__) | |
1498 | static int win_initialized = 0; | |
1499 | #endif | |
1500 | ||
1501 | // -------------------------------------------------------------- | |
1502 | // --------- wxSocketHandler CONSTRUCTOR ------------------------ | |
1503 | // -------------------------------------------------------------- | |
17dff81c SC |
1504 | #ifdef __WXMAC__ |
1505 | ||
1506 | extern "C" int updatestatus(int s) ; | |
1507 | ||
1508 | void wxMacSocketOnRequestProc( void *refcon , short event ) | |
1509 | { | |
1510 | if ( refcon ) | |
1511 | { | |
1512 | wxSocketBase *sock = (wxSocketBase *) refcon ; | |
1513 | ||
1514 | wxSocketBase::wxRequestEvent sk_req; | |
1515 | ||
1516 | int canRead ; | |
1517 | int canWrite ; | |
1518 | int exception ; | |
1519 | ||
1520 | switch (event) { | |
1521 | case FD_READ: | |
1522 | sk_req = wxSocketBase::EVT_READ; | |
1523 | sock->OnRequest(sk_req); | |
1524 | break; | |
1525 | case FD_WRITE: | |
1526 | sk_req = wxSocketBase::EVT_WRITE; | |
1527 | sock->OnRequest(sk_req); | |
1528 | break; | |
1529 | case FD_CLOSE: | |
1530 | sk_req = wxSocketBase::EVT_LOST; | |
1531 | sock->OnRequest(sk_req); | |
1532 | break; | |
1533 | case FD_ACCEPT: | |
1534 | sk_req = wxSocketBase::EVT_ACCEPT; | |
1535 | sock->OnRequest(sk_req); | |
1536 | break; | |
1537 | case FD_CONNECT: | |
1538 | sk_req = wxSocketBase::EVT_CONNECT; | |
1539 | sock->OnRequest(sk_req); | |
1540 | break; | |
1541 | case FD_READY : | |
1542 | break ; | |
1543 | } | |
1544 | updatestatus ( sock->m_fd ) ; | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | void wxMacSocketHandlerProc( void *refcon , short event ) | |
1549 | { | |
1550 | wxMacNetEventsReferences[wxMacNetEventsTop] = refcon ; | |
1551 | wxMacNetEventsEvents[wxMacNetEventsTop] = event ; | |
1552 | ||
1553 | // clumsy construct in order to never have a incorrect wxMacNetEventsTop (above limits) | |
1554 | ||
1555 | if ( wxMacNetEventsTop + 1 == kwxMacNetEventsMax ) | |
1556 | wxMacNetEventsTop = 0 ; | |
1557 | else | |
1558 | wxMacNetEventsTop++ ; | |
1559 | } | |
1560 | ||
1561 | #endif | |
f4ada568 GL |
1562 | #ifdef __WINDOWS__ |
1563 | ||
1564 | extern char wxPanelClassName[]; | |
1565 | ||
1566 | LRESULT APIENTRY _EXPORT wxSocketHandlerWndProc(HWND hWnd, UINT message, | |
1567 | WPARAM wParam, LPARAM lParam) | |
1568 | { | |
1cff61de UU |
1569 | if(message==WM_DESTROY) |
1570 | { | |
17dff81c SC |
1571 | ::SetWindowLong(hWnd, GWL_WNDPROC, (LONG) DefWindowProc); |
1572 | return DefWindowProc(hWnd, message, wParam, lParam); | |
1cff61de | 1573 | } |
f4ada568 GL |
1574 | wxSocketHandler *h_sock = (wxSocketHandler *)GetWindowLong(hWnd, GWL_USERDATA); |
1575 | wxNode *node = h_sock->smsg_list->Find(message); | |
1576 | wxSocketBase *sock; | |
1577 | wxSocketBase::wxRequestEvent sk_req; | |
1578 | UINT event = WSAGETSELECTEVENT(lParam); | |
1579 | ||
1580 | if (!node) | |
1581 | return DefWindowProc(hWnd, message, wParam, lParam); | |
1582 | ||
1583 | sock = (wxSocketBase *)node->Data(); | |
1584 | ||
1585 | switch (event) { | |
1586 | case FD_READ: | |
1587 | sk_req = wxSocketBase::EVT_READ; | |
1588 | break; | |
1589 | case FD_WRITE: | |
1590 | sk_req = wxSocketBase::EVT_WRITE; | |
1591 | break; | |
1592 | case FD_CLOSE: | |
1593 | sk_req = wxSocketBase::EVT_LOST; | |
1594 | break; | |
1595 | case FD_ACCEPT: | |
1596 | sk_req = wxSocketBase::EVT_ACCEPT; | |
1597 | break; | |
1598 | case FD_CONNECT: | |
1599 | sk_req = wxSocketBase::EVT_CONNECT; | |
1600 | break; | |
1601 | } | |
1602 | sock->OnRequest(sk_req); | |
1603 | ||
1604 | return (LRESULT)0; | |
1605 | } | |
1606 | ||
1607 | FARPROC wxSocketSubClassProc = NULL; | |
1608 | ||
1609 | #endif | |
1610 | ||
1611 | wxSocketHandler::wxSocketHandler() | |
1612 | { | |
1613 | #if defined(__WINDOWS__) | |
8c14576d RR |
1614 | if (!win_initialized) |
1615 | { | |
f4ada568 GL |
1616 | WSADATA wsaData; |
1617 | ||
1618 | WSAStartup((1 << 8) | 1, &wsaData); | |
1619 | win_initialized = 1; | |
1620 | } | |
1621 | internal = new wxSockHandlerInternal; | |
1622 | internal->sockWin = ::CreateWindow(wxPanelClassName, NULL, 0, | |
17dff81c SC |
1623 | 0, 0, 0, 0, NULL, (HMENU) NULL, |
1624 | wxhInstance, 0); | |
f4ada568 GL |
1625 | |
1626 | // Subclass the window | |
1627 | if (!wxSocketSubClassProc) | |
1628 | wxSocketSubClassProc = MakeProcInstance((FARPROC) wxSocketHandlerWndProc, wxhInstance); | |
1629 | ::SetWindowLong(internal->sockWin, GWL_WNDPROC, (LONG) wxSocketSubClassProc); | |
1630 | ::SetWindowLong(internal->sockWin, GWL_USERDATA, (LONG) this); | |
1631 | ||
1632 | internal->firstAvailableMsg = 5000; | |
1633 | smsg_list = new wxList(wxKEY_INTEGER); | |
1634 | #endif | |
1635 | ||
1636 | socks = new wxList; | |
1637 | ||
1638 | #ifndef __WINDOWS__ | |
1639 | signal(SIGPIPE, SIG_IGN); | |
1640 | #endif | |
1641 | } | |
1642 | ||
1643 | // -------------------------------------------------------------- | |
1644 | // --------- wxSocketHandler DESTRUCTOR ------------------------- | |
1645 | // -------------------------------------------------------------- | |
1646 | wxSocketHandler::~wxSocketHandler() | |
1647 | { | |
1648 | wxNode *next_node, *node = socks->First(); | |
1649 | ||
8c14576d RR |
1650 | while (node) |
1651 | { | |
f4ada568 GL |
1652 | wxSocketBase* sock = (wxSocketBase*)node->Data(); |
1653 | ||
1654 | delete sock; | |
1655 | next_node = node->Next(); | |
1656 | delete node; | |
1657 | node = next_node; | |
1658 | } | |
1659 | ||
1660 | delete socks; | |
1661 | ||
1662 | #ifdef __WINDOWS__ | |
1663 | delete smsg_list; | |
1664 | ||
1665 | ::DestroyWindow(internal->sockWin); | |
1666 | WSACleanup(); | |
1667 | win_initialized = 0; | |
1668 | ||
1669 | delete internal; | |
1670 | #endif | |
1671 | } | |
1672 | ||
1673 | // -------------------------------------------------------------- | |
1674 | // --------- wxSocketHandler registering functions -------------- | |
1675 | // -------------------------------------------------------------- | |
8c14576d | 1676 | |
f4ada568 GL |
1677 | void wxSocketHandler::Register(wxSocketBase* sock) |
1678 | { | |
1679 | wxNode *node; | |
1680 | ||
8c14576d RR |
1681 | for (node = socks->First(); node != NULL; node = node->Next()) |
1682 | { | |
f4ada568 GL |
1683 | wxSocketBase* s = (wxSocketBase*)node->Data(); |
1684 | ||
1685 | if (s == sock) | |
1686 | return; | |
1687 | } | |
1688 | ||
8c14576d RR |
1689 | if (sock) |
1690 | { | |
f4ada568 GL |
1691 | socks->Append(sock); |
1692 | sock->SetHandler(this); | |
1693 | sock->SetupCallbacks(); | |
1694 | } | |
1695 | } | |
1696 | ||
1697 | void wxSocketHandler::UnRegister(wxSocketBase* sock) | |
1698 | { | |
1699 | wxNode *node; | |
1700 | ||
8c14576d RR |
1701 | for (node = socks->First(); node; node = node->Next()) |
1702 | { | |
f4ada568 | 1703 | wxSocketBase* s = (wxSocketBase*)node->Data(); |
384b4373 | 1704 | |
8c14576d RR |
1705 | if (s == sock) |
1706 | { | |
f4ada568 GL |
1707 | delete node; |
1708 | sock->DestroyCallbacks(); | |
1709 | sock->SetHandler(NULL); | |
1710 | return; | |
1711 | } | |
1712 | } | |
1713 | } | |
1714 | ||
1715 | unsigned long wxSocketHandler::Count() const | |
1716 | { | |
1717 | return socks->Number(); | |
1718 | } | |
1719 | ||
1720 | // -------------------------------------------------------------- | |
1721 | // --------- wxSocketHandler "big" wait functions --------------- | |
1722 | // -------------------------------------------------------------- | |
1723 | void handler_cbk(wxSocketBase& sock, | |
17dff81c SC |
1724 | wxSocketBase::wxRequestEvent WXUNUSED(flags), |
1725 | char *cdata) | |
f4ada568 GL |
1726 | { |
1727 | int *a_wait = (int *)cdata; | |
1728 | ||
1729 | (*a_wait)++; | |
1730 | sock.Notify(FALSE); | |
1731 | } | |
1732 | ||
1733 | int wxSocketHandler::Wait(long seconds, long microseconds) | |
1734 | { | |
1735 | int i; | |
1736 | int on_wait; | |
1737 | wxSockWakeUp s_wake(NULL, &on_wait, -2); | |
1738 | wxNode *node; | |
1739 | ||
8c14576d RR |
1740 | for (node = socks->First(), i=0; node; node = node->Next(), i++) |
1741 | { | |
f4ada568 GL |
1742 | wxSocketBase *sock = (wxSocketBase *)node->Data(); |
1743 | ||
1744 | sock->SaveState(); | |
1745 | ||
1746 | sock->SetupCallbacks(); | |
1747 | ||
1748 | sock->Callback(handler_cbk); | |
1749 | sock->CallbackData((char *)&on_wait); | |
1750 | } | |
1751 | on_wait = 0; | |
1752 | if (seconds != -1) | |
1753 | s_wake.Start((seconds*1000) + (microseconds/1000), TRUE); | |
1754 | ||
1755 | while (!on_wait) | |
1756 | PROCESS_EVENTS(); | |
1757 | ||
8c14576d RR |
1758 | for (node = socks->First(), i=0; node; node = node->Next(), i++) |
1759 | { | |
f4ada568 GL |
1760 | wxSocketBase *sock = (wxSocketBase *)node->Data(); |
1761 | ||
1762 | sock->RestoreState(); | |
1763 | } | |
1764 | ||
1765 | if (on_wait == -2) | |
1766 | return 0; | |
1767 | ||
1768 | return on_wait; | |
1769 | } | |
1770 | ||
1771 | void wxSocketHandler::YieldSock() | |
1772 | { | |
1773 | wxNode *node; | |
1774 | ||
8c14576d RR |
1775 | for (node = socks->First(); node; node = node->Next() ) |
1776 | { | |
f4ada568 GL |
1777 | wxSocketBase *sock = (wxSocketBase *)node->Data(); |
1778 | ||
1779 | sock->SaveState(); | |
1780 | ||
1781 | sock->SetFlags(wxSocketBase::SPEED); | |
1782 | if (sock->IsData()) | |
1783 | sock->DoRequests(wxSocketBase::EVT_READ); | |
1784 | sock->DoRequests(wxSocketBase::EVT_WRITE); | |
1785 | ||
1786 | sock->RestoreState(); | |
1787 | } | |
1788 | } | |
1789 | ||
1790 | // -------------------------------------------------------------- | |
1791 | // --------- wxSocketHandler: create and register the socket ---- | |
1792 | // -------------------------------------------------------------- | |
1793 | wxSocketServer *wxSocketHandler::CreateServer(wxSockAddress& addr, | |
17dff81c | 1794 | wxSocketBase::wxSockFlags flags) |
f4ada568 GL |
1795 | { |
1796 | wxSocketServer *serv = new wxSocketServer(addr, flags); | |
1797 | ||
1798 | Register(serv); | |
1799 | return serv; | |
1800 | } | |
1801 | ||
1802 | wxSocketClient *wxSocketHandler::CreateClient(wxSocketBase::wxSockFlags flags) | |
1803 | { | |
1804 | wxSocketClient *client = new wxSocketClient(flags); | |
1805 | ||
1806 | Register(client); | |
1807 | return client; | |
1808 | } | |
1809 | ||
1810 | #ifdef __WINDOWS__ | |
1811 | // -------------------------------------------------------------- | |
1812 | // --------- wxSocketHandler: Windows specific methods ---------- | |
1813 | // -------------------------------------------------------------- | |
8c14576d | 1814 | |
f4ada568 GL |
1815 | UINT wxSocketHandler::NewMessage(wxSocketBase *sock) |
1816 | { | |
1817 | internal->firstAvailableMsg++; | |
1818 | smsg_list->Append(internal->firstAvailableMsg, sock); | |
1819 | return internal->firstAvailableMsg; | |
1820 | } | |
1821 | ||
1822 | void wxSocketHandler::DestroyMessage(UINT msg) | |
1823 | { | |
1824 | wxNode *node = smsg_list->Find(msg); | |
1825 | delete node; | |
1826 | } | |
1827 | ||
1828 | HWND wxSocketHandler::GetHWND() const | |
1829 | { | |
1830 | return internal->sockWin; | |
1831 | } | |
1832 | ||
1833 | #endif | |
7f555861 | 1834 | |
8c14576d RR |
1835 | bool wxSocketModule::OnInit() |
1836 | { | |
3b4183d8 GL |
1837 | wxSocketHandler::master = new wxSocketHandler(); |
1838 | return TRUE; | |
1839 | } | |
1840 | ||
8c14576d RR |
1841 | void wxSocketModule::OnExit() |
1842 | { | |
3b4183d8 GL |
1843 | delete wxSocketHandler::master; |
1844 | wxSocketHandler::master = NULL; | |
1845 | } | |
1846 | ||
17dff81c SC |
1847 | #ifdef __WXMAC__ |
1848 | void wxMacProcessSocketEvents() ; | |
1849 | void wxMacProcessEvents() | |
1850 | { | |
1851 | wxMacProcessSocketEvents() ; | |
1852 | (*GUSISpin)(SP_MISC, 0) ; | |
1853 | } | |
1854 | ||
1855 | void wxMacProcessSocketEvents() | |
1856 | { | |
1857 | while ( wxMacNetEventsTop != wxMacNetEventsBottom ) | |
1858 | { | |
1859 | // consume event at wxMacNetEventsBottom | |
1860 | wxMacSocketOnRequestProc(wxMacNetEventsReferences[wxMacNetEventsBottom] , wxMacNetEventsEvents[wxMacNetEventsBottom] ) ; | |
1861 | wxMacNetEventsBottom++ ; | |
1862 | if ( wxMacNetEventsBottom == kwxMacNetEventsMax ) | |
1863 | wxMacNetEventsBottom = 0 ; | |
1864 | } | |
1865 | } | |
1866 | #endif | |
1867 | ||
7f555861 JS |
1868 | #endif |
1869 | // __WXSTUBS__ |