Commit 458054e7 by Marcus Winter Committed by unknown

mtapi_network_c: switched from nonblocking sockets to select

parent 010e2be0
......@@ -22,16 +22,6 @@ void embb_mtapi_network_socket_finalize(
}
}
int embb_mtapi_network_socket_setnonblock(
embb_mtapi_network_socket_t * that) {
u_long mode = 1;
if (SOCKET_ERROR == ioctlsocket(that->handle, FIONBIO, &mode)) {
return 0;
} else {
return 1;
}
}
int embb_mtapi_network_socket_bind_and_listen(
embb_mtapi_network_socket_t * that,
char const * host,
......@@ -61,8 +51,7 @@ int embb_mtapi_network_socket_bind_and_listen(
return 0;
}
// set non-block
return embb_mtapi_network_socket_setnonblock(that);
return 1;
}
int embb_mtapi_network_socket_accept(
......@@ -96,6 +85,41 @@ int embb_mtapi_network_socket_connect(
return 1;
}
int embb_mtapi_network_socket_select(
embb_mtapi_network_socket_t * that,
int timeout
) {
fd_set read_set;
int max_fd;
int err;
struct timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = timeout % 1000;
FD_ZERO(&read_set);
FD_SET(that->handle, &read_set);
max_fd = that->handle;
if (timeout >= 0) {
err = select(max_fd, &read_set, NULL, NULL, &tv);
} else {
err = select(max_fd, &read_set, NULL, NULL, NULL);
}
if (0 == err) {
// timeout
return 0;
}
if (SOCKET_ERROR == err) {
return 0;
}
if (FD_ISSET(that->handle, &read_set)) {
return 1;
}
return 0;
}
int embb_mtapi_network_socket_sendbuffer(
embb_mtapi_network_socket_t * that,
embb_mtapi_network_buffer_t * buffer) {
......@@ -111,12 +135,13 @@ int embb_mtapi_network_socket_recvbuffer(
embb_mtapi_network_socket_t * that,
embb_mtapi_network_buffer_t * buffer) {
u_long bytes_available = 0;
int err;
if (0 != ioctlsocket(that->handle, FIONREAD, &bytes_available))
return 0;
if (buffer->capacity > (int)bytes_available)
return 0;
int result = recv(that->handle, buffer->data, buffer->capacity, 0);
if (result != buffer->capacity)
err = recv(that->handle, buffer->data, buffer->capacity, 0);
if (err != buffer->capacity)
return 0;
buffer->size = buffer->capacity;
return buffer->size;
......
......@@ -66,6 +66,11 @@ int embb_mtapi_network_socket_connect(
uint16_t port
);
int embb_mtapi_network_socket_select(
embb_mtapi_network_socket_t * that,
int timeout
);
int embb_mtapi_network_socket_sendbuffer(
embb_mtapi_network_socket_t * that,
embb_mtapi_network_buffer_t * buffer
......
......@@ -55,19 +55,30 @@ void NetworkSocketTest::TestBasic() {
err = embb_mtapi_network_socket_bind_and_listen(&server_sock, "127.0.0.1", 4711);
PT_EXPECT(err != 0);
err = embb_mtapi_network_socket_select(&server_sock, 1);
PT_EXPECT(err == 0);
err = embb_mtapi_network_socket_initialize(&client_sock);
PT_EXPECT(err != 0);
err = embb_mtapi_network_socket_connect(&client_sock, "127.0.0.1", 4711);
PT_EXPECT(err != 0);
err = embb_mtapi_network_socket_select(&server_sock, -1);
PT_EXPECT(err != 0);
err = embb_mtapi_network_socket_accept(&server_sock, &accept_sock);
PT_EXPECT(err != 0);
err = embb_mtapi_network_socket_select(&accept_sock, 1);
PT_EXPECT(err == 0);
err = embb_mtapi_network_buffer_push_back_int32(&send_buffer, 0x12345678);
PT_EXPECT(err == 4);
err = embb_mtapi_network_socket_sendbuffer(&client_sock, &send_buffer);
PT_EXPECT(err == 4);
err = embb_mtapi_network_socket_select(&accept_sock, -1);
PT_EXPECT(err != 0);
err = embb_mtapi_network_socket_recvbuffer(&accept_sock, &recv_buffer);
PT_EXPECT(err == 4);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment