net: websocket: Refactor because of timeout overhaul
Mention in websocket API documentation that the timeout value is in milliseconds. Check timeout values properly using K_TIMEOUT_EQ() macro. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
parent
1b04d44247
commit
5cf75fe091
5 changed files with 32 additions and 23 deletions
|
@ -117,8 +117,8 @@ struct websocket_request {
|
|||
* packets to the Websocket server.
|
||||
* @param req Websocket request. User should allocate and fill the request
|
||||
* data.
|
||||
* @param timeout Max timeout to wait for the connection. The timeout value
|
||||
* cannot be 0 as there would be no time to receive the data.
|
||||
* @param timeout Max timeout to wait for the connection. The timeout value is
|
||||
* in milliseconds. Value NET_WAIT_FOREVER means to wait forever.
|
||||
* @param user_data User specified data that is passed to the callback.
|
||||
*
|
||||
* @return Websocket id to be used when sending/receiving Websocket data.
|
||||
|
@ -142,7 +142,8 @@ int websocket_connect(int http_sock, struct websocket_request *req,
|
|||
* must have opcode WEBSOCKET_OPCODE_CONTINUE. If final == true and this
|
||||
* is the only message, then opcode should have proper opcode (text or
|
||||
* binary) set.
|
||||
* @param timeout How long to try to send the message.
|
||||
* @param timeout How long to try to send the message. The value is in
|
||||
* milliseconds. Value NET_WAIT_FOREVER means to wait forever.
|
||||
*
|
||||
* @return <0 if error, >=0 amount of bytes sent
|
||||
*/
|
||||
|
@ -162,6 +163,8 @@ int websocket_send_msg(int ws_sock, const u8_t *payload, size_t payload_len,
|
|||
* @param message_type Type of the message.
|
||||
* @param remaining How much there is data left in the message after this read.
|
||||
* @param timeout How long to try to receive the message.
|
||||
* The value is in milliseconds. Value NET_WAIT_FOREVER means to wait
|
||||
* forever.
|
||||
*
|
||||
* @return <0 if error, >=0 amount of bytes received
|
||||
*/
|
||||
|
|
|
@ -175,7 +175,7 @@ static size_t how_much_to_send(size_t max_len)
|
|||
static ssize_t sendall_with_ws_api(int sock, const void *buf, size_t len)
|
||||
{
|
||||
return websocket_send_msg(sock, buf, len, WEBSOCKET_OPCODE_DATA_TEXT,
|
||||
true, true, K_FOREVER);
|
||||
true, true, NET_WAIT_FOREVER);
|
||||
}
|
||||
|
||||
static ssize_t sendall_with_bsd_api(int sock, const void *buf, size_t len)
|
||||
|
@ -199,7 +199,7 @@ static void recv_data_wso_api(int sock, size_t amount, u8_t *buf,
|
|||
buf_len - read_pos,
|
||||
&message_type,
|
||||
&remaining,
|
||||
K_NO_WAIT);
|
||||
0);
|
||||
if (ret <= 0) {
|
||||
if (ret == -EAGAIN) {
|
||||
k_sleep(K_MSEC(50));
|
||||
|
@ -326,7 +326,7 @@ void main(void)
|
|||
};
|
||||
int sock4 = -1, sock6 = -1;
|
||||
int websock4 = -1, websock6 = -1;
|
||||
s32_t timeout = K_SECONDS(3);
|
||||
s32_t timeout = 3 * MSEC_PER_SEC;
|
||||
struct sockaddr_in6 addr6;
|
||||
struct sockaddr_in addr4;
|
||||
size_t amount;
|
||||
|
|
|
@ -271,7 +271,6 @@ int websocket_connect(int sock, struct websocket_request *wreq,
|
|||
ctx->real_sock = sock;
|
||||
ctx->tmp_buf = wreq->tmp_buf;
|
||||
ctx->tmp_buf_len = wreq->tmp_buf_len;
|
||||
ctx->timeout = timeout;
|
||||
ctx->sec_accept_key = sec_accept_key;
|
||||
ctx->http_cb = wreq->http_cb;
|
||||
|
||||
|
@ -475,8 +474,14 @@ static int websocket_prepare_and_send(struct websocket_context *ctx,
|
|||
*/
|
||||
return verify_sent_and_received_msg(&msg, !(header[1] & BIT(7)));
|
||||
#else
|
||||
k_timeout_t tout = K_FOREVER;
|
||||
|
||||
if (timeout != NET_WAIT_FOREVER) {
|
||||
tout = K_MSEC(timeout);
|
||||
}
|
||||
|
||||
return sendmsg(ctx->real_sock, &msg,
|
||||
timeout == K_NO_WAIT ? MSG_DONTWAIT : 0);
|
||||
K_TIMEOUT_EQ(tout, K_NO_WAIT) ? MSG_DONTWAIT : 0);
|
||||
#endif /* CONFIG_NET_TEST */
|
||||
}
|
||||
|
||||
|
@ -659,6 +664,11 @@ int websocket_recv_msg(int ws_sock, u8_t *buf, size_t buf_len,
|
|||
int recv_len = 0;
|
||||
size_t can_copy, left;
|
||||
int ret;
|
||||
k_timeout_t tout = K_FOREVER;
|
||||
|
||||
if (timeout != NET_WAIT_FOREVER) {
|
||||
tout = K_MSEC(timeout);
|
||||
}
|
||||
|
||||
#if defined(CONFIG_NET_TEST)
|
||||
/* Websocket unit test does not use socket layer but feeds
|
||||
|
@ -697,7 +707,7 @@ int websocket_recv_msg(int ws_sock, u8_t *buf, size_t buf_len,
|
|||
#else
|
||||
ret = recv(ctx->real_sock, &ctx->tmp_buf[ctx->tmp_buf_pos],
|
||||
ctx->tmp_buf_len - ctx->tmp_buf_pos,
|
||||
timeout == K_NO_WAIT ? MSG_DONTWAIT : 0);
|
||||
K_TIMEOUT_EQ(tout, K_NO_WAIT) ? MSG_DONTWAIT : 0);
|
||||
#endif /* CONFIG_NET_TEST */
|
||||
|
||||
if (ret < 0) {
|
||||
|
@ -783,7 +793,7 @@ int websocket_recv_msg(int ws_sock, u8_t *buf, size_t buf_len,
|
|||
ret = input_len;
|
||||
#else
|
||||
ret = recv(ctx->real_sock, ctx->tmp_buf, ctx->tmp_buf_len,
|
||||
timeout == K_NO_WAIT ? MSG_DONTWAIT : 0);
|
||||
K_TIMEOUT_EQ(tout, K_NO_WAIT) ? MSG_DONTWAIT : 0);
|
||||
#endif /* CONFIG_NET_TEST */
|
||||
|
||||
if (ret < 0) {
|
||||
|
@ -903,13 +913,13 @@ static int websocket_recv(struct websocket_context *ctx, u8_t *buf,
|
|||
|
||||
static ssize_t websocket_read_vmeth(void *obj, void *buffer, size_t count)
|
||||
{
|
||||
return (ssize_t)websocket_recv(obj, buffer, count, K_FOREVER);
|
||||
return (ssize_t)websocket_recv(obj, buffer, count, NET_WAIT_FOREVER);
|
||||
}
|
||||
|
||||
static ssize_t websocket_write_vmeth(void *obj, const void *buffer,
|
||||
size_t count)
|
||||
{
|
||||
return (ssize_t)websocket_send(obj, buffer, count, K_FOREVER);
|
||||
return (ssize_t)websocket_send(obj, buffer, count, NET_WAIT_FOREVER);
|
||||
}
|
||||
|
||||
static ssize_t websocket_sendto_ctx(void *obj, const void *buf, size_t len,
|
||||
|
@ -918,10 +928,10 @@ static ssize_t websocket_sendto_ctx(void *obj, const void *buf, size_t len,
|
|||
socklen_t addrlen)
|
||||
{
|
||||
struct websocket_context *ctx = obj;
|
||||
s32_t timeout = K_FOREVER;
|
||||
s32_t timeout = NET_WAIT_FOREVER;
|
||||
|
||||
if (flags & ZSOCK_MSG_DONTWAIT) {
|
||||
timeout = K_NO_WAIT;
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
ARG_UNUSED(dest_addr);
|
||||
|
@ -935,10 +945,10 @@ static ssize_t websocket_recvfrom_ctx(void *obj, void *buf, size_t max_len,
|
|||
socklen_t *addrlen)
|
||||
{
|
||||
struct websocket_context *ctx = obj;
|
||||
s32_t timeout = K_FOREVER;
|
||||
s32_t timeout = NET_WAIT_FOREVER;
|
||||
|
||||
if (flags & ZSOCK_MSG_DONTWAIT) {
|
||||
timeout = K_NO_WAIT;
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
ARG_UNUSED(src_addr);
|
||||
|
|
|
@ -78,10 +78,6 @@ struct websocket_context {
|
|||
/** Websocket connection masking value */
|
||||
u32_t masking_value;
|
||||
|
||||
/** Timeout for Websocket operations.
|
||||
*/
|
||||
s32_t timeout;
|
||||
|
||||
/** Amount of data received. */
|
||||
u64_t total_read;
|
||||
|
||||
|
|
|
@ -73,7 +73,7 @@ static int test_recv_buf(u8_t *feed_buf, size_t feed_len,
|
|||
ctx_ptr = POINTER_TO_INT(&test_data);
|
||||
|
||||
return websocket_recv_msg(ctx_ptr, recv_buf, recv_len,
|
||||
msg_type, remaining, K_NO_WAIT);
|
||||
msg_type, remaining, 0);
|
||||
}
|
||||
|
||||
/* Websocket frame, header is 6 bytes, FIN bit is set, opcode is text (1),
|
||||
|
@ -347,7 +347,7 @@ static void test_send_and_recv_lorem_ipsum(void)
|
|||
ret = websocket_send_msg(POINTER_TO_INT(&ctx),
|
||||
lorem_ipsum, test_msg_len,
|
||||
WEBSOCKET_OPCODE_DATA_TEXT, true, true,
|
||||
K_FOREVER);
|
||||
NET_WAIT_FOREVER);
|
||||
zassert_equal(ret, test_msg_len,
|
||||
"Should have sent %zd bytes but sent %d instead",
|
||||
test_msg_len, ret);
|
||||
|
@ -367,7 +367,7 @@ static void test_recv_two_large_split_msg(void)
|
|||
|
||||
ret = websocket_send_msg(POINTER_TO_INT(&ctx), lorem_ipsum,
|
||||
test_msg_len, WEBSOCKET_OPCODE_DATA_TEXT,
|
||||
false, true, K_FOREVER);
|
||||
false, true, NET_WAIT_FOREVER);
|
||||
zassert_equal(ret, test_msg_len,
|
||||
"1st should have sent %zd bytes but sent %d instead",
|
||||
test_msg_len, ret);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue