mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-03-13 12:40:17 +00:00
use static buffer and nonblocking socket in websocket client
This commit is contained in:
parent
c613c65446
commit
94f3b90040
@ -1 +1 @@
|
|||||||
Fri Jun 28 10:39:50 CDT 2013
|
Wed Jul 3 11:09:02 CDT 2013
|
||||||
|
@ -432,7 +432,6 @@ int tport_ws_init_secondary(tport_t *self, int socket, int accepted,
|
|||||||
int one = 1;
|
int one = 1;
|
||||||
tport_ws_primary_t *wspri = (tport_ws_primary_t *)self->tp_pri;
|
tport_ws_primary_t *wspri = (tport_ws_primary_t *)self->tp_pri;
|
||||||
tport_ws_t *wstp = (tport_ws_t *)self;
|
tport_ws_t *wstp = (tport_ws_t *)self;
|
||||||
char *buffer, *wbuffer;
|
|
||||||
|
|
||||||
self->tp_has_connection = 1;
|
self->tp_has_connection = 1;
|
||||||
|
|
||||||
@ -458,10 +457,7 @@ int tport_ws_init_secondary(tport_t *self, int socket, int accepted,
|
|||||||
|
|
||||||
memset(&wstp->ws, 0, sizeof(wstp->ws));
|
memset(&wstp->ws, 0, sizeof(wstp->ws));
|
||||||
|
|
||||||
buffer = (char *) su_alloc((su_home_t *)self, 65536);
|
if (ws_init(&wstp->ws, socket, wstp->ws_secure ? wspri->ssl_ctx : NULL, 0) < 0) {
|
||||||
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;
|
return *return_reason = "WS_INIT", -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
#include "ws.h"
|
#include "ws.h"
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
#include <fcntl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define SHA1_HASH_SIZE 20
|
#define SHA1_HASH_SIZE 20
|
||||||
struct globals_s globals;
|
struct globals_s globals;
|
||||||
|
|
||||||
@ -322,9 +327,9 @@ issize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes)
|
|||||||
#endif
|
#endif
|
||||||
} while (r == -1 && (errno == EAGAIN || errno == EINTR) && x < 100);
|
} while (r == -1 && (errno == EAGAIN || errno == EINTR) && x < 100);
|
||||||
|
|
||||||
//if (r<0) {
|
if (x >= 100) {
|
||||||
// printf("READ FAIL: %s\n", strerror(errno));
|
r = -1;
|
||||||
//}
|
}
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -352,7 +357,54 @@ issize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ws_init(wsh_t *wsh, ws_socket_t sock, char *buffer, char *wbuffer, size_t buflen, SSL_CTX *ssl_ctx, int close_sock)
|
#ifdef _MSC_VER
|
||||||
|
static int setup_socket(ws_socket_t sock)
|
||||||
|
{
|
||||||
|
unsigned log v = 1;
|
||||||
|
|
||||||
|
if (ioctlsocket(ssock, FIONBIO, &v) == SOCKET_ERROR) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int restore_socket(ws_socket_t sock)
|
||||||
|
{
|
||||||
|
unsigned log v = 0;
|
||||||
|
|
||||||
|
if (ioctlsocket(ssock, FIONBIO, &v) == SOCKET_ERROR) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
static int setup_socket(ws_socket_t sock)
|
||||||
|
{
|
||||||
|
int flags = fcntl(sock, F_GETFL, 0);
|
||||||
|
return fcntl(sock, F_SETFL, flags | O_NONBLOCK);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int restore_socket(ws_socket_t sock)
|
||||||
|
{
|
||||||
|
int flags = fcntl(sock, F_GETFL, 0);
|
||||||
|
|
||||||
|
flags &= ~O_NONBLOCK;
|
||||||
|
|
||||||
|
return fcntl(sock, F_SETFL, flags);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock)
|
||||||
{
|
{
|
||||||
memset(wsh, 0, sizeof(*wsh));
|
memset(wsh, 0, sizeof(*wsh));
|
||||||
wsh->sock = sock;
|
wsh->sock = sock;
|
||||||
@ -365,28 +417,10 @@ int ws_init(wsh_t *wsh, ws_socket_t sock, char *buffer, char *wbuffer, size_t bu
|
|||||||
wsh->close_sock = 1;
|
wsh->close_sock = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buflen > MAXLEN) {
|
wsh->buflen = sizeof(wsh->buffer);
|
||||||
buflen = MAXLEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
wsh->buflen = buflen;
|
|
||||||
wsh->secure = ssl_ctx ? 1 : 0;
|
wsh->secure = ssl_ctx ? 1 : 0;
|
||||||
|
|
||||||
if (buffer) {
|
setup_socket(sock);
|
||||||
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) {
|
if (wsh->secure) {
|
||||||
int code;
|
int code;
|
||||||
@ -464,16 +498,6 @@ void ws_destroy(wsh_t *wsh)
|
|||||||
SSL_free(wsh->ssl);
|
SSL_free(wsh->ssl);
|
||||||
wsh->ssl = NULL;
|
wsh->ssl = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wsh->free_buffer && wsh->buffer) {
|
|
||||||
free(wsh->buffer);
|
|
||||||
wsh->buffer = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wsh->free_wbuffer && wsh->wbuffer) {
|
|
||||||
free(wsh->wbuffer);
|
|
||||||
wsh->wbuffer = NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
issize_t ws_close(wsh_t *wsh, int16_t reason)
|
issize_t ws_close(wsh_t *wsh, int16_t reason)
|
||||||
@ -494,6 +518,8 @@ issize_t ws_close(wsh_t *wsh, int16_t reason)
|
|||||||
ws_raw_write(wsh, fr, 4);
|
ws_raw_write(wsh, fr, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
restore_socket(wsh->sock);
|
||||||
|
|
||||||
if (wsh->close_sock) {
|
if (wsh->close_sock) {
|
||||||
close(wsh->sock);
|
close(wsh->sock);
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
//#define WSS_STANDALONE 1
|
//#define WSS_STANDALONE 1
|
||||||
|
|
||||||
#define MAXLEN 0x10000
|
|
||||||
#define WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
#define WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
||||||
#define B64BUFFLEN 1024
|
#define B64BUFFLEN 1024
|
||||||
|
|
||||||
@ -59,8 +58,8 @@ typedef enum {
|
|||||||
|
|
||||||
typedef struct wsh_s {
|
typedef struct wsh_s {
|
||||||
ws_socket_t sock;
|
ws_socket_t sock;
|
||||||
char *buffer;
|
char buffer[65536];
|
||||||
char *wbuffer;
|
char wbuffer[65536];
|
||||||
size_t buflen;
|
size_t buflen;
|
||||||
issize_t datalen;
|
issize_t datalen;
|
||||||
issize_t wdatalen;
|
issize_t wdatalen;
|
||||||
@ -71,8 +70,6 @@ typedef struct wsh_s {
|
|||||||
int handshake;
|
int handshake;
|
||||||
uint8_t down;
|
uint8_t down;
|
||||||
int secure;
|
int secure;
|
||||||
uint8_t free_buffer;
|
|
||||||
uint8_t free_wbuffer;
|
|
||||||
uint8_t close_sock;
|
uint8_t close_sock;
|
||||||
} wsh_t;
|
} wsh_t;
|
||||||
|
|
||||||
@ -84,7 +81,7 @@ issize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes);
|
|||||||
issize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes);
|
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_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);
|
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, char *buffer, char *wbuffer, size_t buflen, SSL_CTX *ssl_ctx, int close_sock);
|
int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock);
|
||||||
issize_t ws_close(wsh_t *wsh, int16_t reason);
|
issize_t ws_close(wsh_t *wsh, int16_t reason);
|
||||||
void ws_destroy(wsh_t *wsh);
|
void ws_destroy(wsh_t *wsh);
|
||||||
void init_ssl(void);
|
void init_ssl(void);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user