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