8 #ifndef INCL_evo_impl_sysio_sock_h 9 #define INCL_evo_impl_sysio_sock_h 12 #include "../string.h" 18 #pragma comment(lib, "Ws2_32.lib") 21 #include <sys/socket.h> 23 #include <netinet/in.h> 24 #include <arpa/inet.h> 26 #if defined(__APPLE__) && !defined(SOCK_NONBLOCK) 27 #define SOCK_NONBLOCK O_NONBLOCK 40 static void set_timeval_ms(
struct timeval& tm, ulong ms) {
41 const ulong MSEC_PER_SEC = 1000;
42 const ulong USEC_PER_MSEC = 1000;
43 tm.tv_sec = ms / MSEC_PER_SEC;
44 tm.tv_usec = (ms - (tm.tv_sec * MSEC_PER_SEC)) * USEC_PER_MSEC;
59 case EAccess:
return "Permission denied for socket (EAccess)";
60 case EClosed:
return "Socket is closed (EClosed)";
61 case EExist:
return "Socket address/port in use or not available (EExist)";
62 case ESize:
return "Message too long (ESize)";
63 case ELimit:
return "No more socket descriptors available (ELimit)";
64 case ELength:
return "Host or address string too long (ELength)";
65 case EFail:
return "Connection refused or reset (EFail)";
66 case ENotFound:
return "Address not found (ENotFound)";
114 struct sockaddr addr;
126 struct addrinfo hints;
130 static const int NUMERIC = AI_NUMERICHOST | AI_NUMERICHOST;
136 memset(&hints, 0,
sizeof(hints));
137 hints.ai_family = family;
152 memset(&hints, 0,
sizeof(hints));
153 hints.ai_family = family;
163 hints.ai_socktype = SOCK_STREAM;
164 hints.ai_protocol = IPPROTO_TCP;
172 hints.ai_socktype = SOCK_DGRAM;
173 hints.ai_protocol = IPPROTO_UDP;
185 Error resolve(
const char* host,
const char* port=NULL,
int flags=AI_NUMERICSERV) {
187 hints.ai_flags = flags;
188 code = ::getaddrinfo(host, port, &hints, &ptr);
191 case 0: err =
ENone;
break;
193 case EAI_FAIL: err =
EFail;
break;
197 case EAI_BADFLAGS: err =
EInval;
break;
198 case EAI_MEMORY: err =
ESpace;
break;
199 case EAI_AGAIN: err =
ERetry;
break;
200 #if defined(EAI_NODATA) && !defined(_WIN32) // win32: same as EAI_NONAME 203 #if defined(EAI_ADDRFAMILY) 204 case EAI_ADDRFAMILY: err =
ENotFound;
break;
221 return resolve(host, port_str.
data_, flags | AI_NUMERICSERV);
233 const uint MAX_HOST_SIZE = 256;
234 char hostbuf[MAX_HOST_SIZE];
237 if (hostlen < MAX_HOST_SIZE) {
238 memcpy(hostbuf, host.
data(), hostlen);
239 hostbuf[hostlen] =
'\0';
241 flags |= AI_NUMERICSERV;
244 return resolve(hostbuf, port_str.
data_, flags);
246 return resolve(hostbuf,
EVO_CNULL, flags);
248 errno = ENAMETOOLONG;
259 {
return resolve(host,
EVO_CNULL, NUMERIC); }
269 {
return resolve(host, port, NUMERIC); }
279 {
return resolve(host, port, NUMERIC); }
308 static const bool STREAM_SEEKABLE =
false;
309 static const ulong TIMEOUT_DEFAULT = 30000;
329 Init() : active(
false)
337 active = (::WSAStartup(MAKEWORD(2, 2), &data) == 0);
341 if (active && ::WSACleanup() == 0)
349 static const Handle INVALID = INVALID_SOCKET;
350 static const int SOCK_ERROR = SOCKET_ERROR;
353 timeout_ms = TIMEOUT_DEFAULT;
359 timeout_ms = TIMEOUT_DEFAULT;
364 bool shutdown(
Shutdown how=sINOUT) {
365 if (handle == INVALID)
367 else if (::shutdown(handle, how) != 0)
368 errno = WSAGetLastError();
375 if (handle == INVALID)
377 else if (::closesocket(handle) == SOCKET_ERROR)
378 errno = WSAGetLastError();
383 Error set_nonblock(
bool enable=
true) {
384 if (handle != INVALID) {
385 ulong flag = (enable ? 1 : 0);
386 if (::ioctlsocket(handle, FIONBIO, &flag) != 0) {
388 const int last_error = WSAGetLastError();
390 switch (last_error) {
391 case WSANOTINITIALISED:
392 case WSAENOTSOCK: err =
EClosed;
break;
403 if (handle == INVALID) {
407 if (timeout_ms == 0 || read_wait(err, timeout_ms)) {
408 Handle client_handle;
409 if (client_address != NULL)
410 client_handle = ::accept(handle, &((
SocketAddress*)client_address)->addr, &client_address->
addrlen);
412 client_handle = ::accept(handle, NULL, NULL);
413 if (client_handle != INVALID) {
414 client_socket.
attach(client_handle);
418 err = get_socket_error();
425 if (accept(err, client_socket, client_address)) {
426 err = set_nonblock();
427 return (err ==
ENone);
432 bool read_wait(
Error& err, ulong read_timeout_ms) {
435 FD_SET(handle, &read_set);
437 struct timeval timeout;
438 SysWindows::set_timeval_ms(timeout, read_timeout_ms);
440 int waitresult = ::select(0, &read_set, NULL, NULL, &timeout);
441 if (waitresult == SOCK_ERROR) {
442 const int last_error = WSAGetLastError();
444 switch (last_error) {
445 case WSANOTINITIALISED:
446 case WSAENOTSOCK: err =
EClosed;
break;
450 }
else if (waitresult == 0) {
451 errno = WSAETIMEDOUT;
458 ulong read(
Error& err,
void* buf, ulong size) {
459 if (handle == INVALID) {
466 if (timeout_ms > 0 && !read_wait(err, timeout_ms))
468 int result = ::recv(handle, (
char*)buf, size, 0);
469 if (result == SOCK_ERROR) {
470 const int last_error = WSAGetLastError();
472 switch (last_error) {
473 case WSANOTINITIALISED:
476 case WSAENOTCONN: err =
EClosed;
break;
477 case WSAEMSGSIZE: err =
ESize;
break;
478 case WSAEFAULT: err =
EPtr;
break;
479 case WSAEWOULDBLOCK: err =
ENonBlock;
break;
480 default: err =
ERead;
break;
485 return (ulong)result;
488 ulong readfrom(
Error& err,
void* buf, ulong size,
struct sockaddr* address=NULL, socklen_t* address_len=NULL,
int flags=0) {
489 if (handle == INVALID) {
496 if (timeout_ms > 0 && !read_wait(err, timeout_ms))
498 int result = ::recvfrom(handle, (
char*)buf, size, flags, address, address_len);
503 }
else if (result < 0) {
504 const int last_error = WSAGetLastError();
506 switch (last_error) {
507 case WSANOTINITIALISED:
510 case WSAENOTCONN: err =
EClosed;
break;
511 case WSAEMSGSIZE: err =
ESize;
break;
512 case WSAEFAULT: err =
EPtr;
break;
513 case WSAEWOULDBLOCK: err =
ENonBlock;
break;
514 case WSAECONNRESET: err =
EFail;
break;
515 default: err =
ERead;
break;
520 return (ulong)result;
523 bool write_wait(
Error& err, ulong write_timeout_ms) {
526 FD_SET(handle, &write_set);
528 struct timeval timeout;
529 SysWindows::set_timeval_ms(timeout, write_timeout_ms);
531 int waitresult = ::select(0, NULL, &write_set, NULL, &timeout);
532 if (waitresult == SOCK_ERROR) {
533 const int last_error = WSAGetLastError();
535 switch (last_error) {
536 case WSANOTINITIALISED:
537 case WSAENOTSOCK: err =
EClosed;
break;
541 }
else if (waitresult == 0) {
542 errno = WSAETIMEDOUT;
549 ulong write(
Error& err,
const void* buf, ulong size) {
550 if (handle == INVALID) {
557 if (timeout_ms > 0 && !write_wait(err, timeout_ms))
559 int result = ::send(handle, (
const char*)buf, (
int)size, 0);
560 if (result == SOCK_ERROR) {
561 const int last_error = WSAGetLastError();
563 switch (last_error) {
564 case WSANOTINITIALISED:
567 case WSAENOTCONN: err =
EClosed;
break;
568 case WSAENOBUFS: err =
ESpace;
break;
569 case WSAEMSGSIZE: err =
ESize;
break;
570 case WSAEFAULT: err =
EPtr;
break;
571 case WSAEWOULDBLOCK: err =
ENonBlock;
break;
572 default: err =
EWrite;
break;
577 return (ulong)result;
580 ulong writeto(
Error& err,
const void* buf, ulong size,
const struct sockaddr* address, socklen_t address_len,
int flags=0) {
581 if (handle == INVALID) {
591 if (timeout_ms > 0 && !write_wait(err, timeout_ms))
593 int result = ::sendto(handle, (
const char*)buf, (
int)size, flags, address, address_len);
595 const int last_error = WSAGetLastError();
597 switch (last_error) {
598 case WSANOTINITIALISED:
601 case WSAENOTCONN: err =
EClosed;
break;
602 case WSAEACCES: err =
EAccess;
break;
603 case WSAENOBUFS: err =
ESpace;
break;
604 case WSAEMSGSIZE: err =
ESize;
break;
605 case WSAEFAULT: err =
EPtr;
break;
606 case WSAEWOULDBLOCK: err =
ENonBlock;
break;
607 case WSAECONNABORTED:
608 case WSAECONNRESET: err =
EFail;
break;
609 case WSAEADDRNOTAVAIL:
610 case WSAEDESTADDRREQ:
611 case WSAEHOSTUNREACH: err =
ENotFound;
break;
612 case WSAEAFNOSUPPORT: err =
EInvalOp;
break;
613 default: err =
EWrite;
break;
618 return (ulong)result;
621 Error getopt(
int level,
int optname,
void* buf, uint& size) {
622 if (size > (uint)INT_MAX)
623 size = (uint)INT_MAX;
624 int optlen = (int)size;
625 int result = ::getsockopt(handle, level, optname, (
char*)buf, &optlen);
628 const int last_error = WSAGetLastError();
630 switch (last_error) {
631 case WSANOTINITIALISED:
633 case WSAENOTCONN: err =
EClosed;
break;
634 case WSAEFAULT: err =
EPtr;
break;
635 case WSAEINVAL: err =
EInval;
break;
636 case WSAENOPROTOOPT: err =
EInvalOp;
break;
641 size = (optlen > 0) ? (uint)optlen : 0;
645 Error setopt(
int level,
int optname,
const void* buf, uint size) {
646 if (size > (uint)INT_MAX) {
650 int result = ::setsockopt(handle, level, optname, (
const char*)buf, (
int)size);
653 const int last_error = WSAGetLastError();
655 switch (last_error) {
656 case WSANOTINITIALISED:
658 case WSAENOTCONN: err =
EClosed;
break;
659 case WSAEFAULT: err =
EPtr;
break;
660 case WSAEINVAL: err =
EInval;
break;
661 case WSAENOPROTOOPT: err =
EInvalOp;
break;
671 const int lasterror = errno;
672 out << errormsg_socket(err) << ListBase<char>(
" (errno:", 8) << lasterror <<
')';
673 if (err !=
ENone && lasterror != 0) {
674 const int BUF_SIZE = 256;
676 DWORD len = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, lasterror, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, BUF_SIZE, NULL);
700 static const Handle INVALID = -1;
701 static const int SOCK_ERROR = -1;
705 timeout_ms = TIMEOUT_DEFAULT;
717 timeout_ms = TIMEOUT_DEFAULT;
731 if (handle == INVALID)
733 else if (::shutdown(handle, (
int)how) == 0)
742 if (handle != INVALID) {
757 #if defined(SOCK_NONBLOCK) 758 if (handle != INVALID) {
760 int flags = ::fcntl(handle, F_GETFL, NULL);
765 flags &= ~(int)O_NONBLOCK;
766 error_flag = (::fcntl(handle, F_SETFL, flags) != 0);
774 case EACCES: err =
EAccess;
break;
775 case EINVAL: err =
EInval;
break;
776 case EBADF: err =
EClosed;
break;
800 if (handle == INVALID) {
803 }
else if (write_wait(err, timeout_ms)) {
806 socklen_t len =
sizeof(value);
807 if (::getsockopt(handle, SOL_SOCKET, SO_ERROR, &value, &len) == 0) {
808 if (value == EINTR && autoresume) {
810 }
else if (value == 0 || value == EISCONN) {
814 err = get_socket_error(value);
816 err = get_socket_error();
836 {
return SysLinuxIo::read(err, handle, buf, size, timeout_ms, autoresume); }
850 ulong
readfrom(
Error& err,
void* buf, ulong size,
struct sockaddr* address=NULL, socklen_t* address_len=NULL,
int flags=0) {
851 if (handle == INVALID) {
856 if (size > SSIZE_MAX)
862 result = ::recvfrom(handle, buf, size, flags, address, address_len);
869 case ENOSPC: err =
ESpace;
break;
870 case EFBIG: err =
ESize;
break;
871 case EFAULT: err =
EPtr;
break;
872 case EBADF: err =
EClosed;
break;
873 #if EAGAIN != EWOULDBLOCK 876 case EWOULDBLOCK: err =
ENonBlock;
break;
877 default: err =
ERead;
break;
884 return (ulong)result;
914 ulong
writeto(
Error& err,
const void* buf, ulong size,
const struct sockaddr* address, socklen_t address_len,
int flags=0) {
915 if (handle == INVALID) {
920 if (size > SSIZE_MAX) {
929 result = ::sendto(handle, buf, size, flags, address, address_len);
933 }
else if (result < 0) {
939 case EACCES: err =
EAccess;
break;
940 case ENOSPC: err =
ESpace;
break;
941 case EFBIG: err =
ESize;
break;
942 case EFAULT: err =
EPtr;
break;
946 case EBADF: err =
EClosed;
break;
947 #if EAGAIN != EWOULDBLOCK 950 case EWOULDBLOCK: err =
ENonBlock;
break;
951 case EOPNOTSUPP: err =
EInvalOp;
break;
952 case ECONNRESET: err =
EFail;
break;
953 case EDESTADDRREQ: err =
ENotFound;
break;
954 default: err =
EWrite;
break;
961 return (ulong)result;
973 if (handle == INVALID) {
980 if (client_address != NULL)
981 client_handle = ::accept(handle, &((
SocketAddress*)client_address)->addr, &client_address->
addrlen);
983 client_handle = ::accept(handle, NULL, NULL);
985 if (client_handle > 0) {
986 client_socket.
attach(client_handle);
990 const int last_error = errno;
991 if (last_error == EINTR && autoresume)
993 err = get_socket_error(last_error);
1012 if (handle == INVALID) {
1018 #if defined(__linux) && defined(SOCK_NONBLOCK) && defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10)) && !defined(EVO_SOCKET_NO_ACCEPT4) 1022 if (client_address != NULL)
1023 client_handle = ::accept4(handle, &((
SocketAddress*)client_address)->addr, &client_address->
addrlen, SOCK_NONBLOCK);
1025 client_handle = ::accept4(handle, NULL, NULL, SOCK_NONBLOCK);
1026 if (client_handle > 0) {
1027 client_socket.
attach(client_handle);
1032 const int last_error = errno;
1033 if (last_error == EINTR && autoresume)
1035 err = get_socket_error(last_error);
1040 if (accept(err, client_socket, client_address)) {
1041 err = set_nonblock();
1042 return (err ==
ENone);
1060 socklen_t optlen = (socklen_t)size;
1061 int result = ::getsockopt(handle, level, optname, buf, &optlen);
1066 case ENOTSOCK: err =
EClosed;
break;
1067 case EFAULT: err =
EPtr;
break;
1068 case EINVAL: err =
EInval;
break;
1069 case ENOPROTOOPT: err =
EInvalOp;
break;
1074 size = (optlen > 0) ? (uint)optlen : 0;
1092 const socklen_t optlen = (socklen_t)size;
1093 int result = ::setsockopt(handle, level, optname, buf, optlen);
1098 case ENOTSOCK: err =
EClosed;
break;
1099 case EFAULT: err =
EPtr;
break;
1100 case EINVAL: err =
EInval;
break;
1101 case ENOPROTOOPT: err =
EInvalOp;
break;
1117 template<
class TOut>
1119 const int lasterror = errno;
1120 out << errormsg_socket(err) << ListBase<char>(
" (errno:", 8) << lasterror <<
')';
1121 if (err !=
ENone && lasterror != 0) {
1122 const int BUF_SIZE = 256;
1125 #if defined(__linux) 1126 p = strerror_r(lasterror, buf, BUF_SIZE);
1128 p = (strerror_r(lasterror, buf, BUF_SIZE) == 0 ? buf : NULL);
1131 out << ListBase<char>(
": ", 2) << p;
1147 {
return (handle != INVALID); }
1156 bool listen(
Error& err,
struct addrinfo* address_info,
int backlog=SOMAXCONN,
bool all=
true) {
1157 assert( address_info != NULL );
1160 uint best_state = 0;
1162 for (ulong counter=0; (counter == 0 || all) && address_info != NULL; address_info=address_info->ai_next, ++counter) {
1163 Error sock_err = create_socket(address_info->ai_family, address_info->ai_socktype, address_info->ai_protocol);
1164 if (sock_err !=
ENone) {
1165 if (best_state == 0) {
1173 if (::bind(handle, address_info->ai_addr, (
int)address_info->ai_addrlen) != SOCK_ERROR) {
1175 if (::listen(handle, backlog) != SOCK_ERROR) {
1180 if (cur_state >= best_state) {
1181 err = IoSocket::get_socket_error();
1183 best_state = cur_state;
1200 bool listen(
Error& err,
struct sockaddr* address, socklen_t address_len,
int backlog=SOMAXCONN,
int socktype=SOCK_STREAM,
int protocol=0) {
1201 assert( address != NULL );
1202 assert( address_len > 0 );
1204 err = create_socket(address->sa_family, socktype, protocol);
1206 if (::bind(handle, address, address_len) != SOCK_ERROR) {
1207 if (::listen(handle, backlog) != SOCK_ERROR) {
1212 err = IoSocket::get_socket_error();
1213 const int last_errno = errno;
1228 bool bind(
Error& err,
struct sockaddr* address, socklen_t address_len,
int socktype=SOCK_DGRAM,
int protocol=0) {
1229 assert( address != NULL );
1230 assert( address_len > 0 );
1232 err = create_socket(address->sa_family, socktype, protocol);
1234 if (::bind(handle, address, address_len) != SOCK_ERROR) {
1238 err = IoSocket::get_socket_error();
1239 const int last_errno = errno;
1253 bool cast(
Error& err,
int family=AF_INET,
int socktype=SOCK_DGRAM,
int protocol=0) {
1255 err = create_socket(family, socktype, protocol);
1258 err = IoSocket::get_socket_error();
1259 const int last_errno = errno;
1280 uint best_state = 0;
1282 for (ulong counter=0; (counter == 0 || all) && address_info != NULL; address_info=address_info->ai_next, ++counter) {
1283 Error sock_err = create_socket(address_info->ai_family, address_info->ai_socktype, address_info->ai_protocol);
1284 if (sock_err !=
ENone) {
1285 if (best_state == 0) {
1293 const int result = ::connect(handle, address_info->ai_addr, (
int)address_info->ai_addrlen);
1294 if (result != SOCK_ERROR) {
1298 err = IoSocket::get_socket_error();
1316 bool connect(
Error& err,
struct sockaddr* address, socklen_t address_len,
int socktype=SOCK_STREAM,
int protocol=0) {
1317 assert( address != NULL );
1318 assert( address_len > 0 );
1320 err = create_socket(address->sa_family, socktype, protocol);
1322 if (::connect(handle, address, address_len) != SOCK_ERROR) {
1326 err = IoSocket::get_socket_error();
1327 const int last_errno = errno;
1348 Handle result = handle;
1389 Error create_socket(
int domain,
int socktype,
int protocol) {
1390 if ((handle=::socket(domain, socktype, protocol)) == INVALID) {
1391 return IoSocket::get_socket_error();
1392 }
else if (nonblock)
1393 return set_nonblock(
true);
1397 static Error get_socket_error() {
1399 const int code = WSAGetLastError();
1401 case WSAEACCES: err =
EAccess;
break;
1402 case WSANOTINITIALISED:
1405 case WSAEPROTONOSUPPORT:
1407 case WSAESOCKTNOSUPPORT:
1408 case WSAEAFNOSUPPORT:
1411 case WSAEADDRNOTAVAIL: err =
EInval;
break;
1412 case WSAEOPNOTSUPP: err =
EInvalOp;
break;
1413 case WSAEADDRINUSE: err =
EExist;
break;
1414 case WSAENOBUFS: err =
ESpace;
break;
1415 case WSAEMFILE: err =
ELimit;
break;
1416 case WSAEHOSTUNREACH:
1417 case WSAENETUNREACH:
1419 case WSAECONNREFUSED: err =
EFail;
break;
1420 case WSAETIMEDOUT: err =
ETimeout;
break;
1422 case WSAEWOULDBLOCK: err =
ENonBlock;
break;
1423 case WSAEISCONN: err =
ENone;
break;
1433 Error create_socket(
int domain,
int type,
int protocol) {
1434 #if defined(SOCK_NONBLOCK) 1435 const int flags = (nonblock ? SOCK_NONBLOCK : 0);
1442 const int flags = 0;
1444 if ((handle=::socket(domain, type | flags, protocol)) == INVALID)
1445 return IoSocket::get_socket_error();
1449 static Error get_socket_error(
int code) {
1453 case EACCES: err =
EAccess;
break;
1455 case EPROTONOSUPPORT:
1460 case EAFNOSUPPORT: err =
EInval;
break;
1461 case EOPNOTSUPP: err =
EInvalOp;
break;
1463 case EADDRINUSE: err =
EExist;
break;
1465 case ENOBUFS: err =
ESpace;
break;
1467 case EMFILE: err =
ELimit;
break;
1470 case ECONNREFUSED: err =
EFail;
break;
1471 case ETIMEDOUT: err =
ETimeout;
break;
1474 #if EAGAIN != EWOULDBLOCK 1478 case EINTR: err =
ESignal;
break;
1484 static Error get_socket_error()
1485 {
return IoSocket::get_socket_error(errno); }
1490 case EAccess:
return "Permission denied for socket (EAccess)";
1491 case EClosed:
return "Socket is closed (EClosed)";
1492 case EExist:
return "Socket address/port in use or not available (EExist)";
1493 case ESize:
return "Message too long (ESize)";
1494 case ELimit:
return "No more socket descriptors available (ELimit)";
1495 case ELength:
return "Host or address string too long (ELength)";
1496 case EFail:
return "Connection refused or reset (EFail)";
1497 case ENotFound:
return "Address not found (ENotFound)";
Error convert(const char *host)
Convert host address to one or more socket addresses.
Definition: sysio_sock.h:258
TOut & errormsg_out(TOut &out, Error err)
Write error message with errno to output stream/string.
Definition: sys.h:1203
ExceptionSocketIn ExceptionInT
Input exception type for socket stream.
Definition: sysio_sock.h:311
Base stream exception for all stream errors, see Exception.
Definition: sys.h:1389
Operation timed out.
Definition: sys.h:1131
T & max(T &a, T &b)
Returns highest of given values.
Definition: alg.h:47
Shutdown
Used with shutdown().
Definition: sysio_sock.h:687
Seek
Seek starting position.
Definition: sysio.h:203
System call interrupted by signal (Linux/Unix), auto-resume disabled.
Definition: sys.h:1132
General stream or resource read error.
Definition: sys.h:1143
Error convert(const char *host, ushort port)
Convert host address to one or more socket addresses.
Definition: sysio_sock.h:268
ulongl pos(Error &err)
Not supported with sockets.
Definition: sysio_sock.h:1369
Socket config exception for getopt() or setopt() errors, see Exception.
Definition: sysio_sock.h:73
Operation would block (non-blocking I/O)
Definition: sys.h:1118
Invalid argument or data.
Definition: sys.h:1123
bool autoresume
Whether to auto-resume I/O operation after signal received [Linux/Unix].
Definition: sysio_sock.h:1136
Permission denied.
Definition: sys.h:1135
ulong write(Error &err, const void *buf, ulong size)
Definition: sysio_sock.h:899
Invalid pointer used.
Definition: sys.h:1133
bool listen(Error &err, struct sockaddr *address, socklen_t address_len, int backlog=SOMAXCONN, int socktype=SOCK_STREAM, int protocol=0)
Create and bind socket to address and listen for connections.
Definition: sysio_sock.h:1200
socklen_t addrlen
Address length.
Definition: sysio_sock.h:96
Invalid or unsupported operation.
Definition: sys.h:1122
Socket open exception for socket connect/bind/listen errors, see Exception.
Definition: sysio_sock.h:77
Stream or resource is closed
Definition: sys.h:1130
void free()
Free results allocated from resolve() or resolve_address.
Definition: sysio_sock.h:284
bool accept_nonblock(Error &err, IoSocket &client_socket, SocketAddressBase *client_address=NULL)
Accept connection from listening socket and set the new connection as non-blocking.
Definition: sysio_sock.h:1011
Not enough space/memory available.
Definition: sys.h:1134
Output stream exception for stream write errors, see Exception.
Definition: sys.h:1401
String fixed-size buffer for formatting an integer.
Definition: string.h:54
ExceptionSocketOut ExceptionOutT
Output exception type for socket stream.
Definition: sysio_sock.h:312
ulong readfrom(Error &err, void *buf, ulong size, struct sockaddr *address=NULL, socklen_t *address_len=NULL, int flags=0)
Read message from socket device.
Definition: sysio_sock.h:850
Socket input stream exception for socket read errors, see Exception.
Definition: sysio_sock.h:81
Handle handle
Socket handle/descriptor.
Definition: sysio_sock.h:1353
bool listen(Error &err, struct addrinfo *address_info, int backlog=SOMAXCONN, bool all=true)
Create and bind socket using address info and listen for connections.
Definition: sysio_sock.h:1156
IoSocket(Handle socket)
Constructor attaching to existing socket.
Definition: sysio_sock.h:716
bool cast(Error &err, int family=AF_INET, int socktype=SOCK_DGRAM, int protocol=0)
Create unbound datagram socket for casting.
Definition: sysio_sock.h:1253
Resource not found.
Definition: sys.h:1137
int Handle
System socket handle.
Definition: sysio_sock.h:697
Size size() const
Get size.
System resource limit reached.
Definition: sys.h:1140
struct addrinfo * ptr
Pointer to first address in resolve results.
Definition: sysio_sock.h:127
Size limit exceeded.
Definition: sys.h:1139
Handle detach()
Detach and return socket handle.
Definition: sysio_sock.h:1347
No error.
Definition: sys.h:1115
bool connect(Error &err, struct addrinfo *address_info, bool all=true)
Create and connect socket using address info.
Definition: sysio_sock.h:1277
static const Init & init()
Initialize socket library.
Definition: sysio_sock.h:1360
int OptNum
General number type for socket options.
Definition: sysio_sock.h:698
Error
General Evo error code stored in exceptions, or used directly when exceptions are disabled...
Definition: sys.h:1113
Socket output stream exception for socket write errors, see Exception.
Definition: sysio_sock.h:85
static TOut & errormsg_out(TOut &out, Error err)
Write detailed error message with errno to output stream/string.
Definition: sysio_sock.h:1118
const char * errormsg_socket(Error error)
Get socket error message for error code.
Definition: sysio_sock.h:57
bool accept(Error &err, IoSocket &client_socket, SocketAddressBase *client_address=NULL)
Accept connection from listening socket.
Definition: sysio_sock.h:972
Error getopt(int level, int optname, void *buf, uint &size)
Get socket option value.
Definition: sysio_sock.h:1057
StrSizeT Size
List size integer type.
Definition: sublist.h:150
bool write_wait(Error &err, ulong timeout_ms)
Wait until socket is ready to write.
Definition: sysio_sock.h:895
Socket I/O device (used internally).
Definition: sysio_sock.h:307
static ulong read(Error &err, int handle, void *buf, ulong size, ulong timeout_ms, bool autoresume)
Definition: sysio.h:73
~IoSocket()
Destructor.
Definition: sysio_sock.h:1140
SocketAddressInfo(int family=AF_INET)
Constructor.
Definition: sysio_sock.h:135
IoSocket()
Constructor.
Definition: sysio_sock.h:704
Evo system I/O implementation.
SocketAddressInfo & reset(int family=AF_INET)
Reset data.
Definition: sysio_sock.h:151
SocketAddressInfo & tcp()
Setup for resolving to TCP address.
Definition: sysio_sock.h:162
String length limit exceeded
Definition: sys.h:1141
Used internally to initialize system socket API.
Definition: sysio_sock.h:694
int code
Internal return code from resolve()
Definition: sysio_sock.h:128
Temporary failure occurred, try again.
Definition: sys.h:1126
ulong timeout_ms
Current timeout in milliseconds, 0 for indefinite.
Definition: sysio_sock.h:314
bool read_wait(Error &err, ulong timeout_ms)
Wait until socket is ready to read.
Definition: sysio_sock.h:831
bool isopen() const
Get whether socket is open.
Definition: sysio_sock.h:1146
ulong writeto(Error &err, const void *buf, ulong size, const struct sockaddr *address, socklen_t address_len, int flags=0)
Write message to device and socket address.
Definition: sysio_sock.h:914
Error setopt(int level, int optname, const void *buf, uint size)
Set socket option with new value.
Definition: sysio_sock.h:1087
Unknown or unspecified error.
Definition: sys.h:1120
virtual void set_maxsize()
Set addrlen to max size for socket address.
Definition: sysio_sock.h:102
Evo C++ Library namespace.
Definition: alg.h:11
Input stream exception for stream read errors, see Exception.
Definition: sys.h:1397
static ulong write(Error &err, int handle, const void *buf, ulong size, ulong timeout_ms, bool autoresume)
Definition: sysio.h:143
static bool write_wait(Error &err, int handle, ulong timeout_ms, bool autoresume)
Definition: sysio.h:110
Resource already exists.
Definition: sys.h:1136
Error resolve(const char *host, const char *port=NULL, int flags=AI_NUMERICSERV)
Resolve or convert host name/address and port to one or more socket addresses.
Definition: sysio_sock.h:185
bool nonblock
Whether non-blocking I/O is enabled.
Definition: sysio_sock.h:1354
I/O device base class for streams.
Definition: sysio.h:230
bool connect_wait(Error &err, ulong timeout_ms)
Wait until socket connection is established and ready to write.
Definition: sysio_sock.h:799
Resolves socket name/address to socket address info.
Definition: sysio_sock.h:125
Error resolve(const SubString &host, ushort port=0, int flags=0)
Resolve or convert host name/address and port to one or more socket addresses.
Definition: sysio_sock.h:232
ulongl seek(Error &err, ulongl, Seek)
Not supported with sockets.
Definition: sysio_sock.h:1379
Error set_nonblock(bool enable=true)
Enable/Disable non-blocking I/O.
Definition: sysio_sock.h:756
char * data_
Data pointer, NULL if null.
Definition: sys.h:979
const char * errormsg(Error error)
Get general error message for error code.
Definition: sys.h:1155
Operation failed.
Definition: sys.h:1124
#define EVO_CREATE_EXCEPTION_IMPL_2(NAME, BASE, ERRMSG_FUNC)
Create an Evo exception implementation with custom errormsg() function.
Definition: sys.h:1338
void close()
Close connection and socket.
Definition: sysio_sock.h:741
Error convert(const SubString &host, ushort port=0)
Convert host address to one or more socket addresses.
Definition: sysio_sock.h:278
bool shutdown(Shutdown how=sINOUT)
Shutdown socket communication.
Definition: sysio_sock.h:730
static bool read_wait(Error &err, int handle, ulong timeout_ms, bool autoresume)
Definition: sysio.h:37
SocketAddressInfo & udp()
Setup for resolving to UDP address.
Definition: sysio_sock.h:171
Stream open exception for errors opening a stream, see Exception.
Definition: sys.h:1393
Reference and access existing string data.
Definition: substring.h:229
void attach(Handle socket)
Attach new socket handle.
Definition: sysio_sock.h:1339
#define EVO_CNULL
Shortcut for NULL as const-char pointer.
Definition: sys.h:611
Base for all Evo list types (used internally).
Definition: sys.h:976
bool bind(Error &err, struct sockaddr *address, socklen_t address_len, int socktype=SOCK_DGRAM, int protocol=0)
Create and bind datagram socket to address.
Definition: sysio_sock.h:1228
Base socket address.
Definition: sysio_sock.h:95
bool connect(Error &err, struct sockaddr *address, socklen_t address_len, int socktype=SOCK_STREAM, int protocol=0)
Create and connect socket using address.
Definition: sysio_sock.h:1316
Error resolve(const char *host, ushort port, int flags=0)
Resolve host name/address and port to one or more socket addresses.
Definition: sysio_sock.h:219
General stream or resource write error.
Definition: sys.h:1144
ulong read(Error &err, void *buf, ulong size)
Definition: sysio_sock.h:835
const char * data() const
Get data pointer.
Generic socket address (used internally).
Definition: sysio_sock.h:113
~SocketAddressInfo()
Destructor.
Definition: sysio_sock.h:144