Format ports/bsd for standard style.
This commit is contained in:
+51
-35
@@ -57,7 +57,8 @@ typedef struct {
|
||||
|
||||
/* Some forward function declarations */
|
||||
|
||||
static int bws_cli_websocket_event(struct lws *wsi,
|
||||
static int bws_cli_websocket_event(
|
||||
struct lws *wsi,
|
||||
enum lws_callback_reasons reason,
|
||||
void *user,
|
||||
void *in,
|
||||
@@ -72,13 +73,13 @@ static pthread_mutex_t bws_cli_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
|
||||
|
||||
static struct lws_protocols bws_cli_direct_protocol[] = {
|
||||
{ BSC_WEBSOCKET_DIRECT_PROTOCOL_STR, bws_cli_websocket_event, 0, 0, 0, NULL,
|
||||
0 },
|
||||
0 },
|
||||
LWS_PROTOCOL_LIST_TERM
|
||||
};
|
||||
|
||||
static struct lws_protocols bws_cli_hub_protocol[] = {
|
||||
{ BSC_WEBSOCKET_HUB_PROTOCOL_STR, bws_cli_websocket_event, 0, 0, 0, NULL,
|
||||
0 },
|
||||
0 },
|
||||
LWS_PROTOCOL_LIST_TERM
|
||||
};
|
||||
|
||||
@@ -205,7 +206,8 @@ static void bws_set_disconnect_reason(BSC_WEBSOCKET_HANDLE h, uint16_t err_code)
|
||||
}
|
||||
}
|
||||
|
||||
static int bws_cli_websocket_event(struct lws *wsi,
|
||||
static int bws_cli_websocket_event(
|
||||
struct lws *wsi,
|
||||
enum lws_callback_reasons reason,
|
||||
void *user,
|
||||
void *in,
|
||||
@@ -267,8 +269,9 @@ static int bws_cli_websocket_event(struct lws *wsi,
|
||||
the WebSocket connection shall be closed with a
|
||||
status code of 1003 -WEBSOCKET_DATA_NOT_ACCEPTED.
|
||||
*/
|
||||
DEBUG_PRINTF("bws_cli_websocket_event() got non-binary frame, "
|
||||
"close connection for socket %d\n",
|
||||
DEBUG_PRINTF(
|
||||
"bws_cli_websocket_event() got non-binary frame, "
|
||||
"close connection for socket %d\n",
|
||||
h);
|
||||
lws_close_reason(
|
||||
wsi, LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE, NULL, 0);
|
||||
@@ -280,16 +283,18 @@ static int bws_cli_websocket_event(struct lws *wsi,
|
||||
pthread_mutex_unlock(&bws_cli_mutex);
|
||||
} else {
|
||||
if (!bws_cli_conn[h].fragment_buffer) {
|
||||
DEBUG_PRINTF("bws_cli_websocket_event() alloc %d bytes for "
|
||||
"socket %d\n",
|
||||
DEBUG_PRINTF(
|
||||
"bws_cli_websocket_event() alloc %d bytes for "
|
||||
"socket %d\n",
|
||||
len, h);
|
||||
bws_cli_conn[h].fragment_buffer = malloc(BSC_RX_BUFFER_LEN);
|
||||
if (!bws_cli_conn[h].fragment_buffer) {
|
||||
lws_close_reason(
|
||||
wsi, LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE, NULL, 0);
|
||||
pthread_mutex_unlock(&bws_cli_mutex);
|
||||
DEBUG_PRINTF("bws_cli_websocket_event() <<< ret = -1, "
|
||||
"allocation of %d bytes failed\n",
|
||||
DEBUG_PRINTF(
|
||||
"bws_cli_websocket_event() <<< ret = -1, "
|
||||
"allocation of %d bytes failed\n",
|
||||
len <= BSC_RX_BUFFER_LEN ? BSC_RX_BUFFER_LEN : len);
|
||||
return -1;
|
||||
}
|
||||
@@ -303,26 +308,29 @@ static int bws_cli_websocket_event(struct lws *wsi,
|
||||
"for socket %d to %d bytes\n",
|
||||
bws_cli_conn[h].fragment_buffer_len, h,
|
||||
bws_cli_conn[h].fragment_buffer_len + len);
|
||||
bws_cli_conn[h].fragment_buffer =
|
||||
realloc(bws_cli_conn[h].fragment_buffer,
|
||||
bws_cli_conn[h].fragment_buffer_len + len);
|
||||
bws_cli_conn[h].fragment_buffer = realloc(
|
||||
bws_cli_conn[h].fragment_buffer,
|
||||
bws_cli_conn[h].fragment_buffer_len + len);
|
||||
if (!bws_cli_conn[h].fragment_buffer) {
|
||||
lws_close_reason(
|
||||
wsi, LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE, NULL, 0);
|
||||
pthread_mutex_unlock(&bws_cli_mutex);
|
||||
DEBUG_PRINTF("bws_cli_websocket_event() <<< ret = -1, "
|
||||
"re-allocation of %d bytes failed\n",
|
||||
DEBUG_PRINTF(
|
||||
"bws_cli_websocket_event() <<< ret = -1, "
|
||||
"re-allocation of %d bytes failed\n",
|
||||
bws_cli_conn[h].fragment_buffer_len + len);
|
||||
return -1;
|
||||
}
|
||||
bws_cli_conn[h].fragment_buffer_size =
|
||||
bws_cli_conn[h].fragment_buffer_len + len;
|
||||
}
|
||||
DEBUG_PRINTF("bws_cli_websocket_event() got next %d bytes for "
|
||||
"socket %d\n",
|
||||
DEBUG_PRINTF(
|
||||
"bws_cli_websocket_event() got next %d bytes for "
|
||||
"socket %d\n",
|
||||
len, h);
|
||||
memcpy(&bws_cli_conn[h].fragment_buffer[
|
||||
bws_cli_conn[h].fragment_buffer_len],
|
||||
memcpy(
|
||||
&bws_cli_conn[h]
|
||||
.fragment_buffer[bws_cli_conn[h].fragment_buffer_len],
|
||||
in, len);
|
||||
bws_cli_conn[h].fragment_buffer_len += len;
|
||||
|
||||
@@ -332,7 +340,8 @@ static int bws_cli_websocket_event(struct lws *wsi,
|
||||
dispatch_func = bws_cli_conn[h].dispatch_func;
|
||||
user_param = bws_cli_conn[h].user_param;
|
||||
pthread_mutex_unlock(&bws_cli_mutex);
|
||||
dispatch_func(h, BSC_WEBSOCKET_RECEIVED, 0, NULL,
|
||||
dispatch_func(
|
||||
h, BSC_WEBSOCKET_RECEIVED, 0, NULL,
|
||||
bws_cli_conn[h].fragment_buffer,
|
||||
bws_cli_conn[h].fragment_buffer_len, user_param);
|
||||
pthread_mutex_lock(&bws_cli_mutex);
|
||||
@@ -358,9 +367,11 @@ static int bws_cli_websocket_event(struct lws *wsi,
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEBUG_PRINTF("bws_cli_websocket_event() can write, state = %d\n",
|
||||
DEBUG_PRINTF(
|
||||
"bws_cli_websocket_event() can write, state = %d\n",
|
||||
bws_cli_conn[h].state);
|
||||
DEBUG_PRINTF("bws_cli_websocket_event() ws = %d, cs = %d\n",
|
||||
DEBUG_PRINTF(
|
||||
"bws_cli_websocket_event() ws = %d, cs = %d\n",
|
||||
bws_cli_conn[h].want_send_data, bws_cli_conn[h].can_send_data);
|
||||
if (bws_cli_conn[h].state == BSC_WEBSOCKET_STATE_CONNECTED &&
|
||||
bws_cli_conn[h].want_send_data) {
|
||||
@@ -450,9 +461,9 @@ static void *bws_cli_worker(void *arg)
|
||||
DEBUG_PRINTF("bws_cli_worker() process disconnecting event\n");
|
||||
DEBUG_PRINTF("bws_cli_worker() destroy ctx %p\n", conn->ctx);
|
||||
/* TRICKY: Libwebsockets API is not designed to be used from
|
||||
multipe service threads, as a result lws_context_destroy()
|
||||
is not thread safe. More over, on different platforms the
|
||||
function behaves in different ways. Call of
|
||||
multipe service threads, as a result
|
||||
lws_context_destroy() is not thread safe. More over, on different
|
||||
platforms the function behaves in different ways. Call of
|
||||
lws_context_destroy() leads to several calls of
|
||||
bws_cli_websocket_event() callback (LWS_CALLBACK_CLOSED,
|
||||
etc..). But under some OS (MacOSx) that callback is
|
||||
@@ -478,7 +489,8 @@ static void *bws_cli_worker(void *arg)
|
||||
bws_cli_free_connection(h);
|
||||
pthread_mutex_unlock(&bws_cli_mutex);
|
||||
DEBUG_PRINTF("bws_cli_worker() unlock mutex\n");
|
||||
dispatch_func(h, BSC_WEBSOCKET_DISCONNECTED, err_code,
|
||||
dispatch_func(
|
||||
h, BSC_WEBSOCKET_DISCONNECTED, err_code,
|
||||
err_code != ERROR_CODE_SUCCESS ? err_desc : NULL, NULL, 0,
|
||||
user_param);
|
||||
return NULL;
|
||||
@@ -492,7 +504,8 @@ static void *bws_cli_worker(void *arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BSC_WEBSOCKET_RET bws_cli_connect(BSC_WEBSOCKET_PROTOCOL proto,
|
||||
BSC_WEBSOCKET_RET bws_cli_connect(
|
||||
BSC_WEBSOCKET_PROTOCOL proto,
|
||||
char *url,
|
||||
uint8_t *ca_cert,
|
||||
size_t ca_cert_size,
|
||||
@@ -541,8 +554,8 @@ BSC_WEBSOCKET_RET bws_cli_connect(BSC_WEBSOCKET_PROTOCOL proto,
|
||||
bsc_websocket_init_log();
|
||||
pthread_mutex_lock(&bws_cli_mutex);
|
||||
|
||||
if (lws_parse_uri(tmp_url, &prot, &addr, &port, &path) != 0 ||
|
||||
port == -1 || !prot || !addr || !path) {
|
||||
if (lws_parse_uri(tmp_url, &prot, &addr, &port, &path) != 0 || port == -1 ||
|
||||
!prot || !addr || !path) {
|
||||
pthread_mutex_unlock(&bws_cli_mutex);
|
||||
DEBUG_PRINTF("bws_cli_connect() <<< ret = BSC_WEBSOCKET_BAD_PARAM\n");
|
||||
return BSC_WEBSOCKET_BAD_PARAM;
|
||||
@@ -633,15 +646,16 @@ BSC_WEBSOCKET_RET bws_cli_connect(BSC_WEBSOCKET_PROTOCOL proto,
|
||||
bsc_websocket_global_unlock();
|
||||
r = pthread_attr_init(&attr);
|
||||
|
||||
if(!r) {
|
||||
if (!r) {
|
||||
r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
}
|
||||
|
||||
if(!r) {
|
||||
r = pthread_create(&thread_id, &attr, &bws_cli_worker, &bws_cli_conn[h]);
|
||||
if (!r) {
|
||||
r = pthread_create(
|
||||
&thread_id, &attr, &bws_cli_worker, &bws_cli_conn[h]);
|
||||
}
|
||||
|
||||
if(r) {
|
||||
if (r) {
|
||||
/* TRICKY: Libwebsockets API is not designed to be used from
|
||||
multipe service threads, as a result lws_context_destroy()
|
||||
is not thread safe. More over, on different platforms the
|
||||
@@ -679,7 +693,8 @@ void bws_cli_disconnect(BSC_WEBSOCKET_HANDLE h)
|
||||
|
||||
if (h >= 0 && h < BSC_CLIENT_WEBSOCKETS_MAX_NUM) {
|
||||
pthread_mutex_lock(&bws_cli_mutex);
|
||||
DEBUG_PRINTF("bws_cli_disconnect() state = %d\n", bws_cli_conn[h].state);
|
||||
DEBUG_PRINTF(
|
||||
"bws_cli_disconnect() state = %d\n", bws_cli_conn[h].state);
|
||||
if (bws_cli_conn[h].state == BSC_WEBSOCKET_STATE_CONNECTING ||
|
||||
bws_cli_conn[h].state == BSC_WEBSOCKET_STATE_CONNECTED) {
|
||||
/* tell worker to process change of connection state */
|
||||
@@ -732,7 +747,8 @@ BSC_WEBSOCKET_RET bws_cli_dispatch_send(
|
||||
|
||||
if ((bws_cli_conn[h].state != BSC_WEBSOCKET_STATE_CONNECTED) ||
|
||||
!bws_cli_conn[h].want_send_data || !bws_cli_conn[h].can_send_data) {
|
||||
DEBUG_PRINTF("bws_cli_dispatch_send() state = %d, ws = %d, cs = %d\n",
|
||||
DEBUG_PRINTF(
|
||||
"bws_cli_dispatch_send() state = %d, ws = %d, cs = %d\n",
|
||||
bws_cli_conn[h].state, bws_cli_conn[h].want_send_data,
|
||||
bws_cli_conn[h].can_send_data);
|
||||
DEBUG_PRINTF("bws_cli_dispatch_send() <<< ret = "
|
||||
|
||||
Reference in New Issue
Block a user