]> git.saurik.com Git - wxWidgets.git/blame - src/common/socket.cpp
wxBase compilation fix
[wxWidgets.git] / src / common / socket.cpp
CommitLineData
56d8adc0 1/////////////////////////////////////////////////////////////////////////////
f4ada568
GL
2// Name: socket.cpp
3// Purpose: Socket handler classes
56d8adc0 4// Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
f4ada568 5// Created: April 1997
d3ea6527
GRG
6// Copyright: (C) 1999-1997, Guilhem Lavaux
7// (C) 2000-1999, Guillermo Rodriguez Garcia
f4ada568 8// RCS_ID: $Id$
55d99c7a 9// License: see wxWindows licence
56d8adc0
GRG
10/////////////////////////////////////////////////////////////////////////////
11
bffc1eaa
GRG
12// ==========================================================================
13// Declarations
14// ==========================================================================
15
14f355c2 16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
f4ada568 17#pragma implementation "socket.h"
f4ada568
GL
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
dc5c1114 29#include "wx/app.h"
e2478fde 30#include "wx/apptrait.h"
56d8adc0
GRG
31#include "wx/defs.h"
32#include "wx/object.h"
33#include "wx/string.h"
34#include "wx/timer.h"
35#include "wx/utils.h"
36#include "wx/module.h"
37#include "wx/log.h"
58c837a4 38#include "wx/intl.h"
f187448d 39#include "wx/event.h"
52a07708 40
56d8adc0
GRG
41#include "wx/sckaddr.h"
42#include "wx/socket.h"
3b4183d8 43
34fdf762
VS
44// DLL options compatibility check:
45#include "wx/build.h"
46WX_CHECK_BUILD_OPTIONS("wxNet")
47
bffc1eaa
GRG
48// --------------------------------------------------------------------------
49// macros and constants
50// --------------------------------------------------------------------------
ef57d866 51
dc5c1114
GRG
52// discard buffer
53#define MAX_DISCARD_SIZE (10 * 1024)
96db102a 54
e2478fde
VZ
55// what to do within waits: we have 2 cases: from the main thread itself we
56// have to call wxYield() to let the events (including the GUI events and the
57// low-level (not wxWindows) events from GSocket) be processed. From another
58// thread it is enough to just call wxThread::Yield() which will give away the
59// rest of our time slice: the explanation is that the events will be processed
60// by the main thread anyhow, without calling wxYield(), but we don't want to
61// eat the CPU time uselessly while sitting in the loop waiting for the data
62#if wxUSE_THREADS
63 #define PROCESS_EVENTS() \
64 { \
65 if ( wxThread::IsMain() ) \
66 wxYield(); \
67 else \
68 wxThread::Yield(); \
69 }
70#else // !wxUSE_THREADS
71 #define PROCESS_EVENTS() wxYield()
72#endif // wxUSE_THREADS/!wxUSE_THREADS
96db102a 73
007c77ab
RL
74#define wxTRACE_Socket _T("wxSocket")
75
ef57d866 76// --------------------------------------------------------------------------
bffc1eaa 77// wxWin macros
ef57d866 78// --------------------------------------------------------------------------
81b92e17 79
a737331d
GL
80IMPLEMENT_CLASS(wxSocketBase, wxObject)
81IMPLEMENT_CLASS(wxSocketServer, wxSocketBase)
82IMPLEMENT_CLASS(wxSocketClient, wxSocketBase)
dc5c1114 83IMPLEMENT_CLASS(wxDatagramSocket, wxSocketBase)
a737331d 84IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent)
f4ada568 85
bffc1eaa
GRG
86// --------------------------------------------------------------------------
87// private classes
88// --------------------------------------------------------------------------
89
56d8adc0
GRG
90class wxSocketState : public wxObject
91{
a324a7bc 92public:
71622a7a 93 wxSocketFlags m_flags;
bffc1eaa
GRG
94 wxSocketEventFlags m_eventmask;
95 bool m_notify;
96 void *m_clientData;
97#if WXWIN_COMPATIBILITY
81b92e17
GRG
98 wxSocketBase::wxSockCbk m_cbk;
99 char *m_cdata;
bffc1eaa 100#endif // WXWIN_COMPATIBILITY
a324a7bc
GL
101
102public:
103 wxSocketState() : wxObject() {}
22f3361e
VZ
104
105 DECLARE_NO_COPY_CLASS(wxSocketState)
a324a7bc
GL
106};
107
ef57d866
GRG
108// ==========================================================================
109// wxSocketBase
110// ==========================================================================
111
6c0d0845
VZ
112// --------------------------------------------------------------------------
113// Initialization and shutdown
114// --------------------------------------------------------------------------
115
116// FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
117// to m_countInit with a crit section
118size_t wxSocketBase::m_countInit = 0;
119
120bool wxSocketBase::IsInitialized()
121{
122 return m_countInit > 0;
123}
124
125bool wxSocketBase::Initialize()
126{
127 if ( !m_countInit++ )
128 {
38bb138f
VS
129 wxAppTraits *traits = wxAppConsole::GetInstance() ?
130 wxAppConsole::GetInstance()->GetTraits() : NULL;
131 GSocketGUIFunctionsTable *functions =
132 traits ? traits->GetSocketGUIFunctionsTable() : NULL;
133 GSocket_SetGUIFunctions(functions);
134
6c0d0845
VZ
135 if ( !GSocket_Init() )
136 {
137 m_countInit--;
138
139 return FALSE;
140 }
141 }
142
143 return TRUE;
144}
145
146void wxSocketBase::Shutdown()
147{
148 // we should be initialized
149 wxASSERT_MSG( m_countInit, _T("extra call to Shutdown()") );
150 if ( !--m_countInit )
151 {
152 GSocket_Cleanup();
153 }
154}
155
ef57d866
GRG
156// --------------------------------------------------------------------------
157// Ctor and dtor
158// --------------------------------------------------------------------------
56d8adc0 159
71622a7a 160void wxSocketBase::Init()
f4ada568 161{
71622a7a
GRG
162 m_socket = NULL;
163 m_type = wxSOCKET_UNINIT;
164
165 // state
166 m_flags = 0;
167 m_connected =
168 m_establishing =
169 m_reading =
170 m_writing =
171 m_error = FALSE;
172 m_lcount = 0;
173 m_timeout = 600;
174 m_beingDeleted = FALSE;
175
176 // pushback buffer
177 m_unread = NULL;
178 m_unrd_size = 0;
179 m_unrd_cur = 0;
180
181 // events
71622a7a 182 m_id = -1;
f187448d
GRG
183 m_handler = NULL;
184 m_clientData = NULL;
bffc1eaa
GRG
185 m_notify = FALSE;
186 m_eventmask = 0;
187#if WXWIN_COMPATIBILITY
71622a7a
GRG
188 m_cbk = NULL;
189 m_cdata = NULL;
bffc1eaa 190#endif // WXWIN_COMPATIBILITY
6c0d0845
VZ
191
192 if ( !IsInitialized() )
193 {
194 // this Initialize() will be undone by wxSocketModule::OnExit(), all the
195 // other calls to it should be matched by a call to Shutdown()
196 Initialize();
197 }
f4ada568
GL
198}
199
f187448d
GRG
200wxSocketBase::wxSocketBase()
201{
202 Init();
203}
71622a7a
GRG
204
205wxSocketBase::wxSocketBase(wxSocketFlags flags, wxSocketType type)
f4ada568 206{
71622a7a
GRG
207 Init();
208
209 m_flags = flags;
210 m_type = type;
f4ada568
GL
211}
212
f4ada568
GL
213wxSocketBase::~wxSocketBase()
214{
9181a383
GRG
215 // Just in case the app called Destroy() *and* then deleted
216 // the socket immediately: don't leave dangling pointers.
e2478fde
VZ
217 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
218 if ( traits )
219 traits->RemoveFromPendingDelete(this);
a737331d 220
56d8adc0 221 // Shutdown and close the socket
9181a383
GRG
222 if (!m_beingDeleted)
223 Close();
f4ada568 224
56d8adc0 225 // Destroy the GSocket object
a324a7bc
GL
226 if (m_socket)
227 GSocket_destroy(m_socket);
9181a383
GRG
228
229 // Free the pushback buffer
230 if (m_unread)
231 free(m_unread);
f4ada568
GL
232}
233
ef57d866
GRG
234bool wxSocketBase::Destroy()
235{
236 // Delayed destruction: the socket will be deleted during the next
237 // idle loop iteration. This ensures that all pending events have
238 // been processed.
ef57d866 239 m_beingDeleted = TRUE;
9181a383
GRG
240
241 // Shutdown and close the socket
ef57d866
GRG
242 Close();
243
bffc1eaa
GRG
244 // Supress events from now on
245 Notify(FALSE);
246
e2478fde
VZ
247 // schedule this object for deletion
248 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
249 if ( traits )
250 {
251 // let the traits object decide what to do with us
252 traits->ScheduleForDestroy(this);
253 }
254 else // no app or no traits
255 {
256 // in wxBase we might have no app object at all, don't leak memory
257 delete this;
258 }
ef57d866
GRG
259
260 return TRUE;
261}
9181a383 262
ef57d866 263// --------------------------------------------------------------------------
bffc1eaa 264// Basic IO calls
ef57d866
GRG
265// --------------------------------------------------------------------------
266
267// The following IO operations update m_error and m_lcount:
268// {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
269//
270// TODO: Should Connect, Accept and AcceptWith update m_error?
271
f4ada568
GL
272bool wxSocketBase::Close()
273{
81b92e17 274 // Interrupt pending waits
02d15cad 275 InterruptWait();
81b92e17 276
31528cd3 277 if (m_socket)
8c14576d 278 {
56d8adc0
GRG
279 // Disable callbacks
280 GSocket_UnsetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
281 GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG);
f4ada568 282
56d8adc0 283 // Shutdown the connection
a324a7bc 284 GSocket_Shutdown(m_socket);
f4ada568
GL
285 }
286
ef57d866 287 m_connected = FALSE;
04e1eb03 288 m_establishing = FALSE;
f4ada568
GL
289 return TRUE;
290}
291
f187448d 292wxSocketBase& wxSocketBase::Read(void* buffer, wxUint32 nbytes)
f4ada568 293{
96db102a
GRG
294 // Mask read events
295 m_reading = TRUE;
a324a7bc 296
96db102a 297 m_lcount = _Read(buffer, nbytes);
17aa2bec 298
d3ea6527
GRG
299 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
300 if (m_flags & wxSOCKET_WAITALL)
96db102a
GRG
301 m_error = (m_lcount != nbytes);
302 else
303 m_error = (m_lcount == 0);
f4ada568 304
dc5c1114 305 // Allow read events from now on
96db102a 306 m_reading = FALSE;
17aa2bec 307
96db102a
GRG
308 return *this;
309}
310
f187448d 311wxUint32 wxSocketBase::_Read(void* buffer, wxUint32 nbytes)
96db102a
GRG
312{
313 int total;
314 int ret = 1;
a324a7bc 315
04e1eb03 316 // Try the pushback buffer first
96db102a
GRG
317 total = GetPushback(buffer, nbytes, FALSE);
318 nbytes -= total;
f187448d 319 buffer = (char *)buffer + total;
96db102a 320
5f3629ba
GRG
321 // Return now in one of the following cases:
322 // - the socket is invalid,
323 // - we got all the data,
324 // - we got *some* data and we are not using wxSOCKET_WAITALL.
325 if ( !m_socket ||
326 !nbytes ||
327 ((total != 0) && !(m_flags & wxSOCKET_WAITALL)) )
96db102a 328 return total;
f4ada568 329
af2fd961 330 // Possible combinations (they are checked in this order)
81b92e17 331 // wxSOCKET_NOWAIT
5c9eff30 332 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
81b92e17
GRG
333 // wxSOCKET_BLOCK
334 // wxSOCKET_NONE
af2fd961 335 //
81b92e17 336 if (m_flags & wxSOCKET_NOWAIT)
af2fd961 337 {
5c9eff30 338 GSocket_SetNonBlocking(m_socket, 1);
f187448d 339 ret = GSocket_Read(m_socket, (char *)buffer, nbytes);
5c9eff30 340 GSocket_SetNonBlocking(m_socket, 0);
af2fd961
GRG
341
342 if (ret > 0)
96db102a 343 total += ret;
af2fd961 344 }
5c9eff30 345 else
56d8adc0 346 {
5c9eff30
GRG
347 bool more = TRUE;
348
349 while (more)
56d8adc0 350 {
5c9eff30
GRG
351 if ( !(m_flags & wxSOCKET_BLOCK) && !WaitForRead() )
352 break;
81b92e17 353
f187448d 354 ret = GSocket_Read(m_socket, (char *)buffer, nbytes);
a324a7bc 355
81b92e17
GRG
356 if (ret > 0)
357 {
358 total += ret;
81b92e17 359 nbytes -= ret;
f187448d 360 buffer = (char *)buffer + ret;
81b92e17 361 }
a324a7bc 362
5c9eff30
GRG
363 // If we got here and wxSOCKET_WAITALL is not set, we can leave
364 // now. Otherwise, wait until we recv all the data or until there
365 // is an error.
366 //
367 more = (ret > 0 && nbytes > 0 && (m_flags & wxSOCKET_WAITALL));
81b92e17 368 }
a324a7bc 369 }
f4ada568 370
96db102a 371 return total;
f4ada568
GL
372}
373
f187448d 374wxSocketBase& wxSocketBase::ReadMsg(void* buffer, wxUint32 nbytes)
062c4861 375{
96db102a
GRG
376 wxUint32 len, len2, sig, total;
377 bool error;
17aa2bec 378 int old_flags;
17aa2bec
GRG
379 struct
380 {
96db102a
GRG
381 unsigned char sig[4];
382 unsigned char len[4];
062c4861
GL
383 } msg;
384
96db102a
GRG
385 // Mask read events
386 m_reading = TRUE;
062c4861 387
96db102a
GRG
388 total = 0;
389 error = TRUE;
17aa2bec 390 old_flags = m_flags;
81b92e17 391 SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL);
17aa2bec 392
f187448d 393 if (_Read(&msg, sizeof(msg)) != sizeof(msg))
96db102a 394 goto exit;
062c4861 395
f187448d 396 sig = (wxUint32)msg.sig[0];
96db102a
GRG
397 sig |= (wxUint32)(msg.sig[1] << 8);
398 sig |= (wxUint32)(msg.sig[2] << 16);
399 sig |= (wxUint32)(msg.sig[3] << 24);
062c4861
GL
400
401 if (sig != 0xfeeddead)
17aa2bec 402 {
5c9eff30 403 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
96db102a 404 goto exit;
17aa2bec
GRG
405 }
406
96db102a
GRG
407 len = (wxUint32)msg.len[0];
408 len |= (wxUint32)(msg.len[1] << 8);
409 len |= (wxUint32)(msg.len[2] << 16);
410 len |= (wxUint32)(msg.len[3] << 24);
411
17aa2bec
GRG
412 if (len > nbytes)
413 {
062c4861
GL
414 len2 = len - nbytes;
415 len = nbytes;
416 }
417 else
418 len2 = 0;
419
04e1eb03 420 // Don't attemp to read if the msg was zero bytes long.
96db102a 421 if (len)
17aa2bec 422 {
96db102a
GRG
423 total = _Read(buffer, len);
424
425 if (total != len)
426 goto exit;
17aa2bec
GRG
427 }
428 if (len2)
429 {
96db102a 430 char *discard_buffer = new char[MAX_DISCARD_SIZE];
17aa2bec
GRG
431 long discard_len;
432
96db102a 433 // NOTE: discarded bytes don't add to m_lcount.
17aa2bec
GRG
434 do
435 {
96db102a 436 discard_len = ((len2 > MAX_DISCARD_SIZE)? MAX_DISCARD_SIZE : len2);
479cd5de
VZ
437 discard_len = _Read(discard_buffer, (wxUint32)discard_len);
438 len2 -= (wxUint32)discard_len;
17aa2bec
GRG
439 }
440 while ((discard_len > 0) && len2);
441
442 delete [] discard_buffer;
443
444 if (len2 != 0)
96db102a 445 goto exit;
17aa2bec 446 }
f187448d 447 if (_Read(&msg, sizeof(msg)) != sizeof(msg))
96db102a 448 goto exit;
062c4861 449
96db102a
GRG
450 sig = (wxUint32)msg.sig[0];
451 sig |= (wxUint32)(msg.sig[1] << 8);
452 sig |= (wxUint32)(msg.sig[2] << 16);
453 sig |= (wxUint32)(msg.sig[3] << 24);
a324a7bc 454
062c4861 455 if (sig != 0xdeadfeed)
17aa2bec 456 {
5c9eff30 457 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
96db102a 458 goto exit;
17aa2bec 459 }
062c4861 460
96db102a
GRG
461 // everything was OK
462 error = FALSE;
463
464exit:
465 m_error = error;
466 m_lcount = total;
467 m_reading = FALSE;
17aa2bec 468 SetFlags(old_flags);
96db102a 469
062c4861
GL
470 return *this;
471}
472
f187448d 473wxSocketBase& wxSocketBase::Peek(void* buffer, wxUint32 nbytes)
f4ada568 474{
96db102a
GRG
475 // Mask read events
476 m_reading = TRUE;
477
478 m_lcount = _Read(buffer, nbytes);
dc5c1114 479 Pushback(buffer, m_lcount);
96db102a 480
81b92e17
GRG
481 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
482 if (m_flags & wxSOCKET_WAITALL)
96db102a
GRG
483 m_error = (m_lcount != nbytes);
484 else
485 m_error = (m_lcount == 0);
486
dc5c1114 487 // Allow read events again
96db102a 488 m_reading = FALSE;
f4ada568
GL
489
490 return *this;
491}
492
f187448d 493wxSocketBase& wxSocketBase::Write(const void *buffer, wxUint32 nbytes)
f4ada568 494{
96db102a
GRG
495 // Mask write events
496 m_writing = TRUE;
56d8adc0 497
96db102a
GRG
498 m_lcount = _Write(buffer, nbytes);
499
81b92e17
GRG
500 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
501 if (m_flags & wxSOCKET_WAITALL)
96db102a
GRG
502 m_error = (m_lcount != nbytes);
503 else
504 m_error = (m_lcount == 0);
505
dc5c1114 506 // Allow write events again
96db102a
GRG
507 m_writing = FALSE;
508
96db102a
GRG
509 return *this;
510}
511
f187448d 512wxUint32 wxSocketBase::_Write(const void *buffer, wxUint32 nbytes)
96db102a
GRG
513{
514 wxUint32 total = 0;
515 int ret = 1;
56d8adc0 516
5c9eff30
GRG
517 // If the socket is invalid or parameters are ill, return immediately
518 if (!m_socket || !buffer || !nbytes)
96db102a 519 return 0;
a324a7bc 520
af2fd961 521 // Possible combinations (they are checked in this order)
81b92e17 522 // wxSOCKET_NOWAIT
5c9eff30 523 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
81b92e17
GRG
524 // wxSOCKET_BLOCK
525 // wxSOCKET_NONE
af2fd961 526 //
81b92e17 527 if (m_flags & wxSOCKET_NOWAIT)
af2fd961 528 {
5c9eff30 529 GSocket_SetNonBlocking(m_socket, 1);
f187448d 530 ret = GSocket_Write(m_socket, (const char *)buffer, nbytes);
5c9eff30 531 GSocket_SetNonBlocking(m_socket, 0);
af2fd961
GRG
532
533 if (ret > 0)
96db102a 534 total = ret;
af2fd961 535 }
5c9eff30 536 else
56d8adc0 537 {
5c9eff30
GRG
538 bool more = TRUE;
539
c3e646b4 540 while (more)
56d8adc0 541 {
5c9eff30
GRG
542 if ( !(m_flags & wxSOCKET_BLOCK) && !WaitForWrite() )
543 break;
81b92e17 544
f187448d 545 ret = GSocket_Write(m_socket, (const char *)buffer, nbytes);
5c9eff30 546
81b92e17
GRG
547 if (ret > 0)
548 {
549 total += ret;
81b92e17 550 nbytes -= ret;
f187448d 551 buffer = (const char *)buffer + ret;
81b92e17 552 }
a324a7bc 553
5c9eff30
GRG
554 // If we got here and wxSOCKET_WAITALL is not set, we can leave
555 // now. Otherwise, wait until we send all the data or until there
556 // is an error.
557 //
558 more = (ret > 0 && nbytes > 0 && (m_flags & wxSOCKET_WAITALL));
81b92e17 559 }
56d8adc0 560 }
a324a7bc 561
96db102a 562 return total;
f4ada568
GL
563}
564
f187448d 565wxSocketBase& wxSocketBase::WriteMsg(const void *buffer, wxUint32 nbytes)
062c4861 566{
96db102a
GRG
567 wxUint32 total;
568 bool error;
f187448d
GRG
569 struct
570 {
96db102a
GRG
571 unsigned char sig[4];
572 unsigned char len[4];
062c4861
GL
573 } msg;
574
96db102a
GRG
575 // Mask write events
576 m_writing = TRUE;
577
578 error = TRUE;
579 total = 0;
81b92e17 580 SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL);
96db102a 581
96db102a
GRG
582 msg.sig[0] = (unsigned char) 0xad;
583 msg.sig[1] = (unsigned char) 0xde;
584 msg.sig[2] = (unsigned char) 0xed;
585 msg.sig[3] = (unsigned char) 0xfe;
062c4861 586
04e1eb03
GRG
587 msg.len[0] = (unsigned char) (nbytes & 0xff);
588 msg.len[1] = (unsigned char) ((nbytes >> 8) & 0xff);
589 msg.len[2] = (unsigned char) ((nbytes >> 16) & 0xff);
590 msg.len[3] = (unsigned char) ((nbytes >> 24) & 0xff);
17aa2bec 591
f187448d 592 if (_Write(&msg, sizeof(msg)) < sizeof(msg))
96db102a
GRG
593 goto exit;
594
595 total = _Write(buffer, nbytes);
596
597 if (total < nbytes)
598 goto exit;
062c4861 599
96db102a
GRG
600 msg.sig[0] = (unsigned char) 0xed;
601 msg.sig[1] = (unsigned char) 0xfe;
602 msg.sig[2] = (unsigned char) 0xad;
603 msg.sig[3] = (unsigned char) 0xde;
062c4861 604 msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = (char) 0;
062c4861 605
f187448d 606 if ((_Write(&msg, sizeof(msg))) < sizeof(msg))
96db102a 607 goto exit;
17aa2bec 608
96db102a
GRG
609 // everything was OK
610 error = FALSE;
611
612exit:
613 m_error = error;
614 m_lcount = total;
615 m_writing = FALSE;
616
062c4861 617 return *this;
062c4861
GL
618}
619
f187448d 620wxSocketBase& wxSocketBase::Unread(const void *buffer, wxUint32 nbytes)
f4ada568 621{
56d8adc0 622 if (nbytes != 0)
96db102a 623 Pushback(buffer, nbytes);
f4ada568 624
96db102a
GRG
625 m_error = FALSE;
626 m_lcount = nbytes;
f4ada568 627
96db102a 628 return *this;
a324a7bc
GL
629}
630
96db102a 631wxSocketBase& wxSocketBase::Discard()
f4ada568 632{
04e1eb03
GRG
633 char *buffer = new char[MAX_DISCARD_SIZE];
634 wxUint32 ret;
17aa2bec 635 wxUint32 total = 0;
f4ada568 636
96db102a
GRG
637 // Mask read events
638 m_reading = TRUE;
639
81b92e17 640 SetFlags(wxSOCKET_NOWAIT);
384b4373 641
04e1eb03 642 do
8c14576d 643 {
04e1eb03
GRG
644 ret = _Read(buffer, MAX_DISCARD_SIZE);
645 total += ret;
f4ada568 646 }
04e1eb03 647 while (ret == MAX_DISCARD_SIZE);
f4ada568 648
04e1eb03 649 delete[] buffer;
17aa2bec 650 m_lcount = total;
04e1eb03 651 m_error = FALSE;
17aa2bec 652
dc5c1114 653 // Allow read events again
96db102a
GRG
654 m_reading = FALSE;
655
96db102a 656 return *this;
f4ada568
GL
657}
658
ef57d866
GRG
659// --------------------------------------------------------------------------
660// Wait functions
661// --------------------------------------------------------------------------
f4ada568 662
ef57d866
GRG
663// All Wait functions poll the socket using GSocket_Select() to
664// check for the specified combination of conditions, until one
665// of these conditions become true, an error occurs, or the
666// timeout elapses. The polling loop calls PROCESS_EVENTS(), so
667// this won't block the GUI.
f4ada568 668
bffc1eaa
GRG
669bool wxSocketBase::_Wait(long seconds,
670 long milliseconds,
d3ea6527 671 wxSocketEventFlags flags)
375abe3d 672{
56d8adc0 673 GSocketEventFlags result;
56d8adc0 674 long timeout;
375abe3d 675
81b92e17
GRG
676 // Set this to TRUE to interrupt ongoing waits
677 m_interrupt = FALSE;
678
ef57d866 679 // Check for valid socket
af2fd961
GRG
680 if (!m_socket)
681 return FALSE;
682
71622a7a 683 // Check for valid timeout value.
56d8adc0
GRG
684 if (seconds != -1)
685 timeout = seconds * 1000 + milliseconds;
686 else
687 timeout = m_timeout * 1000;
a324a7bc 688
bffc1eaa 689 // Wait in an active polling loop.
af2fd961 690 //
bffc1eaa
GRG
691 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
692 // hurt. It has to be here because the (GSocket) event might arrive
693 // a bit delayed, and it has to be in OnRequest as well because we
694 // don't know whether the Wait functions are being used.
af2fd961 695 //
96db102a 696 // Do this at least once (important if timeout == 0, when
d3ea6527 697 // we are just polling). Also, if just polling, do not yield.
ef57d866 698
71622a7a
GRG
699 wxStopWatch chrono;
700 bool done = FALSE;
701
702 while (!done)
56d8adc0 703 {
af2fd961
GRG
704 result = GSocket_Select(m_socket, flags | GSOCK_LOST_FLAG);
705
af2fd961
GRG
706 // Incoming connection (server) or connection established (client)
707 if (result & GSOCK_CONNECTION_FLAG)
708 {
af2fd961
GRG
709 m_connected = TRUE;
710 m_establishing = FALSE;
711 return TRUE;
712 }
713
04e1eb03 714 // Data available or output buffer ready
81b92e17 715 if ((result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG))
af2fd961 716 {
81b92e17 717 return TRUE;
af2fd961
GRG
718 }
719
c0043a50
GRG
720 // Connection lost
721 if (result & GSOCK_LOST_FLAG)
722 {
ef57d866 723 m_connected = FALSE;
04e1eb03 724 m_establishing = FALSE;
378d2bd3 725 return (flags & GSOCK_LOST_FLAG) != 0;
c0043a50
GRG
726 }
727
81b92e17 728 // Wait more?
71622a7a
GRG
729 if ((!timeout) || (chrono.Time() > timeout) || (m_interrupt))
730 done = TRUE;
d3ea6527
GRG
731 else
732 PROCESS_EVENTS();
56d8adc0 733 }
f4ada568 734
af2fd961 735 return FALSE;
f4ada568 736}
f4ada568 737
a737331d 738bool wxSocketBase::Wait(long seconds, long milliseconds)
f4ada568 739{
d80d1aba
GRG
740 return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG |
741 GSOCK_OUTPUT_FLAG |
742 GSOCK_CONNECTION_FLAG |
743 GSOCK_LOST_FLAG);
f4ada568 744}
f4ada568 745
a737331d 746bool wxSocketBase::WaitForRead(long seconds, long milliseconds)
f4ada568 747{
ef57d866 748 // Check pushback buffer before entering _Wait
96db102a
GRG
749 if (m_unread)
750 return TRUE;
751
ef57d866
GRG
752 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
753 // _Wait becuase of the semantics of WaitForRead: a return
754 // value of TRUE means that a GSocket_Read call will return
755 // immediately, not that there is actually data to read.
756
757 return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG |
758 GSOCK_LOST_FLAG);
f4ada568
GL
759}
760
a737331d 761bool wxSocketBase::WaitForWrite(long seconds, long milliseconds)
f4ada568 762{
af2fd961 763 return _Wait(seconds, milliseconds, GSOCK_OUTPUT_FLAG);
a737331d 764}
f4ada568 765
a737331d
GL
766bool wxSocketBase::WaitForLost(long seconds, long milliseconds)
767{
a324a7bc 768 return _Wait(seconds, milliseconds, GSOCK_LOST_FLAG);
f4ada568 769}
a737331d 770
ef57d866
GRG
771// --------------------------------------------------------------------------
772// Miscellaneous
773// --------------------------------------------------------------------------
774
775//
776// Get local or peer address
777//
778
779bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const
780{
781 GAddress *peer;
782
783 if (!m_socket)
784 return FALSE;
785
786 peer = GSocket_GetPeer(m_socket);
007c77ab
RL
787
788 // copying a null address would just trigger an assert anyway
789
790 if (!peer)
791 return FALSE;
792
ef57d866
GRG
793 addr_man.SetAddress(peer);
794 GAddress_destroy(peer);
795
796 return TRUE;
797}
798
799bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const
800{
801 GAddress *local;
802
803 if (!m_socket)
804 return FALSE;
805
806 local = GSocket_GetLocal(m_socket);
807 addr_man.SetAddress(local);
808 GAddress_destroy(local);
809
810 return TRUE;
811}
812
813//
814// Save and restore socket state
815//
816
817void wxSocketBase::SaveState()
818{
819 wxSocketState *state;
820
821 state = new wxSocketState();
822
bffc1eaa
GRG
823 state->m_flags = m_flags;
824 state->m_notify = m_notify;
825 state->m_eventmask = m_eventmask;
826 state->m_clientData = m_clientData;
827#if WXWIN_COMPATIBILITY
828 state->m_cbk = m_cbk;
829 state->m_cdata = m_cdata;
830#endif // WXWIN_COMPATIBILITY
ef57d866
GRG
831
832 m_states.Append(state);
833}
834
835void wxSocketBase::RestoreState()
836{
df5168c4 837 wxList::compatibility_iterator node;
ef57d866
GRG
838 wxSocketState *state;
839
b1d4dd7a 840 node = m_states.GetLast();
ef57d866
GRG
841 if (!node)
842 return;
843
b1d4dd7a 844 state = (wxSocketState *)node->GetData();
ef57d866 845
bffc1eaa
GRG
846 m_flags = state->m_flags;
847 m_notify = state->m_notify;
848 m_eventmask = state->m_eventmask;
849 m_clientData = state->m_clientData;
850#if WXWIN_COMPATIBILITY
851 m_cbk = state->m_cbk;
852 m_cdata = state->m_cdata;
853#endif // WXWIN_COMPATIBILITY
ef57d866 854
df5168c4 855 m_states.Erase(node);
ef57d866
GRG
856 delete state;
857}
858
859//
860// Timeout and flags
861//
862
17aa2bec
GRG
863void wxSocketBase::SetTimeout(long seconds)
864{
865 m_timeout = seconds;
866
867 if (m_socket)
e8f4c584 868 GSocket_SetTimeout(m_socket, m_timeout * 1000);
17aa2bec
GRG
869}
870
71622a7a 871void wxSocketBase::SetFlags(wxSocketFlags flags)
f4ada568 872{
71622a7a 873 m_flags = flags;
f4ada568 874}
f0a56ab0 875
f187448d 876
ef57d866
GRG
877// --------------------------------------------------------------------------
878// Callbacks (now obsolete - use events instead)
879// --------------------------------------------------------------------------
56d8adc0 880
bffc1eaa
GRG
881#if WXWIN_COMPATIBILITY
882
56d8adc0 883wxSocketBase::wxSockCbk wxSocketBase::Callback(wxSockCbk cbk_)
f4ada568 884{
56d8adc0 885 wxSockCbk old_cbk = cbk_;
f4ada568 886
56d8adc0
GRG
887 m_cbk = cbk_;
888 return old_cbk;
889}
890
891char *wxSocketBase::CallbackData(char *data)
892{
893 char *old_data = m_cdata;
894
895 m_cdata = data;
896 return old_data;
f4ada568
GL
897}
898
bffc1eaa
GRG
899#endif // WXWIN_COMPATIBILITY
900
ef57d866 901// --------------------------------------------------------------------------
f187448d 902// Event handling
ef57d866 903// --------------------------------------------------------------------------
a324a7bc 904
bffc1eaa
GRG
905// A note on how events are processed, which is probably the most
906// difficult thing to get working right while keeping the same API
907// and functionality for all platforms.
908//
909// When GSocket detects an event, it calls wx_socket_callback, which in
910// turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
911// object. OnRequest does some housekeeping, and if the event is to be
912// propagated to the user, it creates a new wxSocketEvent object and
913// posts it. The event is not processed immediately, but delayed with
914// AddPendingEvent instead. This is necessary in order to decouple the
915// event processing from wx_socket_callback; otherwise, subsequent IO
916// calls made from the user event handler would fail, as gtk callbacks
917// are not reentrant.
918//
919// Note that, unlike events, user callbacks (now deprecated) are _not_
920// decoupled from wx_socket_callback and thus they suffer from a variety
921// of problems. Avoid them where possible and use events instead.
96db102a 922
90350682
VZ
923extern "C"
924void LINKAGEMODE wx_socket_callback(GSocket * WXUNUSED(socket),
925 GSocketEvent notification,
926 char *cdata)
a324a7bc
GL
927{
928 wxSocketBase *sckobj = (wxSocketBase *)cdata;
929
bffc1eaa 930 sckobj->OnRequest((wxSocketNotify) notification);
56d8adc0 931}
a324a7bc 932
bffc1eaa 933void wxSocketBase::OnRequest(wxSocketNotify notification)
f4ada568 934{
bffc1eaa
GRG
935 // NOTE: We duplicate some of the code in _Wait, but this doesn't
936 // hurt. It has to be here because the (GSocket) event might arrive
937 // a bit delayed, and it has to be in _Wait as well because we don't
938 // know whether the Wait functions are being used.
ef57d866 939
bffc1eaa 940 switch(notification)
d80d1aba 941 {
04e1eb03
GRG
942 case wxSOCKET_CONNECTION:
943 m_establishing = FALSE;
944 m_connected = TRUE;
945 break;
946
d3ea6527
GRG
947 // If we are in the middle of a R/W operation, do not
948 // propagate events to users. Also, filter 'late' events
949 // which are no longer valid.
ef57d866 950
04e1eb03
GRG
951 case wxSOCKET_INPUT:
952 if (m_reading || !GSocket_Select(m_socket, GSOCK_INPUT_FLAG))
953 return;
954 break;
955
956 case wxSOCKET_OUTPUT:
957 if (m_writing || !GSocket_Select(m_socket, GSOCK_OUTPUT_FLAG))
958 return;
959 break;
960
961 case wxSOCKET_LOST:
962 m_connected = FALSE;
963 m_establishing = FALSE;
964 break;
965
966 default:
967 break;
96db102a
GRG
968 }
969
f187448d
GRG
970 // Schedule the event
971
52fbdb58 972 wxSocketEventFlags flag = 0;
bffc1eaa 973 switch (notification)
f187448d
GRG
974 {
975 case GSOCK_INPUT: flag = GSOCK_INPUT_FLAG; break;
976 case GSOCK_OUTPUT: flag = GSOCK_OUTPUT_FLAG; break;
977 case GSOCK_CONNECTION: flag = GSOCK_CONNECTION_FLAG; break;
978 case GSOCK_LOST: flag = GSOCK_LOST_FLAG; break;
52fbdb58 979 default:
5c9eff30 980 wxLogWarning(_("wxSocket: unknown event!."));
52fbdb58 981 return;
f187448d
GRG
982 }
983
bffc1eaa 984 if (((m_eventmask & flag) == flag) && m_notify)
d80d1aba 985 {
f187448d 986 if (m_handler)
bffc1eaa
GRG
987 {
988 wxSocketEvent event(m_id);
989 event.m_event = notification;
990 event.m_clientData = m_clientData;
991 event.SetEventObject(this);
992
f187448d 993 m_handler->AddPendingEvent(event);
bffc1eaa 994 }
56d8adc0 995
bffc1eaa 996#if WXWIN_COMPATIBILITY
56d8adc0 997 if (m_cbk)
bffc1eaa
GRG
998 m_cbk(*this, notification, m_cdata);
999#endif // WXWIN_COMPATIBILITY
f4ada568 1000 }
f4ada568
GL
1001}
1002
f187448d
GRG
1003void wxSocketBase::Notify(bool notify)
1004{
bffc1eaa 1005 m_notify = notify;
f187448d
GRG
1006}
1007
1008void wxSocketBase::SetNotify(wxSocketEventFlags flags)
1009{
bffc1eaa 1010 m_eventmask = flags;
f187448d
GRG
1011}
1012
71622a7a 1013void wxSocketBase::SetEventHandler(wxEvtHandler& handler, int id)
f4ada568 1014{
71622a7a
GRG
1015 m_handler = &handler;
1016 m_id = id;
f4ada568
GL
1017}
1018
ef57d866
GRG
1019// --------------------------------------------------------------------------
1020// Pushback buffer
1021// --------------------------------------------------------------------------
db131261 1022
f187448d 1023void wxSocketBase::Pushback(const void *buffer, wxUint32 size)
f4ada568 1024{
dc5c1114
GRG
1025 if (!size) return;
1026
a324a7bc 1027 if (m_unread == NULL)
f187448d 1028 m_unread = malloc(size);
dc5c1114
GRG
1029 else
1030 {
f187448d 1031 void *tmp;
f4ada568 1032
f187448d
GRG
1033 tmp = malloc(m_unrd_size + size);
1034 memcpy((char *)tmp + size, m_unread, m_unrd_size);
41895a05 1035 free(m_unread);
a324a7bc
GL
1036
1037 m_unread = tmp;
41895a05 1038 }
31528cd3 1039
f4ada568 1040 m_unrd_size += size;
a324a7bc
GL
1041
1042 memcpy(m_unread, buffer, size);
f4ada568
GL
1043}
1044
f187448d 1045wxUint32 wxSocketBase::GetPushback(void *buffer, wxUint32 size, bool peek)
f4ada568
GL
1046{
1047 if (!m_unrd_size)
1048 return 0;
1049
a324a7bc
GL
1050 if (size > (m_unrd_size-m_unrd_cur))
1051 size = m_unrd_size-m_unrd_cur;
96db102a 1052
f187448d 1053 memcpy(buffer, (char *)m_unread + m_unrd_cur, size);
f4ada568 1054
dc5c1114
GRG
1055 if (!peek)
1056 {
a324a7bc 1057 m_unrd_cur += size;
dc5c1114
GRG
1058 if (m_unrd_size == m_unrd_cur)
1059 {
f4ada568
GL
1060 free(m_unread);
1061 m_unread = NULL;
a324a7bc
GL
1062 m_unrd_size = 0;
1063 m_unrd_cur = 0;
f4ada568
GL
1064 }
1065 }
1066
1067 return size;
1068}
1069
f187448d 1070
ef57d866 1071// ==========================================================================
c3e646b4 1072// wxSocketServer
ef57d866 1073// ==========================================================================
f4ada568 1074
ef57d866
GRG
1075// --------------------------------------------------------------------------
1076// Ctor
1077// --------------------------------------------------------------------------
56d8adc0 1078
f4ada568 1079wxSocketServer::wxSocketServer(wxSockAddress& addr_man,
71622a7a
GRG
1080 wxSocketFlags flags)
1081 : wxSocketBase(flags, wxSOCKET_SERVER)
f4ada568 1082{
007c77ab 1083 wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") );
f4ada568 1084
007c77ab 1085 m_socket = GSocket_new();
384b4373 1086
007c77ab
RL
1087 if (!m_socket)
1088 {
1089 wxLogTrace( wxTRACE_Socket, _T("*** GSocket_new failed") );
1090 return;
1091 }
a737331d 1092
007c77ab 1093 // Setup the socket as server
56d8adc0 1094
007c77ab
RL
1095 GSocket_SetLocal(m_socket, addr_man.GetAddress());
1096 if (GSocket_SetServer(m_socket) != GSOCK_NOERROR)
1097 {
1098 GSocket_destroy(m_socket);
1099 m_socket = NULL;
1100
1101 wxLogTrace( wxTRACE_Socket, _T("*** GSocket_SetServer failed") );
1102 return;
1103 }
1104
1105 GSocket_SetTimeout(m_socket, m_timeout * 1000);
1106 GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
1107 GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
1108 wx_socket_callback, (char *)this);
f4ada568
GL
1109}
1110
ef57d866
GRG
1111// --------------------------------------------------------------------------
1112// Accept
1113// --------------------------------------------------------------------------
8c14576d 1114
d80d1aba 1115bool wxSocketServer::AcceptWith(wxSocketBase& sock, bool wait)
f4ada568 1116{
a324a7bc 1117 GSocket *child_socket;
f4ada568 1118
17aa2bec
GRG
1119 if (!m_socket)
1120 return FALSE;
1121
04e1eb03 1122 // If wait == FALSE, then the call should be nonblocking.
56d8adc0
GRG
1123 // When we are finished, we put the socket to blocking mode
1124 // again.
d80d1aba
GRG
1125
1126 if (!wait)
5c9eff30 1127 GSocket_SetNonBlocking(m_socket, 1);
d80d1aba 1128
a324a7bc 1129 child_socket = GSocket_WaitConnection(m_socket);
384b4373 1130
d80d1aba 1131 if (!wait)
5c9eff30 1132 GSocket_SetNonBlocking(m_socket, 0);
d80d1aba 1133
04e1eb03 1134 if (!child_socket)
d80d1aba
GRG
1135 return FALSE;
1136
71622a7a 1137 sock.m_type = wxSOCKET_BASE;
a324a7bc 1138 sock.m_socket = child_socket;
f4ada568
GL
1139 sock.m_connected = TRUE;
1140
e8f4c584 1141 GSocket_SetTimeout(sock.m_socket, sock.m_timeout * 1000);
56d8adc0
GRG
1142 GSocket_SetCallback(sock.m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
1143 GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
1144 wx_socket_callback, (char *)&sock);
1145
f4ada568
GL
1146 return TRUE;
1147}
1148
d80d1aba 1149wxSocketBase *wxSocketServer::Accept(bool wait)
f4ada568
GL
1150{
1151 wxSocketBase* sock = new wxSocketBase();
1152
71622a7a 1153 sock->SetFlags(m_flags);
f4ada568 1154
d80d1aba 1155 if (!AcceptWith(*sock, wait))
c3e646b4
VZ
1156 {
1157 sock->Destroy();
1158 sock = NULL;
1159 }
f4ada568 1160
f4ada568
GL
1161 return sock;
1162}
1163
17aa2bec 1164bool wxSocketServer::WaitForAccept(long seconds, long milliseconds)
d80d1aba 1165{
af2fd961 1166 return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG);
d80d1aba
GRG
1167}
1168
ef57d866 1169// ==========================================================================
8c14576d 1170// wxSocketClient
ef57d866 1171// ==========================================================================
f4ada568 1172
ef57d866
GRG
1173// --------------------------------------------------------------------------
1174// Ctor and dtor
1175// --------------------------------------------------------------------------
56d8adc0 1176
71622a7a
GRG
1177wxSocketClient::wxSocketClient(wxSocketFlags flags)
1178 : wxSocketBase(flags, wxSOCKET_CLIENT)
f4ada568
GL
1179{
1180}
1181
f4ada568
GL
1182wxSocketClient::~wxSocketClient()
1183{
1184}
1185
ef57d866
GRG
1186// --------------------------------------------------------------------------
1187// Connect
1188// --------------------------------------------------------------------------
dc5c1114 1189
791b24c4 1190bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait)
f4ada568 1191{
791b24c4
GRG
1192 GSocketError err;
1193
ef57d866 1194 if (m_socket)
04e1eb03
GRG
1195 {
1196 // Shutdown and destroy the socket
f4ada568 1197 Close();
a324a7bc 1198 GSocket_destroy(m_socket);
04e1eb03 1199 }
a324a7bc 1200
a324a7bc 1201 m_socket = GSocket_new();
d80d1aba
GRG
1202 m_connected = FALSE;
1203 m_establishing = FALSE;
384b4373 1204
a324a7bc 1205 if (!m_socket)
f4ada568 1206 return FALSE;
384b4373 1207
e8f4c584 1208 GSocket_SetTimeout(m_socket, m_timeout * 1000);
96db102a
GRG
1209 GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
1210 GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
1211 wx_socket_callback, (char *)this);
56d8adc0 1212
04e1eb03 1213 // If wait == FALSE, then the call should be nonblocking.
56d8adc0
GRG
1214 // When we are finished, we put the socket to blocking mode
1215 // again.
f4ada568 1216
791b24c4 1217 if (!wait)
5c9eff30 1218 GSocket_SetNonBlocking(m_socket, 1);
791b24c4 1219
a324a7bc 1220 GSocket_SetPeer(m_socket, addr_man.GetAddress());
791b24c4
GRG
1221 err = GSocket_Connect(m_socket, GSOCK_STREAMED);
1222
1223 if (!wait)
5c9eff30 1224 GSocket_SetNonBlocking(m_socket, 0);
791b24c4
GRG
1225
1226 if (err != GSOCK_NOERROR)
56d8adc0
GRG
1227 {
1228 if (err == GSOCK_WOULDBLOCK)
1229 m_establishing = TRUE;
1230
f4ada568 1231 return FALSE;
56d8adc0 1232 }
9111db68 1233
f4ada568
GL
1234 m_connected = TRUE;
1235 return TRUE;
1236}
1237
d80d1aba 1238bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds)
f4ada568 1239{
af2fd961 1240 if (m_connected) // Already connected
d80d1aba
GRG
1241 return TRUE;
1242
af2fd961 1243 if (!m_establishing || !m_socket) // No connection in progress
d80d1aba
GRG
1244 return FALSE;
1245
2b396caa
GRG
1246 return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG |
1247 GSOCK_LOST_FLAG);
f4ada568
GL
1248}
1249
ef57d866 1250// ==========================================================================
dc5c1114 1251// wxDatagramSocket
ef57d866 1252// ==========================================================================
dc5c1114
GRG
1253
1254/* NOTE: experimental stuff - might change */
1255
71622a7a
GRG
1256wxDatagramSocket::wxDatagramSocket( wxSockAddress& addr,
1257 wxSocketFlags flags )
1258 : wxSocketBase( flags, wxSOCKET_DATAGRAM )
dc5c1114
GRG
1259{
1260 // Create the socket
1261 m_socket = GSocket_new();
1262
1263 if(!m_socket)
1264 return;
1265
1266 // Setup the socket as non connection oriented
1267 GSocket_SetLocal(m_socket, addr.GetAddress());
1268 if( GSocket_SetNonOriented(m_socket) != GSOCK_NOERROR )
1269 {
1270 GSocket_destroy(m_socket);
1271 m_socket = NULL;
1272 return;
1273 }
1274
1275 // Initialize all stuff
1276 m_connected = FALSE;
1277 m_establishing = FALSE;
1278 GSocket_SetTimeout( m_socket, m_timeout );
1279 GSocket_SetCallback( m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
1280 GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
1281 wx_socket_callback, (char*)this );
1282
1283}
1284
1285wxDatagramSocket& wxDatagramSocket::RecvFrom( wxSockAddress& addr,
f187448d 1286 void* buf,
dc5c1114
GRG
1287 wxUint32 nBytes )
1288{
1289 Read(buf, nBytes);
1290 GetPeer(addr);
1291 return (*this);
1292}
1293
1294wxDatagramSocket& wxDatagramSocket::SendTo( wxSockAddress& addr,
f187448d 1295 const void* buf,
dc5c1114
GRG
1296 wxUint32 nBytes )
1297{
1298 GSocket_SetPeer(m_socket, addr.GetAddress());
1299 Write(buf, nBytes);
1300 return (*this);
1301}
1302
ef57d866 1303// ==========================================================================
a58d5df4 1304// wxSocketModule
ef57d866 1305// ==========================================================================
dc5c1114 1306
ed4c6c69 1307class wxSocketModule : public wxModule
dc5c1114 1308{
dc5c1114 1309public:
6c0d0845
VZ
1310 virtual bool OnInit()
1311 {
1312 // wxSocketBase will call GSocket_Init() itself when/if needed
1313 return TRUE;
1314 }
1315
1316 virtual void OnExit()
1317 {
1318 if ( wxSocketBase::IsInitialized() )
1319 wxSocketBase::Shutdown();
1320 }
1321
1322private:
1323 DECLARE_DYNAMIC_CLASS(wxSocketModule)
a58d5df4
GL
1324};
1325
1326IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule)
1327
35a4dab7
GL
1328#endif
1329 // wxUSE_SOCKETS
007c77ab
RL
1330
1331// vi:sts=4:sw=4:et