From fa8ccff7c3abdafa59a0dd53a9bc459fa1004127 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Fri, 25 Jan 2013 21:19:22 -0600 Subject: [PATCH] polish --- .../libsofia-sip-ua/tport/tport_type_ws.c | 26 ++------ libs/sofia-sip/libsofia-sip-ua/tport/ws.c | 64 ++++++++++++++++--- libs/sofia-sip/libsofia-sip-ua/tport/ws.h | 10 ++- 3 files changed, 70 insertions(+), 30 deletions(-) diff --git a/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c b/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c index fabd68e349..de354a1e15 100644 --- a/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c +++ b/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c @@ -205,7 +205,7 @@ int tport_recv_stream_ws(tport_t *self) ws_opcode_t oc; if ( !wstp->ws_initialized ) { - if (ws_init(ws, self->tp_socket, 65336, wstp->ws_secure ? wspri->ssl_ctx : NULL) == -2) { + if (ws_init(ws, self->tp_socket, 65336, wstp->ws_secure ? wspri->ssl_ctx : NULL, 0) == -2) { return 2; } wstp->ws_initialized = 1; @@ -266,13 +266,9 @@ ssize_t tport_send_stream_ws(tport_t const *self, msg_t *msg, ssize_t nerror; tport_ws_t *wstp = (tport_ws_t *)self; wsh_t *ws = wstp->ws; - char xbuf[65536] = ""; - int blen = 0; - enum { WSBUFSIZE = 2048 }; - for (i = 0; i < iovlen; i = j) { char *buf = wstp->wstp_buffer; unsigned wsbufsize = WSBUFSIZE; @@ -303,18 +299,9 @@ ssize_t tport_send_stream_ws(tport_t const *self, msg_t *msg, } else { iov[j].siv_base = buf, iov[j].siv_len = m; } - - //* hacked to push to buffer - if (blen + m < sizeof(xbuf)) { - memcpy(xbuf+blen, buf, m); - nerror = m; - blen += m; - } else { - nerror = -1; - } - //*/ - //nerror = ws_write_frame(ws, WSOC_TEXT, buf, m); + nerror = ws_feed_buf(ws, buf, m); + SU_DEBUG_9(("tport_ws_writevec: vec %p %p %lu ("MOD_ZD")\n", (void *)ws, (void *)iov[i].siv_base, (LU)iov[i].siv_len, nerror)); @@ -335,11 +322,8 @@ ssize_t tport_send_stream_ws(tport_t const *self, msg_t *msg, break; } - //* hacked .... - if (size) { - size = ws_write_frame(ws, WSOC_TEXT, xbuf, blen); - } - //*/ + ws_send_buf(ws, WSOC_TEXT); + return size; } diff --git a/libs/sofia-sip/libsofia-sip-ua/tport/ws.c b/libs/sofia-sip/libsofia-sip-ua/tport/ws.c index bdbef0aebc..65fa826e1c 100644 --- a/libs/sofia-sip/libsofia-sip-ua/tport/ws.c +++ b/libs/sofia-sip/libsofia-sip-ua/tport/ws.c @@ -288,7 +288,7 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes) if (wsh->ssl) { do { r = SSL_read(wsh->ssl, data, bytes); - if (x++) {usleep(10000); + if (x++) usleep(10000); } while (r == -1 && SSL_get_error(wsh->ssl, r) == SSL_ERROR_WANT_READ && x < 100); return r; @@ -296,12 +296,12 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes) do { r = recv(wsh->sock, data, bytes, 0); - if (x++) {usleep(10000); + if (x++) usleep(10000); } while (r == -1 && (errno == EAGAIN || errno == EINTR) && x < 100); - if (r<0) { + //if (r<0) { //printf("READ FAIL: %s\n", strerror(errno)); - } + //} return r; } @@ -322,18 +322,22 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes) r = send(wsh->sock, data, bytes, 0); } while (r == -1 && (errno == EAGAIN || errno == EINTR)); - if (r<0) { + //if (r<0) { //printf("wRITE FAIL: %s\n", strerror(errno)); - } + //} return r; } -int ws_init(wsh_t *wsh, ws_socket_t sock, size_t buflen, SSL_CTX *ssl_ctx) +int ws_init(wsh_t *wsh, ws_socket_t sock, size_t buflen, SSL_CTX *ssl_ctx, int close_sock) { memset(wsh, 0, sizeof(*wsh)); wsh->sock = sock; + if (close_sock) { + wsh->close_sock = 1; + } + if (buflen > MAXLEN) { buflen = MAXLEN; } @@ -396,7 +400,10 @@ ssize_t ws_close(wsh_t *wsh, int16_t reason) wsh->ssl = NULL; } - //close(wsh->sock); + if (wsh->close_sock) { + close(wsh->sock); + } + wsh->sock = ws_sock_invalid; if (wsh->buffer) { @@ -404,6 +411,11 @@ ssize_t ws_close(wsh_t *wsh, int16_t reason) wsh->buffer = NULL; } + if (wsh->wbuffer) { + free(wsh->wbuffer); + wsh->wbuffer = NULL; + } + return reason * -1; @@ -560,6 +572,42 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data) } } +ssize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes) +{ + + if (bytes + wsh->wdatalen > wsh->buflen) { + return -1; + } + + + if (!wsh->wbuffer) { + wsh->wbuffer = malloc(wsh->buflen); + assert(wsh->wbuffer); + } + + + memcpy(wsh->wbuffer + wsh->wdatalen, data, bytes); + + wsh->wdatalen += bytes; + + return bytes; +} + +ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc) +{ + ssize_t r = 0; + + if (!wsh->wdatalen) { + return -1; + } + + r = ws_write_frame(wsh, oc, wsh->wbuffer, wsh->wdatalen); + + wsh->wdatalen = 0; + + return r; +} + ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes) { diff --git a/libs/sofia-sip/libsofia-sip-ua/tport/ws.h b/libs/sofia-sip/libsofia-sip-ua/tport/ws.h index 58c74eee83..2b4d139097 100644 --- a/libs/sofia-sip/libsofia-sip-ua/tport/ws.h +++ b/libs/sofia-sip/libsofia-sip-ua/tport/ws.h @@ -53,8 +53,10 @@ typedef enum { typedef struct wsh_s { ws_socket_t sock; char *buffer; + char *wbuffer; size_t buflen; ssize_t datalen; + ssize_t wdatalen; char *payload; ssize_t plen; ssize_t rplen; @@ -62,13 +64,19 @@ typedef struct wsh_s { int handshake; uint8_t down; int secure; + unsigned close_sock:1; + unsigned :0; } wsh_t; +ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc); +ssize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes); + + ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes); ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes); ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data); ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes); -int ws_init(wsh_t *wsh, ws_socket_t sock, size_t buflen, SSL_CTX *ssl_ctx); +int ws_init(wsh_t *wsh, ws_socket_t sock, size_t buflen, SSL_CTX *ssl_ctx, int close_sock); ssize_t ws_close(wsh_t *wsh, int16_t reason); void init_ssl(void); void deinit_ssl(void);