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