Added socket send and recv timeout options (#1419)
Added socket send and recv timeout options with implementation for posix platform. This is part of a extending support for sockets in WASI. #1336. Also add sample that sets and reads back the send and receive timeouts using the native function binding.
This commit is contained in:
@ -351,6 +351,54 @@ os_socket_convert_sockaddr(struct sockaddr *addr, uint8_t *buf, size_t buflen,
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_send_timeout(bh_socket_t socket, uint64 timeout_us)
|
||||
{
|
||||
struct timeval tv;
|
||||
tv.tv_sec = timeout_us / 1000000UL;
|
||||
tv.tv_usec = timeout_us % 1000000UL;
|
||||
if (setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_send_timeout(bh_socket_t socket, uint64 *timeout_us)
|
||||
{
|
||||
struct timeval tv;
|
||||
socklen_t tv_len = sizeof(tv);
|
||||
if (getsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, &tv_len) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*timeout_us = (tv.tv_sec * 1000000UL) + tv.tv_usec;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_recv_timeout(bh_socket_t socket, uint64 timeout_us)
|
||||
{
|
||||
struct timeval tv;
|
||||
tv.tv_sec = timeout_us / 1000000UL;
|
||||
tv.tv_usec = timeout_us % 1000000UL;
|
||||
if (setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us)
|
||||
{
|
||||
struct timeval tv;
|
||||
socklen_t tv_len = sizeof(tv);
|
||||
if (getsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, &tv_len) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*timeout_us = (tv.tv_sec * 1000000UL) + tv.tv_usec;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_local(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4)
|
||||
|
||||
@ -511,6 +511,50 @@ int
|
||||
os_socket_addr_remote(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4);
|
||||
|
||||
/**
|
||||
* Set the send timeout until reporting an error
|
||||
*
|
||||
* @param socket the socket to set
|
||||
* @param time_us microseconds until timeout
|
||||
*
|
||||
* @return 0 if success, -1 otherwise
|
||||
*/
|
||||
int
|
||||
os_socket_set_send_timeout(bh_socket_t socket, uint64 timeout_us);
|
||||
|
||||
/**
|
||||
* Get the send timeout until reporting an error
|
||||
*
|
||||
* @param socket the socket to set
|
||||
* @param time_us the returned microseconds until timeout
|
||||
*
|
||||
* @return 0 if success, -1 otherwise
|
||||
*/
|
||||
int
|
||||
os_socket_get_send_timeout(bh_socket_t socket, uint64 *timeout_us);
|
||||
|
||||
/**
|
||||
* Set the recv timeout until reporting an error
|
||||
*
|
||||
* @param socket the socket to set
|
||||
* @param time_us microseconds until timeout
|
||||
*
|
||||
* @return 0 if success, -1 otherwise
|
||||
*/
|
||||
int
|
||||
os_socket_set_recv_timeout(bh_socket_t socket, uint64 timeout_us);
|
||||
|
||||
/**
|
||||
* Get the recv timeout until reporting an error
|
||||
*
|
||||
* @param socket the socket to set
|
||||
* @param time_us the returned microseconds until timeout
|
||||
*
|
||||
* @return 0 if success, -1 otherwise
|
||||
*/
|
||||
int
|
||||
os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -713,4 +713,36 @@ os_socket_addr_remote(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_send_timeout(bh_socket_t socket, uint64 timeout_us)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_send_timeout(bh_socket_t socket, uint64 *timeout_us)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_recv_timeout(bh_socket_t socket, uint64 timeout_us)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -209,6 +209,38 @@ os_socket_addr_local(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_send_timeout(bh_socket_t socket, uint64 timeout_us)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_send_timeout(bh_socket_t socket, uint64 *timeout_us)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_recv_timeout(bh_socket_t socket, uint64 timeout_us)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_remote(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4)
|
||||
|
||||
Reference in New Issue
Block a user