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