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