]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/socket.h
Restore wxFile docs
[wxWidgets.git] / interface / wx / socket.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: socket.h
e54c96f1 3// Purpose: interface of wxIPV4address
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9/**
10 @class wxIPV4address
7c913512
FM
11
12
23324ae1
FM
13 @library{wxbase}
14 @category{net}
15*/
16class wxIPV4address : public wxIPaddress
17{
18public:
19 /**
20 Set address to any of the addresses of the current machine. Whenever
21 possible, use this function instead of LocalHost(),
22 as this correctly handles multi-homed hosts and avoids other small
23 problems. Internally, this is the same as setting the IP address
24 to @b INADDR_ANY.
3c4f71cc 25
d29a9a8a 26 @return Returns @true on success, @false if something went wrong.
23324ae1
FM
27 */
28 bool AnyAddress();
29
30 //@{
31 /**
32 Returns the hostname which matches the IP address.
33 */
34 bool Hostname(const wxString& hostname);
7c913512 35 Return value wxString Hostname();
23324ae1
FM
36 //@}
37
38 /**
39 Returns a wxString containing the IP address in dot quad (127.0.0.1) format.
40 */
41 wxString IPAddress();
42
43 /**
7c913512 44 Set address to localhost (127.0.0.1). Whenever possible, use the
23324ae1
FM
45 AnyAddress(),
46 function instead of this one, as this will correctly handle multi-homed
47 hosts and avoid other small problems.
48 */
49 bool LocalHost();
50
51 //@{
52 /**
53 Returns the current service.
54 */
55 bool Service(const wxString& service);
7c913512
FM
56 Return value bool Service(unsigned short service);
57 Return value unsigned short Service();
23324ae1
FM
58 //@}
59};
60
61
e54c96f1 62
23324ae1
FM
63/**
64 @class wxSocketServer
7c913512
FM
65
66
23324ae1
FM
67 @library{wxnet}
68 @category{net}
7c913512 69
e54c96f1
FM
70 @see wxSocketServer::WaitForAccept, wxSocketBase::SetNotify,
71 wxSocketBase::Notify, wxSocketServer::AcceptWith
23324ae1
FM
72*/
73class wxSocketServer : public wxSocketBase
74{
75public:
76 /**
77 Constructs a new server and tries to bind to the specified @e address.
7c913512 78 Before trying to accept new connections, test whether it succeeded with
23324ae1 79 @ref wxSocketBase::isok wxSocketBase:IsOk.
3c4f71cc 80
7c913512 81 @param address
4cc4bfaf 82 Specifies the local address for the server (e.g. port number).
7c913512 83 @param flags
4cc4bfaf 84 Socket flags (See wxSocketBase::SetFlags)
23324ae1
FM
85 */
86 wxSocketServer(const wxSockAddress& address,
87 wxSocketFlags flags = wxSOCKET_NONE);
88
89 /**
90 Destructor (it doesn't close the accepted connections).
91 */
92 ~wxSocketServer();
93
94 /**
7c913512 95 Accepts an incoming connection request, and creates a new
23324ae1
FM
96 wxSocketBase object which represents
97 the server-side of the connection.
4cc4bfaf 98 If @a wait is @true and there are no pending connections to be
23324ae1
FM
99 accepted, it will wait for the next incoming connection to
100 arrive. @b Warning: This will block the GUI.
4cc4bfaf 101 If @a wait is @false, it will try to accept a pending connection
23324ae1
FM
102 if there is one, but it will always return immediately without blocking
103 the GUI. If you want to use Accept in this way, you can either check for
7c913512 104 incoming connections with WaitForAccept()
23324ae1
FM
105 or catch @b wxSOCKET_CONNECTION events, then call Accept once you know
106 that there is an incoming connection waiting to be accepted.
3c4f71cc 107
d29a9a8a 108 @return Returns an opened socket connection, or @NULL if an error
4cc4bfaf
FM
109 occurred or if the wait parameter was @false and there
110 were no pending connections.
3c4f71cc 111
4cc4bfaf
FM
112 @see WaitForAccept(), wxSocketBase::SetNotify,
113 wxSocketBase::Notify, AcceptWith()
23324ae1 114 */
4cc4bfaf 115 wxSocketBase* Accept(bool wait = true);
23324ae1
FM
116
117 /**
118 Accept an incoming connection using the specified socket object.
3c4f71cc 119
7c913512 120 @param socket
4cc4bfaf 121 Socket to be initialized
3c4f71cc 122
d29a9a8a 123 @return Returns @true on success, or @false if an error occurred or if the
4cc4bfaf
FM
124 wait parameter was @false and there were no pending
125 connections.
23324ae1 126 */
4cc4bfaf 127 bool AcceptWith(wxSocketBase& socket, bool wait = true);
23324ae1
FM
128
129 /**
7c913512
FM
130 This function waits for an incoming connection. Use it if you want to call
131 Accept() or AcceptWith()
23324ae1
FM
132 with @e wait set to @false, to detect when an incoming connection is waiting
133 to be accepted.
3c4f71cc 134
7c913512 135 @param seconds
4cc4bfaf
FM
136 Number of seconds to wait.
137 If -1, it will wait for the default timeout,
138 as set with SetTimeout.
7c913512 139 @param millisecond
4cc4bfaf 140 Number of milliseconds to wait.
3c4f71cc 141
d29a9a8a 142 @return Returns @true if an incoming connection arrived, @false if the
4cc4bfaf 143 timeout elapsed.
23324ae1
FM
144 */
145 bool WaitForAccept(long seconds = -1, long millisecond = 0);
146};
147
148
e54c96f1 149
23324ae1
FM
150/**
151 @class wxIPaddress
7c913512
FM
152
153 wxIPaddress is an abstract base class for all internet protocol address
154 objects. Currently, only wxIPV4address
23324ae1
FM
155 is implemented. An experimental implementation for IPV6, wxIPV6address,
156 is being developed.
7c913512 157
23324ae1
FM
158 @library{wxbase}
159 @category{net}
160*/
161class wxIPaddress : public wxSockAddress
162{
163public:
164 /**
165 Internally, this is the same as setting the IP address
166 to @b INADDR_ANY.
23324ae1 167 On IPV4 implementations, 0.0.0.0
23324ae1 168 On IPV6 implementations, ::
3c4f71cc 169
d29a9a8a 170 @return Returns @true on success, @false if something went wrong.
23324ae1
FM
171 */
172 virtual bool AnyAddress();
173
174 /**
175 Internally, this is the same as setting the IP address
176 to @b INADDR_BROADCAST.
23324ae1 177 On IPV4 implementations, 255.255.255.255
3c4f71cc 178
d29a9a8a 179 @return Returns @true on success, @false if something went wrong.
23324ae1
FM
180 */
181 virtual bool BroadcastAddress();
182
183 //@{
184 /**
185 Returns the hostname which matches the IP address.
186 */
187 virtual bool Hostname(const wxString& hostname);
7c913512 188 Return value virtual wxString Hostname();
23324ae1
FM
189 //@}
190
191 /**
192 Returns a wxString containing the IP address.
193 */
194 virtual wxString IPAddress();
195
196 /**
197 Determines if current address is set to localhost.
198 */
199 virtual bool IsLocalHost();
200
201 /**
7c913512 202 Set address to localhost.
23324ae1 203 On IPV4 implementations, 127.0.0.1
23324ae1 204 On IPV6 implementations, ::1
3c4f71cc 205
d29a9a8a 206 @return Returns @true on success, @false if something went wrong.
23324ae1
FM
207 */
208 virtual bool LocalHost();
209
210 //@{
211 /**
212 Returns the current service.
213 */
214 virtual bool Service(const wxString& service);
7c913512
FM
215 Return value virtual bool Service(unsigned short service);
216 Return value virtual unsigned short Service();
23324ae1
FM
217 //@}
218};
219
220
e54c96f1 221
23324ae1
FM
222/**
223 @class wxSocketClient
7c913512
FM
224
225
23324ae1
FM
226 @library{wxnet}
227 @category{net}
7c913512 228
e54c96f1
FM
229 @see wxSocketClient::WaitOnConnect, wxSocketBase::SetNotify,
230 wxSocketBase::Notify
23324ae1
FM
231*/
232class wxSocketClient : public wxSocketBase
233{
234public:
235 /**
236 Constructor.
3c4f71cc 237
7c913512 238 @param flags
4cc4bfaf 239 Socket flags (See wxSocketBase::SetFlags)
23324ae1
FM
240 */
241 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
242
243 /**
244 Destructor. Please see wxSocketBase::Destroy.
245 */
246 ~wxSocketClient();
247
248 //@{
249 /**
250 Connects to a server using the specified address.
4cc4bfaf 251 If @a wait is @true, Connect will wait until the connection
23324ae1 252 completes. @b Warning: This will block the GUI.
4cc4bfaf 253 If @a wait is @false, Connect will try to establish the connection and
23324ae1
FM
254 return immediately, without blocking the GUI. When used this way, even if
255 Connect returns @false, the connection request can be completed later.
256 To detect this, use WaitOnConnect(),
257 or catch @b wxSOCKET_CONNECTION events (for successful establishment)
258 and @b wxSOCKET_LOST events (for connection failure).
3c4f71cc 259
7c913512 260 @param address
4cc4bfaf 261 Address of the server.
7c913512 262 @param local
4cc4bfaf
FM
263 Bind to the specified local address and port before connecting.
264 The local address and port can also be set using SetLocal,
265 and then using the 2-parameter Connect method.
7c913512 266 @param wait
4cc4bfaf 267 If @true, waits for the connection to complete.
3c4f71cc 268
d29a9a8a 269 @return Returns @true if the connection is established and no error
4cc4bfaf 270 occurs.
3c4f71cc 271
4cc4bfaf
FM
272 @see WaitOnConnect(), wxSocketBase::SetNotify,
273 wxSocketBase::Notify
23324ae1 274 */
4cc4bfaf 275 bool Connect(wxSockAddress& address, bool wait = true);
7c913512 276 bool Connect(wxSockAddress& address, wxSockAddress& local,
4cc4bfaf 277 bool wait = true);
23324ae1
FM
278 //@}
279
280 /**
281 Wait until a connection request completes, or until the specified timeout
282 elapses. Use this function after issuing a call
283 to Connect() with @e wait set to @false.
3c4f71cc 284
7c913512 285 @param seconds
4cc4bfaf
FM
286 Number of seconds to wait.
287 If -1, it will wait for the default timeout,
288 as set with SetTimeout.
7c913512 289 @param millisecond
4cc4bfaf 290 Number of milliseconds to wait.
3c4f71cc 291
d29a9a8a 292 @return WaitOnConnect returns @true if the connection request completes.
4cc4bfaf
FM
293 This does not necessarily mean that the connection was
294 successfully established; it might also happen that the
295 connection was refused by the peer. Use IsConnected to
296 distinguish between these two situations.
23324ae1
FM
297 */
298 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
299};
300
301
e54c96f1 302
23324ae1
FM
303/**
304 @class wxSockAddress
7c913512 305
23324ae1 306 You are unlikely to need to use this class: only wxSocketBase uses it.
7c913512 307
23324ae1
FM
308 @library{wxbase}
309 @category{FIXME}
7c913512 310
e54c96f1 311 @see wxSocketBase, wxIPaddress, wxIPV4address
23324ae1
FM
312*/
313class wxSockAddress : public wxObject
314{
315public:
316 /**
317 Default constructor.
318 */
319 wxSockAddress();
320
321 /**
322 Default destructor.
323 */
324 ~wxSockAddress();
325
326 /**
327 Delete all informations about the address.
328 */
329 void Clear();
330
331 /**
332 Returns the length of the socket address.
333 */
334 int SockAddrLen();
335};
336
337
e54c96f1 338
23324ae1
FM
339/**
340 @class wxSocketEvent
7c913512 341
23324ae1 342 This event class contains information about socket events.
7c913512 343
23324ae1
FM
344 @library{wxnet}
345 @category{net}
7c913512 346
e54c96f1 347 @see wxSocketBase, wxSocketClient, wxSocketServer
23324ae1
FM
348*/
349class wxSocketEvent : public wxEvent
350{
351public:
352 /**
353 Constructor.
354 */
355 wxSocketEvent(int id = 0);
356
357 /**
358 Gets the client data of the socket which generated this event, as
359 set with wxSocketBase::SetClientData.
360 */
4cc4bfaf 361 void* GetClientData();
23324ae1
FM
362
363 /**
364 Returns the socket object to which this event refers to. This makes
365 it possible to use the same event handler for different sockets.
366 */
328f5751 367 wxSocketBase* GetSocket() const;
23324ae1
FM
368
369 /**
370 Returns the socket event type.
371 */
328f5751 372 wxSocketNotify GetSocketEvent() const;
23324ae1
FM
373};
374
375
e54c96f1 376
23324ae1
FM
377/**
378 @class wxSocketBase
7c913512 379
23324ae1
FM
380 wxSocketBase is the base class for all socket-related objects, and it
381 defines all basic IO functionality.
7c913512 382
23324ae1
FM
383 Note: (Workaround for implementation limitation for wxWidgets up to 2.5.x)
384 If you want to use sockets or derived classes such as wxFTP in a secondary
385 thread,
386 call wxSocketBase::Initialize() (undocumented) from the main thread before
7c913512
FM
387 creating
388 any sockets - in wxApp::OnInit for example.
23324ae1
FM
389 See http://wiki.wxwidgets.org/wiki.pl?WxSocket or
390 http://www.litwindow.com/knowhow/knowhow.html for more details.
7c913512 391
23324ae1
FM
392 @library{wxnet}
393 @category{net}
7c913512 394
e54c96f1 395 @see wxSocketEvent, wxSocketClient, wxSocketServer, @ref overview_samplesockets
23324ae1
FM
396 "Sockets sample"
397*/
398class wxSocketBase : public wxObject
399{
400public:
401 /**
7c913512
FM
402 Default constructor. Don't use it directly; instead, use
403 wxSocketClient to construct a socket client, or
23324ae1
FM
404 wxSocketServer to construct a socket server.
405 */
406 wxSocketBase();
407
408 /**
409 Destructor. Do not destroy a socket using the delete operator directly;
410 use Destroy() instead. Also, do not create
411 socket objects in the stack.
412 */
413 ~wxSocketBase();
414
415 /**
416 Functions that perform basic IO functionality.
23324ae1 417 Close()
3c4f71cc 418
23324ae1 419 Discard()
3c4f71cc 420
23324ae1 421 Peek()
3c4f71cc 422
23324ae1 423 Read()
3c4f71cc 424
23324ae1 425 ReadMsg()
3c4f71cc 426
23324ae1 427 Unread()
3c4f71cc 428
23324ae1 429 Write()
3c4f71cc 430
23324ae1 431 WriteMsg()
23324ae1 432 Functions that perform a timed wait on a certain IO condition.
23324ae1 433 InterruptWait()
3c4f71cc 434
23324ae1 435 Wait()
3c4f71cc 436
23324ae1 437 WaitForLost()
3c4f71cc 438
23324ae1 439 WaitForRead()
3c4f71cc 440
23324ae1 441 WaitForWrite()
3c4f71cc 442
4cc4bfaf 443 and also:
23324ae1 444 wxSocketServer::WaitForAccept
3c4f71cc 445
23324ae1 446 wxSocketClient::WaitOnConnect
23324ae1 447 Functions that allow applications to customize socket IO as needed.
23324ae1 448 GetFlags()
3c4f71cc 449
23324ae1 450 SetFlags()
3c4f71cc 451
23324ae1 452 SetTimeout()
3c4f71cc 453
23324ae1
FM
454 SetLocal()
455 */
456
457
458 /**
459 This function shuts down the socket, disabling further transmission and
460 reception of data; it also disables events for the socket and frees the
461 associated system resources. Upon socket destruction, Close is automatically
462 called, so in most cases you won't need to do it yourself, unless you
463 explicitly want to shut down the socket, typically to notify the peer
464 that you are closing the connection.
465 */
466 void Close();
467
468 /**
469 @ref construct() wxSocketBase
3c4f71cc 470
23324ae1 471 @ref destruct() ~wxSocketBase
3c4f71cc 472
23324ae1
FM
473 Destroy()
474 */
475
476
477 /**
478 Destroys the socket safely. Use this function instead of the delete operator,
479 since otherwise socket events could reach the application even after the
480 socket has been destroyed. To prevent this problem, this function appends
481 the wxSocket to a list of object to be deleted on idle time, after all
482 events have been processed. For the same reason, you should avoid creating
483 socket objects in the stack.
23324ae1 484 Destroy calls Close() automatically.
3c4f71cc 485
d29a9a8a 486 @return Always @true.
23324ae1
FM
487 */
488 bool Destroy();
489
490 /**
491 This function simply deletes all bytes in the incoming queue. This function
492 always returns immediately and its operation is not affected by IO flags.
23324ae1 493 Use LastCount() to verify the number of bytes actually discarded.
23324ae1
FM
494 If you use Error(), it will always return @false.
495 */
496 wxSocketBase Discard();
497
498 /**
499 Returns @true if an error occurred in the last IO operation.
23324ae1
FM
500 Use this function to check for an error condition after one of the
501 following calls: Discard, Peek, Read, ReadMsg, Unread, Write, WriteMsg.
502 */
328f5751 503 bool Error() const;
23324ae1
FM
504
505 /**
7c913512 506 Returns a pointer of the client data for this socket, as set with
23324ae1
FM
507 SetClientData()
508 */
328f5751 509 void* GetClientData() const;
23324ae1
FM
510
511 /**
512 Returns current IO flags, as set with SetFlags()
513 */
328f5751 514 wxSocketFlags GetFlags() const;
23324ae1
FM
515
516 /**
517 This function returns the local address field of the socket. The local
518 address field contains the complete local address of the socket (local
519 address, local port, ...).
3c4f71cc 520
d29a9a8a 521 @return @true if no error happened, @false otherwise.
23324ae1 522 */
328f5751 523 bool GetLocal(wxSockAddress& addr) const;
23324ae1
FM
524
525 /**
7c913512 526 This function returns the peer address field of the socket. The peer
23324ae1
FM
527 address field contains the complete peer host address of the socket
528 (address, port, ...).
3c4f71cc 529
d29a9a8a 530 @return @true if no error happened, @false otherwise.
23324ae1 531 */
328f5751 532 bool GetPeer(wxSockAddress& addr) const;
23324ae1
FM
533
534 /**
535 Functions that allow applications to receive socket events.
23324ae1 536 Notify()
3c4f71cc 537
23324ae1 538 SetNotify()
3c4f71cc 539
23324ae1 540 GetClientData()
3c4f71cc 541
23324ae1 542 SetClientData()
3c4f71cc 543
23324ae1
FM
544 SetEventHandler()
545 */
546
547
548 /**
549 Use this function to interrupt any wait operation currently in progress.
550 Note that this is not intended as a regular way to interrupt a Wait call,
551 but only as an escape mechanism for exceptional situations where it is
552 absolutely necessary to use it, for example to abort an operation due to
553 some exception or abnormal problem. InterruptWait is automatically called
554 when you Close() a socket (and thus also upon
555 socket destruction), so you don't need to use it in these cases.
7c913512
FM
556 Wait(),
557 wxSocketServer::WaitForAccept,
558 WaitForLost(),
559 WaitForRead(),
560 WaitForWrite(),
23324ae1
FM
561 wxSocketClient::WaitOnConnect
562 */
563 void InterruptWait();
564
565 /**
566 Returns @true if the socket is connected.
567 */
328f5751 568 bool IsConnected() const;
23324ae1
FM
569
570 /**
571 This function waits until the socket is readable. This might mean that
572 queued data is available for reading or, for streamed sockets, that
573 the connection has been closed, so that a read operation will complete
574 immediately without blocking (unless the @b wxSOCKET_WAITALL flag
575 is set, in which case the operation might still block).
576 */
328f5751 577 bool IsData() const;
23324ae1
FM
578
579 /**
580 Returns @true if the socket is not connected.
581 */
328f5751 582 bool IsDisconnected() const;
23324ae1
FM
583
584 /**
585 Returns @true if the socket is initialized and ready and @false in other
586 cases.
587 */
328f5751 588 bool IsOk() const;
23324ae1
FM
589
590 /**
591 Returns the number of bytes read or written by the last IO call.
23324ae1
FM
592 Use this function to get the number of bytes actually transferred
593 after using one of the following IO calls: Discard, Peek, Read,
594 ReadMsg, Unread, Write, WriteMsg.
595 */
328f5751 596 wxUint32 LastCount() const;
23324ae1
FM
597
598 /**
599 Returns the last wxSocket error. See @ref overview_wxsocketbase "wxSocket
600 errors".
23324ae1
FM
601 Please note that this function merely returns the last error code,
602 but it should not be used to determine if an error has occurred (this
603 is because successful operations do not change the LastError value).
604 Use Error() first, in order to determine
605 if the last IO call failed. If this returns @true, use LastError
606 to discover the cause of the error.
607 */
328f5751 608 wxSocketError LastError() const;
23324ae1
FM
609
610 /**
4cc4bfaf
FM
611 According to the @a notify value, this function enables
612 or disables socket events. If @a notify is @true, the events
23324ae1 613 configured with SetNotify() will
4cc4bfaf 614 be sent to the application. If @a notify is @false; no events
23324ae1
FM
615 will be sent.
616 */
617 void Notify(bool notify);
618
619 /**
4cc4bfaf 620 This function peeks a buffer of @a nbytes bytes from the socket.
23324ae1 621 Peeking a buffer doesn't delete it from the socket input queue.
23324ae1 622 Use LastCount() to verify the number of bytes actually peeked.
23324ae1 623 Use Error() to determine if the operation succeeded.
3c4f71cc 624
7c913512 625 @param buffer
4cc4bfaf 626 Buffer where to put peeked data.
7c913512 627 @param nbytes
4cc4bfaf 628 Number of bytes.
3c4f71cc 629
d29a9a8a 630 @return Returns a reference to the current object.
3c4f71cc 631
4cc4bfaf
FM
632 @see Error(), LastError(), LastCount(),
633 SetFlags()
23324ae1 634 */
4cc4bfaf 635 wxSocketBase Peek(void* buffer, wxUint32 nbytes);
23324ae1
FM
636
637 /**
4cc4bfaf 638 This function reads a buffer of @a nbytes bytes from the socket.
23324ae1 639 Use LastCount() to verify the number of bytes actually read.
23324ae1 640 Use Error() to determine if the operation succeeded.
3c4f71cc 641
7c913512 642 @param buffer
4cc4bfaf 643 Buffer where to put read data.
7c913512 644 @param nbytes
4cc4bfaf 645 Number of bytes.
3c4f71cc 646
d29a9a8a 647 @return Returns a reference to the current object.
3c4f71cc 648
4cc4bfaf
FM
649 @see Error(), LastError(), LastCount(),
650 SetFlags()
23324ae1 651 */
4cc4bfaf 652 wxSocketBase Read(void* buffer, wxUint32 nbytes);
23324ae1
FM
653
654 /**
7c913512 655 This function reads a buffer sent by WriteMsg()
23324ae1
FM
656 on a socket. If the buffer passed to the function isn't big enough, the
657 remaining bytes will be discarded. This function always waits for the
658 buffer to be entirely filled, unless an error occurs.
23324ae1 659 Use LastCount() to verify the number of bytes actually read.
23324ae1 660 Use Error() to determine if the operation succeeded.
3c4f71cc 661
7c913512 662 @param buffer
4cc4bfaf 663 Buffer where to put read data.
7c913512 664 @param nbytes
4cc4bfaf 665 Size of the buffer.
3c4f71cc 666
d29a9a8a 667 @return Returns a reference to the current object.
3c4f71cc 668
4cc4bfaf
FM
669 @see Error(), LastError(), LastCount(),
670 SetFlags(), WriteMsg()
23324ae1 671 */
4cc4bfaf 672 wxSocketBase ReadMsg(void* buffer, wxUint32 nbytes);
23324ae1
FM
673
674 /**
675 This function restores the previous state of the socket, as saved
676 with SaveState()
23324ae1 677 Calls to SaveState and RestoreState can be nested.
3c4f71cc 678
4cc4bfaf 679 @see SaveState()
23324ae1
FM
680 */
681 void RestoreState();
682
683 /**
684 This function saves the current state of the socket in a stack. Socket
685 state includes flags, as set with SetFlags(),
7c913512
FM
686 event mask, as set with SetNotify() and
687 Notify(), user data, as set with
23324ae1 688 SetClientData().
23324ae1 689 Calls to SaveState and RestoreState can be nested.
3c4f71cc 690
4cc4bfaf 691 @see RestoreState()
23324ae1
FM
692 */
693 void SaveState();
694
695 /**
696 Sets user-supplied client data for this socket. All socket events will
697 contain a pointer to this data, which can be retrieved with
698 the wxSocketEvent::GetClientData function.
699 */
4cc4bfaf 700 void SetClientData(void* data);
23324ae1
FM
701
702 /**
703 Sets an event handler to be called when a socket event occurs. The
704 handler will be called for those events for which notification is
7c913512 705 enabled with SetNotify() and
23324ae1 706 Notify().
3c4f71cc 707
7c913512 708 @param handler
4cc4bfaf 709 Specifies the event handler you want to use.
7c913512 710 @param id
4cc4bfaf 711 The id of socket event.
3c4f71cc 712
4cc4bfaf 713 @see SetNotify(), Notify(), wxSocketEvent, wxEvtHandler
23324ae1
FM
714 */
715 void SetEventHandler(wxEvtHandler& handler, int id = -1);
716
717 /**
718 Use SetFlags to customize IO operation for this socket.
4cc4bfaf 719 The @a flags parameter may be a combination of flags ORed together.
23324ae1 720 The following flags can be used:
3c4f71cc 721
23324ae1 722 @b wxSOCKET_NONE
3c4f71cc 723
23324ae1 724 Normal functionality.
3c4f71cc 725
23324ae1 726 @b wxSOCKET_NOWAIT
3c4f71cc 727
23324ae1 728 Read/write as much data as possible and return immediately.
3c4f71cc 729
23324ae1 730 @b wxSOCKET_WAITALL
3c4f71cc 731
23324ae1 732 Wait for all required data to be read/written unless an error occurs.
3c4f71cc 733
23324ae1 734 @b wxSOCKET_BLOCK
3c4f71cc 735
23324ae1 736 Block the GUI (do not yield) while reading/writing data.
3c4f71cc 737
23324ae1 738 @b wxSOCKET_REUSEADDR
3c4f71cc 739
23324ae1 740 Allows the use of an in-use port (wxServerSocket only)
3c4f71cc 741
23324ae1 742 @b wxSOCKET_BROADCAST
3c4f71cc 743
23324ae1 744 Switches the socket to broadcast mode
3c4f71cc 745
23324ae1 746 @b wxSOCKET_NOBIND
3c4f71cc 747
23324ae1
FM
748 Stops the socket from being bound to a specific adapter (normally used in
749 conjunction with @b wxSOCKET_BROADCAST)
3c4f71cc 750
23324ae1 751 A brief overview on how to use these flags follows.
23324ae1
FM
752 If no flag is specified (this is the same as @b wxSOCKET_NONE),
753 IO calls will return after some data has been read or written, even
754 when the transfer might not be complete. This is the same as issuing
755 exactly one blocking low-level call to recv() or send(). Note
756 that @e blocking here refers to when the function returns, not
757 to whether the GUI blocks during this time.
23324ae1
FM
758 If @b wxSOCKET_NOWAIT is specified, IO calls will return immediately.
759 Read operations will retrieve only available data. Write operations will
760 write as much data as possible, depending on how much space is available
761 in the output buffer. This is the same as issuing exactly one nonblocking
762 low-level call to recv() or send(). Note that @e nonblocking here
763 refers to when the function returns, not to whether the GUI blocks during
764 this time.
23324ae1
FM
765 If @b wxSOCKET_WAITALL is specified, IO calls won't return until ALL
766 the data has been read or written (or until an error occurs), blocking if
767 necessary, and issuing several low level calls if necessary. This is the
768 same as having a loop which makes as many blocking low-level calls to
769 recv() or send() as needed so as to transfer all the data. Note
770 that @e blocking here refers to when the function returns, not
771 to whether the GUI blocks during this time.
23324ae1
FM
772 The @b wxSOCKET_BLOCK flag controls whether the GUI blocks during
773 IO operations. If this flag is specified, the socket will not yield
774 during IO calls, so the GUI will remain blocked until the operation
775 completes. If it is not used, then the application must take extra
776 care to avoid unwanted reentrance.
23324ae1
FM
777 The @b wxSOCKET_REUSEADDR flag controls the use of the SO_REUSEADDR standard
778 setsockopt() flag. This flag allows the socket to bind to a port that is
779 already in use.
780 This is mostly used on UNIX-based systems to allow rapid starting and stopping
7c913512 781 of a server -
23324ae1
FM
782 otherwise you may have to wait several minutes for the port to become available.
783 wxSOCKET_REUSEADDR can also be used with socket clients to (re)bind to a
784 particular local port
785 for an outgoing connection.
786 This option can have surprising platform dependent behavior, so check the
787 documentation for
788 your platform's implementation of setsockopt(). Note that on BSD-based systems
789 (e.g. Mac OS X),
790 use of wxSOCKET_REUSEADDR implies SO_REUSEPORT in addition to SO_REUSEADDR to
791 be consistent
792 with Windows.
23324ae1
FM
793 The @b wxSOCKET_BROADCAST flag controls the use of the SO_BROADCAST standard
794 setsockopt() flag. This flag allows the socket to use the broadcast address,
795 and is generally
796 used in conjunction with @b wxSOCKET_NOBIND and wxIPaddress::BroadcastAddress.
23324ae1 797 So:
23324ae1 798 @b wxSOCKET_NONE will try to read at least SOME data, no matter how much.
23324ae1
FM
799 @b wxSOCKET_NOWAIT will always return immediately, even if it cannot
800 read or write ANY data.
23324ae1
FM
801 @b wxSOCKET_WAITALL will only return when it has read or written ALL
802 the data.
23324ae1
FM
803 @b wxSOCKET_BLOCK has nothing to do with the previous flags and
804 it controls whether the GUI blocks.
23324ae1
FM
805 @b wxSOCKET_REUSEADDR controls special platform-specific behavior for
806 reusing local addresses/ports.
807 */
808 void SetFlags(wxSocketFlags flags);
809
810 /**
811 This function allows you to set the local address and port,
812 useful when an application needs to reuse a particular port. When
813 a local port is set for a wxSocketClient,
814 @b bind will be called before @b connect.
815 */
816 bool SetLocal(wxIPV4address& local);
817
818 /**
819 SetNotify specifies which socket events are to be sent to the event handler.
4cc4bfaf 820 The @a flags parameter may be combination of flags ORed together. The
23324ae1 821 following flags can be used:
3c4f71cc 822
23324ae1 823 @b wxSOCKET_INPUT_FLAG
3c4f71cc 824
23324ae1 825 to receive wxSOCKET_INPUT
3c4f71cc 826
23324ae1 827 @b wxSOCKET_OUTPUT_FLAG
3c4f71cc 828
23324ae1 829 to receive wxSOCKET_OUTPUT
3c4f71cc 830
23324ae1 831 @b wxSOCKET_CONNECTION_FLAG
3c4f71cc 832
23324ae1 833 to receive wxSOCKET_CONNECTION
3c4f71cc 834
23324ae1 835 @b wxSOCKET_LOST_FLAG
3c4f71cc 836
23324ae1 837 to receive wxSOCKET_LOST
3c4f71cc 838
23324ae1 839 For example:
3c4f71cc 840
23324ae1
FM
841 In this example, the user will be notified about incoming socket data and
842 whenever the connection is closed.
23324ae1
FM
843 For more information on socket events see @ref overview_wxsocketbase "wxSocket
844 events".
845 */
846 void SetNotify(wxSocketEventFlags flags);
847
848 /**
849 This function sets the default socket timeout in seconds. This timeout
850 applies to all IO calls, and also to the Wait() family
851 of functions if you don't specify a wait interval. Initially, the default
852 timeout is 10 minutes.
853 */
854 void SetTimeout(int seconds);
855
856 /**
857 Functions to retrieve current state and miscellaneous info.
23324ae1 858 Error()
3c4f71cc 859
23324ae1 860 GetLocal()
3c4f71cc 861
23324ae1
FM
862 GetPeer()
863 IsConnected()
3c4f71cc 864
23324ae1 865 IsData()
3c4f71cc 866
23324ae1 867 IsDisconnected()
3c4f71cc 868
23324ae1 869 LastCount()
3c4f71cc 870
23324ae1 871 LastError()
3c4f71cc 872
23324ae1 873 IsOk()
3c4f71cc 874
23324ae1 875 SaveState()
3c4f71cc 876
23324ae1
FM
877 RestoreState()
878 */
879
880
881 /**
882 This function unreads a buffer. That is, the data in the buffer is put back
883 in the incoming queue. This function is not affected by wxSocket flags.
23324ae1 884 If you use LastCount(), it will always return @e nbytes.
23324ae1 885 If you use Error(), it will always return @false.
3c4f71cc 886
7c913512 887 @param buffer
4cc4bfaf 888 Buffer to be unread.
7c913512 889 @param nbytes
4cc4bfaf 890 Number of bytes.
3c4f71cc 891
d29a9a8a 892 @return Returns a reference to the current object.
3c4f71cc 893
4cc4bfaf 894 @see Error(), LastCount(), LastError()
23324ae1 895 */
4cc4bfaf 896 wxSocketBase Unread(const void* buffer, wxUint32 nbytes);
23324ae1
FM
897
898 /**
899 This function waits until any of the following conditions is @true:
3c4f71cc 900
23324ae1
FM
901 The socket becomes readable.
902 The socket becomes writable.
903 An ongoing connection request has completed (wxSocketClient only)
904 An incoming connection request has arrived (wxSocketServer only)
905 The connection has been closed.
23324ae1
FM
906 Note that it is recommended to use the individual Wait functions
907 to wait for the required condition, instead of this one.
3c4f71cc 908
7c913512 909 @param seconds
4cc4bfaf
FM
910 Number of seconds to wait.
911 If -1, it will wait for the default timeout,
912 as set with SetTimeout.
7c913512 913 @param millisecond
4cc4bfaf 914 Number of milliseconds to wait.
3c4f71cc 915
d29a9a8a 916 @return Returns @true when any of the above conditions is satisfied,
4cc4bfaf 917 @false if the timeout was reached.
3c4f71cc 918
4cc4bfaf
FM
919 @see InterruptWait(), wxSocketServer::WaitForAccept,
920 WaitForLost(), WaitForRead(),
921 WaitForWrite(), wxSocketClient::WaitOnConnect
23324ae1
FM
922 */
923 bool Wait(long seconds = -1, long millisecond = 0);
924
925 /**
926 This function waits until the connection is lost. This may happen if
927 the peer gracefully closes the connection or if the connection breaks.
3c4f71cc 928
7c913512 929 @param seconds
4cc4bfaf
FM
930 Number of seconds to wait.
931 If -1, it will wait for the default timeout,
932 as set with SetTimeout.
7c913512 933 @param millisecond
4cc4bfaf 934 Number of milliseconds to wait.
3c4f71cc 935
d29a9a8a 936 @return Returns @true if the connection was lost, @false if the timeout
4cc4bfaf 937 was reached.
3c4f71cc 938
4cc4bfaf 939 @see InterruptWait(), Wait()
23324ae1 940 */
fc377125 941 bool WaitForLost(long seconds = -1, long millisecond = 0);
23324ae1
FM
942
943 /**
944 This function waits until the socket is readable. This might mean that
945 queued data is available for reading or, for streamed sockets, that
946 the connection has been closed, so that a read operation will complete
947 immediately without blocking (unless the @b wxSOCKET_WAITALL flag
948 is set, in which case the operation might still block).
3c4f71cc 949
7c913512 950 @param seconds
4cc4bfaf
FM
951 Number of seconds to wait.
952 If -1, it will wait for the default timeout,
953 as set with SetTimeout.
7c913512 954 @param millisecond
4cc4bfaf 955 Number of milliseconds to wait.
3c4f71cc 956
d29a9a8a 957 @return Returns @true if the socket becomes readable, @false on timeout.
3c4f71cc 958
4cc4bfaf 959 @see InterruptWait(), Wait()
23324ae1
FM
960 */
961 bool WaitForRead(long seconds = -1, long millisecond = 0);
962
963 /**
964 This function waits until the socket becomes writable. This might mean that
965 the socket is ready to send new data, or for streamed sockets, that the
966 connection has been closed, so that a write operation is guaranteed to
967 complete immediately (unless the @b wxSOCKET_WAITALL flag is set,
968 in which case the operation might still block).
3c4f71cc 969
7c913512 970 @param seconds
4cc4bfaf
FM
971 Number of seconds to wait.
972 If -1, it will wait for the default timeout,
973 as set with SetTimeout.
7c913512 974 @param millisecond
4cc4bfaf 975 Number of milliseconds to wait.
3c4f71cc 976
d29a9a8a 977 @return Returns @true if the socket becomes writable, @false on timeout.
3c4f71cc 978
4cc4bfaf 979 @see InterruptWait(), Wait()
23324ae1
FM
980 */
981 bool WaitForWrite(long seconds = -1, long millisecond = 0);
982
983 /**
4cc4bfaf 984 This function writes a buffer of @a nbytes bytes to the socket.
23324ae1 985 Use LastCount() to verify the number of bytes actually written.
23324ae1 986 Use Error() to determine if the operation succeeded.
3c4f71cc 987
7c913512 988 @param buffer
4cc4bfaf 989 Buffer with the data to be sent.
7c913512 990 @param nbytes
4cc4bfaf 991 Number of bytes.
3c4f71cc 992
d29a9a8a 993 @return Returns a reference to the current object.
3c4f71cc 994
4cc4bfaf
FM
995 @see Error(), LastError(), LastCount(),
996 SetFlags()
23324ae1 997 */
4cc4bfaf 998 wxSocketBase Write(const void* buffer, wxUint32 nbytes);
23324ae1
FM
999
1000 /**
4cc4bfaf 1001 This function writes a buffer of @a nbytes bytes from the socket, but it
7c913512
FM
1002 writes a short header before so that ReadMsg()
1003 knows how much data should it actually read. So, a buffer sent with WriteMsg
23324ae1
FM
1004 @b must be read with ReadMsg. This function always waits for the entire
1005 buffer to be sent, unless an error occurs.
23324ae1 1006 Use LastCount() to verify the number of bytes actually written.
23324ae1 1007 Use Error() to determine if the operation succeeded.
3c4f71cc 1008
7c913512 1009 @param buffer
4cc4bfaf 1010 Buffer with the data to be sent.
7c913512 1011 @param nbytes
4cc4bfaf 1012 Number of bytes to send.
3c4f71cc 1013
d29a9a8a 1014 @return Returns a reference to the current object.
23324ae1 1015 */
4cc4bfaf 1016 wxSocketBase WriteMsg(const void* buffer, wxUint32 nbytes);
23324ae1
FM
1017};
1018
1019
e54c96f1 1020
23324ae1
FM
1021/**
1022 @class wxDatagramSocket
7c913512
FM
1023
1024
23324ae1
FM
1025 @library{wxnet}
1026 @category{FIXME}
7c913512 1027
e54c96f1 1028 @see wxSocketBase::Error, wxSocketBase::LastError, wxSocketBase::LastCount,
23324ae1
FM
1029 wxSocketBase::SetFlags,
1030*/
1031class wxDatagramSocket : public wxSocketBase
1032{
1033public:
1034 /**
1035 Constructor.
3c4f71cc 1036
7c913512 1037 @param flags
4cc4bfaf 1038 Socket flags (See wxSocketBase::SetFlags)
23324ae1
FM
1039 */
1040 wxDatagramSocket(wxSocketFlags flags = wxSOCKET_NONE);
1041
1042 /**
1043 Destructor. Please see wxSocketBase::Destroy.
1044 */
1045 ~wxDatagramSocket();
1046
1047 /**
4cc4bfaf 1048 This function reads a buffer of @a nbytes bytes from the socket.
23324ae1 1049 Use wxSocketBase::LastCount to verify the number of bytes actually read.
23324ae1 1050 Use wxSocketBase::Error to determine if the operation succeeded.
3c4f71cc 1051
7c913512 1052 @param address
4cc4bfaf
FM
1053 Any address - will be overwritten with the address of the peer that sent
1054 that data.
7c913512 1055 @param buffer
4cc4bfaf 1056 Buffer where to put read data.
7c913512 1057 @param nbytes
4cc4bfaf 1058 Number of bytes.
3c4f71cc 1059
d29a9a8a 1060 @return Returns a reference to the current object, and the address of
4cc4bfaf 1061 the peer that sent the data on address param.
3c4f71cc 1062
4cc4bfaf
FM
1063 @see wxSocketBase::Error, wxSocketBase::LastError, wxSocketBase::LastCount,
1064 wxSocketBase::SetFlags,
23324ae1
FM
1065 */
1066 wxDatagramSocket ReceiveFrom(wxSockAddress& address,
4cc4bfaf 1067 void* buffer,
23324ae1
FM
1068 wxUint32 nbytes);
1069
1070 /**
4cc4bfaf 1071 This function writes a buffer of @a nbytes bytes to the socket.
23324ae1 1072 Use wxSocketBase::LastCount to verify the number of bytes actually wrote.
23324ae1 1073 Use wxSocketBase::Error to determine if the operation succeeded.
3c4f71cc 1074
7c913512 1075 @param address
4cc4bfaf 1076 The address of the destination peer for this data.
7c913512 1077 @param buffer
4cc4bfaf 1078 Buffer where read data is.
7c913512 1079 @param nbytes
4cc4bfaf 1080 Number of bytes.
3c4f71cc 1081
d29a9a8a 1082 @return Returns a reference to the current object.
23324ae1
FM
1083 */
1084 wxDatagramSocket SendTo(const wxSockAddress& address,
4cc4bfaf 1085 const void* buffer,
23324ae1
FM
1086 wxUint32 nbytes);
1087};
e54c96f1 1088