]>
Commit | Line | Data |
---|---|---|
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: April 1999 | |
8 | // Copyright: (C) 1999 1998, 1997, Guilhem Lavaux | |
9 | // RCS_ID: $Id$ | |
10 | // License: see wxWindows license | |
11 | //////////////////////////////////////////////////////////////////////////////// | |
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "socket.h" | |
14 | #endif | |
15 | ||
16 | #ifdef __MWERKS__ | |
17 | typedef int socklen_t ; | |
18 | #endif | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_SOCKETS | |
28 | ||
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 | ||
38 | // Not enough OS behaviour defined for wxStubs | |
39 | #ifndef __WXSTUBS__ | |
40 | ||
41 | #include <stdlib.h> | |
42 | #include <string.h> | |
43 | #include <ctype.h> | |
44 | ||
45 | ///////////////////////////////////////////////////////////////////////////// | |
46 | // System specific headers | |
47 | ///////////////////////////////////////////////////////////////////////////// | |
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 | ||
55 | #endif | |
56 | ||
57 | #if defined(__WINDOWS__) | |
58 | #include <winsock.h> | |
59 | #endif // __WINDOWS__ | |
60 | ||
61 | #if defined(__UNIX__) | |
62 | ||
63 | #ifdef VMS | |
64 | #include <socket.h> | |
65 | #else | |
66 | #include <sys/socket.h> | |
67 | #endif | |
68 | #include <sys/ioctl.h> | |
69 | ||
70 | #include <sys/time.h> | |
71 | #include <unistd.h> | |
72 | ||
73 | #ifdef sun | |
74 | #include <sys/filio.h> | |
75 | #endif | |
76 | ||
77 | #endif // __UNIX__ | |
78 | ||
79 | #include <signal.h> | |
80 | #include <errno.h> | |
81 | ||
82 | #ifdef __VISUALC__ | |
83 | #include <io.h> | |
84 | #endif | |
85 | ||
86 | ///////////////////////////////////////////////////////////////////////////// | |
87 | // wxSocket headers | |
88 | ///////////////////////////////////////////////////////////////////////////// | |
89 | #include <wx/module.h> | |
90 | #define WXSOCK_INTERNAL | |
91 | #include <wx/sckaddr.h> | |
92 | #include <wx/socket.h> | |
93 | #include <wx/sckint.h> | |
94 | ||
95 | // ---------------------- | |
96 | // Some patch ----- BEGIN | |
97 | // ---------------------- | |
98 | #ifdef __WINDOWS__ | |
99 | #define close closesocket | |
100 | #define ioctl ioctlsocket | |
101 | #ifdef errno | |
102 | #undef errno | |
103 | #endif | |
104 | #define errno WSAGetLastError() | |
105 | #ifdef EWOULDBLOCK | |
106 | #undef EWOULDBLOCK | |
107 | #endif | |
108 | #define EWOULDBLOCK WSAEWOULDBLOCK | |
109 | #define ETIMEDOUT WSAETIMEDOUT | |
110 | #undef EINTR | |
111 | #define EINTR WSAEINTR | |
112 | #endif | |
113 | ||
114 | #ifndef __WINDOWS__ | |
115 | #define INVALID_SOCKET -1 | |
116 | #endif | |
117 | ||
118 | #ifdef __WINDOWS__ | |
119 | // This is an MS TCP/IP routine and is not needed here. Some WinSock | |
120 | // implementations (such as PC-NFS) will require you to include this | |
121 | // or a similar routine (see appendix in WinSock doc or help file). | |
122 | ||
123 | #if defined( NEED_WSAFDIsSet ) || defined( __VISUALC__ ) | |
124 | int PASCAL FAR __WSAFDIsSet(SOCKET fd, fd_set FAR *set) | |
125 | { | |
126 | int i = set->fd_count; | |
127 | ||
128 | while (i--) | |
129 | { | |
130 | if (set->fd_array[i] == fd) | |
131 | return 1; | |
132 | } | |
133 | ||
134 | return 0; | |
135 | } | |
136 | #endif | |
137 | #endif | |
138 | ||
139 | // ------------------- | |
140 | // Some patch ---- END | |
141 | // ------------------- | |
142 | ||
143 | #ifdef GetClassInfo | |
144 | #undef GetClassInfo | |
145 | #endif | |
146 | ||
147 | // -------------------------------------------------------------- | |
148 | // Module | |
149 | // -------------------------------------------------------------- | |
150 | class wxSocketModule: public wxModule | |
151 | { | |
152 | DECLARE_DYNAMIC_CLASS(wxSocketModule) | |
153 | public: | |
154 | wxSocketModule() {} | |
155 | bool OnInit(); | |
156 | void OnExit(); | |
157 | }; | |
158 | ||
159 | // -------------------------------------------------------------- | |
160 | // ClassInfos | |
161 | // -------------------------------------------------------------- | |
162 | #if !USE_SHARED_LIBRARY | |
163 | IMPLEMENT_CLASS(wxSocketBase, wxObject) | |
164 | IMPLEMENT_CLASS(wxSocketServer, wxSocketBase) | |
165 | IMPLEMENT_CLASS(wxSocketClient, wxSocketBase) | |
166 | IMPLEMENT_CLASS(wxSocketHandler, wxObject) | |
167 | IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent) | |
168 | IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule) | |
169 | #endif | |
170 | ||
171 | // -------------------------------------------------------------- | |
172 | // --------- wxSocketBase CONSTRUCTOR --------------------------- | |
173 | // -------------------------------------------------------------- | |
174 | wxSocketBase::wxSocketBase(wxSocketBase::wxSockFlags _flags, | |
175 | wxSocketBase::wxSockType _type) : | |
176 | wxEvtHandler(), | |
177 | m_flags(_flags), m_type(_type), m_connected(FALSE), m_connecting(FALSE), | |
178 | m_fd(INVALID_SOCKET), m_id(-1), | |
179 | m_handler(0), | |
180 | m_neededreq((wxRequestNotify)(REQ_READ | REQ_LOST)), | |
181 | m_timeout(3600), | |
182 | m_unread(NULL), m_unrd_size(0), | |
183 | m_cbk(NULL), m_cdata(NULL), | |
184 | m_notify_state(FALSE) | |
185 | { | |
186 | m_internal = new wxSocketInternal(this); | |
187 | } | |
188 | ||
189 | wxSocketBase::wxSocketBase() : | |
190 | wxEvtHandler(), | |
191 | m_flags(WAITALL), m_type(SOCK_UNINIT), m_connected(FALSE), | |
192 | m_connecting(FALSE), m_fd(INVALID_SOCKET), | |
193 | m_id(-1), m_handler(0), | |
194 | m_neededreq((wxRequestNotify)(REQ_READ | REQ_LOST)), | |
195 | m_timeout(3600), | |
196 | m_unread(NULL), m_unrd_size(0), | |
197 | m_cbk(NULL), m_cdata(NULL), | |
198 | m_notify_state(FALSE) | |
199 | { | |
200 | m_internal = new wxSocketInternal(this); | |
201 | } | |
202 | ||
203 | // -------------------------------------------------------------- | |
204 | // wxSocketBase | |
205 | // -------------------------------------------------------------- | |
206 | ||
207 | wxSocketBase::~wxSocketBase() | |
208 | { | |
209 | // First, close the file descriptor. | |
210 | Close(); | |
211 | ||
212 | m_internal->FinalizeSocket(); | |
213 | ||
214 | if (m_unread) | |
215 | free(m_unread); | |
216 | // Unregister from the handler database. | |
217 | if (m_handler) | |
218 | m_handler->UnRegister(this); | |
219 | ||
220 | // Destroy all saved states. | |
221 | m_states.DeleteContents(TRUE); | |
222 | ||
223 | // Destroy the socket manager. | |
224 | delete m_internal; | |
225 | } | |
226 | ||
227 | bool wxSocketBase::Close() | |
228 | { | |
229 | if (m_fd != INVALID_SOCKET) | |
230 | { | |
231 | // Pause all running socket thread. | |
232 | m_internal->PauseSocket(); | |
233 | ||
234 | // Shutdown the connection. | |
235 | shutdown(m_fd, 2); | |
236 | close(m_fd); | |
237 | m_fd = INVALID_SOCKET; | |
238 | m_connected = FALSE; | |
239 | } | |
240 | ||
241 | return TRUE; | |
242 | } | |
243 | ||
244 | // -------------------------------------------------------------- | |
245 | // wxSocketBase base IO function | |
246 | // -------------------------------------------------------------- | |
247 | ||
248 | wxSocketBase& wxSocketBase::Read(char* buffer, size_t nbytes) | |
249 | { | |
250 | m_lcount = GetPushback(buffer, nbytes, FALSE); | |
251 | nbytes -= m_lcount; | |
252 | buffer += m_lcount; | |
253 | ||
254 | // If we have got the whole needed buffer or if we don't want to | |
255 | // wait then it returns immediately. | |
256 | if (!nbytes || (m_lcount && !(m_flags & WAITALL)) ) { | |
257 | return *this; | |
258 | } | |
259 | ||
260 | WantBuffer(buffer, nbytes, EVT_READ); | |
261 | ||
262 | return *this; | |
263 | } | |
264 | ||
265 | wxSocketBase& wxSocketBase::Peek(char* buffer, size_t nbytes) | |
266 | { | |
267 | m_lcount = GetPushback(buffer, nbytes, TRUE); | |
268 | if (nbytes-m_lcount == 0) | |
269 | { | |
270 | return *this; | |
271 | } | |
272 | buffer += m_lcount; | |
273 | nbytes -= m_lcount; | |
274 | ||
275 | WantBuffer(buffer, nbytes, EVT_PEEK); | |
276 | ||
277 | return *this; | |
278 | } | |
279 | ||
280 | wxSocketBase& wxSocketBase::Write(const char *buffer, size_t nbytes) | |
281 | { | |
282 | m_lcount = 0; | |
283 | WantBuffer((char *)buffer, nbytes, EVT_WRITE); | |
284 | return *this; | |
285 | } | |
286 | ||
287 | wxSocketBase& wxSocketBase::Unread(const char *buffer, size_t nbytes) | |
288 | { | |
289 | CreatePushbackAfter(buffer, nbytes); | |
290 | return *this; | |
291 | } | |
292 | ||
293 | bool wxSocketBase::IsData() const | |
294 | { | |
295 | struct timeval tv; | |
296 | fd_set sock_set; | |
297 | ||
298 | if (m_fd < 0) | |
299 | return FALSE; | |
300 | if (m_unrd_size > 0) | |
301 | return TRUE; | |
302 | ||
303 | m_internal->AcquireFD(); | |
304 | ||
305 | tv.tv_sec = 0; | |
306 | tv.tv_usec = 0; | |
307 | FD_ZERO(&sock_set); | |
308 | FD_SET(m_fd, &sock_set); | |
309 | select(FD_SETSIZE, &sock_set, NULL, NULL, &tv); | |
310 | ||
311 | m_internal->ReleaseFD(); | |
312 | ||
313 | return (FD_ISSET(m_fd, &sock_set) != 0); | |
314 | } | |
315 | ||
316 | // --------------------------------------------------------------------- | |
317 | // --------- wxSocketBase Discard(): deletes all byte in the input queue | |
318 | // --------------------------------------------------------------------- | |
319 | void wxSocketBase::Discard() | |
320 | { | |
321 | #define MAX_BUFSIZE (10*1024) | |
322 | char *my_data = new char[MAX_BUFSIZE]; | |
323 | size_t recv_size = MAX_BUFSIZE; | |
324 | ||
325 | SaveState(); | |
326 | SetFlags((wxSockFlags)(NOWAIT | SPEED)); | |
327 | ||
328 | while (recv_size == MAX_BUFSIZE) | |
329 | { | |
330 | recv_size = Read(my_data, MAX_BUFSIZE).LastCount(); | |
331 | } | |
332 | ||
333 | RestoreState(); | |
334 | delete [] my_data; | |
335 | ||
336 | #undef MAX_BUFSIZE | |
337 | } | |
338 | ||
339 | // If what? Who seems to need unsigned int? | |
340 | // BTW uint isn't even defined on wxMSW for VC++ for some reason. Even if it | |
341 | // were, getpeername/getsockname don't take unsigned int*, they take int*. | |
342 | // | |
343 | // Under glibc 2.0.7, socketbits.h declares socklen_t to be unsigned int | |
344 | // and it uses *socklen_t as the 3rd parameter. Robert. | |
345 | ||
346 | // JACS - How can we detect this? | |
347 | // Meanwhile, if your compiler complains about socklen_t, | |
348 | // switch lines below. | |
349 | ||
350 | #if wxHAVE_GLIBC2 | |
351 | # define wxSOCKET_INT socklen_t | |
352 | #else | |
353 | # define wxSOCKET_INT int | |
354 | #endif | |
355 | ||
356 | // -------------------------------------------------------------- | |
357 | // wxSocketBase socket info functions | |
358 | // -------------------------------------------------------------- | |
359 | ||
360 | bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const | |
361 | { | |
362 | struct sockaddr my_addr; | |
363 | wxSOCKET_INT len_addr = sizeof(my_addr); | |
364 | ||
365 | if (m_fd < 0) | |
366 | return FALSE; | |
367 | ||
368 | m_internal->AcquireFD(); | |
369 | ||
370 | if (getpeername(m_fd, (struct sockaddr *)&my_addr, &len_addr) < 0) { | |
371 | m_internal->ReleaseFD(); | |
372 | return FALSE; | |
373 | } | |
374 | ||
375 | m_internal->ReleaseFD(); | |
376 | addr_man.Disassemble(&my_addr, len_addr); | |
377 | return TRUE; | |
378 | } | |
379 | ||
380 | bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const | |
381 | { | |
382 | struct sockaddr my_addr; | |
383 | wxSOCKET_INT len_addr = sizeof(my_addr); | |
384 | ||
385 | if (m_fd < 0) | |
386 | return FALSE; | |
387 | ||
388 | m_internal->AcquireFD(); | |
389 | ||
390 | if (getsockname(m_fd, (struct sockaddr *)&my_addr, &len_addr) < 0) { | |
391 | m_internal->ReleaseFD(); | |
392 | return FALSE; | |
393 | } | |
394 | m_internal->ReleaseFD(); | |
395 | ||
396 | addr_man.Disassemble(&my_addr, len_addr); | |
397 | return TRUE; | |
398 | } | |
399 | ||
400 | // -------------------------------------------------------------- | |
401 | // wxSocketBase wait functions | |
402 | // -------------------------------------------------------------- | |
403 | ||
404 | void wxSocketBase::SaveState() | |
405 | { | |
406 | SocketState *state = new SocketState; | |
407 | ||
408 | state->notify_state = m_notify_state; | |
409 | state->evt_notify_state = m_neededreq; | |
410 | state->socket_flags = m_flags; | |
411 | state->c_callback = m_cbk; | |
412 | state->c_callback_data = m_cdata; | |
413 | ||
414 | m_states.Append((wxObject *)state); | |
415 | } | |
416 | ||
417 | void wxSocketBase::RestoreState() | |
418 | { | |
419 | wxNode *node; | |
420 | SocketState *state; | |
421 | ||
422 | node = m_states.Last(); | |
423 | if (!node) | |
424 | return; | |
425 | ||
426 | state = (SocketState *)node->Data(); | |
427 | ||
428 | SetFlags(state->socket_flags); | |
429 | m_neededreq = state->evt_notify_state; | |
430 | m_cbk = state->c_callback; | |
431 | m_cdata = state->c_callback_data; | |
432 | Notify(state->notify_state); | |
433 | ||
434 | delete node; | |
435 | delete state; | |
436 | } | |
437 | ||
438 | // -------------------------------------------------------------- | |
439 | // --------- wxSocketBase callback functions -------------------- | |
440 | // -------------------------------------------------------------- | |
441 | ||
442 | wxSocketBase::wxSockCbk wxSocketBase::Callback(wxSockCbk cbk_) | |
443 | { | |
444 | wxSockCbk old_cbk = cbk_; | |
445 | ||
446 | m_cbk = cbk_; | |
447 | return old_cbk; | |
448 | } | |
449 | ||
450 | char *wxSocketBase::CallbackData(char *data) | |
451 | { | |
452 | char *old_data = m_cdata; | |
453 | ||
454 | m_cdata = data; | |
455 | return old_data; | |
456 | } | |
457 | ||
458 | // -------------------------------------------------------------- | |
459 | // --------- wxSocketBase wait functions ------------------------ | |
460 | // -------------------------------------------------------------- | |
461 | ||
462 | bool wxSocketBase::_Wait(long seconds, long milliseconds, int type) | |
463 | { | |
464 | SockRequest *req; | |
465 | ||
466 | if ((!m_connected && !m_connecting) || m_fd < 0) | |
467 | return FALSE; | |
468 | ||
469 | req = new SockRequest; | |
470 | ||
471 | req->type = REQ_WAIT | type; | |
472 | req->timeout = seconds * 1000 + milliseconds; | |
473 | req->done = FALSE; | |
474 | req->buffer = NULL; | |
475 | req->size = 0; | |
476 | req->error = 0; | |
477 | req->wait = TRUE; | |
478 | m_internal->QueueRequest(req, TRUE); | |
479 | ||
480 | return (req->io_nbytes != 0); | |
481 | } | |
482 | ||
483 | bool wxSocketBase::Wait(long seconds, long milliseconds) | |
484 | { | |
485 | return _Wait(seconds, milliseconds, REQ_ACCEPT | REQ_CONNECT | | |
486 | REQ_READ | REQ_WRITE | REQ_LOST); | |
487 | } | |
488 | ||
489 | bool wxSocketBase::WaitForRead(long seconds, long milliseconds) | |
490 | { | |
491 | return _Wait(seconds, milliseconds, REQ_READ | REQ_LOST); | |
492 | } | |
493 | ||
494 | bool wxSocketBase::WaitForWrite(long seconds, long milliseconds) | |
495 | { | |
496 | return _Wait(seconds, milliseconds, REQ_WRITE); | |
497 | } | |
498 | ||
499 | bool wxSocketBase::WaitForLost(long seconds, long milliseconds) | |
500 | { | |
501 | return _Wait(seconds, milliseconds, REQ_LOST); | |
502 | } | |
503 | ||
504 | // -------------------------------------------------------------- | |
505 | // --------- wxSocketBase callback management ------------------- | |
506 | // -------------------------------------------------------------- | |
507 | ||
508 | wxSocketBase::wxRequestNotify wxSocketBase::EventToNotify(wxRequestEvent evt) | |
509 | { | |
510 | switch (evt) | |
511 | { | |
512 | case EVT_READ: | |
513 | return REQ_READ; | |
514 | case EVT_PEEK: | |
515 | return REQ_PEEK; | |
516 | case EVT_WRITE: | |
517 | return REQ_WRITE; | |
518 | case EVT_LOST: | |
519 | return REQ_LOST; | |
520 | case EVT_ACCEPT: | |
521 | return REQ_ACCEPT; | |
522 | case EVT_CONNECT: | |
523 | return REQ_CONNECT; | |
524 | } | |
525 | return 0; | |
526 | } | |
527 | ||
528 | void wxSocketBase::SetFlags(wxSockFlags _flags) | |
529 | { | |
530 | m_flags = _flags; | |
531 | if (_flags & SPEED) { | |
532 | // SPEED and WAITALL are antagonists. | |
533 | m_flags = (wxSockFlags)(m_flags & ~WAITALL); | |
534 | } | |
535 | } | |
536 | ||
537 | wxSocketBase::wxSockFlags wxSocketBase::GetFlags() const | |
538 | { | |
539 | return m_flags; | |
540 | } | |
541 | ||
542 | void wxSocketBase::SetNotify(wxRequestNotify flags) | |
543 | { | |
544 | /* Check if server */ | |
545 | if (m_type != SOCK_SERVER) | |
546 | flags &= ~REQ_ACCEPT; | |
547 | ||
548 | m_neededreq = flags; | |
549 | if (m_neededreq == 0) | |
550 | m_internal->DisableWaiter(); | |
551 | else | |
552 | Notify(m_notify_state); | |
553 | } | |
554 | ||
555 | void wxSocketBase::Notify(bool notify) | |
556 | { | |
557 | if (notify) | |
558 | m_internal->EnableWaiter(); | |
559 | else | |
560 | m_internal->DisableWaiter(); | |
561 | m_notify_state = notify; | |
562 | } | |
563 | ||
564 | void wxSocketBase::OnRequest(wxRequestEvent req_evt) | |
565 | { | |
566 | wxSocketEvent event(m_id); | |
567 | wxRequestNotify notify = EventToNotify(req_evt); | |
568 | ||
569 | if ((m_neededreq & notify) == notify) { | |
570 | event.m_socket = this; | |
571 | event.m_skevt = req_evt; | |
572 | ProcessEvent(event); | |
573 | // TODOTODO | |
574 | // OldOnNotify(req_evt); | |
575 | ||
576 | // We disable the event reporting. | |
577 | SetNotify(m_neededreq & ~notify); | |
578 | } | |
579 | } | |
580 | ||
581 | wxSocketEvent::wxSocketEvent(int id) | |
582 | : wxEvent(id) | |
583 | { | |
584 | wxEventType type = (wxEventType)wxEVT_SOCKET; | |
585 | ||
586 | SetEventType(type); | |
587 | } | |
588 | ||
589 | wxObject *wxSocketEvent::Clone() const | |
590 | { | |
591 | wxSocketEvent *event = (wxSocketEvent *)wxEvent::Clone(); | |
592 | ||
593 | event->m_skevt = m_skevt; | |
594 | event->m_socket = m_socket; | |
595 | ||
596 | return event; | |
597 | } | |
598 | ||
599 | void wxSocketBase::OldOnNotify(wxRequestEvent evt) | |
600 | { | |
601 | } | |
602 | ||
603 | // -------------------------------------------------------------- | |
604 | // --------- wxSocketBase functions [Callback, CallbackData] ---- | |
605 | // -------------------------------------------------------------- | |
606 | ||
607 | void wxSocketBase::SetEventHandler(wxEvtHandler& h_evt, int id) | |
608 | { | |
609 | SetNextHandler(&h_evt); | |
610 | m_id = id; | |
611 | } | |
612 | ||
613 | // -------------------------------------------------------------- | |
614 | // --------- wxSocketBase pushback library ---------------------- | |
615 | // -------------------------------------------------------------- | |
616 | ||
617 | void wxSocketBase::CreatePushbackAfter(const char *buffer, size_t size) | |
618 | { | |
619 | char *curr_pos; | |
620 | ||
621 | if (m_unread != NULL) | |
622 | m_unread = (char *) realloc(m_unread, m_unrd_size+size); | |
623 | else | |
624 | m_unread = (char *) malloc(size); | |
625 | curr_pos = m_unread + m_unrd_size; | |
626 | ||
627 | memcpy(curr_pos, buffer, size); | |
628 | m_unrd_size += size; | |
629 | } | |
630 | ||
631 | void wxSocketBase::CreatePushbackBefore(const char *buffer, size_t size) | |
632 | { | |
633 | char *curr_pos, *new_buf; | |
634 | ||
635 | new_buf = (char *) malloc(m_unrd_size+size); | |
636 | curr_pos = new_buf + size; | |
637 | ||
638 | memcpy(new_buf, buffer, size); | |
639 | if (m_unrd_size != 0) { | |
640 | memcpy(curr_pos, m_unread, m_unrd_size); | |
641 | free(m_unread); | |
642 | } | |
643 | m_unread = new_buf; | |
644 | m_unrd_size += size; | |
645 | } | |
646 | ||
647 | size_t wxSocketBase::GetPushback(char *buffer, size_t size, bool peek) | |
648 | { | |
649 | if (!m_unrd_size) | |
650 | return 0; | |
651 | ||
652 | if (size > m_unrd_size) | |
653 | size = m_unrd_size; | |
654 | memcpy(buffer, m_unread, size); | |
655 | ||
656 | if (!peek) { | |
657 | m_unrd_size -= size; | |
658 | if (m_unrd_size == 0) { | |
659 | free(m_unread); | |
660 | m_unread = NULL; | |
661 | } | |
662 | } | |
663 | ||
664 | return size; | |
665 | } | |
666 | ||
667 | // -------------------------------------------------------------- | |
668 | // --------- wxSocketBase buffer core requester ----------------- | |
669 | // -------------------------------------------------------------- | |
670 | ||
671 | void wxSocketBase::WantBuffer(char *buffer, size_t nbytes, | |
672 | wxRequestEvent evt) | |
673 | { | |
674 | bool buf_timed_out; | |
675 | ||
676 | if (m_fd == INVALID_SOCKET || !m_handler || !m_connected) | |
677 | return; | |
678 | ||
679 | SockRequest *buf = new SockRequest; | |
680 | ||
681 | SaveState(); | |
682 | m_internal->DisableWaiter(); | |
683 | buf->buffer = buffer; | |
684 | buf->size = nbytes; | |
685 | buf->done = FALSE; | |
686 | buf->type = EventToNotify(evt); | |
687 | buf->io_nbytes = 0; | |
688 | buf->error = 0; | |
689 | buf->wait = TRUE; | |
690 | buf->timeout = 1000; | |
691 | buf_timed_out = FALSE; | |
692 | ||
693 | if (m_flags & SPEED) | |
694 | m_internal->QueueRequest(buf, FALSE); | |
695 | else | |
696 | if (m_flags & NOWAIT) | |
697 | m_internal->QueueRequest(buf, TRUE); | |
698 | else | |
699 | m_internal->QueueRequest(buf, TRUE); | |
700 | m_lcount += buf->io_nbytes; | |
701 | if (buf_timed_out) | |
702 | m_error = ETIMEDOUT; | |
703 | else | |
704 | m_error = buf->error; | |
705 | ||
706 | delete buf; | |
707 | RestoreState(); | |
708 | } | |
709 | ||
710 | // -------------------------------------------------------------- | |
711 | // wxSocketServer | |
712 | // -------------------------------------------------------------- | |
713 | ||
714 | wxSocketServer::wxSocketServer(wxSockAddress& addr_man, | |
715 | wxSockFlags flags) : | |
716 | wxSocketBase(flags, SOCK_SERVER) | |
717 | { | |
718 | m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0); | |
719 | ||
720 | if (m_fd == INVALID_SOCKET) | |
721 | return; | |
722 | ||
723 | int flag = 1; | |
724 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(int)); | |
725 | ||
726 | struct sockaddr *myaddr; | |
727 | size_t len; | |
728 | ||
729 | addr_man.Build(myaddr, len); | |
730 | if (bind(m_fd, myaddr, addr_man.SockAddrLen()) < 0) | |
731 | return; | |
732 | ||
733 | if (listen(m_fd, 5) < 0) { | |
734 | m_fd = INVALID_SOCKET; | |
735 | return; | |
736 | } | |
737 | ||
738 | m_internal->InitializeSocket(); | |
739 | } | |
740 | ||
741 | // -------------------------------------------------------------- | |
742 | // wxSocketServer Accept | |
743 | // -------------------------------------------------------------- | |
744 | ||
745 | bool wxSocketServer::AcceptWith(wxSocketBase& sock) | |
746 | { | |
747 | int fd2; | |
748 | ||
749 | if ((fd2 = accept(m_fd, 0, 0)) < 0) | |
750 | return FALSE; | |
751 | ||
752 | struct linger linger; | |
753 | linger.l_onoff = 0; | |
754 | linger.l_linger = 1; | |
755 | ||
756 | setsockopt(fd2, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger)); | |
757 | ||
758 | int flag = 0; | |
759 | setsockopt(fd2, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int)); | |
760 | ||
761 | sock.m_type = SOCK_INTERNAL; | |
762 | sock.m_fd = fd2; | |
763 | sock.m_connected = TRUE; | |
764 | ||
765 | sock.m_internal->InitializeSocket(); | |
766 | ||
767 | return TRUE; | |
768 | } | |
769 | ||
770 | wxSocketBase *wxSocketServer::Accept() | |
771 | { | |
772 | wxSocketBase* sock = new wxSocketBase(); | |
773 | ||
774 | sock->SetFlags((wxSockFlags)m_flags); | |
775 | ||
776 | if (!AcceptWith(*sock)) | |
777 | return NULL; | |
778 | ||
779 | if (m_handler) | |
780 | m_handler->Register(sock); | |
781 | ||
782 | return sock; | |
783 | } | |
784 | ||
785 | // -------------------------------------------------------------- | |
786 | // wxSocketClient | |
787 | // -------------------------------------------------------------- | |
788 | ||
789 | // --------- wxSocketClient CONSTRUCTOR ------------------------- | |
790 | // -------------------------------------------------------------- | |
791 | wxSocketClient::wxSocketClient(wxSockFlags _flags) : | |
792 | wxSocketBase(_flags, SOCK_CLIENT) | |
793 | { | |
794 | } | |
795 | ||
796 | // -------------------------------------------------------------- | |
797 | // --------- wxSocketClient DESTRUCTOR -------------------------- | |
798 | // -------------------------------------------------------------- | |
799 | wxSocketClient::~wxSocketClient() | |
800 | { | |
801 | } | |
802 | ||
803 | // -------------------------------------------------------------- | |
804 | // --------- wxSocketClient Connect functions ------------------- | |
805 | // -------------------------------------------------------------- | |
806 | bool wxSocketClient::Connect(wxSockAddress& addr_man, bool WXUNUSED(wait) ) | |
807 | { | |
808 | struct linger linger; | |
809 | ||
810 | if (IsConnected()) | |
811 | Close(); | |
812 | ||
813 | // Initializes all socket stuff ... | |
814 | // -------------------------------- | |
815 | m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0); | |
816 | ||
817 | if (m_fd < 0) | |
818 | return FALSE; | |
819 | ||
820 | m_connected = FALSE; | |
821 | ||
822 | linger.l_onoff = 1; | |
823 | linger.l_linger = 5; | |
824 | setsockopt(m_fd, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger)); | |
825 | ||
826 | // Stay in touch with the state of things... | |
827 | ||
828 | unsigned long flag = 1; | |
829 | setsockopt(m_fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int)); | |
830 | ||
831 | // Disable the nagle algorithm, which delays sends till the | |
832 | // buffer is full (or a certain time period has passed?)... | |
833 | ||
834 | #if defined(__WINDOWS__) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY)) | |
835 | flag = 1; | |
836 | setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int)); | |
837 | #endif | |
838 | ||
839 | struct sockaddr *remote; | |
840 | size_t len; | |
841 | ||
842 | addr_man.Build(remote, len); | |
843 | ||
844 | if (connect(m_fd, remote, len) != 0) | |
845 | return FALSE; | |
846 | ||
847 | // Initializes the background threads ... | |
848 | // -------------------------------------- | |
849 | m_internal->InitializeSocket(); | |
850 | ||
851 | // Enables bg events. | |
852 | // ------------------ | |
853 | Notify(TRUE); | |
854 | ||
855 | m_connected = TRUE; | |
856 | return TRUE; | |
857 | } | |
858 | ||
859 | bool wxSocketClient::WaitOnConnect(long seconds, long microseconds) | |
860 | { | |
861 | int ret = _Wait(seconds, microseconds, REQ_CONNECT | REQ_LOST); | |
862 | ||
863 | if (ret) | |
864 | m_connected = TRUE; | |
865 | ||
866 | return m_connected; | |
867 | } | |
868 | ||
869 | void wxSocketClient::OnRequest(wxRequestEvent evt) | |
870 | { | |
871 | if (evt == EVT_CONNECT) | |
872 | { | |
873 | if (m_connected) | |
874 | { | |
875 | SetNotify(m_neededreq & ~REQ_CONNECT); | |
876 | return; | |
877 | } | |
878 | m_connected = TRUE; | |
879 | OldOnNotify(EVT_CONNECT); | |
880 | return; | |
881 | } | |
882 | wxSocketBase::OnRequest(evt); | |
883 | } | |
884 | ||
885 | ///////////////////////////////////////////////////////////////// | |
886 | // wxSocketHandler /////////////////////////////////////////////// | |
887 | ///////////////////////////////////////////////////////////////// | |
888 | ||
889 | wxSocketHandler *wxSocketHandler::master = NULL; | |
890 | #if defined(__WINDOWS__) | |
891 | static int win_initialized = 0; | |
892 | #endif | |
893 | ||
894 | // -------------------------------------------------------------- | |
895 | // --------- wxSocketHandler CONSTRUCTOR ------------------------ | |
896 | // -------------------------------------------------------------- | |
897 | wxSocketHandler::wxSocketHandler() | |
898 | { | |
899 | #if defined(__WINDOWS__) | |
900 | if (!win_initialized) | |
901 | { | |
902 | WSADATA wsaData; | |
903 | ||
904 | WSAStartup((1 << 8) | 1, &wsaData); | |
905 | win_initialized = 1; | |
906 | } | |
907 | #endif | |
908 | ||
909 | socks = new wxList; | |
910 | ||
911 | #ifndef __WINDOWS__ | |
912 | signal(SIGPIPE, SIG_IGN); | |
913 | #endif | |
914 | } | |
915 | ||
916 | // -------------------------------------------------------------- | |
917 | // --------- wxSocketHandler DESTRUCTOR ------------------------- | |
918 | // -------------------------------------------------------------- | |
919 | wxSocketHandler::~wxSocketHandler() | |
920 | { | |
921 | wxNode *next_node, *node = socks->First(); | |
922 | ||
923 | while (node) | |
924 | { | |
925 | wxSocketBase* sock = (wxSocketBase*)node->Data(); | |
926 | ||
927 | delete sock; | |
928 | next_node = node->Next(); | |
929 | delete node; | |
930 | node = next_node; | |
931 | } | |
932 | ||
933 | delete socks; | |
934 | ||
935 | #ifdef __WINDOWS__ | |
936 | WSACleanup(); | |
937 | win_initialized = 0; | |
938 | #endif | |
939 | } | |
940 | ||
941 | // -------------------------------------------------------------- | |
942 | // --------- wxSocketHandler registering functions -------------- | |
943 | // -------------------------------------------------------------- | |
944 | ||
945 | void wxSocketHandler::Register(wxSocketBase* sock) | |
946 | { | |
947 | wxNode *node; | |
948 | ||
949 | for (node = socks->First(); node != NULL; node = node->Next()) | |
950 | { | |
951 | wxSocketBase* s = (wxSocketBase*)node->Data(); | |
952 | ||
953 | if (s == sock) | |
954 | return; | |
955 | } | |
956 | ||
957 | if (sock) | |
958 | { | |
959 | socks->Append(sock); | |
960 | sock->SetHandler(this); | |
961 | } | |
962 | } | |
963 | ||
964 | void wxSocketHandler::UnRegister(wxSocketBase* sock) | |
965 | { | |
966 | wxNode *node; | |
967 | ||
968 | for (node = socks->First(); node; node = node->Next()) | |
969 | { | |
970 | wxSocketBase* s = (wxSocketBase*)node->Data(); | |
971 | ||
972 | if (s == sock) | |
973 | { | |
974 | delete node; | |
975 | sock->SetHandler(NULL); | |
976 | return; | |
977 | } | |
978 | } | |
979 | } | |
980 | ||
981 | unsigned long wxSocketHandler::Count() const | |
982 | { | |
983 | return socks->Number(); | |
984 | } | |
985 | ||
986 | // -------------------------------------------------------------- | |
987 | // --------- wxSocketHandler "big" wait functions --------------- | |
988 | // -------------------------------------------------------------- | |
989 | ||
990 | int wxSocketHandler::Wait(long seconds, long microseconds) | |
991 | { | |
992 | // TODO Needs the completely asynchronous notifier. | |
993 | ||
994 | /* | |
995 | int i; | |
996 | int on_wait; | |
997 | wxNode *node; | |
998 | for (node = socks->First(), i=0; node; node = node->Next(), i++) | |
999 | { | |
1000 | wxSocketBase *sock = (wxSocketBase *)node->Data(); | |
1001 | ||
1002 | sock->SaveState(); | |
1003 | ||
1004 | sock->SetupCallbacks(); | |
1005 | ||
1006 | sock->Callback(handler_cbk); | |
1007 | sock->CallbackData((char *)&on_wait); | |
1008 | } | |
1009 | on_wait = 0; | |
1010 | if (seconds != -1) | |
1011 | s_wake.Start((seconds*1000) + (microseconds/1000), TRUE); | |
1012 | ||
1013 | while (!on_wait) | |
1014 | PROCESS_EVENTS(); | |
1015 | ||
1016 | for (node = socks->First(), i=0; node; node = node->Next(), i++) | |
1017 | { | |
1018 | wxSocketBase *sock = (wxSocketBase *)node->Data(); | |
1019 | ||
1020 | sock->RestoreState(); | |
1021 | } | |
1022 | ||
1023 | if (on_wait == -2) | |
1024 | return 0; | |
1025 | ||
1026 | return on_wait; | |
1027 | */ | |
1028 | return 0; | |
1029 | } | |
1030 | ||
1031 | void wxSocketHandler::YieldSock() | |
1032 | { | |
1033 | wxNode *node; | |
1034 | ||
1035 | // Nothing to do anymore here except waiting for the queue emptying. | |
1036 | for (node = socks->First(); node; node=node->Next()) { | |
1037 | wxSocketBase *sock = (wxSocketBase *)node->Data(); | |
1038 | ||
1039 | sock->m_internal->WaitForEnd(NULL); | |
1040 | } | |
1041 | } | |
1042 | ||
1043 | // -------------------------------------------------------------- | |
1044 | // --------- wxSocketHandler: create and register the socket ---- | |
1045 | // -------------------------------------------------------------- | |
1046 | wxSocketServer *wxSocketHandler::CreateServer(wxSockAddress& addr, | |
1047 | wxSocketBase::wxSockFlags flags) | |
1048 | { | |
1049 | wxSocketServer *serv = new wxSocketServer(addr, flags); | |
1050 | ||
1051 | Register(serv); | |
1052 | return serv; | |
1053 | } | |
1054 | ||
1055 | wxSocketClient *wxSocketHandler::CreateClient(wxSocketBase::wxSockFlags flags) | |
1056 | { | |
1057 | wxSocketClient *client = new wxSocketClient(flags); | |
1058 | ||
1059 | Register(client); | |
1060 | return client; | |
1061 | } | |
1062 | ||
1063 | bool wxSocketModule::OnInit() | |
1064 | { | |
1065 | wxSocketHandler::master = new wxSocketHandler(); | |
1066 | return TRUE; | |
1067 | } | |
1068 | ||
1069 | void wxSocketModule::OnExit() | |
1070 | { | |
1071 | delete wxSocketHandler::master; | |
1072 | wxSocketHandler::master = NULL; | |
1073 | } | |
1074 | ||
1075 | #endif | |
1076 | // __WXSTUBS__ | |
1077 | ||
1078 | #endif | |
1079 | // wxUSE_SOCKETS |