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