diff options
author | José Bollo <jose.bollo@iot.bzh> | 2016-04-21 17:45:44 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2016-04-21 17:45:44 +0200 |
commit | 5eaa2c12a8b89f8a16f0759db88d65b56c82918c (patch) | |
tree | 5ff91305ba212269b3b546f7dc33168b13d452af /src/websock.c | |
parent | 45e4c16ed05d6b5fed969b08aa873d6c24b5f948 (diff) |
websocket refactoring
Change-Id: Ia39ec6c01ce1fc6b3921b0433ab872d47ebdbbc4
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/websock.c')
-rw-r--r-- | src/websock.c | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/websock.c b/src/websock.c index 945ccd9d..afb832e8 100644 --- a/src/websock.c +++ b/src/websock.c @@ -146,15 +146,18 @@ static inline int websock_send(struct websock *ws, int last, int rsv1, int rsv2, return websock_send_internal(ws, first, buffer, size); } -int websock_close(struct websock *ws) +int websock_close_empty(struct websock *ws) { - return websock_send(ws, 1, 0, 0, 0, OPCODE_CLOSE, NULL, 0); + return websock_close(ws, WEBSOCKET_CODE_NOT_SET, NULL, 0); } -int websock_close_code(struct websock *ws, uint16_t code, const void *data, size_t length) +int websock_close(struct websock *ws, uint16_t code, const void *data, size_t length) { unsigned char buffer[125]; + if (code == WEBSOCKET_CODE_NOT_SET && length == 0) + return websock_send(ws, 1, 0, 0, 0, OPCODE_CLOSE, NULL, 0); + /* checks the length */ if (length > 123) { errno = EINVAL; @@ -203,6 +206,14 @@ int websock_binary(struct websock *ws, int last, const void *data, size_t length return websock_send(ws, last, 0, 0, 0, OPCODE_BINARY, data, length); } +int websock_error(struct websock *ws, uint16_t code, const void *data, size_t size) +{ + int rc = websock_close(ws, code, data, size); + if (ws->itf->on_error != NULL) + ws->itf->on_error(ws->closure, code, data, size); + return rc; +} + static int read_header(struct websock *ws) { if (ws->lenhead < ws->szhead) { @@ -397,11 +408,11 @@ loop: goto loop; too_long_error: - websock_close_code(ws, WEBSOCKET_CODE_MESSAGE_TOO_LARGE, NULL, 0); + websock_error(ws, WEBSOCKET_CODE_MESSAGE_TOO_LARGE, NULL, 0); return 0; protocol_error: - websock_close_code(ws, WEBSOCKET_CODE_PROTOCOL_ERROR, NULL, 0); + websock_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR, NULL, 0); return 0; } |