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