]>
Commit | Line | Data |
---|---|---|
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$ | |
6 | // Licence: wxWindows license | |
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 | ||
17 | @library{wxbase} | |
18 | @category{net} | |
19 | */ | |
20 | class wxIPaddress : public wxSockAddress | |
21 | { | |
22 | public: | |
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 | |
23324ae1 FM |
106 | @library{wxbase} |
107 | @category{net} | |
108 | */ | |
109 | class wxIPV4address : public wxIPaddress | |
110 | { | |
111 | public: | |
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 | */ |
183 | class wxSocketServer : public wxSocketBase | |
184 | { | |
185 | public: | |
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 | ||
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 | */ |
277 | class wxSocketClient : public wxSocketBase | |
278 | { | |
279 | public: | |
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 | ||
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 | |
23324ae1 | 403 | @library{wxbase} |
3d7548cb | 404 | @category{net} |
7c913512 | 405 | |
e54c96f1 | 406 | @see wxSocketBase, wxIPaddress, wxIPV4address |
23324ae1 FM |
407 | */ |
408 | class wxSockAddress : public wxObject | |
409 | { | |
410 | public: | |
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(); | |
430 | }; | |
431 | ||
432 | ||
e54c96f1 | 433 | |
23324ae1 FM |
434 | /** |
435 | @class wxSocketEvent | |
7c913512 | 436 | |
23324ae1 | 437 | This event class contains information about socket events. |
7c913512 | 438 | |
3d7548cb BP |
439 | @beginEventTable{wxSocketEvent} |
440 | @event{EVT_SOCKET(id, func)} | |
441 | Process a socket event, supplying the member function. | |
442 | @endEventTable | |
443 | ||
23324ae1 FM |
444 | @library{wxnet} |
445 | @category{net} | |
7c913512 | 446 | |
e54c96f1 | 447 | @see wxSocketBase, wxSocketClient, wxSocketServer |
23324ae1 FM |
448 | */ |
449 | class wxSocketEvent : public wxEvent | |
450 | { | |
451 | public: | |
452 | /** | |
453 | Constructor. | |
454 | */ | |
455 | wxSocketEvent(int id = 0); | |
456 | ||
457 | /** | |
458 | Gets the client data of the socket which generated this event, as | |
3d7548cb | 459 | set with wxSocketBase::SetClientData(). |
23324ae1 | 460 | */ |
adaaa686 | 461 | void* GetClientData() const; |
23324ae1 FM |
462 | |
463 | /** | |
e725ba4f FM |
464 | Returns the socket object to which this event refers to. |
465 | This makes it possible to use the same event handler for different sockets. | |
23324ae1 | 466 | */ |
328f5751 | 467 | wxSocketBase* GetSocket() const; |
23324ae1 FM |
468 | |
469 | /** | |
470 | Returns the socket event type. | |
471 | */ | |
328f5751 | 472 | wxSocketNotify GetSocketEvent() const; |
23324ae1 FM |
473 | }; |
474 | ||
475 | ||
3d7548cb BP |
476 | /** |
477 | wxSocket error return values. | |
478 | */ | |
479 | enum wxSocketError | |
480 | { | |
481 | wxSOCKET_NOERROR, ///< No error happened. | |
482 | wxSOCKET_INVOP, ///< Invalid operation. | |
483 | wxSOCKET_IOERR, ///< Input/Output error. | |
484 | wxSOCKET_INVADDR, ///< Invalid address passed to wxSocket. | |
485 | wxSOCKET_INVSOCK, ///< Invalid socket (uninitialized). | |
486 | wxSOCKET_NOHOST, ///< No corresponding host. | |
487 | wxSOCKET_INVPORT, ///< Invalid port. | |
488 | wxSOCKET_WOULDBLOCK, ///< The socket is non-blocking and the operation would block. | |
489 | wxSOCKET_TIMEDOUT, ///< The timeout for this operation expired. | |
490 | wxSOCKET_MEMERR ///< Memory exhausted. | |
491 | }; | |
492 | ||
493 | ||
494 | /** | |
e725ba4f FM |
495 | @anchor wxSocketEventFlags |
496 | ||
3d7548cb BP |
497 | wxSocket Event Flags. |
498 | ||
499 | A brief note on how to use these events: | |
500 | ||
501 | The @b wxSOCKET_INPUT event will be issued whenever there is data available | |
502 | for reading. This will be the case if the input queue was empty and new data | |
503 | arrives, or if the application has read some data yet there is still more data | |
504 | available. This means that the application does not need to read all available | |
505 | data in response to a @b wxSOCKET_INPUT event, as more events will be produced | |
506 | as necessary. | |
507 | ||
508 | The @b wxSOCKET_OUTPUT event is issued when a socket is first connected with | |
509 | Connect() or accepted with Accept(). After that, new events will be generated | |
510 | only after an output operation fails with @b wxSOCKET_WOULDBLOCK and buffer space | |
511 | becomes available again. This means that the application should assume that it can | |
512 | write data to the socket until an @b wxSOCKET_WOULDBLOCK error occurs; after this, | |
513 | whenever the socket becomes writable again the application will be notified with | |
514 | another @b wxSOCKET_OUTPUT event. | |
515 | ||
516 | The @b wxSOCKET_CONNECTION event is issued when a delayed connection request completes | |
517 | successfully (client) or when a new connection arrives at the incoming queue (server). | |
518 | ||
519 | The @b wxSOCKET_LOST event is issued when a close indication is received for the socket. | |
520 | This means that the connection broke down or that it was closed by the peer. Also, this | |
521 | event will be issued if a connection request fails. | |
522 | */ | |
523 | enum wxSocketEventFlags | |
524 | { | |
525 | wxSOCKET_INPUT, ///< There is data available for reading. | |
526 | wxSOCKET_OUTPUT, ///< The socket is ready to be written to. | |
527 | wxSOCKET_CONNECTION, ///< Incoming connection request (server), or | |
528 | ///< successful connection establishment (client). | |
529 | wxSOCKET_LOST ///< The connection has been closed. | |
530 | }; | |
531 | ||
532 | ||
533 | /** | |
534 | @anchor wxSocketFlags | |
535 | ||
536 | wxSocket Flags. | |
537 | ||
538 | A brief overview on how to use these flags follows. | |
539 | ||
540 | If no flag is specified (this is the same as @b wxSOCKET_NONE), | |
541 | IO calls will return after some data has been read or written, even | |
542 | when the transfer might not be complete. This is the same as issuing | |
543 | exactly one blocking low-level call to @b recv() or @b send(). Note | |
544 | that @e blocking here refers to when the function returns, not | |
545 | to whether the GUI blocks during this time. | |
546 | ||
547 | If @b wxSOCKET_NOWAIT is specified, IO calls will return immediately. | |
548 | Read operations will retrieve only available data. Write operations will | |
549 | write as much data as possible, depending on how much space is available | |
550 | in the output buffer. This is the same as issuing exactly one nonblocking | |
551 | low-level call to @b recv() or @b send(). Note that @e nonblocking here | |
552 | refers to when the function returns, not to whether the GUI blocks during | |
553 | this time. | |
554 | ||
555 | If @b wxSOCKET_WAITALL is specified, IO calls won't return until ALL | |
556 | the data has been read or written (or until an error occurs), blocking if | |
557 | necessary, and issuing several low level calls if necessary. This is the | |
558 | same as having a loop which makes as many blocking low-level calls to | |
559 | @b recv() or @b send() as needed so as to transfer all the data. Note | |
560 | that @e blocking here refers to when the function returns, not | |
561 | to whether the GUI blocks during this time. | |
562 | ||
563 | The @b wxSOCKET_BLOCK flag controls whether the GUI blocks during | |
564 | IO operations. If this flag is specified, the socket will not yield | |
565 | during IO calls, so the GUI will remain blocked until the operation | |
566 | completes. If it is not used, then the application must take extra | |
567 | care to avoid unwanted reentrance. | |
568 | ||
569 | The @b wxSOCKET_REUSEADDR flag controls the use of the @b SO_REUSEADDR standard | |
570 | @b setsockopt() flag. This flag allows the socket to bind to a port that is | |
571 | already in use. This is mostly used on UNIX-based systems to allow rapid starting | |
572 | and stopping of a server, otherwise you may have to wait several minutes for the | |
573 | port to become available. | |
574 | ||
575 | @b wxSOCKET_REUSEADDR can also be used with socket clients to (re)bind to a | |
576 | particular local port for an outgoing connection. | |
577 | This option can have surprising platform dependent behavior, so check the | |
578 | documentation for your platform's implementation of setsockopt(). | |
579 | ||
580 | Note that on BSD-based systems(e.g. Mac OS X), use of | |
581 | @b wxSOCKET_REUSEADDR implies @b SO_REUSEPORT in addition to | |
582 | @b SO_REUSEADDR to be consistent with Windows. | |
583 | ||
584 | The @b wxSOCKET_BROADCAST flag controls the use of the @b SO_BROADCAST standard | |
585 | @b setsockopt() flag. This flag allows the socket to use the broadcast address, | |
586 | and is generally used in conjunction with @b wxSOCKET_NOBIND and | |
587 | wxIPaddress::BroadcastAddress(). | |
588 | ||
589 | So: | |
e725ba4f FM |
590 | - @b wxSOCKET_NONE will try to read at least SOME data, no matter how much. |
591 | - @b wxSOCKET_NOWAIT will always return immediately, even if it cannot | |
592 | read or write ANY data. | |
593 | - @b wxSOCKET_WAITALL will only return when it has read or written ALL | |
594 | the data. | |
595 | - @b wxSOCKET_BLOCK has nothing to do with the previous flags and | |
596 | it controls whether the GUI blocks. | |
597 | - @b wxSOCKET_REUSEADDR controls special platform-specific behavior for | |
598 | reusing local addresses/ports. | |
3d7548cb BP |
599 | */ |
600 | enum | |
601 | { | |
602 | wxSOCKET_NONE = 0, ///< Normal functionality. | |
603 | wxSOCKET_NOWAIT = 1, ///< Read/write as much data as possible and return immediately. | |
604 | wxSOCKET_WAITALL = 2, ///< Wait for all required data to be read/written unless an error occurs. | |
605 | wxSOCKET_BLOCK = 4, ///< Block the GUI (do not yield) while reading/writing data. | |
606 | wxSOCKET_REUSEADDR = 8, ///< Allows the use of an in-use port (wxServerSocket only) | |
607 | wxSOCKET_BROADCAST = 16, ///< Switches the socket to broadcast mode | |
608 | wxSOCKET_NOBIND = 32 ///< Stops the socket from being bound to a specific | |
609 | ///< adapter (normally used in conjunction with | |
610 | ///< @b wxSOCKET_BROADCAST) | |
611 | }; | |
612 | ||
e54c96f1 | 613 | |
23324ae1 FM |
614 | /** |
615 | @class wxSocketBase | |
7c913512 | 616 | |
23324ae1 FM |
617 | wxSocketBase is the base class for all socket-related objects, and it |
618 | defines all basic IO functionality. | |
7c913512 | 619 | |
e725ba4f | 620 | @note |
4cb591b9 VZ |
621 | When using wxSocket from multiple threads, even implicitly (e.g. by using |
622 | wxFTP or wxHTTP in another thread) you must initialize the sockets from the | |
623 | main thread by calling Initialize() before creating the other ones. | |
7c913512 | 624 | |
3d7548cb BP |
625 | @beginEventTable{wxSocketEvent} |
626 | @event{EVT_SOCKET(id, func)} | |
627 | Process a @c wxEVT_SOCKET event. | |
e725ba4f | 628 | See @ref wxSocketEventFlags and @ref wxSocketFlags for more info. |
3d7548cb BP |
629 | @endEventTable |
630 | ||
e725ba4f FM |
631 | @library{wxnet} |
632 | @category{net} | |
633 | ||
3d7548cb BP |
634 | @see wxSocketEvent, wxSocketClient, wxSocketServer, @sample{sockets}, |
635 | @ref wxSocketFlags, ::wxSocketEventFlags, ::wxSocketError | |
23324ae1 FM |
636 | */ |
637 | class wxSocketBase : public wxObject | |
638 | { | |
639 | public: | |
23324ae1 FM |
640 | |
641 | /** | |
3d7548cb | 642 | @name Construction and Destruction |
23324ae1 | 643 | */ |
3d7548cb | 644 | //@{ |
23324ae1 FM |
645 | |
646 | /** | |
3d7548cb | 647 | Default constructor. |
3c4f71cc | 648 | |
3d7548cb BP |
649 | Don't use it directly; instead, use wxSocketClient to construct a socket client, |
650 | or wxSocketServer to construct a socket server. | |
23324ae1 | 651 | */ |
3d7548cb | 652 | wxSocketBase(); |
23324ae1 FM |
653 | |
654 | /** | |
3d7548cb BP |
655 | Destructor. |
656 | ||
657 | Do not destroy a socket using the delete operator directly; | |
658 | use Destroy() instead. Also, do not create socket objects in the stack. | |
23324ae1 | 659 | */ |
382f12e4 | 660 | virtual ~wxSocketBase(); |
23324ae1 FM |
661 | |
662 | /** | |
3d7548cb | 663 | Destroys the socket safely. |
3c4f71cc | 664 | |
3d7548cb BP |
665 | Use this function instead of the delete operator, since otherwise socket events |
666 | could reach the application even after the socket has been destroyed. To prevent | |
667 | this problem, this function appends the wxSocket to a list of object to be deleted | |
668 | on idle time, after all events have been processed. For the same reason, you should | |
669 | avoid creating socket objects in the stack. | |
3c4f71cc | 670 | |
3d7548cb | 671 | Destroy() calls Close() automatically. |
3c4f71cc | 672 | |
d29a9a8a | 673 | @return Always @true. |
23324ae1 FM |
674 | */ |
675 | bool Destroy(); | |
676 | ||
4cb591b9 VZ |
677 | /** |
678 | Perform the initialization needed in order to use the sockets. | |
679 | ||
680 | This function is called from wxSocket constructor implicitly and so | |
681 | normally doesn't need to be called explicitly. There is however one | |
682 | important exception: as this function must be called from the main | |
683 | (UI) thread, if you use wxSocket from multiple threads you must call | |
684 | Initialize() from the main thread before creating wxSocket objects in | |
685 | the other ones. | |
686 | ||
687 | It is safe to call this function multiple times (only the first call | |
688 | does anything) but you must call Shutdown() exactly once for every call | |
689 | to Initialize(). | |
690 | ||
691 | @return | |
692 | @true if the sockets can be used, @false if the initialization | |
693 | failed and sockets are not available at all. | |
694 | */ | |
695 | static bool Initialize(); | |
696 | ||
697 | /** | |
698 | Shut down the sockets. | |
699 | ||
700 | This function undoes the call to Initialize() and must be called after | |
701 | every successful call to Initialize(). | |
702 | */ | |
703 | static void Shutdown(); | |
704 | ||
3d7548cb BP |
705 | //@} |
706 | ||
707 | ||
23324ae1 | 708 | /** |
3d7548cb | 709 | @name Socket State |
23324ae1 | 710 | */ |
3d7548cb | 711 | //@{ |
23324ae1 FM |
712 | |
713 | /** | |
714 | Returns @true if an error occurred in the last IO operation. | |
c9157492 | 715 | |
3d7548cb BP |
716 | Use this function to check for an error condition after one of the |
717 | following calls: Discard(), Peek(), Read(), ReadMsg(), Unread(), Write(), WriteMsg(). | |
23324ae1 | 718 | */ |
328f5751 | 719 | bool Error() const; |
23324ae1 | 720 | |
23324ae1 | 721 | /** |
9940bebf | 722 | Return the local address of the socket. |
3c4f71cc | 723 | |
d29a9a8a | 724 | @return @true if no error happened, @false otherwise. |
23324ae1 | 725 | */ |
382f12e4 | 726 | virtual bool GetLocal(wxSockAddress& addr) const; |
23324ae1 FM |
727 | |
728 | /** | |
9940bebf | 729 | Return the peer address field of the socket. |
3c4f71cc | 730 | |
d29a9a8a | 731 | @return @true if no error happened, @false otherwise. |
23324ae1 | 732 | */ |
382f12e4 | 733 | virtual bool GetPeer(wxSockAddress& addr) const; |
23324ae1 | 734 | |
2d46f281 VZ |
735 | /** |
736 | Return the socket timeout in seconds. | |
737 | ||
738 | The timeout can be set using SetTimeout() and is 10 minutes by default. | |
739 | */ | |
740 | long GetTimeout() const; | |
741 | ||
23324ae1 FM |
742 | /** |
743 | Returns @true if the socket is connected. | |
744 | */ | |
328f5751 | 745 | bool IsConnected() const; |
23324ae1 | 746 | |
c9157492 | 747 | /** |
9940bebf | 748 | Check if the socket can be currently read or written. |
c9157492 | 749 | |
3d7548cb BP |
750 | This might mean that queued data is available for reading or, for streamed |
751 | sockets, that the connection has been closed, so that a read operation will | |
752 | complete immediately without blocking (unless the @b wxSOCKET_WAITALL flag | |
23324ae1 FM |
753 | is set, in which case the operation might still block). |
754 | */ | |
382f12e4 | 755 | bool IsData(); |
23324ae1 FM |
756 | |
757 | /** | |
758 | Returns @true if the socket is not connected. | |
759 | */ | |
328f5751 | 760 | bool IsDisconnected() const; |
23324ae1 FM |
761 | |
762 | /** | |
763 | Returns @true if the socket is initialized and ready and @false in other | |
764 | cases. | |
3d7548cb BP |
765 | |
766 | @remarks | |
3d7548cb | 767 | For wxSocketClient, IsOk() won't return @true unless the client is connected to a server. |
3d7548cb BP |
768 | For wxSocketServer, IsOk() will return @true if the server could bind to the specified address |
769 | and is already listening for new connections. | |
3d7548cb | 770 | IsOk() does not check for IO errors; use Error() instead for that purpose. |
23324ae1 | 771 | */ |
328f5751 | 772 | bool IsOk() const; |
23324ae1 FM |
773 | |
774 | /** | |
775 | Returns the number of bytes read or written by the last IO call. | |
3d7548cb | 776 | |
23324ae1 | 777 | Use this function to get the number of bytes actually transferred |
3d7548cb BP |
778 | after using one of the following IO calls: Discard(), Peek(), Read(), |
779 | ReadMsg(), Unread(), Write(), WriteMsg(). | |
23324ae1 | 780 | */ |
328f5751 | 781 | wxUint32 LastCount() const; |
23324ae1 FM |
782 | |
783 | /** | |
3d7548cb BP |
784 | Returns the last wxSocket error. See @ref wxSocketError . |
785 | ||
786 | @note | |
3d7548cb | 787 | This function merely returns the last error code, |
23324ae1 FM |
788 | but it should not be used to determine if an error has occurred (this |
789 | is because successful operations do not change the LastError value). | |
3d7548cb BP |
790 | Use Error() first, in order to determine if the last IO call failed. |
791 | If this returns @true, use LastError() to discover the cause of the error. | |
23324ae1 | 792 | */ |
328f5751 | 793 | wxSocketError LastError() const; |
23324ae1 FM |
794 | |
795 | /** | |
9940bebf | 796 | Restore the previous state of the socket, as saved with SaveState(). |
3d7548cb BP |
797 | |
798 | Calls to SaveState() and RestoreState() can be nested. | |
799 | ||
800 | @see SaveState() | |
23324ae1 | 801 | */ |
3d7548cb BP |
802 | void RestoreState(); |
803 | ||
804 | /** | |
9940bebf VZ |
805 | Save the current state of the socket in a stack. |
806 | ||
e725ba4f FM |
807 | Socket state includes flags, as set with SetFlags(), event mask, as set |
808 | with SetNotify() and Notify(), user data, as set with SetClientData(). | |
3d7548cb BP |
809 | Calls to SaveState and RestoreState can be nested. |
810 | ||
811 | @see RestoreState() | |
812 | */ | |
813 | void SaveState(); | |
814 | ||
815 | //@} | |
816 | ||
817 | ||
818 | /** | |
819 | @name Basic I/O | |
820 | ||
821 | See also: wxSocketServer::WaitForAccept(), wxSocketClient::WaitOnConnect() | |
822 | */ | |
823 | //@{ | |
824 | ||
825 | /** | |
9940bebf VZ |
826 | Shut down the socket, disabling further transmission and reception of |
827 | data and disable events for the socket and frees the associated system | |
828 | resources. | |
3d7548cb BP |
829 | |
830 | Upon socket destruction, Close() is automatically called, so in most cases | |
831 | you won't need to do it yourself, unless you explicitly want to shut down | |
832 | the socket, typically to notify the peer that you are closing the connection. | |
833 | ||
834 | @remarks | |
3d7548cb BP |
835 | Although Close() immediately disables events for the socket, it is possible |
836 | that event messages may be waiting in the application's event queue. | |
837 | The application must therefore be prepared to handle socket event messages even | |
838 | after calling Close(). | |
839 | */ | |
382f12e4 | 840 | virtual bool Close(); |
3d7548cb | 841 | |
b67397a7 VZ |
842 | /** |
843 | Shuts down the writing end of the socket. | |
844 | ||
845 | This function simply calls the standard shutdown() function on the | |
846 | underlying socket, indicating that nothing will be written to this | |
847 | socket any more. | |
848 | */ | |
849 | void ShutdownOutput(); | |
850 | ||
3d7548cb | 851 | /** |
9940bebf VZ |
852 | Delete all bytes in the incoming queue. |
853 | ||
854 | This function always returns immediately and its operation is not | |
855 | affected by IO flags. | |
3d7548cb BP |
856 | |
857 | Use LastCount() to verify the number of bytes actually discarded. | |
858 | ||
859 | If you use Error(), it will always return @false. | |
860 | */ | |
9940bebf | 861 | wxSocketBase& Discard(); |
3d7548cb BP |
862 | |
863 | /** | |
864 | Returns current IO flags, as set with SetFlags() | |
865 | */ | |
866 | wxSocketFlags GetFlags() const; | |
867 | ||
868 | /** | |
869 | Use this function to interrupt any wait operation currently in progress. | |
870 | ||
871 | Note that this is not intended as a regular way to interrupt a Wait call, | |
872 | but only as an escape mechanism for exceptional situations where it is | |
873 | absolutely necessary to use it, for example to abort an operation due to | |
874 | some exception or abnormal problem. InterruptWait is automatically called | |
875 | when you Close() a socket (and thus also upon | |
876 | socket destruction), so you don't need to use it in these cases. | |
877 | ||
878 | @see Wait(), WaitForLost(), WaitForRead(), WaitForWrite(), | |
e725ba4f | 879 | wxSocketServer::WaitForAccept(), wxSocketClient::WaitOnConnect() |
3d7548cb BP |
880 | */ |
881 | void InterruptWait(); | |
23324ae1 FM |
882 | |
883 | /** | |
9940bebf VZ |
884 | Peek into the socket by copying the next bytes which would be read by |
885 | Read() into the provided buffer. | |
3d7548cb | 886 | |
9940bebf VZ |
887 | Peeking a buffer doesn't delete it from the socket input queue, i.e. |
888 | calling Read() will return the same data. | |
3d7548cb | 889 | |
23324ae1 | 890 | Use LastCount() to verify the number of bytes actually peeked. |
3d7548cb | 891 | |
23324ae1 | 892 | Use Error() to determine if the operation succeeded. |
3c4f71cc | 893 | |
7c913512 | 894 | @param buffer |
4cc4bfaf | 895 | Buffer where to put peeked data. |
7c913512 | 896 | @param nbytes |
4cc4bfaf | 897 | Number of bytes. |
3c4f71cc | 898 | |
d29a9a8a | 899 | @return Returns a reference to the current object. |
3c4f71cc | 900 | |
3d7548cb | 901 | @remarks |
e725ba4f FM |
902 | The exact behaviour of Peek() depends on the combination of flags being used. |
903 | For a detailed explanation, see SetFlags() | |
3d7548cb BP |
904 | |
905 | @see Error(), LastError(), LastCount(), SetFlags() | |
23324ae1 | 906 | */ |
9940bebf | 907 | wxSocketBase& Peek(void* buffer, wxUint32 nbytes); |
23324ae1 FM |
908 | |
909 | /** | |
9940bebf VZ |
910 | Read up to the given number of bytes from the socket. |
911 | ||
23324ae1 | 912 | Use LastCount() to verify the number of bytes actually read. |
23324ae1 | 913 | Use Error() to determine if the operation succeeded. |
3c4f71cc | 914 | |
7c913512 | 915 | @param buffer |
4cc4bfaf | 916 | Buffer where to put read data. |
7c913512 | 917 | @param nbytes |
4cc4bfaf | 918 | Number of bytes. |
3c4f71cc | 919 | |
d29a9a8a | 920 | @return Returns a reference to the current object. |
3c4f71cc | 921 | |
3d7548cb | 922 | @remarks |
e725ba4f FM |
923 | The exact behaviour of Read() depends on the combination of flags being used. |
924 | For a detailed explanation, see SetFlags() | |
3d7548cb | 925 | |
4cc4bfaf FM |
926 | @see Error(), LastError(), LastCount(), |
927 | SetFlags() | |
23324ae1 | 928 | */ |
9940bebf | 929 | wxSocketBase& Read(void* buffer, wxUint32 nbytes); |
23324ae1 FM |
930 | |
931 | /** | |
9940bebf VZ |
932 | Receive a message sent by WriteMsg(). |
933 | ||
934 | If the buffer passed to the function isn't big enough, the remaining | |
935 | bytes will be discarded. This function always waits for the buffer to | |
936 | be entirely filled, unless an error occurs. | |
3d7548cb | 937 | |
23324ae1 | 938 | Use LastCount() to verify the number of bytes actually read. |
3d7548cb | 939 | |
23324ae1 | 940 | Use Error() to determine if the operation succeeded. |
3c4f71cc | 941 | |
7c913512 | 942 | @param buffer |
4cc4bfaf | 943 | Buffer where to put read data. |
7c913512 | 944 | @param nbytes |
4cc4bfaf | 945 | Size of the buffer. |
3c4f71cc | 946 | |
d29a9a8a | 947 | @return Returns a reference to the current object. |
3c4f71cc | 948 | |
3d7548cb | 949 | @remarks |
e725ba4f FM |
950 | ReadMsg() will behave as if the @b wxSOCKET_WAITALL flag was always set |
951 | and it will always ignore the @b wxSOCKET_NOWAIT flag. | |
952 | The exact behaviour of ReadMsg() depends on the @b wxSOCKET_BLOCK flag. | |
953 | For a detailed explanation, see SetFlags(). | |
3c4f71cc | 954 | |
3d7548cb | 955 | @see Error(), LastError(), LastCount(), SetFlags(), WriteMsg() |
23324ae1 | 956 | */ |
9940bebf | 957 | wxSocketBase& ReadMsg(void* buffer, wxUint32 nbytes); |
23324ae1 FM |
958 | |
959 | /** | |
960 | Use SetFlags to customize IO operation for this socket. | |
ee533e88 | 961 | |
4cc4bfaf | 962 | The @a flags parameter may be a combination of flags ORed together. |
ee533e88 VZ |
963 | Notice that not all combinations of flags affecting the IO calls |
964 | (Read() and Write()) make sense, e.g. @b wxSOCKET_NOWAIT can't be | |
965 | combined with @b wxSOCKET_WAITALL nor with @b wxSOCKET_BLOCK. | |
3c4f71cc | 966 | |
ee533e88 | 967 | The following flags can be used: |
3d7548cb BP |
968 | @beginFlagTable |
969 | @flag{wxSOCKET_NONE} | |
ee533e88 VZ |
970 | Default mode: the socket will read some data in the IO calls and |
971 | will process events to avoid blocking UI while waiting for the data | |
972 | to become available. | |
3d7548cb | 973 | @flag{wxSOCKET_NOWAIT} |
ee533e88 VZ |
974 | Don't wait for the socket to become ready in IO calls, read as much |
975 | data as is available -- potentially 0 bytes -- and return | |
976 | immediately. | |
3d7548cb | 977 | @flag{wxSOCKET_WAITALL} |
ee533e88 VZ |
978 | Don't return before the entire amount of data specified in IO calls |
979 | is read or written unless an error occurs. If this flag is not | |
980 | specified, the IO calls return as soon as any amount of data, even | |
981 | less than the total number of bytes, is processed. | |
3d7548cb | 982 | @flag{wxSOCKET_BLOCK} |
ee533e88 VZ |
983 | Don't process the UI events while waiting for the socket to become |
984 | ready. This means that UI will be unresponsive during socket IO. | |
3d7548cb BP |
985 | @flag{wxSOCKET_REUSEADDR} |
986 | Allows the use of an in-use port (wxServerSocket only). | |
987 | @flag{wxSOCKET_BROADCAST} | |
988 | Switches the socket to broadcast mode. | |
989 | @flag{wxSOCKET_NOBIND} | |
990 | Stops the socket from being bound to a specific adapter (normally | |
991 | used in conjunction with @b wxSOCKET_BROADCAST). | |
992 | @endFlagTable | |
993 | ||
994 | For more information on socket events see @ref wxSocketFlags . | |
23324ae1 FM |
995 | */ |
996 | void SetFlags(wxSocketFlags flags); | |
997 | ||
998 | /** | |
9940bebf VZ |
999 | Set the local address and port to use. |
1000 | ||
1001 | This function must always be called for the server sockets but may also | |
1002 | be called for client sockets, if it is, @b bind() is called before @b | |
1003 | connect(). | |
23324ae1 | 1004 | */ |
382f12e4 | 1005 | virtual bool SetLocal(const wxIPV4address& local); |
23324ae1 | 1006 | |
23324ae1 | 1007 | /** |
9940bebf VZ |
1008 | Set the default socket timeout in seconds. |
1009 | ||
1010 | This timeout applies to all IO calls, and also to the Wait() family of | |
1011 | functions if you don't specify a wait interval. Initially, the default | |
23324ae1 FM |
1012 | timeout is 10 minutes. |
1013 | */ | |
382f12e4 | 1014 | void SetTimeout(long seconds); |
23324ae1 | 1015 | |
23324ae1 | 1016 | /** |
9940bebf VZ |
1017 | Put the specified data into the input queue. |
1018 | ||
1019 | The data in the buffer will be returned by the next call to Read(). | |
1020 | ||
1021 | This function is not affected by wxSocket flags. | |
3d7548cb BP |
1022 | |
1023 | If you use LastCount(), it will always return @a nbytes. | |
1024 | ||
23324ae1 | 1025 | If you use Error(), it will always return @false. |
3c4f71cc | 1026 | |
7c913512 | 1027 | @param buffer |
4cc4bfaf | 1028 | Buffer to be unread. |
7c913512 | 1029 | @param nbytes |
4cc4bfaf | 1030 | Number of bytes. |
3c4f71cc | 1031 | |
d29a9a8a | 1032 | @return Returns a reference to the current object. |
3c4f71cc | 1033 | |
4cc4bfaf | 1034 | @see Error(), LastCount(), LastError() |
23324ae1 | 1035 | */ |
9940bebf | 1036 | wxSocketBase& Unread(const void* buffer, wxUint32 nbytes); |
23324ae1 FM |
1037 | |
1038 | /** | |
9940bebf | 1039 | Wait for any socket event. |
3c4f71cc | 1040 | |
9940bebf | 1041 | Possible socket events are: |
3d7548cb BP |
1042 | @li The socket becomes readable. |
1043 | @li The socket becomes writable. | |
1044 | @li An ongoing connection request has completed (wxSocketClient only) | |
1045 | @li An incoming connection request has arrived (wxSocketServer only) | |
1046 | @li The connection has been closed. | |
1047 | ||
9940bebf VZ |
1048 | Note that it is recommended to use the individual @b WaitForXXX() |
1049 | functions to wait for the required condition, instead of this one. | |
3c4f71cc | 1050 | |
7c913512 | 1051 | @param seconds |
4cc4bfaf FM |
1052 | Number of seconds to wait. |
1053 | If -1, it will wait for the default timeout, | |
3d7548cb | 1054 | as set with SetTimeout(). |
7c913512 | 1055 | @param millisecond |
4cc4bfaf | 1056 | Number of milliseconds to wait. |
3c4f71cc | 1057 | |
9940bebf VZ |
1058 | @return |
1059 | @true when any of the above conditions is satisfied or @false if the | |
1060 | timeout was reached. | |
3c4f71cc | 1061 | |
3d7548cb | 1062 | @see InterruptWait(), wxSocketServer::WaitForAccept(), |
4cc4bfaf | 1063 | WaitForLost(), WaitForRead(), |
3d7548cb | 1064 | WaitForWrite(), wxSocketClient::WaitOnConnect() |
23324ae1 FM |
1065 | */ |
1066 | bool Wait(long seconds = -1, long millisecond = 0); | |
1067 | ||
1068 | /** | |
9940bebf VZ |
1069 | Wait until the connection is lost. |
1070 | ||
1071 | This may happen if the peer gracefully closes the connection or if the | |
1072 | connection breaks. | |
3c4f71cc | 1073 | |
7c913512 | 1074 | @param seconds |
4cc4bfaf FM |
1075 | Number of seconds to wait. |
1076 | If -1, it will wait for the default timeout, | |
3d7548cb | 1077 | as set with SetTimeout(). |
7c913512 | 1078 | @param millisecond |
4cc4bfaf | 1079 | Number of milliseconds to wait. |
3c4f71cc | 1080 | |
d29a9a8a | 1081 | @return Returns @true if the connection was lost, @false if the timeout |
e725ba4f | 1082 | was reached. |
3c4f71cc | 1083 | |
4cc4bfaf | 1084 | @see InterruptWait(), Wait() |
23324ae1 | 1085 | */ |
fc377125 | 1086 | bool WaitForLost(long seconds = -1, long millisecond = 0); |
23324ae1 FM |
1087 | |
1088 | /** | |
9940bebf | 1089 | Wait until the socket is readable. |
3d7548cb BP |
1090 | |
1091 | This might mean that queued data is available for reading or, for streamed | |
1092 | sockets, that the connection has been closed, so that a read operation will | |
1093 | complete immediately without blocking (unless the @b wxSOCKET_WAITALL flag | |
23324ae1 | 1094 | is set, in which case the operation might still block). |
3c4f71cc | 1095 | |
9940bebf VZ |
1096 | Notice that this function should not be called if there is already data |
1097 | available for reading on the socket. | |
1098 | ||
7c913512 | 1099 | @param seconds |
4cc4bfaf FM |
1100 | Number of seconds to wait. |
1101 | If -1, it will wait for the default timeout, | |
3d7548cb | 1102 | as set with SetTimeout(). |
7c913512 | 1103 | @param millisecond |
4cc4bfaf | 1104 | Number of milliseconds to wait. |
3c4f71cc | 1105 | |
d29a9a8a | 1106 | @return Returns @true if the socket becomes readable, @false on timeout. |
3c4f71cc | 1107 | |
4cc4bfaf | 1108 | @see InterruptWait(), Wait() |
23324ae1 FM |
1109 | */ |
1110 | bool WaitForRead(long seconds = -1, long millisecond = 0); | |
1111 | ||
1112 | /** | |
9940bebf | 1113 | Wait until the socket becomes writable. |
3d7548cb BP |
1114 | |
1115 | This might mean that the socket is ready to send new data, or for streamed | |
1116 | sockets, that the connection has been closed, so that a write operation is | |
1117 | guaranteed to complete immediately (unless the @b wxSOCKET_WAITALL flag is set, | |
23324ae1 | 1118 | in which case the operation might still block). |
3c4f71cc | 1119 | |
9940bebf VZ |
1120 | Notice that this function should not be called if the socket is already |
1121 | writable. | |
1122 | ||
7c913512 | 1123 | @param seconds |
4cc4bfaf FM |
1124 | Number of seconds to wait. |
1125 | If -1, it will wait for the default timeout, | |
3d7548cb | 1126 | as set with SetTimeout(). |
7c913512 | 1127 | @param millisecond |
4cc4bfaf | 1128 | Number of milliseconds to wait. |
3c4f71cc | 1129 | |
d29a9a8a | 1130 | @return Returns @true if the socket becomes writable, @false on timeout. |
3c4f71cc | 1131 | |
4cc4bfaf | 1132 | @see InterruptWait(), Wait() |
23324ae1 FM |
1133 | */ |
1134 | bool WaitForWrite(long seconds = -1, long millisecond = 0); | |
1135 | ||
1136 | /** | |
9940bebf | 1137 | Write up to the given number of bytes to the socket. |
3d7548cb | 1138 | |
23324ae1 | 1139 | Use LastCount() to verify the number of bytes actually written. |
3d7548cb | 1140 | |
23324ae1 | 1141 | Use Error() to determine if the operation succeeded. |
3c4f71cc | 1142 | |
7c913512 | 1143 | @param buffer |
4cc4bfaf | 1144 | Buffer with the data to be sent. |
7c913512 | 1145 | @param nbytes |
4cc4bfaf | 1146 | Number of bytes. |
3c4f71cc | 1147 | |
d29a9a8a | 1148 | @return Returns a reference to the current object. |
3c4f71cc | 1149 | |
3d7548cb BP |
1150 | @remarks |
1151 | ||
1152 | The exact behaviour of Write() depends on the combination of flags being used. | |
1153 | For a detailed explanation, see SetFlags(). | |
1154 | ||
1155 | @see Error(), LastError(), LastCount(), SetFlags() | |
23324ae1 | 1156 | */ |
9940bebf | 1157 | wxSocketBase& Write(const void* buffer, wxUint32 nbytes); |
23324ae1 FM |
1158 | |
1159 | /** | |
9940bebf VZ |
1160 | Sends a buffer which can be read using ReadMsg(). |
1161 | ||
1162 | WriteMsg() sends a short header before the data so that ReadMsg() | |
1163 | knows how much data should be actually read. | |
3d7548cb | 1164 | |
9940bebf VZ |
1165 | This function always waits for the entire buffer to be sent, unless an |
1166 | error occurs. | |
3d7548cb | 1167 | |
23324ae1 | 1168 | Use LastCount() to verify the number of bytes actually written. |
3d7548cb | 1169 | |
23324ae1 | 1170 | Use Error() to determine if the operation succeeded. |
3c4f71cc | 1171 | |
7c913512 | 1172 | @param buffer |
4cc4bfaf | 1173 | Buffer with the data to be sent. |
7c913512 | 1174 | @param nbytes |
4cc4bfaf | 1175 | Number of bytes to send. |
3c4f71cc | 1176 | |
d29a9a8a | 1177 | @return Returns a reference to the current object. |
3d7548cb BP |
1178 | |
1179 | @remarks | |
1180 | ||
1181 | WriteMsg() will behave as if the @b wxSOCKET_WAITALL flag was always set and | |
1182 | it will always ignore the @b wxSOCKET_NOWAIT flag. The exact behaviour of | |
1183 | WriteMsg() depends on the @b wxSOCKET_BLOCK flag. For a detailed explanation, | |
1184 | see SetFlags(). | |
1185 | ||
1186 | @see Error(), LastError(), LastCount(), SetFlags(), ReadMsg() | |
1187 | ||
23324ae1 | 1188 | */ |
9940bebf | 1189 | wxSocketBase& WriteMsg(const void* buffer, wxUint32 nbytes); |
3d7548cb BP |
1190 | |
1191 | //@} | |
1192 | ||
1193 | ||
1194 | /** | |
1195 | @name Handling Socket Events | |
1196 | */ | |
1197 | //@{ | |
1198 | ||
1199 | /** | |
1200 | Returns a pointer of the client data for this socket, as set with | |
1201 | SetClientData() | |
1202 | */ | |
1203 | void* GetClientData() const; | |
1204 | ||
1205 | /** | |
1206 | According to the @a notify value, this function enables | |
1207 | or disables socket events. If @a notify is @true, the events | |
1208 | configured with SetNotify() will | |
1209 | be sent to the application. If @a notify is @false; no events | |
1210 | will be sent. | |
1211 | */ | |
1212 | void Notify(bool notify); | |
1213 | ||
1214 | /** | |
1215 | Sets user-supplied client data for this socket. All socket events will | |
1216 | contain a pointer to this data, which can be retrieved with | |
1217 | the wxSocketEvent::GetClientData() function. | |
1218 | */ | |
1219 | void SetClientData(void* data); | |
1220 | ||
1221 | /** | |
1222 | Sets an event handler to be called when a socket event occurs. The | |
1223 | handler will be called for those events for which notification is | |
1224 | enabled with SetNotify() and | |
1225 | Notify(). | |
1226 | ||
1227 | @param handler | |
1228 | Specifies the event handler you want to use. | |
1229 | @param id | |
1230 | The id of socket event. | |
1231 | ||
1232 | @see SetNotify(), Notify(), wxSocketEvent, wxEvtHandler | |
1233 | */ | |
1234 | void SetEventHandler(wxEvtHandler& handler, int id = -1); | |
1235 | ||
1236 | /** | |
1237 | Specifies which socket events are to be sent to the event handler. | |
1238 | The @a flags parameter may be combination of flags ORed together. The | |
1239 | following flags can be used: | |
1240 | ||
1241 | @beginFlagTable | |
1242 | @flag{wxSOCKET_INPUT_FLAG} to receive @b wxSOCKET_INPUT. | |
1243 | @flag{wxSOCKET_OUTPUT_FLAG} to receive @b wxSOCKET_OUTPUT. | |
1244 | @flag{wxSOCKET_CONNECTION_FLAG} to receive @b wxSOCKET_CONNECTION. | |
1245 | @flag{wxSOCKET_LOST_FLAG} to receive @b wxSOCKET_LOST. | |
1246 | @endFlagTable | |
1247 | ||
1248 | For example: | |
1249 | ||
1250 | @code | |
1251 | sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); | |
1252 | sock.Notify(true); | |
1253 | @endcode | |
1254 | ||
1255 | In this example, the user will be notified about incoming socket data and | |
1256 | whenever the connection is closed. | |
1257 | ||
1258 | For more information on socket events see @ref wxSocketEventFlags . | |
1259 | */ | |
1260 | void SetNotify(wxSocketEventFlags flags); | |
1261 | ||
1262 | //@} | |
23324ae1 FM |
1263 | }; |
1264 | ||
1265 | ||
e54c96f1 | 1266 | |
23324ae1 FM |
1267 | /** |
1268 | @class wxDatagramSocket | |
7c913512 | 1269 | |
41e69d79 FM |
1270 | @todo docme |
1271 | ||
23324ae1 | 1272 | @library{wxnet} |
3d7548cb | 1273 | @category{net} |
23324ae1 FM |
1274 | */ |
1275 | class wxDatagramSocket : public wxSocketBase | |
1276 | { | |
1277 | public: | |
1278 | /** | |
1279 | Constructor. | |
3c4f71cc | 1280 | |
41e69d79 FM |
1281 | @param addr |
1282 | The socket address. | |
7c913512 | 1283 | @param flags |
41e69d79 | 1284 | Socket flags (See wxSocketBase::SetFlags()). |
23324ae1 | 1285 | */ |
8067ee11 FM |
1286 | wxDatagramSocket(const wxSockAddress& addr, |
1287 | wxSocketFlags flags = wxSOCKET_NONE); | |
23324ae1 FM |
1288 | |
1289 | /** | |
3d7548cb | 1290 | Destructor. Please see wxSocketBase::Destroy(). |
23324ae1 | 1291 | */ |
adaaa686 | 1292 | virtual ~wxDatagramSocket(); |
23324ae1 | 1293 | |
23324ae1 | 1294 | /** |
9940bebf VZ |
1295 | Write a buffer of @a nbytes bytes to the socket. |
1296 | ||
3d7548cb BP |
1297 | Use wxSocketBase::LastCount() to verify the number of bytes actually wrote. |
1298 | Use wxSocketBase::Error() to determine if the operation succeeded. | |
3c4f71cc | 1299 | |
7c913512 | 1300 | @param address |
4cc4bfaf | 1301 | The address of the destination peer for this data. |
7c913512 | 1302 | @param buffer |
4cc4bfaf | 1303 | Buffer where read data is. |
7c913512 | 1304 | @param nbytes |
4cc4bfaf | 1305 | Number of bytes. |
3c4f71cc | 1306 | |
d29a9a8a | 1307 | @return Returns a reference to the current object. |
3d7548cb BP |
1308 | |
1309 | @see wxSocketBase::LastError(), wxSocketBase::SetFlags() | |
23324ae1 | 1310 | */ |
7323ff1a FM |
1311 | wxDatagramSocket& SendTo(const wxSockAddress& address, |
1312 | const void* buffer, wxUint32 nbytes); | |
23324ae1 | 1313 | }; |
e54c96f1 | 1314 |