]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/socket.h
Convert wxFSW_EVENT_{WARNING,ERROR} to string correctly.
[wxWidgets.git] / interface / wx / socket.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: socket.h
e725ba4f 3// Purpose: interface of wxIP*address, wxSocket* classes
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
526954c5 6// Licence: wxWindows licence
23324ae1
FM
7/////////////////////////////////////////////////////////////////////////////
8
ccf39540
FM
9
10/**
11 @class wxIPaddress
12
13 wxIPaddress is an abstract base class for all internet protocol address
14 objects. Currently, only wxIPV4address is implemented. An experimental
15 implementation for IPV6, wxIPV6address, is being developed.
16
f00f01b3 17 @library{wxnet}
ccf39540
FM
18 @category{net}
19*/
20class wxIPaddress : public wxSockAddress
21{
22public:
23 /**
24 Internally, this is the same as setting the IP address to @b INADDR_ANY.
25
26 On IPV4 implementations, 0.0.0.0
27
28 On IPV6 implementations, ::
29
30 @return @true on success, @false if something went wrong.
31 */
32 bool AnyAddress();
33
34 /**
35 Internally, this is the same as setting the IP address to @b INADDR_BROADCAST.
36
37 On IPV4 implementations, 255.255.255.255
38
39 @return @true on success, @false if something went wrong.
40 */
41 virtual bool BroadcastAddress() = 0;
42
43 /**
44 Set the address to hostname, which can be a host name or an IP-style address
45 in a format dependent on implementation.
46
47 @return @true on success, @false if something goes wrong (invalid
48 hostname or invalid IP address).
49 */
50 bool Hostname(const wxString& hostname);
51
52 /**
53 Returns the hostname which matches the IP address.
54 */
55 wxString Hostname() const;
56
57 /**
58 Returns a wxString containing the IP address.
59 */
60 virtual wxString IPAddress() const = 0;
61
62 /**
63 Determines if current address is set to localhost.
64
65 @return @true if address is localhost, @false if internet address.
66 */
67 virtual bool IsLocalHost() const = 0;
68
69 /**
70 Set address to localhost.
71
72 On IPV4 implementations, 127.0.0.1
73
74 On IPV6 implementations, ::1
75
76 @return @true on success, @false if something went wrong.
77 */
78 bool LocalHost();
79
80 /**
81 Set the port to that corresponding to the specified service.
82
83 @return @true on success, @false if something goes wrong (invalid @a service).
84 */
85 bool Service(const wxString& service);
86
87 /**
88 Set the port to that corresponding to the specified service.
89
90 @return @true on success, @false if something goes wrong (invalid @a service).
91 */
92 bool Service(unsigned short service);
93
94 /**
95 Returns the current service.
96 */
97 unsigned short Service() const;
98};
99
100
23324ae1
FM
101/**
102 @class wxIPV4address
7c913512 103
3d7548cb 104 A class for working with IPv4 network addresses.
7c913512 105
f00f01b3 106 @library{wxnet}
23324ae1
FM
107 @category{net}
108*/
109class wxIPV4address : public wxIPaddress
110{
111public:
112 /**
3d7548cb
BP
113 Set address to any of the addresses of the current machine.
114
115 Whenever possible, use this function instead of LocalHost(),
23324ae1
FM
116 as this correctly handles multi-homed hosts and avoids other small
117 problems. Internally, this is the same as setting the IP address
118 to @b INADDR_ANY.
3c4f71cc 119
3d7548cb 120 @return @true on success, @false if something went wrong.
23324ae1
FM
121 */
122 bool AnyAddress();
123
23324ae1 124 /**
3d7548cb 125 Set the address to hostname, which can be a host name or an IP-style address
e725ba4f 126 in dot notation(<tt>a.b.c.d</tt>).
3d7548cb 127
e725ba4f
FM
128 @return @true on success, @false if something goes wrong (invalid
129 hostname or invalid IP address).
23324ae1
FM
130 */
131 bool Hostname(const wxString& hostname);
3d7548cb
BP
132
133 /**
134 Returns the hostname which matches the IP address.
135 */
adaaa686 136 virtual wxString Hostname() const;
23324ae1
FM
137
138 /**
139 Returns a wxString containing the IP address in dot quad (127.0.0.1) format.
140 */
adaaa686 141 virtual wxString IPAddress() const;
23324ae1
FM
142
143 /**
3d7548cb
BP
144 Set address to localhost (127.0.0.1).
145
146 Whenever possible, use AnyAddress() instead of this one, as that one will
147 correctly handle multi-homed hosts and avoid other small problems.
148
149 @return @true on success, @false if something went wrong.
23324ae1
FM
150 */
151 bool LocalHost();
152
23324ae1 153 /**
3d7548cb
BP
154 Set the port to that corresponding to the specified @a service.
155
156 @return @true on success, @false if something goes wrong (invalid @a service).
23324ae1
FM
157 */
158 bool Service(const wxString& service);
3d7548cb
BP
159
160 /**
161 Set the port to that corresponding to the specified @a service.
162
163 @return @true on success, @false if something goes wrong (invalid @a service).
164 */
ccf39540 165 bool Service(unsigned short service);
3d7548cb
BP
166
167 /**
168 Returns the current service.
169 */
ccf39540 170 unsigned short Service() const;
23324ae1
FM
171};
172
173
e54c96f1 174
23324ae1
FM
175/**
176 @class wxSocketServer
7c913512 177
e725ba4f
FM
178 @todo describe me.
179
23324ae1
FM
180 @library{wxnet}
181 @category{net}
23324ae1
FM
182*/
183class wxSocketServer : public wxSocketBase
184{
185public:
186 /**
187 Constructs a new server and tries to bind to the specified @e address.
3d7548cb
BP
188
189 Before trying to accept new connections, remember to test whether it succeeded
190 with wxSocketBase:IsOk().
3c4f71cc 191
7c913512 192 @param address
4cc4bfaf 193 Specifies the local address for the server (e.g. port number).
7c913512 194 @param flags
e725ba4f 195 Socket flags (See wxSocketBase::SetFlags()).
23324ae1
FM
196 */
197 wxSocketServer(const wxSockAddress& address,
198 wxSocketFlags flags = wxSOCKET_NONE);
199
200 /**
201 Destructor (it doesn't close the accepted connections).
202 */
adaaa686 203 virtual ~wxSocketServer();
23324ae1
FM
204
205 /**
e725ba4f
FM
206 Accepts an incoming connection request, and creates a new wxSocketBase
207 object which represents the server-side of the connection.
3d7548cb 208
4cc4bfaf 209 If @a wait is @true and there are no pending connections to be
23324ae1 210 accepted, it will wait for the next incoming connection to
e725ba4f
FM
211 arrive.
212
488addd5 213 @warning This method will block the GUI.
3d7548cb 214
4cc4bfaf 215 If @a wait is @false, it will try to accept a pending connection
23324ae1 216 if there is one, but it will always return immediately without blocking
3d7548cb
BP
217 the GUI. If you want to use Accept() in this way, you can either check for
218 incoming connections with WaitForAccept() or catch @b wxSOCKET_CONNECTION events,
219 then call Accept() once you know that there is an incoming connection waiting
220 to be accepted.
3c4f71cc 221
d29a9a8a 222 @return Returns an opened socket connection, or @NULL if an error
3d7548cb
BP
223 occurred or if the wait parameter was @false and there
224 were no pending connections.
3c4f71cc 225
3d7548cb
BP
226 @see WaitForAccept(), wxSocketBase::SetNotify(),
227 wxSocketBase::Notify(), AcceptWith()
23324ae1 228 */
4cc4bfaf 229 wxSocketBase* Accept(bool wait = true);
23324ae1
FM
230
231 /**
232 Accept an incoming connection using the specified socket object.
3c4f71cc 233
7c913512 234 @param socket
4cc4bfaf 235 Socket to be initialized
e725ba4f
FM
236 @param wait
237 See Accept() for more info.
3c4f71cc 238
e725ba4f
FM
239 @return Returns @true on success, or @false if an error occurred or
240 if the wait parameter was @false and there were no pending
241 connections.
3d7548cb
BP
242
243 @see WaitForAccept(), wxSocketBase::SetNotify(),
244 wxSocketBase::Notify(), Accept()
23324ae1 245 */
4cc4bfaf 246 bool AcceptWith(wxSocketBase& socket, bool wait = true);
23324ae1
FM
247
248 /**
9940bebf 249 Wait for an incoming connection.
e725ba4f
FM
250
251 Use it if you want to call Accept() or AcceptWith() with @e wait set
252 to @false, to detect when an incoming connection is waiting to be accepted.
3c4f71cc 253
7c913512 254 @param seconds
3d7548cb
BP
255 Number of seconds to wait. If -1, it will wait for the default
256 timeout, as set with wxSocketBase::SetTimeout().
7c913512 257 @param millisecond
4cc4bfaf 258 Number of milliseconds to wait.
3c4f71cc 259
3d7548cb
BP
260 @return @true if an incoming connection arrived, @false if the timeout
261 elapsed.
262
263 @see Accept(), AcceptWith(), wxSocketBase::InterruptWait()
23324ae1
FM
264 */
265 bool WaitForAccept(long seconds = -1, long millisecond = 0);
266};
267
268
23324ae1
FM
269/**
270 @class wxSocketClient
7c913512 271
e725ba4f
FM
272 @todo describe me.
273
23324ae1
FM
274 @library{wxnet}
275 @category{net}
23324ae1
FM
276*/
277class wxSocketClient : public wxSocketBase
278{
279public:
280 /**
281 Constructor.
3c4f71cc 282
7c913512 283 @param flags
3d7548cb 284 Socket flags (See wxSocketBase::SetFlags())
23324ae1
FM
285 */
286 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
287
288 /**
3d7548cb 289 Destructor. Please see wxSocketBase::Destroy().
23324ae1 290 */
adaaa686 291 virtual ~wxSocketClient();
23324ae1 292
23324ae1
FM
293 /**
294 Connects to a server using the specified address.
3d7548cb
BP
295
296 If @a wait is @true, Connect() will wait until the connection
e725ba4f
FM
297 completes.
298
488addd5 299 @warning This method will block the GUI.
3d7548cb
BP
300
301 If @a wait is @false, Connect() will try to establish the connection
302 and return immediately, without blocking the GUI. When used this way,
303 even if Connect() returns @false, the connection request can be
304 completed later. To detect this, use WaitOnConnect(), or catch
305 @b wxSOCKET_CONNECTION events (for successful establishment) and
306 @b wxSOCKET_LOST events (for connection failure).
307
308 @param address
309 Address of the server.
310 @param wait
311 If @true, waits for the connection to complete.
312
313 @return @true if the connection is established and no error occurs.
e725ba4f
FM
314 If @a wait was true, and Connect() returns @false, an error
315 occurred and the connection failed.
316 If @a wait was @false, and Connect() returns @false, you should
317 still be prepared to handle the completion of this connection request,
318 either with WaitOnConnect() or by watching wxSOCKET_CONNECTION
319 and wxSOCKET_LOST events.
3d7548cb
BP
320
321 @see WaitOnConnect(), wxSocketBase::SetNotify(), wxSocketBase::Notify()
322 */
adaaa686 323 virtual bool Connect(const wxSockAddress& address, bool wait = true);
3d7548cb
BP
324
325 /**
326 Connects to a server using the specified address.
327
328 If @a wait is @true, Connect() will wait until the connection
329 completes. @b Warning: This will block the GUI.
330
331 If @a wait is @false, Connect() will try to establish the connection
332 and return immediately, without blocking the GUI. When used this way,
333 even if Connect() returns @false, the connection request can be
334 completed later. To detect this, use WaitOnConnect(), or catch
335 @b wxSOCKET_CONNECTION events (for successful establishment) and
336 @b wxSOCKET_LOST events (for connection failure).
3c4f71cc 337
7c913512 338 @param address
4cc4bfaf 339 Address of the server.
7c913512 340 @param local
4cc4bfaf 341 Bind to the specified local address and port before connecting.
3d7548cb
BP
342 The local address and port can also be set using SetLocal(),
343 and then using the 2-parameter Connect() method.
7c913512 344 @param wait
4cc4bfaf 345 If @true, waits for the connection to complete.
3c4f71cc 346
3d7548cb 347 @return @true if the connection is established and no error occurs.
e725ba4f
FM
348 If @a wait was true, and Connect() returns @false, an error
349 occurred and the connection failed.
350 If @a wait was @false, and Connect() returns @false, you should
351 still be prepared to handle the completion of this connection request,
352 either with WaitOnConnect() or by watching wxSOCKET_CONNECTION
353 and wxSOCKET_LOST events.
3c4f71cc 354
3d7548cb 355 @see WaitOnConnect(), wxSocketBase::SetNotify(), wxSocketBase::Notify()
23324ae1 356 */
72ac4e88 357 bool Connect(const wxSockAddress& address, const wxSockAddress& local,
4cc4bfaf 358 bool wait = true);
23324ae1
FM
359
360 /**
361 Wait until a connection request completes, or until the specified timeout
e725ba4f
FM
362 elapses. Use this function after issuing a call to Connect() with
363 @e wait set to @false.
3c4f71cc 364
7c913512 365 @param seconds
4cc4bfaf 366 Number of seconds to wait.
e725ba4f
FM
367 If -1, it will wait for the default timeout, as set with wxSocketBase::SetTimeout().
368 @param milliseconds
4cc4bfaf 369 Number of milliseconds to wait.
3c4f71cc 370
e725ba4f
FM
371 @return
372 WaitOnConnect() returns @true if the connection request completes.
373 This does not necessarily mean that the connection was
374 successfully established; it might also happen that the
375 connection was refused by the peer. Use wxSocketBase::IsConnected()
376 to distinguish between these two situations.
377 @n @n If the timeout elapses, WaitOnConnect() returns @false.
378 @n @n These semantics allow code like this:
379 @code
380 // Issue the connection request
381 client->Connect(addr, false);
382
383 // Wait until the request completes or until we decide to give up
384 bool waitmore = true;
385 while ( !client->WaitOnConnect(seconds, millis) && waitmore )
386 {
387 // possibly give some feedback to the user,
388 // and update waitmore as needed.
389 }
390 bool success = client->IsConnected();
391 @endcode
23324ae1
FM
392 */
393 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
394};
395
396
e54c96f1 397
23324ae1
FM
398/**
399 @class wxSockAddress
7c913512 400
23324ae1 401 You are unlikely to need to use this class: only wxSocketBase uses it.
7c913512 402
f00f01b3 403 @library{wxnet}
3d7548cb 404 @category{net}
7c913512 405
e54c96f1 406 @see wxSocketBase, wxIPaddress, wxIPV4address
23324ae1
FM
407*/
408class wxSockAddress : public wxObject
409{
410public:
411 /**
412 Default constructor.
413 */
414 wxSockAddress();
415
416 /**
417 Default destructor.
418 */
adaaa686 419 virtual ~wxSockAddress();
23324ae1
FM
420
421 /**
422 Delete all informations about the address.
423 */
adaaa686 424 virtual void Clear();
23324ae1
FM
425
426 /**
427 Returns the length of the socket address.
428 */
429 int SockAddrLen();
5fab0c8d
VZ
430
431 /**
432 Returns the pointer to the low-level representation of the address.
433
434 This can be used to pass socket address information to a 3rd party
435 library.
436
437 @return
438 Pointer to a sockaddr-derived struct.
439 */
440 const sockaddr *GetAddressData() const;
441
442 /**
443 Returns the length of the buffer retrieved by GetAddressData().
444
445 @return
446 The size of the sockaddr-derived struct corresponding to this
447 address.
448 */
449 int GetAddressDataLen() const;
23324ae1
FM
450};
451
452
e54c96f1 453
23324ae1
FM
454/**
455 @class wxSocketEvent
7c913512 456
23324ae1 457 This event class contains information about socket events.
3051a44a
FM
458 This kind of events are sent to the event handler specified with
459 wxSocketBase::SetEventHandler.
7c913512 460
3d7548cb
BP
461 @beginEventTable{wxSocketEvent}
462 @event{EVT_SOCKET(id, func)}
3051a44a 463 Process a socket event, supplying the member function.
3d7548cb
BP
464 @endEventTable
465
23324ae1
FM
466 @library{wxnet}
467 @category{net}
7c913512 468
e54c96f1 469 @see wxSocketBase, wxSocketClient, wxSocketServer
23324ae1
FM
470*/
471class wxSocketEvent : public wxEvent
472{
473public:
474 /**
475 Constructor.
476 */
477 wxSocketEvent(int id = 0);
478
479 /**
480 Gets the client data of the socket which generated this event, as
3d7548cb 481 set with wxSocketBase::SetClientData().
23324ae1 482 */
adaaa686 483 void* GetClientData() const;
23324ae1
FM
484
485 /**
e725ba4f
FM
486 Returns the socket object to which this event refers to.
487 This makes it possible to use the same event handler for different sockets.
23324ae1 488 */
328f5751 489 wxSocketBase* GetSocket() const;
23324ae1
FM
490
491 /**
492 Returns the socket event type.
493 */
328f5751 494 wxSocketNotify GetSocketEvent() const;
23324ae1
FM
495};
496
497
3d7548cb
BP
498/**
499 wxSocket error return values.
500*/
501enum wxSocketError
502{
503 wxSOCKET_NOERROR, ///< No error happened.
504 wxSOCKET_INVOP, ///< Invalid operation.
505 wxSOCKET_IOERR, ///< Input/Output error.
506 wxSOCKET_INVADDR, ///< Invalid address passed to wxSocket.
507 wxSOCKET_INVSOCK, ///< Invalid socket (uninitialized).
508 wxSOCKET_NOHOST, ///< No corresponding host.
509 wxSOCKET_INVPORT, ///< Invalid port.
510 wxSOCKET_WOULDBLOCK, ///< The socket is non-blocking and the operation would block.
511 wxSOCKET_TIMEDOUT, ///< The timeout for this operation expired.
512 wxSOCKET_MEMERR ///< Memory exhausted.
513};
514
515
516/**
e725ba4f
FM
517 @anchor wxSocketEventFlags
518
3d7548cb
BP
519 wxSocket Event Flags.
520
521 A brief note on how to use these events:
522
523 The @b wxSOCKET_INPUT event will be issued whenever there is data available
524 for reading. This will be the case if the input queue was empty and new data
525 arrives, or if the application has read some data yet there is still more data
526 available. This means that the application does not need to read all available
527 data in response to a @b wxSOCKET_INPUT event, as more events will be produced
528 as necessary.
529
530 The @b wxSOCKET_OUTPUT event is issued when a socket is first connected with
531 Connect() or accepted with Accept(). After that, new events will be generated
532 only after an output operation fails with @b wxSOCKET_WOULDBLOCK and buffer space
533 becomes available again. This means that the application should assume that it can
534 write data to the socket until an @b wxSOCKET_WOULDBLOCK error occurs; after this,
535 whenever the socket becomes writable again the application will be notified with
536 another @b wxSOCKET_OUTPUT event.
537
538 The @b wxSOCKET_CONNECTION event is issued when a delayed connection request completes
539 successfully (client) or when a new connection arrives at the incoming queue (server).
540
541 The @b wxSOCKET_LOST event is issued when a close indication is received for the socket.
542 This means that the connection broke down or that it was closed by the peer. Also, this
543 event will be issued if a connection request fails.
544*/
545enum wxSocketEventFlags
546{
547 wxSOCKET_INPUT, ///< There is data available for reading.
548 wxSOCKET_OUTPUT, ///< The socket is ready to be written to.
549 wxSOCKET_CONNECTION, ///< Incoming connection request (server), or
550 ///< successful connection establishment (client).
551 wxSOCKET_LOST ///< The connection has been closed.
552};
553
554
555/**
556 @anchor wxSocketFlags
557
558 wxSocket Flags.
559
560 A brief overview on how to use these flags follows.
561
562 If no flag is specified (this is the same as @b wxSOCKET_NONE),
563 IO calls will return after some data has been read or written, even
564 when the transfer might not be complete. This is the same as issuing
565 exactly one blocking low-level call to @b recv() or @b send(). Note
566 that @e blocking here refers to when the function returns, not
567 to whether the GUI blocks during this time.
568
569 If @b wxSOCKET_NOWAIT is specified, IO calls will return immediately.
570 Read operations will retrieve only available data. Write operations will
571 write as much data as possible, depending on how much space is available
572 in the output buffer. This is the same as issuing exactly one nonblocking
573 low-level call to @b recv() or @b send(). Note that @e nonblocking here
574 refers to when the function returns, not to whether the GUI blocks during
294a09aa
VZ
575 this time. Also note that this flag impacts both Read and Write
576 operations. If it is desired to control Read independently of Write, for
577 example you want no wait on Read(), but you do want to wait on Write(), then
578 use wxSOCKET_NOWAIT_READ and wxSOCKET_NOWAIT_WRITE.
579
580 If @b wxSOCKET_NOWAIT_READ (this flag is new since wxWidgets 2.9.5) is
581 specified, Read operations will return immediately. Read operations will
582 retrieve only available data. This is the same as issuing exactly one
583 nonblocking low-level call to @b recv(). Note that @e nonblocking here
584 refers to when the function returns, not to whether the GUI blocks during
585 this time. This flag should not be enabled if ReadMsg() is going to be
586 used (it will be ignored), if you do then thread-safety may be at risk.
587 Note that wxSOCKET_NOWAIT_READ impacts only Read operations and does not
588 impact Write operations, allowing Read and Write operations to be set
589 differently.
590
591 If @b wxSOCKET_NOWAIT_WRITE (this flag is new since wxWidgets 2.9.5) is
592 specified, Write operations will return immediately. Write operations will
593 write as much data as possible, depending on how much space is available in
594 the output buffer. This is the same as issuing exactly one nonblocking
595 low-level call to @b send(). Note that @e nonblocking here refers to when
596 the function returns, not to whether the GUI blocks during this time. This
597 flag should not be enabled if WriteMsg() is going to be used (it will be
598 ignored), if you use it then thread safety may be at risk. Note that
599 wxSOCKET_NOWAIT_WRITE impacts only Write operations and does not impact
600 Write operations, allowing Read and Write operations to be set differently.
3d7548cb
BP
601
602 If @b wxSOCKET_WAITALL is specified, IO calls won't return until ALL
603 the data has been read or written (or until an error occurs), blocking if
604 necessary, and issuing several low level calls if necessary. This is the
605 same as having a loop which makes as many blocking low-level calls to
606 @b recv() or @b send() as needed so as to transfer all the data. Note
607 that @e blocking here refers to when the function returns, not
294a09aa
VZ
608 to whether the GUI blocks during this time. Note that wxSOCKET_WAITALL
609 impacts both Read and Write operations. If you desire to wait
610 for all on just Read operations, but not on Write operations, (or vice versa),
611 use wxSOCKET_WAITALL_READ or wxSOCKET_WAITALL_WRITE.
612
613 If @b wxSOCKET_WAITALL_READ (this flag is new since wxWidgets 2.9.5) is
614 specified, Read operations won't return until ALL the data has been read
615 (or until an error occurs), blocking if necessary, and issuing several low
616 level calls if necessary. This is the same as having a loop which makes as
617 many blocking low-level calls to @b recv() as needed so as to transfer all
618 the data. Note that @e blocking here refers to when the function returns,
619 not to whether the GUI blocks during this time. Note that
620 wxSOCKET_WAITALL_READ only has an impact on Read operations, and has no
621 impact on Write operations, allowing Read and Write operations to have
622 different settings.
623
624 If @b wxSOCKET_WAITALL_WRITE (this flag is new since wxWidgets 2.9.5) is
625 specified, Write() and WriteMsg() calls won't return until ALL the data has
626 been written (or until an error occurs), blocking if necessary, and issuing
627 several low level calls if necessary. This is the same as having a loop
628 which makes as many blocking low-level calls to @b send() as needed so as
629 to transfer all the data. Note that @e blocking here refers to when the
630 function returns, not to whether the GUI blocks during this time. Note
631 that wxSOCKET_WAITALL_WRITE only has an impact on Write operations, and has
632 no impact on Read operations, allowing Read and Write operations to have
633 different settings.
3d7548cb
BP
634
635 The @b wxSOCKET_BLOCK flag controls whether the GUI blocks during
636 IO operations. If this flag is specified, the socket will not yield
637 during IO calls, so the GUI will remain blocked until the operation
638 completes. If it is not used, then the application must take extra
639 care to avoid unwanted reentrance.
640
641 The @b wxSOCKET_REUSEADDR flag controls the use of the @b SO_REUSEADDR standard
642 @b setsockopt() flag. This flag allows the socket to bind to a port that is
643 already in use. This is mostly used on UNIX-based systems to allow rapid starting
644 and stopping of a server, otherwise you may have to wait several minutes for the
645 port to become available.
646
647 @b wxSOCKET_REUSEADDR can also be used with socket clients to (re)bind to a
648 particular local port for an outgoing connection.
4c51a665 649 This option can have surprising platform dependent behaviour, so check the
3d7548cb
BP
650 documentation for your platform's implementation of setsockopt().
651
652 Note that on BSD-based systems(e.g. Mac OS X), use of
653 @b wxSOCKET_REUSEADDR implies @b SO_REUSEPORT in addition to
654 @b SO_REUSEADDR to be consistent with Windows.
655
656 The @b wxSOCKET_BROADCAST flag controls the use of the @b SO_BROADCAST standard
657 @b setsockopt() flag. This flag allows the socket to use the broadcast address,
658 and is generally used in conjunction with @b wxSOCKET_NOBIND and
659 wxIPaddress::BroadcastAddress().
660
661 So:
e725ba4f
FM
662 - @b wxSOCKET_NONE will try to read at least SOME data, no matter how much.
663 - @b wxSOCKET_NOWAIT will always return immediately, even if it cannot
664 read or write ANY data.
665 - @b wxSOCKET_WAITALL will only return when it has read or written ALL
666 the data.
667 - @b wxSOCKET_BLOCK has nothing to do with the previous flags and
668 it controls whether the GUI blocks.
4c51a665 669 - @b wxSOCKET_REUSEADDR controls special platform-specific behaviour for
e725ba4f 670 reusing local addresses/ports.
3d7548cb
BP
671*/
672enum
673{
674 wxSOCKET_NONE = 0, ///< Normal functionality.
675 wxSOCKET_NOWAIT = 1, ///< Read/write as much data as possible and return immediately.
676 wxSOCKET_WAITALL = 2, ///< Wait for all required data to be read/written unless an error occurs.
677 wxSOCKET_BLOCK = 4, ///< Block the GUI (do not yield) while reading/writing data.
3bab636d 678 wxSOCKET_REUSEADDR = 8, ///< Allows the use of an in-use port.
3d7548cb 679 wxSOCKET_BROADCAST = 16, ///< Switches the socket to broadcast mode
294a09aa 680 wxSOCKET_NOBIND = 32, ///< Stops the socket from being bound to a specific
3d7548cb
BP
681 ///< adapter (normally used in conjunction with
682 ///< @b wxSOCKET_BROADCAST)
294a09aa
VZ
683 wxSOCKET_NOWAIT_READ = 64, ///< Read as much data as possible and return immediately
684 wxSOCKET_WAITALL_READ = 128, ///< Wait for all required data to be read unless an error occurs.
685 wxSOCKET_NOWAIT_WRITE = 256, ///< Write as much data as possible and return immediately
686 wxSOCKET_WAITALL_WRITE = 512 ///< Wait for all required data to be written unless an error occurs.
3d7548cb
BP
687};
688
e54c96f1 689
23324ae1
FM
690/**
691 @class wxSocketBase
7c913512 692
23324ae1
FM
693 wxSocketBase is the base class for all socket-related objects, and it
694 defines all basic IO functionality.
7c913512 695
e725ba4f 696 @note
4cb591b9
VZ
697 When using wxSocket from multiple threads, even implicitly (e.g. by using
698 wxFTP or wxHTTP in another thread) you must initialize the sockets from the
699 main thread by calling Initialize() before creating the other ones.
7c913512 700
3051a44a 701 @beginEventEmissionTable{wxSocketEvent}
3d7548cb
BP
702 @event{EVT_SOCKET(id, func)}
703 Process a @c wxEVT_SOCKET event.
e725ba4f 704 See @ref wxSocketEventFlags and @ref wxSocketFlags for more info.
3d7548cb
BP
705 @endEventTable
706
e725ba4f
FM
707 @library{wxnet}
708 @category{net}
709
3d7548cb
BP
710 @see wxSocketEvent, wxSocketClient, wxSocketServer, @sample{sockets},
711 @ref wxSocketFlags, ::wxSocketEventFlags, ::wxSocketError
23324ae1
FM
712*/
713class wxSocketBase : public wxObject
714{
715public:
23324ae1
FM
716
717 /**
3d7548cb 718 @name Construction and Destruction
23324ae1 719 */
3d7548cb 720 //@{
23324ae1
FM
721
722 /**
3d7548cb 723 Default constructor.
3c4f71cc 724
3d7548cb
BP
725 Don't use it directly; instead, use wxSocketClient to construct a socket client,
726 or wxSocketServer to construct a socket server.
23324ae1 727 */
3d7548cb 728 wxSocketBase();
23324ae1
FM
729
730 /**
3d7548cb
BP
731 Destructor.
732
733 Do not destroy a socket using the delete operator directly;
734 use Destroy() instead. Also, do not create socket objects in the stack.
23324ae1 735 */
382f12e4 736 virtual ~wxSocketBase();
23324ae1
FM
737
738 /**
3d7548cb 739 Destroys the socket safely.
3c4f71cc 740
3d7548cb
BP
741 Use this function instead of the delete operator, since otherwise socket events
742 could reach the application even after the socket has been destroyed. To prevent
743 this problem, this function appends the wxSocket to a list of object to be deleted
744 on idle time, after all events have been processed. For the same reason, you should
745 avoid creating socket objects in the stack.
3c4f71cc 746
3d7548cb 747 Destroy() calls Close() automatically.
3c4f71cc 748
d29a9a8a 749 @return Always @true.
23324ae1
FM
750 */
751 bool Destroy();
752
4cb591b9
VZ
753 /**
754 Perform the initialization needed in order to use the sockets.
755
756 This function is called from wxSocket constructor implicitly and so
757 normally doesn't need to be called explicitly. There is however one
758 important exception: as this function must be called from the main
759 (UI) thread, if you use wxSocket from multiple threads you must call
760 Initialize() from the main thread before creating wxSocket objects in
761 the other ones.
762
763 It is safe to call this function multiple times (only the first call
764 does anything) but you must call Shutdown() exactly once for every call
765 to Initialize().
766
4017f5ca
VZ
767 This function should only be called from the main thread.
768
4cb591b9
VZ
769 @return
770 @true if the sockets can be used, @false if the initialization
771 failed and sockets are not available at all.
772 */
773 static bool Initialize();
774
775 /**
776 Shut down the sockets.
777
778 This function undoes the call to Initialize() and must be called after
779 every successful call to Initialize().
4017f5ca
VZ
780
781 This function should only be called from the main thread, just as
782 Initialize().
4cb591b9
VZ
783 */
784 static void Shutdown();
785
3d7548cb
BP
786 //@}
787
788
23324ae1 789 /**
3d7548cb 790 @name Socket State
23324ae1 791 */
3d7548cb 792 //@{
23324ae1
FM
793
794 /**
795 Returns @true if an error occurred in the last IO operation.
c9157492 796
3d7548cb
BP
797 Use this function to check for an error condition after one of the
798 following calls: Discard(), Peek(), Read(), ReadMsg(), Unread(), Write(), WriteMsg().
23324ae1 799 */
328f5751 800 bool Error() const;
23324ae1 801
23324ae1 802 /**
9940bebf 803 Return the local address of the socket.
3c4f71cc 804
d29a9a8a 805 @return @true if no error happened, @false otherwise.
23324ae1 806 */
382f12e4 807 virtual bool GetLocal(wxSockAddress& addr) const;
23324ae1
FM
808
809 /**
9940bebf 810 Return the peer address field of the socket.
3c4f71cc 811
d29a9a8a 812 @return @true if no error happened, @false otherwise.
23324ae1 813 */
382f12e4 814 virtual bool GetPeer(wxSockAddress& addr) const;
23324ae1 815
2d46f281
VZ
816 /**
817 Return the socket timeout in seconds.
818
819 The timeout can be set using SetTimeout() and is 10 minutes by default.
820 */
821 long GetTimeout() const;
822
23324ae1
FM
823 /**
824 Returns @true if the socket is connected.
825 */
328f5751 826 bool IsConnected() const;
23324ae1 827
c9157492 828 /**
9940bebf 829 Check if the socket can be currently read or written.
c9157492 830
3d7548cb
BP
831 This might mean that queued data is available for reading or, for streamed
832 sockets, that the connection has been closed, so that a read operation will
833 complete immediately without blocking (unless the @b wxSOCKET_WAITALL flag
23324ae1
FM
834 is set, in which case the operation might still block).
835 */
382f12e4 836 bool IsData();
23324ae1
FM
837
838 /**
839 Returns @true if the socket is not connected.
840 */
328f5751 841 bool IsDisconnected() const;
23324ae1
FM
842
843 /**
844 Returns @true if the socket is initialized and ready and @false in other
845 cases.
3d7548cb
BP
846
847 @remarks
3d7548cb 848 For wxSocketClient, IsOk() won't return @true unless the client is connected to a server.
3d7548cb
BP
849 For wxSocketServer, IsOk() will return @true if the server could bind to the specified address
850 and is already listening for new connections.
3d7548cb 851 IsOk() does not check for IO errors; use Error() instead for that purpose.
23324ae1 852 */
328f5751 853 bool IsOk() const;
23324ae1
FM
854
855 /**
856 Returns the number of bytes read or written by the last IO call.
3d7548cb 857
23324ae1 858 Use this function to get the number of bytes actually transferred
3d7548cb
BP
859 after using one of the following IO calls: Discard(), Peek(), Read(),
860 ReadMsg(), Unread(), Write(), WriteMsg().
294a09aa
VZ
861
862 @deprecated
863 This function is kept mostly for backwards compatibility. Use
864 LastReadCount() or LastWriteCount() instead. LastCount() is still
865 needed for use with less commonly used functions: Discard(),
866 Peek(), and Unread().
23324ae1 867 */
328f5751 868 wxUint32 LastCount() const;
23324ae1 869
294a09aa
VZ
870 /**
871 Returns the number of bytes read by the last Read() or ReadMsg()
872 call (receive direction only).
873
874 This function is thread-safe, in case Read() is executed in a
875 different thread than Write(). Use LastReadCount() instead of
876 LastCount() for this reason.
877
878 Unlike LastCount(), the functions Discard(), Peek(), and Unread()
879 are currently not supported by LastReadCount().
880
881 @since 2.9.5
882 */
883 wxUint32 LastReadCount() const;
884
885 /**
886 Returns the number of bytes written by the last Write() or WriteMsg()
887 call (transmit direction only).
888
889 This function is thread-safe, in case Write() is executed in a
890 different thread than Read(). Use LastWriteCount() instead of
891 LastCount() for this reason.
892
893 @since 2.9.5
894 */
895 wxUint32 LastWriteCount() const;
896
23324ae1 897 /**
3d7548cb
BP
898 Returns the last wxSocket error. See @ref wxSocketError .
899
900 @note
3d7548cb 901 This function merely returns the last error code,
23324ae1
FM
902 but it should not be used to determine if an error has occurred (this
903 is because successful operations do not change the LastError value).
3d7548cb
BP
904 Use Error() first, in order to determine if the last IO call failed.
905 If this returns @true, use LastError() to discover the cause of the error.
23324ae1 906 */
328f5751 907 wxSocketError LastError() const;
23324ae1
FM
908
909 /**
9940bebf 910 Restore the previous state of the socket, as saved with SaveState().
3d7548cb
BP
911
912 Calls to SaveState() and RestoreState() can be nested.
913
914 @see SaveState()
23324ae1 915 */
3d7548cb
BP
916 void RestoreState();
917
918 /**
9940bebf
VZ
919 Save the current state of the socket in a stack.
920
e725ba4f
FM
921 Socket state includes flags, as set with SetFlags(), event mask, as set
922 with SetNotify() and Notify(), user data, as set with SetClientData().
3d7548cb
BP
923 Calls to SaveState and RestoreState can be nested.
924
925 @see RestoreState()
926 */
927 void SaveState();
928
929 //@}
930
931
932 /**
933 @name Basic I/O
934
935 See also: wxSocketServer::WaitForAccept(), wxSocketClient::WaitOnConnect()
936 */
937 //@{
938
939 /**
9940bebf
VZ
940 Shut down the socket, disabling further transmission and reception of
941 data and disable events for the socket and frees the associated system
942 resources.
3d7548cb
BP
943
944 Upon socket destruction, Close() is automatically called, so in most cases
945 you won't need to do it yourself, unless you explicitly want to shut down
946 the socket, typically to notify the peer that you are closing the connection.
947
948 @remarks
3d7548cb
BP
949 Although Close() immediately disables events for the socket, it is possible
950 that event messages may be waiting in the application's event queue.
951 The application must therefore be prepared to handle socket event messages even
952 after calling Close().
953 */
382f12e4 954 virtual bool Close();
3d7548cb 955
b67397a7
VZ
956 /**
957 Shuts down the writing end of the socket.
958
959 This function simply calls the standard shutdown() function on the
960 underlying socket, indicating that nothing will be written to this
961 socket any more.
962 */
963 void ShutdownOutput();
964
3d7548cb 965 /**
9940bebf
VZ
966 Delete all bytes in the incoming queue.
967
968 This function always returns immediately and its operation is not
969 affected by IO flags.
3d7548cb
BP
970
971 Use LastCount() to verify the number of bytes actually discarded.
972
973 If you use Error(), it will always return @false.
974 */
9940bebf 975 wxSocketBase& Discard();
3d7548cb
BP
976
977 /**
978 Returns current IO flags, as set with SetFlags()
979 */
980 wxSocketFlags GetFlags() const;
981
982 /**
983 Use this function to interrupt any wait operation currently in progress.
984
985 Note that this is not intended as a regular way to interrupt a Wait call,
986 but only as an escape mechanism for exceptional situations where it is
987 absolutely necessary to use it, for example to abort an operation due to
988 some exception or abnormal problem. InterruptWait is automatically called
989 when you Close() a socket (and thus also upon
990 socket destruction), so you don't need to use it in these cases.
991
992 @see Wait(), WaitForLost(), WaitForRead(), WaitForWrite(),
e725ba4f 993 wxSocketServer::WaitForAccept(), wxSocketClient::WaitOnConnect()
3d7548cb
BP
994 */
995 void InterruptWait();
23324ae1
FM
996
997 /**
9940bebf
VZ
998 Peek into the socket by copying the next bytes which would be read by
999 Read() into the provided buffer.
3d7548cb 1000
9940bebf
VZ
1001 Peeking a buffer doesn't delete it from the socket input queue, i.e.
1002 calling Read() will return the same data.
3d7548cb 1003
23324ae1 1004 Use LastCount() to verify the number of bytes actually peeked.
3d7548cb 1005
23324ae1 1006 Use Error() to determine if the operation succeeded.
3c4f71cc 1007
7c913512 1008 @param buffer
4cc4bfaf 1009 Buffer where to put peeked data.
7c913512 1010 @param nbytes
4cc4bfaf 1011 Number of bytes.
3c4f71cc 1012
d29a9a8a 1013 @return Returns a reference to the current object.
3c4f71cc 1014
3d7548cb 1015 @remarks
e725ba4f
FM
1016 The exact behaviour of Peek() depends on the combination of flags being used.
1017 For a detailed explanation, see SetFlags()
3d7548cb
BP
1018
1019 @see Error(), LastError(), LastCount(), SetFlags()
23324ae1 1020 */
9940bebf 1021 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
23324ae1
FM
1022
1023 /**
9940bebf
VZ
1024 Read up to the given number of bytes from the socket.
1025
294a09aa 1026 Use LastReadCount() to verify the number of bytes actually read.
23324ae1 1027 Use Error() to determine if the operation succeeded.
3c4f71cc 1028
7c913512 1029 @param buffer
4cc4bfaf 1030 Buffer where to put read data.
7c913512 1031 @param nbytes
4cc4bfaf 1032 Number of bytes.
3c4f71cc 1033
d29a9a8a 1034 @return Returns a reference to the current object.
3c4f71cc 1035
3d7548cb 1036 @remarks
e725ba4f
FM
1037 The exact behaviour of Read() depends on the combination of flags being used.
1038 For a detailed explanation, see SetFlags()
3d7548cb 1039
294a09aa 1040 @see Error(), LastError(), LastReadCount(),
4cc4bfaf 1041 SetFlags()
23324ae1 1042 */
9940bebf 1043 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
23324ae1
FM
1044
1045 /**
9940bebf
VZ
1046 Receive a message sent by WriteMsg().
1047
1048 If the buffer passed to the function isn't big enough, the remaining
1049 bytes will be discarded. This function always waits for the buffer to
1050 be entirely filled, unless an error occurs.
3d7548cb 1051
294a09aa 1052 Use LastReadCount() to verify the number of bytes actually read.
3d7548cb 1053
23324ae1 1054 Use Error() to determine if the operation succeeded.
3c4f71cc 1055
7c913512 1056 @param buffer
4cc4bfaf 1057 Buffer where to put read data.
7c913512 1058 @param nbytes
4cc4bfaf 1059 Size of the buffer.
3c4f71cc 1060
d29a9a8a 1061 @return Returns a reference to the current object.
3c4f71cc 1062
3d7548cb 1063 @remarks
e725ba4f
FM
1064 ReadMsg() will behave as if the @b wxSOCKET_WAITALL flag was always set
1065 and it will always ignore the @b wxSOCKET_NOWAIT flag.
1066 The exact behaviour of ReadMsg() depends on the @b wxSOCKET_BLOCK flag.
1067 For a detailed explanation, see SetFlags().
294a09aa
VZ
1068 For thread safety, in case ReadMsg() and WriteMsg() are called in
1069 different threads, it is a good idea to call
1070 SetFlags(wxSOCKET_WAITALL|wx_SOCKET_BLOCK) before the first calls
1071 to ReadMsg() and WriteMsg() in different threads, as each of these
1072 functions will call SetFlags() which performs read/modify/write. By
1073 setting these flags before the multi-threading, it will ensure that
1074 they don't get reset by thread race conditions.
3c4f71cc 1075
294a09aa 1076 @see Error(), LastError(), LastReadCount(), SetFlags(), WriteMsg()
23324ae1 1077 */
9940bebf 1078 wxSocketBase& ReadMsg(void* buffer, wxUint32 nbytes);
23324ae1
FM
1079
1080 /**
1081 Use SetFlags to customize IO operation for this socket.
ee533e88 1082
4cc4bfaf 1083 The @a flags parameter may be a combination of flags ORed together.
ee533e88
VZ
1084 Notice that not all combinations of flags affecting the IO calls
1085 (Read() and Write()) make sense, e.g. @b wxSOCKET_NOWAIT can't be
1086 combined with @b wxSOCKET_WAITALL nor with @b wxSOCKET_BLOCK.
3c4f71cc 1087
ee533e88 1088 The following flags can be used:
3d7548cb
BP
1089 @beginFlagTable
1090 @flag{wxSOCKET_NONE}
ee533e88
VZ
1091 Default mode: the socket will read some data in the IO calls and
1092 will process events to avoid blocking UI while waiting for the data
1093 to become available.
3d7548cb 1094 @flag{wxSOCKET_NOWAIT}
ee533e88
VZ
1095 Don't wait for the socket to become ready in IO calls, read as much
1096 data as is available -- potentially 0 bytes -- and return
1097 immediately.
3d7548cb 1098 @flag{wxSOCKET_WAITALL}
ee533e88
VZ
1099 Don't return before the entire amount of data specified in IO calls
1100 is read or written unless an error occurs. If this flag is not
1101 specified, the IO calls return as soon as any amount of data, even
1102 less than the total number of bytes, is processed.
3d7548cb 1103 @flag{wxSOCKET_BLOCK}
ee533e88
VZ
1104 Don't process the UI events while waiting for the socket to become
1105 ready. This means that UI will be unresponsive during socket IO.
3d7548cb
BP
1106 @flag{wxSOCKET_REUSEADDR}
1107 Allows the use of an in-use port (wxServerSocket only).
1108 @flag{wxSOCKET_BROADCAST}
1109 Switches the socket to broadcast mode.
1110 @flag{wxSOCKET_NOBIND}
1111 Stops the socket from being bound to a specific adapter (normally
1112 used in conjunction with @b wxSOCKET_BROADCAST).
1113 @endFlagTable
1114
1115 For more information on socket events see @ref wxSocketFlags .
23324ae1
FM
1116 */
1117 void SetFlags(wxSocketFlags flags);
1118
1119 /**
9940bebf
VZ
1120 Set the local address and port to use.
1121
1122 This function must always be called for the server sockets but may also
1123 be called for client sockets, if it is, @b bind() is called before @b
1124 connect().
23324ae1 1125 */
382f12e4 1126 virtual bool SetLocal(const wxIPV4address& local);
23324ae1 1127
23324ae1 1128 /**
9940bebf
VZ
1129 Set the default socket timeout in seconds.
1130
1131 This timeout applies to all IO calls, and also to the Wait() family of
1132 functions if you don't specify a wait interval. Initially, the default
23324ae1
FM
1133 timeout is 10 minutes.
1134 */
382f12e4 1135 void SetTimeout(long seconds);
23324ae1 1136
23324ae1 1137 /**
9940bebf
VZ
1138 Put the specified data into the input queue.
1139
1140 The data in the buffer will be returned by the next call to Read().
1141
1142 This function is not affected by wxSocket flags.
3d7548cb
BP
1143
1144 If you use LastCount(), it will always return @a nbytes.
1145
23324ae1 1146 If you use Error(), it will always return @false.
3c4f71cc 1147
7c913512 1148 @param buffer
4cc4bfaf 1149 Buffer to be unread.
7c913512 1150 @param nbytes
4cc4bfaf 1151 Number of bytes.
3c4f71cc 1152
d29a9a8a 1153 @return Returns a reference to the current object.
3c4f71cc 1154
4cc4bfaf 1155 @see Error(), LastCount(), LastError()
23324ae1 1156 */
9940bebf 1157 wxSocketBase& Unread(const void* buffer, wxUint32 nbytes);
23324ae1
FM
1158
1159 /**
9940bebf 1160 Wait for any socket event.
3c4f71cc 1161
9940bebf 1162 Possible socket events are:
3d7548cb
BP
1163 @li The socket becomes readable.
1164 @li The socket becomes writable.
1165 @li An ongoing connection request has completed (wxSocketClient only)
1166 @li An incoming connection request has arrived (wxSocketServer only)
1167 @li The connection has been closed.
1168
9940bebf
VZ
1169 Note that it is recommended to use the individual @b WaitForXXX()
1170 functions to wait for the required condition, instead of this one.
3c4f71cc 1171
7c913512 1172 @param seconds
4cc4bfaf
FM
1173 Number of seconds to wait.
1174 If -1, it will wait for the default timeout,
3d7548cb 1175 as set with SetTimeout().
7c913512 1176 @param millisecond
4cc4bfaf 1177 Number of milliseconds to wait.
3c4f71cc 1178
9940bebf
VZ
1179 @return
1180 @true when any of the above conditions is satisfied or @false if the
1181 timeout was reached.
3c4f71cc 1182
3d7548cb 1183 @see InterruptWait(), wxSocketServer::WaitForAccept(),
4cc4bfaf 1184 WaitForLost(), WaitForRead(),
3d7548cb 1185 WaitForWrite(), wxSocketClient::WaitOnConnect()
23324ae1
FM
1186 */
1187 bool Wait(long seconds = -1, long millisecond = 0);
1188
1189 /**
9940bebf
VZ
1190 Wait until the connection is lost.
1191
1192 This may happen if the peer gracefully closes the connection or if the
1193 connection breaks.
3c4f71cc 1194
7c913512 1195 @param seconds
4cc4bfaf
FM
1196 Number of seconds to wait.
1197 If -1, it will wait for the default timeout,
3d7548cb 1198 as set with SetTimeout().
7c913512 1199 @param millisecond
4cc4bfaf 1200 Number of milliseconds to wait.
3c4f71cc 1201
d29a9a8a 1202 @return Returns @true if the connection was lost, @false if the timeout
e725ba4f 1203 was reached.
3c4f71cc 1204
4cc4bfaf 1205 @see InterruptWait(), Wait()
23324ae1 1206 */
fc377125 1207 bool WaitForLost(long seconds = -1, long millisecond = 0);
23324ae1
FM
1208
1209 /**
9940bebf 1210 Wait until the socket is readable.
3d7548cb
BP
1211
1212 This might mean that queued data is available for reading or, for streamed
1213 sockets, that the connection has been closed, so that a read operation will
1214 complete immediately without blocking (unless the @b wxSOCKET_WAITALL flag
23324ae1 1215 is set, in which case the operation might still block).
3c4f71cc 1216
9940bebf
VZ
1217 Notice that this function should not be called if there is already data
1218 available for reading on the socket.
1219
7c913512 1220 @param seconds
4cc4bfaf
FM
1221 Number of seconds to wait.
1222 If -1, it will wait for the default timeout,
3d7548cb 1223 as set with SetTimeout().
7c913512 1224 @param millisecond
4cc4bfaf 1225 Number of milliseconds to wait.
3c4f71cc 1226
d29a9a8a 1227 @return Returns @true if the socket becomes readable, @false on timeout.
3c4f71cc 1228
4cc4bfaf 1229 @see InterruptWait(), Wait()
23324ae1
FM
1230 */
1231 bool WaitForRead(long seconds = -1, long millisecond = 0);
1232
1233 /**
9940bebf 1234 Wait until the socket becomes writable.
3d7548cb
BP
1235
1236 This might mean that the socket is ready to send new data, or for streamed
1237 sockets, that the connection has been closed, so that a write operation is
1238 guaranteed to complete immediately (unless the @b wxSOCKET_WAITALL flag is set,
23324ae1 1239 in which case the operation might still block).
3c4f71cc 1240
9940bebf
VZ
1241 Notice that this function should not be called if the socket is already
1242 writable.
1243
7c913512 1244 @param seconds
4cc4bfaf
FM
1245 Number of seconds to wait.
1246 If -1, it will wait for the default timeout,
3d7548cb 1247 as set with SetTimeout().
7c913512 1248 @param millisecond
4cc4bfaf 1249 Number of milliseconds to wait.
3c4f71cc 1250
d29a9a8a 1251 @return Returns @true if the socket becomes writable, @false on timeout.
3c4f71cc 1252
4cc4bfaf 1253 @see InterruptWait(), Wait()
23324ae1
FM
1254 */
1255 bool WaitForWrite(long seconds = -1, long millisecond = 0);
1256
1257 /**
9940bebf 1258 Write up to the given number of bytes to the socket.
3d7548cb 1259
294a09aa 1260 Use LastWriteCount() to verify the number of bytes actually written.
3d7548cb 1261
23324ae1 1262 Use Error() to determine if the operation succeeded.
3c4f71cc 1263
7c913512 1264 @param buffer
4cc4bfaf 1265 Buffer with the data to be sent.
7c913512 1266 @param nbytes
4cc4bfaf 1267 Number of bytes.
3c4f71cc 1268
d29a9a8a 1269 @return Returns a reference to the current object.
3c4f71cc 1270
3d7548cb
BP
1271 @remarks
1272
1273 The exact behaviour of Write() depends on the combination of flags being used.
1274 For a detailed explanation, see SetFlags().
1275
294a09aa 1276 @see Error(), LastError(), LastWriteCount(), SetFlags()
23324ae1 1277 */
9940bebf 1278 wxSocketBase& Write(const void* buffer, wxUint32 nbytes);
23324ae1
FM
1279
1280 /**
9940bebf
VZ
1281 Sends a buffer which can be read using ReadMsg().
1282
1283 WriteMsg() sends a short header before the data so that ReadMsg()
1284 knows how much data should be actually read.
3d7548cb 1285
9940bebf
VZ
1286 This function always waits for the entire buffer to be sent, unless an
1287 error occurs.
3d7548cb 1288
294a09aa 1289 Use LastWriteCount() to verify the number of bytes actually written.
3d7548cb 1290
23324ae1 1291 Use Error() to determine if the operation succeeded.
3c4f71cc 1292
7c913512 1293 @param buffer
4cc4bfaf 1294 Buffer with the data to be sent.
7c913512 1295 @param nbytes
4cc4bfaf 1296 Number of bytes to send.
3c4f71cc 1297
d29a9a8a 1298 @return Returns a reference to the current object.
3d7548cb
BP
1299
1300 @remarks
1301
1302 WriteMsg() will behave as if the @b wxSOCKET_WAITALL flag was always set and
1303 it will always ignore the @b wxSOCKET_NOWAIT flag. The exact behaviour of
1304 WriteMsg() depends on the @b wxSOCKET_BLOCK flag. For a detailed explanation,
1305 see SetFlags().
294a09aa
VZ
1306 For thread safety, in case ReadMsg() and WriteMsg() are called in
1307 different threads, it is a good idea to call
1308 @code SetFlags(wxSOCKET_WAITALL|wx_SOCKET_BLOCK) @endcode before the
1309 first calls to ReadMsg() and WriteMsg() in different threads, as each
1310 of these functions calls SetFlags() which performs read/modify/write.
1311 By setting these flags before the multi-threading, it will ensure that
1312 they don't get reset by thread race conditions.
3d7548cb 1313
294a09aa 1314 @see Error(), LastError(), LastWriteCount(), SetFlags(), ReadMsg()
3d7548cb 1315
23324ae1 1316 */
9940bebf 1317 wxSocketBase& WriteMsg(const void* buffer, wxUint32 nbytes);
3d7548cb
BP
1318
1319 //@}
1320
1321
1322 /**
1323 @name Handling Socket Events
1324 */
1325 //@{
1326
1327 /**
1328 Returns a pointer of the client data for this socket, as set with
1329 SetClientData()
1330 */
1331 void* GetClientData() const;
1332
1333 /**
1334 According to the @a notify value, this function enables
1335 or disables socket events. If @a notify is @true, the events
1336 configured with SetNotify() will
1337 be sent to the application. If @a notify is @false; no events
1338 will be sent.
1339 */
1340 void Notify(bool notify);
1341
1342 /**
1343 Sets user-supplied client data for this socket. All socket events will
1344 contain a pointer to this data, which can be retrieved with
1345 the wxSocketEvent::GetClientData() function.
1346 */
1347 void SetClientData(void* data);
1348
1349 /**
1350 Sets an event handler to be called when a socket event occurs. The
1351 handler will be called for those events for which notification is
1352 enabled with SetNotify() and
1353 Notify().
1354
1355 @param handler
1356 Specifies the event handler you want to use.
1357 @param id
1358 The id of socket event.
1359
1360 @see SetNotify(), Notify(), wxSocketEvent, wxEvtHandler
1361 */
1362 void SetEventHandler(wxEvtHandler& handler, int id = -1);
1363
1364 /**
1365 Specifies which socket events are to be sent to the event handler.
1366 The @a flags parameter may be combination of flags ORed together. The
1367 following flags can be used:
1368
1369 @beginFlagTable
1370 @flag{wxSOCKET_INPUT_FLAG} to receive @b wxSOCKET_INPUT.
1371 @flag{wxSOCKET_OUTPUT_FLAG} to receive @b wxSOCKET_OUTPUT.
1372 @flag{wxSOCKET_CONNECTION_FLAG} to receive @b wxSOCKET_CONNECTION.
1373 @flag{wxSOCKET_LOST_FLAG} to receive @b wxSOCKET_LOST.
1374 @endFlagTable
1375
1376 For example:
1377
1378 @code
1379 sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
1380 sock.Notify(true);
1381 @endcode
1382
1383 In this example, the user will be notified about incoming socket data and
1384 whenever the connection is closed.
1385
1386 For more information on socket events see @ref wxSocketEventFlags .
1387 */
1388 void SetNotify(wxSocketEventFlags flags);
1389
1390 //@}
23324ae1
FM
1391};
1392
1393
e54c96f1 1394
23324ae1
FM
1395/**
1396 @class wxDatagramSocket
7c913512 1397
41e69d79
FM
1398 @todo docme
1399
23324ae1 1400 @library{wxnet}
3d7548cb 1401 @category{net}
23324ae1
FM
1402*/
1403class wxDatagramSocket : public wxSocketBase
1404{
1405public:
1406 /**
1407 Constructor.
3c4f71cc 1408
41e69d79
FM
1409 @param addr
1410 The socket address.
7c913512 1411 @param flags
41e69d79 1412 Socket flags (See wxSocketBase::SetFlags()).
23324ae1 1413 */
8067ee11
FM
1414 wxDatagramSocket(const wxSockAddress& addr,
1415 wxSocketFlags flags = wxSOCKET_NONE);
23324ae1
FM
1416
1417 /**
3d7548cb 1418 Destructor. Please see wxSocketBase::Destroy().
23324ae1 1419 */
adaaa686 1420 virtual ~wxDatagramSocket();
23324ae1 1421
23324ae1 1422 /**
9940bebf
VZ
1423 Write a buffer of @a nbytes bytes to the socket.
1424
294a09aa 1425 Use wxSocketBase::LastWriteCount() to verify the number of bytes actually wrote.
3d7548cb 1426 Use wxSocketBase::Error() to determine if the operation succeeded.
3c4f71cc 1427
7c913512 1428 @param address
4cc4bfaf 1429 The address of the destination peer for this data.
7c913512 1430 @param buffer
4cc4bfaf 1431 Buffer where read data is.
7c913512 1432 @param nbytes
4cc4bfaf 1433 Number of bytes.
3c4f71cc 1434
d29a9a8a 1435 @return Returns a reference to the current object.
3d7548cb
BP
1436
1437 @see wxSocketBase::LastError(), wxSocketBase::SetFlags()
23324ae1 1438 */
7323ff1a
FM
1439 wxDatagramSocket& SendTo(const wxSockAddress& address,
1440 const void* buffer, wxUint32 nbytes);
23324ae1 1441};
e54c96f1 1442