int one = 1;
tport_ws_primary_t *wspri = (tport_ws_primary_t *)self->tp_pri;
tport_ws_t *wstp = (tport_ws_t *)self;
+ char *buffer, *wbuffer;
self->tp_has_connection = 1;
if (setsockopt(socket, SOL_TCP, TCP_NODELAY, (void *)&one, sizeof one) == -1)
- return *return_reason = "TCP_NODELAY", -1;
+ return *return_reason = "TCP_NODELAY", -1;
+
+#if defined(SO_KEEPALIVE)
+ setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (void *)&one, sizeof one);
+#endif
+ one = 30;
+#if defined(TCP_KEEPIDLE)
+ setsockopt(socket, SOL_TCP, TCP_KEEPIDLE, (void *)&one, sizeof one);
+#endif
+#if defined(TCP_KEEPINTVL)
+ setsockopt(socket, SOL_TCP, TCP_KEEPINTVL, (void *)&one, sizeof one);
+#endif
+
if (!accepted)
tport_ws_setsndbuf(socket, 64 * 1024);
if ( wspri->ws_secure ) wstp->ws_secure = 1;
-
memset(&wstp->ws, 0, sizeof(wstp->ws));
- if (ws_init(&wstp->ws, socket, 65336, wstp->ws_secure ? wspri->ssl_ctx : NULL, 0) < 0) {
+
+ buffer = (char *) su_alloc((su_home_t *)self, 65536);
+ wbuffer = (char *) su_alloc((su_home_t *)self, 65536);
+
+ if (ws_init(&wstp->ws, socket, buffer, wbuffer, 65336, wstp->ws_secure ? wspri->ssl_ctx : NULL, 0) < 0) {
return *return_reason = "WS_INIT", -1;
}
wstp->ws_initialized = 1;
self->tp_pre_framed = 1;
+
return 0;
}
return r;
}
-int ws_init(wsh_t *wsh, ws_socket_t sock, size_t buflen, SSL_CTX *ssl_ctx, int close_sock)
+int ws_init(wsh_t *wsh, ws_socket_t sock, char *buffer, char *wbuffer, size_t buflen, SSL_CTX *ssl_ctx, int close_sock)
{
memset(wsh, 0, sizeof(*wsh));
wsh->sock = sock;
wsh->buflen = buflen;
wsh->secure = ssl_ctx ? 1 : 0;
- if (!wsh->buffer) {
+ if (buffer) {
+ wsh->buffer = buffer;
+ } else if (!wsh->buffer) {
wsh->buffer = malloc(wsh->buflen);
assert(wsh->buffer);
+ wsh->free_buffer = 1;
+ }
+
+ if (wbuffer) {
+ wsh->wbuffer = wbuffer;
+ } else if (!wsh->wbuffer) {
+ wsh->wbuffer = malloc(wsh->buflen);
+ assert(wsh->wbuffer);
+ wsh->free_wbuffer = 1;
}
if (wsh->secure) {
wsh->ssl = NULL;
}
- if (wsh->buffer) {
+ if (wsh->free_buffer && wsh->buffer) {
free(wsh->buffer);
wsh->buffer = NULL;
}
- if (wsh->wbuffer) {
+ if (wsh->free_wbuffer && wsh->wbuffer) {
free(wsh->wbuffer);
wsh->wbuffer = NULL;
}
return -1;
}
-
- if (!wsh->wbuffer) {
- wsh->wbuffer = malloc(wsh->buflen);
- assert(wsh->wbuffer);
- }
-
-
memcpy(wsh->wbuffer + wsh->wdatalen, data, bytes);
wsh->wdatalen += bytes;
int handshake;
uint8_t down;
int secure;
- unsigned close_sock:1;
- unsigned :0;
+ uint8_t free_buffer;
+ uint8_t free_wbuffer;
+ uint8_t close_sock;
} wsh_t;
issize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc);
issize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes);
issize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data);
issize_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 close_sock);
+int ws_init(wsh_t *wsh, ws_socket_t sock, char *buffer, char *wbuffer, size_t buflen, SSL_CTX *ssl_ctx, int close_sock);
issize_t ws_close(wsh_t *wsh, int16_t reason);
void ws_destroy(wsh_t *wsh);
void init_ssl(void);