]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: socket.cpp | |
3 | // Purpose: Socket handler classes | |
4 | // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia | |
5 | // Created: April 1997 | |
6 | // Updated: September 1999 | |
7 | // Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux | |
8 | // (C) 1999, Guillermo Rodriguez Garcia | |
9 | // RCS_ID: $Id$ | |
10 | // License: see wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #ifdef __GNUG__ | |
14 | #pragma implementation "socket.h" | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #if wxUSE_SOCKETS | |
25 | ||
26 | ///////////////////////////////////////////////////////////////////////////// | |
27 | // wxWindows headers | |
28 | ///////////////////////////////////////////////////////////////////////////// | |
29 | ||
30 | #include "wx/defs.h" | |
31 | #include "wx/object.h" | |
32 | #include "wx/string.h" | |
33 | #include "wx/timer.h" | |
34 | #include "wx/utils.h" | |
35 | #include "wx/module.h" | |
36 | #include "wx/log.h" | |
37 | #include "wx/intl.h" | |
38 | ||
39 | #include <stdlib.h> | |
40 | #include <string.h> | |
41 | #include <ctype.h> | |
42 | ||
43 | ///////////////////////////////////////////////////////////////////////////// | |
44 | // wxSocket headers | |
45 | ///////////////////////////////////////////////////////////////////////////// | |
46 | ||
47 | #include "wx/sckaddr.h" | |
48 | #include "wx/socket.h" | |
49 | ||
50 | ||
51 | #define PROCESS_EVENTS() wxYield() | |
52 | ||
53 | ||
54 | // -------------------------------------------------------------- | |
55 | // ClassInfos | |
56 | // -------------------------------------------------------------- | |
57 | ||
58 | IMPLEMENT_CLASS(wxSocketBase, wxObject) | |
59 | IMPLEMENT_CLASS(wxSocketServer, wxSocketBase) | |
60 | IMPLEMENT_CLASS(wxSocketClient, wxSocketBase) | |
61 | IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent) | |
62 | ||
63 | class wxSocketState : public wxObject | |
64 | { | |
65 | public: | |
66 | bool m_notify_state; | |
67 | GSocketEventFlags m_neededreq; | |
68 | wxSockFlags m_flags; | |
69 | wxSocketBase::wxSockCbk m_cbk; | |
70 | char *m_cdata; | |
71 | ||
72 | public: | |
73 | wxSocketState() : wxObject() {} | |
74 | }; | |
75 | ||
76 | // -------------------------------------------------------------- | |
77 | // wxSocketBase ctor and dtor | |
78 | // -------------------------------------------------------------- | |
79 | ||
80 | wxSocketBase::wxSocketBase(wxSockFlags _flags, wxSockType _type) : | |
81 | wxEvtHandler(), | |
82 | m_socket(NULL), m_id(-1), | |
83 | m_flags(_flags), m_type(_type), | |
84 | m_neededreq(0), m_notify_state(FALSE), | |
85 | m_connected(FALSE), m_establishing(FALSE), | |
86 | m_reading(FALSE), m_writing(FALSE), | |
87 | m_error(FALSE), m_lcount(0), m_timeout(600), m_states(), | |
88 | m_unread(NULL), m_unrd_size(0), m_unrd_cur(0), | |
89 | m_cbk(NULL), m_cdata(NULL) | |
90 | { | |
91 | } | |
92 | ||
93 | wxSocketBase::wxSocketBase() : | |
94 | wxEvtHandler(), | |
95 | m_socket(NULL), m_id(-1), | |
96 | m_flags(NONE), m_type(SOCK_UNINIT), | |
97 | m_neededreq(0), m_notify_state(FALSE), | |
98 | m_connected(FALSE), m_establishing(FALSE), | |
99 | m_reading(FALSE), m_writing(FALSE), | |
100 | m_error(FALSE), m_lcount(0), m_timeout(600), m_states(), | |
101 | m_unread(NULL), m_unrd_size(0), m_unrd_cur(0), | |
102 | m_cbk(NULL), m_cdata(NULL) | |
103 | { | |
104 | } | |
105 | ||
106 | wxSocketBase::~wxSocketBase() | |
107 | { | |
108 | if (m_unread) | |
109 | free(m_unread); | |
110 | ||
111 | // Shutdown and close the socket | |
112 | Close(); | |
113 | ||
114 | // Destroy the GSocket object | |
115 | if (m_socket) | |
116 | GSocket_destroy(m_socket); | |
117 | } | |
118 | ||
119 | bool wxSocketBase::Close() | |
120 | { | |
121 | // Interrupt pending waits | |
122 | InterruptAllWaits(); | |
123 | ||
124 | if (m_socket) | |
125 | { | |
126 | // Disable callbacks | |
127 | GSocket_UnsetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
128 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG); | |
129 | ||
130 | // Shutdown the connection | |
131 | GSocket_Shutdown(m_socket); | |
132 | m_connected = FALSE; | |
133 | m_establishing = FALSE; | |
134 | } | |
135 | ||
136 | return TRUE; | |
137 | } | |
138 | ||
139 | // -------------------------------------------------------------- | |
140 | // wxSocketBase basic IO operations | |
141 | // -------------------------------------------------------------- | |
142 | ||
143 | // All IO operations {Read, Write, ReadMsg, WriteMsg, Peek, | |
144 | // Unread, Discard} update m_error and m_lcount. | |
145 | // | |
146 | // TODO: Should Connect, Accept and AcceptWith update m_error? | |
147 | ||
148 | class _wxSocketInternalTimer: public wxTimer | |
149 | { | |
150 | public: | |
151 | int *m_state; | |
152 | unsigned long m_new_val; | |
153 | ||
154 | void Notify() | |
155 | { | |
156 | *m_state = (int)m_new_val; // Change the value | |
157 | } | |
158 | }; | |
159 | ||
160 | wxSocketBase& wxSocketBase::Read(char* buffer, wxUint32 nbytes) | |
161 | { | |
162 | // Mask read events | |
163 | m_reading = TRUE; | |
164 | ||
165 | m_lcount = _Read(buffer, nbytes); | |
166 | ||
167 | // If in WAITALL mode, all bytes should have been read. | |
168 | if (m_flags & WAITALL) | |
169 | m_error = (m_lcount != nbytes); | |
170 | else | |
171 | m_error = (m_lcount == 0); | |
172 | ||
173 | // Trigger another read event if there is still data available. | |
174 | m_reading = FALSE; | |
175 | // TODO: TriggerRead | |
176 | ||
177 | return *this; | |
178 | } | |
179 | ||
180 | wxUint32 wxSocketBase::_Read(char* buffer, wxUint32 nbytes) | |
181 | { | |
182 | int total; | |
183 | int ret = 1; | |
184 | ||
185 | // we try this even if the connection has already been closed. | |
186 | total = GetPushback(buffer, nbytes, FALSE); | |
187 | nbytes -= total; | |
188 | buffer += total; | |
189 | ||
190 | // If the socket is not connected, or we have got the whole | |
191 | // needed buffer, return immedately | |
192 | if (!m_connected || !m_socket || !nbytes) | |
193 | return total; | |
194 | ||
195 | // Possible combinations (they are checked in this order) | |
196 | // wxSOCKET_NOWAIT | |
197 | // wxSOCKET_WAITALL | wxSOCKET_BLOCK | |
198 | // wxSOCKET_WAITALL | |
199 | // wxSOCKET_BLOCK | |
200 | // wxSOCKET_NONE | |
201 | // | |
202 | if (m_flags & wxSOCKET_NOWAIT) | |
203 | { | |
204 | GSocket_SetNonBlocking(m_socket, TRUE); | |
205 | ret = GSocket_Read(m_socket, buffer, nbytes); | |
206 | GSocket_SetNonBlocking(m_socket, FALSE); | |
207 | ||
208 | if (ret > 0) | |
209 | total += ret; | |
210 | } | |
211 | else if (m_flags & wxSOCKET_WAITALL) // wxSOCKET_WAITALL | |
212 | { | |
213 | while (ret > 0 && nbytes > 0) | |
214 | { | |
215 | if (!(m_flags & wxSOCKET_BLOCK) && !(WaitForRead())) | |
216 | break; | |
217 | ||
218 | ret = GSocket_Read(m_socket, buffer, nbytes); | |
219 | ||
220 | if (ret > 0) | |
221 | { | |
222 | total += ret; | |
223 | buffer += ret; | |
224 | nbytes -= ret; | |
225 | } | |
226 | } | |
227 | } | |
228 | else | |
229 | { | |
230 | if ((m_flags & wxSOCKET_BLOCK) || WaitForRead()) | |
231 | { | |
232 | ret = GSocket_Read(m_socket, buffer, nbytes); | |
233 | ||
234 | if (ret > 0) | |
235 | total += ret; | |
236 | } | |
237 | } | |
238 | ||
239 | return total; | |
240 | } | |
241 | ||
242 | wxSocketBase& wxSocketBase::ReadMsg(char* buffer, wxUint32 nbytes) | |
243 | { | |
244 | #define MAX_DISCARD_SIZE (10 * 1024) | |
245 | ||
246 | wxUint32 len, len2, sig, total; | |
247 | bool error; | |
248 | int old_flags; | |
249 | struct | |
250 | { | |
251 | unsigned char sig[4]; | |
252 | unsigned char len[4]; | |
253 | } msg; | |
254 | ||
255 | // Mask read events | |
256 | m_reading = TRUE; | |
257 | ||
258 | total = 0; | |
259 | error = TRUE; | |
260 | old_flags = m_flags; | |
261 | SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL); | |
262 | ||
263 | if (_Read((char *)&msg, sizeof(msg)) != sizeof(msg)) | |
264 | goto exit; | |
265 | ||
266 | sig = (wxUint32)msg.sig[0]; | |
267 | sig |= (wxUint32)(msg.sig[1] << 8); | |
268 | sig |= (wxUint32)(msg.sig[2] << 16); | |
269 | sig |= (wxUint32)(msg.sig[3] << 24); | |
270 | ||
271 | if (sig != 0xfeeddead) | |
272 | { | |
273 | wxLogWarning( _("TCP: invalid signature returned to ReadMsg.")); | |
274 | goto exit; | |
275 | } | |
276 | ||
277 | len = (wxUint32)msg.len[0]; | |
278 | len |= (wxUint32)(msg.len[1] << 8); | |
279 | len |= (wxUint32)(msg.len[2] << 16); | |
280 | len |= (wxUint32)(msg.len[3] << 24); | |
281 | ||
282 | //wxLogMessage("Readmsg: %d %d %d %d -> len == %d", | |
283 | // msg.len[0], msg.len[1], msg.len[2], msg.len[3], len); | |
284 | ||
285 | if (len > nbytes) | |
286 | { | |
287 | len2 = len - nbytes; | |
288 | len = nbytes; | |
289 | } | |
290 | else | |
291 | len2 = 0; | |
292 | ||
293 | // This check is necessary so that we don't attemp to read if | |
294 | // the msg was zero bytes long. | |
295 | if (len) | |
296 | { | |
297 | total = _Read(buffer, len); | |
298 | ||
299 | if (total != len) | |
300 | goto exit; | |
301 | } | |
302 | if (len2) | |
303 | { | |
304 | char *discard_buffer = new char[MAX_DISCARD_SIZE]; | |
305 | long discard_len; | |
306 | ||
307 | // NOTE: discarded bytes don't add to m_lcount. | |
308 | do | |
309 | { | |
310 | discard_len = ((len2 > MAX_DISCARD_SIZE)? MAX_DISCARD_SIZE : len2); | |
311 | discard_len = _Read(discard_buffer, (wxUint32)discard_len); | |
312 | len2 -= (wxUint32)discard_len; | |
313 | } | |
314 | while ((discard_len > 0) && len2); | |
315 | ||
316 | delete [] discard_buffer; | |
317 | ||
318 | if (len2 != 0) | |
319 | goto exit; | |
320 | } | |
321 | if (_Read((char *)&msg, sizeof(msg)) != sizeof(msg)) | |
322 | goto exit; | |
323 | ||
324 | sig = (wxUint32)msg.sig[0]; | |
325 | sig |= (wxUint32)(msg.sig[1] << 8); | |
326 | sig |= (wxUint32)(msg.sig[2] << 16); | |
327 | sig |= (wxUint32)(msg.sig[3] << 24); | |
328 | ||
329 | if (sig != 0xdeadfeed) | |
330 | { | |
331 | //wxLogMessage(wxT("Warning: invalid signature returned to ReadMsg")); | |
332 | goto exit; | |
333 | } | |
334 | ||
335 | // everything was OK | |
336 | error = FALSE; | |
337 | ||
338 | exit: | |
339 | m_error = error; | |
340 | m_lcount = total; | |
341 | m_reading = FALSE; | |
342 | SetFlags(old_flags); | |
343 | ||
344 | // TODO: TriggerRead | |
345 | return *this; | |
346 | ||
347 | #undef MAX_DISCARD_SIZE | |
348 | } | |
349 | ||
350 | wxSocketBase& wxSocketBase::Peek(char* buffer, wxUint32 nbytes) | |
351 | { | |
352 | // Mask read events | |
353 | m_reading = TRUE; | |
354 | ||
355 | m_lcount = _Read(buffer, nbytes); | |
356 | Pushback(buffer, nbytes); | |
357 | ||
358 | // If in wxSOCKET_WAITALL mode, all bytes should have been read. | |
359 | if (m_flags & wxSOCKET_WAITALL) | |
360 | m_error = (m_lcount != nbytes); | |
361 | else | |
362 | m_error = (m_lcount == 0); | |
363 | ||
364 | // Trigger another read event if there is still data available. | |
365 | m_reading = FALSE; | |
366 | ||
367 | // TODO: TriggerRead | |
368 | return *this; | |
369 | } | |
370 | ||
371 | wxSocketBase& wxSocketBase::Write(const char *buffer, wxUint32 nbytes) | |
372 | { | |
373 | // Mask write events | |
374 | m_writing = TRUE; | |
375 | ||
376 | m_lcount = _Write(buffer, nbytes); | |
377 | ||
378 | // If in wxSOCKET_WAITALL mode, all bytes should have been written. | |
379 | if (m_flags & wxSOCKET_WAITALL) | |
380 | m_error = (m_lcount != nbytes); | |
381 | else | |
382 | m_error = (m_lcount == 0); | |
383 | ||
384 | // Trigger another write event if the socket is still writable | |
385 | m_writing = FALSE; | |
386 | ||
387 | // TODO: TriggerWrite | |
388 | return *this; | |
389 | } | |
390 | ||
391 | wxUint32 wxSocketBase::_Write(const char *buffer, wxUint32 nbytes) | |
392 | { | |
393 | wxUint32 total = 0; | |
394 | int ret = 1; | |
395 | ||
396 | if (!m_connected || !m_socket) | |
397 | return 0; | |
398 | ||
399 | // Possible combinations (they are checked in this order) | |
400 | // wxSOCKET_NOWAIT | |
401 | // wxSOCKET_WAITALL | wxSOCKET_BLOCK | |
402 | // wxSOCKET_WAITALL | |
403 | // wxSOCKET_BLOCK | |
404 | // wxSOCKET_NONE | |
405 | // | |
406 | if (m_flags & wxSOCKET_NOWAIT) | |
407 | { | |
408 | GSocket_SetNonBlocking(m_socket, TRUE); | |
409 | ret = GSocket_Write(m_socket, buffer, nbytes); | |
410 | GSocket_SetNonBlocking(m_socket, FALSE); | |
411 | ||
412 | if (ret > 0) | |
413 | total = ret; | |
414 | } | |
415 | else if (m_flags & wxSOCKET_WAITALL) | |
416 | { | |
417 | while (ret > 0 && nbytes > 0) | |
418 | { | |
419 | if (!(m_flags & wxSOCKET_BLOCK) && !(WaitForWrite())) | |
420 | break; | |
421 | ||
422 | ret = GSocket_Write(m_socket, buffer, nbytes); | |
423 | ||
424 | if (ret > 0) | |
425 | { | |
426 | total += ret; | |
427 | buffer += ret; | |
428 | nbytes -= ret; | |
429 | } | |
430 | } | |
431 | } | |
432 | else | |
433 | { | |
434 | if ((m_flags & wxSOCKET_BLOCK) || WaitForWrite()) | |
435 | { | |
436 | ret = GSocket_Write(m_socket, buffer, nbytes); | |
437 | ||
438 | if (ret > 0) | |
439 | total = ret; | |
440 | } | |
441 | } | |
442 | ||
443 | return total; | |
444 | } | |
445 | ||
446 | wxSocketBase& wxSocketBase::WriteMsg(const char *buffer, wxUint32 nbytes) | |
447 | { | |
448 | wxUint32 total; | |
449 | bool error; | |
450 | int old_flags; | |
451 | struct { | |
452 | unsigned char sig[4]; | |
453 | unsigned char len[4]; | |
454 | } msg; | |
455 | ||
456 | // Mask write events | |
457 | m_writing = TRUE; | |
458 | ||
459 | error = TRUE; | |
460 | total = 0; | |
461 | old_flags = m_flags; | |
462 | SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL); | |
463 | ||
464 | // warning about 'cast truncates constant value' | |
465 | #ifdef __VISUALC__ | |
466 | # pragma warning(disable: 4310) | |
467 | #endif // __VISUALC__ | |
468 | ||
469 | msg.sig[0] = (unsigned char) 0xad; | |
470 | msg.sig[1] = (unsigned char) 0xde; | |
471 | msg.sig[2] = (unsigned char) 0xed; | |
472 | msg.sig[3] = (unsigned char) 0xfe; | |
473 | ||
474 | msg.len[0] = (unsigned char) nbytes & 0xff; | |
475 | msg.len[1] = (unsigned char) (nbytes >> 8) & 0xff; | |
476 | msg.len[2] = (unsigned char) (nbytes >> 16) & 0xff; | |
477 | msg.len[3] = (unsigned char) (nbytes >> 24) & 0xff; | |
478 | ||
479 | //wxLogMessage("Writemsg: %d %d %d %d -> %d", | |
480 | // nbytes & 0xff, | |
481 | // (nbytes >> 8) & 0xff, | |
482 | // (nbytes >> 16) & 0xff, | |
483 | // (nbytes >> 24) & 0xff, | |
484 | // nbytes | |
485 | // ); | |
486 | ||
487 | if (_Write((char *)&msg, sizeof(msg)) < sizeof(msg)) | |
488 | goto exit; | |
489 | ||
490 | total = _Write(buffer, nbytes); | |
491 | ||
492 | if (total < nbytes) | |
493 | goto exit; | |
494 | ||
495 | msg.sig[0] = (unsigned char) 0xed; | |
496 | msg.sig[1] = (unsigned char) 0xfe; | |
497 | msg.sig[2] = (unsigned char) 0xad; | |
498 | msg.sig[3] = (unsigned char) 0xde; | |
499 | msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = (char) 0; | |
500 | ||
501 | if ((_Write((char *)&msg, sizeof(msg))) < sizeof(msg)) | |
502 | goto exit; | |
503 | ||
504 | // everything was OK | |
505 | error = FALSE; | |
506 | ||
507 | exit: | |
508 | m_error = error; | |
509 | m_lcount = total; | |
510 | m_writing = FALSE; | |
511 | ||
512 | // TODO: TriggerWrite | |
513 | return *this; | |
514 | ||
515 | #ifdef __VISUALC__ | |
516 | # pragma warning(default: 4310) | |
517 | #endif // __VISUALC__ | |
518 | } | |
519 | ||
520 | wxSocketBase& wxSocketBase::Unread(const char *buffer, wxUint32 nbytes) | |
521 | { | |
522 | if (nbytes != 0) | |
523 | Pushback(buffer, nbytes); | |
524 | ||
525 | m_error = FALSE; | |
526 | m_lcount = nbytes; | |
527 | ||
528 | return *this; | |
529 | } | |
530 | ||
531 | wxSocketBase& wxSocketBase::Discard() | |
532 | { | |
533 | #define MAX_BUFSIZE (10*1024) | |
534 | ||
535 | int old_flags; | |
536 | char *my_data = new char[MAX_BUFSIZE]; | |
537 | wxUint32 recv_size = MAX_BUFSIZE; | |
538 | wxUint32 total = 0; | |
539 | ||
540 | // Mask read events | |
541 | m_reading = TRUE; | |
542 | ||
543 | old_flags = m_flags; | |
544 | SetFlags(wxSOCKET_NOWAIT); | |
545 | ||
546 | while (recv_size == MAX_BUFSIZE) | |
547 | { | |
548 | recv_size = _Read(my_data, MAX_BUFSIZE); | |
549 | total += recv_size; | |
550 | } | |
551 | ||
552 | delete [] my_data; | |
553 | m_lcount = total; | |
554 | m_error = FALSE; | |
555 | ||
556 | // Trigger another read event if there is still data available. | |
557 | m_reading = FALSE; | |
558 | ||
559 | // TODO: TriggerRead | |
560 | return *this; | |
561 | ||
562 | #undef MAX_BUFSIZE | |
563 | } | |
564 | ||
565 | // -------------------------------------------------------------- | |
566 | // wxSocketBase get local or peer addresses | |
567 | // -------------------------------------------------------------- | |
568 | ||
569 | bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const | |
570 | { | |
571 | GAddress *peer; | |
572 | ||
573 | if (!m_socket) | |
574 | return FALSE; | |
575 | ||
576 | peer = GSocket_GetPeer(m_socket); | |
577 | addr_man.SetAddress(peer); | |
578 | GAddress_destroy(peer); | |
579 | ||
580 | return TRUE; | |
581 | } | |
582 | ||
583 | bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const | |
584 | { | |
585 | GAddress *local; | |
586 | ||
587 | if (!m_socket) | |
588 | return FALSE; | |
589 | ||
590 | local = GSocket_GetLocal(m_socket); | |
591 | addr_man.SetAddress(local); | |
592 | GAddress_destroy(local); | |
593 | ||
594 | return TRUE; | |
595 | } | |
596 | ||
597 | // -------------------------------------------------------------- | |
598 | // wxSocketBase save and restore socket state | |
599 | // -------------------------------------------------------------- | |
600 | ||
601 | void wxSocketBase::SaveState() | |
602 | { | |
603 | wxSocketState *state; | |
604 | ||
605 | state = new wxSocketState(); | |
606 | ||
607 | state->m_notify_state = m_notify_state; | |
608 | state->m_neededreq = m_neededreq; | |
609 | state->m_flags = m_flags; | |
610 | state->m_cbk = m_cbk; | |
611 | state->m_cdata = m_cdata; | |
612 | ||
613 | m_states.Append(state); | |
614 | } | |
615 | ||
616 | void wxSocketBase::RestoreState() | |
617 | { | |
618 | wxNode *node; | |
619 | wxSocketState *state; | |
620 | ||
621 | node = m_states.Last(); | |
622 | if (!node) | |
623 | return; | |
624 | ||
625 | state = (wxSocketState *)node->Data(); | |
626 | ||
627 | SetFlags(state->m_flags); | |
628 | m_cbk = state->m_cbk; | |
629 | m_cdata = state->m_cdata; | |
630 | m_neededreq = state->m_neededreq; | |
631 | Notify(state->m_notify_state); | |
632 | ||
633 | delete node; | |
634 | delete state; | |
635 | } | |
636 | ||
637 | ||
638 | // -------------------------------------------------------------- | |
639 | // wxSocketBase Wait functions | |
640 | // -------------------------------------------------------------- | |
641 | ||
642 | // GRG: I have completely rewritten this family of functions | |
643 | // so that they don't depend on event notifications; instead, | |
644 | // they poll the socket, using GSocket_Select(), to check for | |
645 | // the specified combination of event flags, until an event | |
646 | // occurs or until the timeout ellapses. The polling loop | |
647 | // calls PROCESS_EVENTS(), so this won't block the GUI. | |
648 | ||
649 | bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags flags) | |
650 | { | |
651 | GSocketEventFlags result; | |
652 | _wxSocketInternalTimer timer; | |
653 | long timeout; | |
654 | int state = -1; | |
655 | ||
656 | // Set this to TRUE to interrupt ongoing waits | |
657 | m_interrupt = FALSE; | |
658 | ||
659 | // Check for valid socket | |
660 | if (!m_socket) | |
661 | return FALSE; | |
662 | ||
663 | // If it is not a server, it must be connected or establishing connection | |
664 | if ((m_type != SOCK_SERVER) && (!m_connected && !m_establishing)) | |
665 | return FALSE; | |
666 | ||
667 | // Check for valid timeout value | |
668 | if (seconds != -1) | |
669 | timeout = seconds * 1000 + milliseconds; | |
670 | else | |
671 | timeout = m_timeout * 1000; | |
672 | ||
673 | // Activate timer | |
674 | if (timeout) | |
675 | { | |
676 | timer.m_state = &state; | |
677 | timer.m_new_val = 0; | |
678 | timer.Start((int)timeout, TRUE); | |
679 | } | |
680 | ||
681 | // Active polling (without using events) | |
682 | // | |
683 | // NOTE: this duplicates some of the code in OnRequest (lost | |
684 | // connection and connection establishment handling) but this | |
685 | // doesn't hurt. It has to be here because the event might | |
686 | // be a bit delayed, and it has to be in OnRequest as well | |
687 | // because maybe the WaitXXX functions are not being used. | |
688 | // | |
689 | // Do this at least once (important if timeout == 0, when | |
690 | // we are just polling) | |
691 | // | |
692 | while (state == -1) | |
693 | { | |
694 | result = GSocket_Select(m_socket, flags | GSOCK_LOST_FLAG); | |
695 | ||
696 | // Connection lost | |
697 | if (result & GSOCK_LOST_FLAG) | |
698 | { | |
699 | timer.Stop(); | |
700 | Close(); | |
701 | return TRUE; | |
702 | } | |
703 | ||
704 | // Incoming connection (server) or connection established (client) | |
705 | if (result & GSOCK_CONNECTION_FLAG) | |
706 | { | |
707 | timer.Stop(); | |
708 | m_connected = TRUE; | |
709 | m_establishing = FALSE; | |
710 | return TRUE; | |
711 | } | |
712 | ||
713 | if ((result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG)) | |
714 | { | |
715 | timer.Stop(); | |
716 | return TRUE; | |
717 | } | |
718 | ||
719 | // Wait more? | |
720 | if ((timeout == 0) || (m_interrupt)) | |
721 | break; | |
722 | ||
723 | PROCESS_EVENTS(); | |
724 | } | |
725 | ||
726 | timer.Stop(); | |
727 | return FALSE; | |
728 | } | |
729 | ||
730 | bool wxSocketBase::Wait(long seconds, long milliseconds) | |
731 | { | |
732 | return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG | | |
733 | GSOCK_OUTPUT_FLAG | | |
734 | GSOCK_CONNECTION_FLAG | | |
735 | GSOCK_LOST_FLAG); | |
736 | } | |
737 | ||
738 | bool wxSocketBase::WaitForRead(long seconds, long milliseconds) | |
739 | { | |
740 | // Check pushback buffer | |
741 | if (m_unread) | |
742 | return TRUE; | |
743 | ||
744 | return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG); | |
745 | } | |
746 | ||
747 | bool wxSocketBase::WaitForWrite(long seconds, long milliseconds) | |
748 | { | |
749 | return _Wait(seconds, milliseconds, GSOCK_OUTPUT_FLAG); | |
750 | } | |
751 | ||
752 | bool wxSocketBase::WaitForLost(long seconds, long milliseconds) | |
753 | { | |
754 | return _Wait(seconds, milliseconds, GSOCK_LOST_FLAG); | |
755 | } | |
756 | ||
757 | void wxSocketBase::SetTimeout(long seconds) | |
758 | { | |
759 | m_timeout = seconds; | |
760 | ||
761 | if (m_socket) | |
762 | GSocket_SetTimeout(m_socket, m_timeout * 1000); | |
763 | } | |
764 | ||
765 | // -------------------------------------------------------------- | |
766 | // wxSocketBase flags | |
767 | // -------------------------------------------------------------- | |
768 | ||
769 | void wxSocketBase::SetFlags(wxSockFlags _flags) | |
770 | { | |
771 | m_flags = _flags; | |
772 | } | |
773 | ||
774 | // -------------------------------------------------------------- | |
775 | // wxSocketBase callback management | |
776 | // -------------------------------------------------------------- | |
777 | ||
778 | wxSocketBase::wxSockCbk wxSocketBase::Callback(wxSockCbk cbk_) | |
779 | { | |
780 | wxSockCbk old_cbk = cbk_; | |
781 | ||
782 | m_cbk = cbk_; | |
783 | return old_cbk; | |
784 | } | |
785 | ||
786 | char *wxSocketBase::CallbackData(char *data) | |
787 | { | |
788 | char *old_data = m_cdata; | |
789 | ||
790 | m_cdata = data; | |
791 | return old_data; | |
792 | } | |
793 | ||
794 | // -------------------------------------------------------------- | |
795 | // wxSocketBase automatic notifier | |
796 | // -------------------------------------------------------------- | |
797 | ||
798 | // All events (INPUT, OUTPUT, CONNECTION, LOST) are now always | |
799 | // internally watched; but users will only be notified of those | |
800 | // events they are interested in. | |
801 | ||
802 | static void LINKAGEMODE wx_socket_callback(GSocket * WXUNUSED(socket), | |
803 | GSocketEvent event, char *cdata) | |
804 | { | |
805 | wxSocketBase *sckobj = (wxSocketBase *)cdata; | |
806 | ||
807 | sckobj->OnRequest((wxSocketNotify)event); | |
808 | } | |
809 | ||
810 | wxSocketEventFlags wxSocketBase::EventToNotify(wxSocketNotify evt) | |
811 | { | |
812 | switch (evt) | |
813 | { | |
814 | case GSOCK_INPUT: return GSOCK_INPUT_FLAG; | |
815 | case GSOCK_OUTPUT: return GSOCK_OUTPUT_FLAG; | |
816 | case GSOCK_CONNECTION: return GSOCK_CONNECTION_FLAG; | |
817 | case GSOCK_LOST: return GSOCK_LOST_FLAG; | |
818 | } | |
819 | return 0; | |
820 | } | |
821 | ||
822 | void wxSocketBase::SetNotify(wxSocketEventFlags flags) | |
823 | { | |
824 | m_neededreq = flags; | |
825 | } | |
826 | ||
827 | void wxSocketBase::Notify(bool notify) | |
828 | { | |
829 | m_notify_state = notify; | |
830 | } | |
831 | ||
832 | void wxSocketBase::OnRequest(wxSocketNotify req_evt) | |
833 | { | |
834 | wxSocketEvent event(m_id); | |
835 | wxSocketEventFlags flag = EventToNotify(req_evt); | |
836 | ||
837 | // fprintf(stderr, "%s: Entering OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt); | |
838 | ||
839 | // NOTE: this duplicates some of the code in _Wait, (lost | |
840 | // connections and delayed connection establishment) but | |
841 | // this doesn't hurt. It has to be here because maybe the | |
842 | // WaitXXX are not being used, and it has to be in _Wait | |
843 | // as well because the event might be a bit delayed. | |
844 | // | |
845 | switch (req_evt) | |
846 | { | |
847 | case wxSOCKET_CONNECTION : | |
848 | m_establishing = FALSE; | |
849 | m_connected = TRUE; | |
850 | break; | |
851 | case wxSOCKET_LOST: | |
852 | Close(); | |
853 | break; | |
854 | ||
855 | // If we are in the middle of a R/W operation, do not | |
856 | // propagate events to users. | |
857 | // | |
858 | case wxSOCKET_INPUT: | |
859 | if (m_reading) return; | |
860 | ||
861 | case wxSOCKET_OUTPUT: | |
862 | if (m_writing) return; | |
863 | } | |
864 | ||
865 | if (((m_neededreq & flag) == flag) && m_notify_state) | |
866 | { | |
867 | // fprintf(stderr, "%s: Evt %d delivered\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt); | |
868 | event.m_socket = this; | |
869 | event.m_skevt = req_evt; | |
870 | ProcessEvent(event); // XXX - should be PostEvent | |
871 | ||
872 | OldOnNotify(req_evt); | |
873 | if (m_cbk) | |
874 | m_cbk(*this, req_evt, m_cdata); | |
875 | ||
876 | } | |
877 | ||
878 | // fprintf(stderr, "%s: Exiting OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt); | |
879 | } | |
880 | ||
881 | void wxSocketBase::OldOnNotify(wxSocketNotify WXUNUSED(evt)) | |
882 | { | |
883 | } | |
884 | ||
885 | // -------------------------------------------------------------- | |
886 | // wxSocketBase set event handler | |
887 | // -------------------------------------------------------------- | |
888 | ||
889 | void wxSocketBase::SetEventHandler(wxEvtHandler& h_evt, int id) | |
890 | { | |
891 | SetNextHandler(&h_evt); | |
892 | m_id = id; | |
893 | } | |
894 | ||
895 | // -------------------------------------------------------------- | |
896 | // wxSocketBase pushback | |
897 | // -------------------------------------------------------------- | |
898 | ||
899 | void wxSocketBase::Pushback(const char *buffer, wxUint32 size) | |
900 | { | |
901 | if (m_unread == NULL) | |
902 | m_unread = (char *)malloc(size); | |
903 | else { | |
904 | char *tmp; | |
905 | ||
906 | tmp = (char *)malloc(m_unrd_size + size); | |
907 | memcpy(tmp+size, m_unread, m_unrd_size); | |
908 | free(m_unread); | |
909 | ||
910 | m_unread = tmp; | |
911 | } | |
912 | ||
913 | m_unrd_size += size; | |
914 | ||
915 | memcpy(m_unread, buffer, size); | |
916 | } | |
917 | ||
918 | wxUint32 wxSocketBase::GetPushback(char *buffer, wxUint32 size, bool peek) | |
919 | { | |
920 | if (!m_unrd_size) | |
921 | return 0; | |
922 | ||
923 | if (size > (m_unrd_size-m_unrd_cur)) | |
924 | size = m_unrd_size-m_unrd_cur; | |
925 | ||
926 | memcpy(buffer, (m_unread+m_unrd_cur), size); | |
927 | ||
928 | if (!peek) { | |
929 | m_unrd_cur += size; | |
930 | if (m_unrd_size == m_unrd_cur) { | |
931 | free(m_unread); | |
932 | m_unread = NULL; | |
933 | m_unrd_size = 0; | |
934 | m_unrd_cur = 0; | |
935 | } | |
936 | } | |
937 | ||
938 | return size; | |
939 | } | |
940 | ||
941 | // -------------------------------------------------------------- | |
942 | // wxSocketServer | |
943 | // -------------------------------------------------------------- | |
944 | ||
945 | // -------------------------------------------------------------- | |
946 | // wxSocketServer ctor and dtor | |
947 | // -------------------------------------------------------------- | |
948 | ||
949 | wxSocketServer::wxSocketServer(wxSockAddress& addr_man, | |
950 | wxSockFlags flags) : | |
951 | wxSocketBase(flags, SOCK_SERVER) | |
952 | { | |
953 | // Create the socket | |
954 | m_socket = GSocket_new(); | |
955 | ||
956 | if (!m_socket) | |
957 | return; | |
958 | ||
959 | // Setup the socket as server | |
960 | GSocket_SetLocal(m_socket, addr_man.GetAddress()); | |
961 | if (GSocket_SetServer(m_socket) != GSOCK_NOERROR) | |
962 | { | |
963 | GSocket_destroy(m_socket); | |
964 | m_socket = NULL; | |
965 | return; | |
966 | } | |
967 | ||
968 | GSocket_SetTimeout(m_socket, m_timeout * 1000); | |
969 | GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
970 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
971 | wx_socket_callback, (char *)this); | |
972 | ||
973 | } | |
974 | ||
975 | // -------------------------------------------------------------- | |
976 | // wxSocketServer Accept | |
977 | // -------------------------------------------------------------- | |
978 | ||
979 | bool wxSocketServer::AcceptWith(wxSocketBase& sock, bool wait) | |
980 | { | |
981 | GSocket *child_socket; | |
982 | ||
983 | if (!m_socket) | |
984 | return FALSE; | |
985 | ||
986 | // GRG: If wait == FALSE, then the call should be nonblocking. | |
987 | // When we are finished, we put the socket to blocking mode | |
988 | // again. | |
989 | ||
990 | if (!wait) | |
991 | GSocket_SetNonBlocking(m_socket, TRUE); | |
992 | ||
993 | child_socket = GSocket_WaitConnection(m_socket); | |
994 | ||
995 | if (!wait) | |
996 | GSocket_SetNonBlocking(m_socket, FALSE); | |
997 | ||
998 | // GRG: this was not being handled! | |
999 | if (child_socket == NULL) | |
1000 | return FALSE; | |
1001 | ||
1002 | sock.m_type = SOCK_INTERNAL; | |
1003 | sock.m_socket = child_socket; | |
1004 | sock.m_connected = TRUE; | |
1005 | ||
1006 | GSocket_SetTimeout(sock.m_socket, sock.m_timeout * 1000); | |
1007 | GSocket_SetCallback(sock.m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
1008 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1009 | wx_socket_callback, (char *)&sock); | |
1010 | ||
1011 | return TRUE; | |
1012 | } | |
1013 | ||
1014 | wxSocketBase *wxSocketServer::Accept(bool wait) | |
1015 | { | |
1016 | wxSocketBase* sock = new wxSocketBase(); | |
1017 | ||
1018 | sock->SetFlags((wxSockFlags)m_flags); | |
1019 | ||
1020 | if (!AcceptWith(*sock, wait)) | |
1021 | return NULL; | |
1022 | ||
1023 | return sock; | |
1024 | } | |
1025 | ||
1026 | bool wxSocketServer::WaitForAccept(long seconds, long milliseconds) | |
1027 | { | |
1028 | return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG); | |
1029 | } | |
1030 | ||
1031 | // -------------------------------------------------------------- | |
1032 | // wxSocketClient | |
1033 | // -------------------------------------------------------------- | |
1034 | ||
1035 | // -------------------------------------------------------------- | |
1036 | // wxSocketClient ctor and dtor | |
1037 | // -------------------------------------------------------------- | |
1038 | ||
1039 | wxSocketClient::wxSocketClient(wxSockFlags _flags) : | |
1040 | wxSocketBase(_flags, SOCK_CLIENT) | |
1041 | { | |
1042 | } | |
1043 | ||
1044 | wxSocketClient::~wxSocketClient() | |
1045 | { | |
1046 | } | |
1047 | ||
1048 | // -------------------------------------------------------------- | |
1049 | // wxSocketClient Connect functions | |
1050 | // -------------------------------------------------------------- | |
1051 | bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait) | |
1052 | { | |
1053 | GSocketError err; | |
1054 | ||
1055 | if (IsConnected()) | |
1056 | Close(); | |
1057 | ||
1058 | // This should never happen. | |
1059 | if (m_socket) | |
1060 | GSocket_destroy(m_socket); | |
1061 | ||
1062 | // Initialize all socket stuff ... | |
1063 | m_socket = GSocket_new(); | |
1064 | m_connected = FALSE; | |
1065 | m_establishing = FALSE; | |
1066 | ||
1067 | if (!m_socket) | |
1068 | return FALSE; | |
1069 | ||
1070 | GSocket_SetTimeout(m_socket, m_timeout * 1000); | |
1071 | GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
1072 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1073 | wx_socket_callback, (char *)this); | |
1074 | ||
1075 | // GRG: If wait == FALSE, then the call should be nonblocking. | |
1076 | // When we are finished, we put the socket to blocking mode | |
1077 | // again. | |
1078 | ||
1079 | if (!wait) | |
1080 | GSocket_SetNonBlocking(m_socket, TRUE); | |
1081 | ||
1082 | GSocket_SetPeer(m_socket, addr_man.GetAddress()); | |
1083 | err = GSocket_Connect(m_socket, GSOCK_STREAMED); | |
1084 | ||
1085 | if (!wait) | |
1086 | GSocket_SetNonBlocking(m_socket, FALSE); | |
1087 | ||
1088 | if (err != GSOCK_NOERROR) | |
1089 | { | |
1090 | if (err == GSOCK_WOULDBLOCK) | |
1091 | m_establishing = TRUE; | |
1092 | ||
1093 | return FALSE; | |
1094 | } | |
1095 | ||
1096 | m_connected = TRUE; | |
1097 | return TRUE; | |
1098 | } | |
1099 | ||
1100 | bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds) | |
1101 | { | |
1102 | if (m_connected) // Already connected | |
1103 | return TRUE; | |
1104 | ||
1105 | if (!m_establishing || !m_socket) // No connection in progress | |
1106 | return FALSE; | |
1107 | ||
1108 | return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG); | |
1109 | } | |
1110 | ||
1111 | // -------------------------------------------------------------- | |
1112 | // wxSocketEvent | |
1113 | // -------------------------------------------------------------- | |
1114 | ||
1115 | wxSocketEvent::wxSocketEvent(int id) | |
1116 | : wxEvent(id) | |
1117 | { | |
1118 | wxEventType type = (wxEventType)wxEVT_SOCKET; | |
1119 | ||
1120 | SetEventType(type); | |
1121 | } | |
1122 | ||
1123 | void wxSocketEvent::CopyObject(wxObject& obj_d) const | |
1124 | { | |
1125 | wxSocketEvent *event = (wxSocketEvent *)&obj_d; | |
1126 | ||
1127 | wxEvent::CopyObject(obj_d); | |
1128 | ||
1129 | event->m_skevt = m_skevt; | |
1130 | event->m_socket = m_socket; | |
1131 | } | |
1132 | ||
1133 | // -------------------------------------------------------------------------- | |
1134 | // wxSocketModule | |
1135 | // -------------------------------------------------------------------------- | |
1136 | class WXDLLEXPORT wxSocketModule: public wxModule { | |
1137 | DECLARE_DYNAMIC_CLASS(wxSocketModule) | |
1138 | public: | |
1139 | bool OnInit() { | |
1140 | return GSocket_Init(); | |
1141 | } | |
1142 | void OnExit() { | |
1143 | GSocket_Cleanup(); | |
1144 | } | |
1145 | }; | |
1146 | ||
1147 | IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule) | |
1148 | ||
1149 | #endif | |
1150 | // wxUSE_SOCKETS |