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:
@ -90,6 +90,7 @@ ExternalProject_Add(wasm-app
|
||||
tcp_client.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build
|
||||
tcp_server.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build
|
||||
send_recv.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build
|
||||
socket_opts.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build
|
||||
)
|
||||
|
||||
add_executable(tcp_server ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src/tcp_server.c)
|
||||
@ -102,6 +103,8 @@ target_link_libraries(send_recv pthread)
|
||||
|
||||
add_executable(addr_resolve ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src/addr_resolve.c)
|
||||
|
||||
add_executable(socket_opts ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src/socket_opts.c)
|
||||
|
||||
############################################
|
||||
## Build iwasm with wasi and pthread support
|
||||
############################################
|
||||
|
||||
@ -82,4 +82,17 @@ Data:
|
||||
And mourns for us
|
||||
```
|
||||
|
||||
`socket_opts.wasm` shows an example of getting and setting various supported socket options
|
||||
```bash
|
||||
$ ./iwasm ./socket_opts.wasm
|
||||
```
|
||||
|
||||
The output is:
|
||||
```bash
|
||||
[Client] Create socket
|
||||
recv_timeout is expected
|
||||
send_timeout is expected
|
||||
[Client] Close socket
|
||||
```
|
||||
|
||||
Refer to [socket api document](../../doc/socket_api.md) for more details.
|
||||
|
||||
@ -80,3 +80,4 @@ compile_with_clang(tcp_server.c)
|
||||
compile_with_clang(tcp_client.c)
|
||||
compile_with_clang(send_recv.c)
|
||||
compile_with_clang(addr_resolve.c)
|
||||
compile_with_clang(socket_opts.c)
|
||||
|
||||
56
samples/socket-api/wasm-src/socket_opts.c
Normal file
56
samples/socket-api/wasm-src/socket_opts.c
Normal file
@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#ifdef __wasi__
|
||||
#include <wasi_socket_ext.h>
|
||||
#endif
|
||||
|
||||
#define OPTION_ASSERT(A, B, OPTION) \
|
||||
if (A == B) { \
|
||||
printf("%s is expected\n", OPTION); \
|
||||
} \
|
||||
else { \
|
||||
printf("%s is unexpected\n", OPTION); \
|
||||
return EXIT_FAILURE; \
|
||||
}
|
||||
|
||||
struct timeval
|
||||
to_timeval(time_t tv_sec, suseconds_t tv_usec)
|
||||
{
|
||||
struct timeval tv = { tv_sec, tv_usec };
|
||||
return tv;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int socket_fd = 0;
|
||||
struct timeval tv;
|
||||
socklen_t tv_len = sizeof(tv);
|
||||
|
||||
printf("[Client] Create socket\n");
|
||||
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (socket_fd == -1) {
|
||||
perror("Create socket failed");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
tv = to_timeval(123, 1000);
|
||||
setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
tv = to_timeval(0, 0);
|
||||
getsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, &tv_len);
|
||||
OPTION_ASSERT(tv.tv_sec, 123, "SO_RCVTIMEO tv_sec");
|
||||
OPTION_ASSERT(tv.tv_usec, 1000, "SO_RCVTIMEO tv_usec");
|
||||
|
||||
tv = to_timeval(456, 2000);
|
||||
setsockopt(socket_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||
tv = to_timeval(0, 0);
|
||||
getsockopt(socket_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, &tv_len);
|
||||
OPTION_ASSERT(tv.tv_sec, 456, "SO_SNDTIMEO tv_sec");
|
||||
OPTION_ASSERT(tv.tv_usec, 2000, "SO_SNDTIMEO tv_usec");
|
||||
|
||||
printf("[Client] Close socket\n");
|
||||
close(socket_fd);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user