Skip to content

Commit

Permalink
when ws fail or disconnect, reconnect it
Browse files Browse the repository at this point in the history
Signed-off-by: staylightblow8 <[email protected]>
  • Loading branch information
liudf0716 committed Feb 26, 2024
1 parent ee571b3 commit 7f34d77
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/dhcp_cpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void thread_dhcp_cpi(const void *arg)
}

if (nfq_unbind_pf(nfq, AF_INET) < 0) {
debug (LOG_ERR, " nfq_unbind_pf failed");
debug (LOG_ERR, " nfq_unbind_pf failed: %s", strerror(errno));
nfq_close(nfq);
return;
}
Expand Down
22 changes: 17 additions & 5 deletions src/ws_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ ws_receive(char *data, const size_t data_len){
if(data_len < 2)
return;

char temp[20] = {0};
strncpy(temp, data, data_len>=20?20:data_len);
debug(LOG_DEBUG, "first 3 char of data is %x %x %x\n", data[0], data[1], data[2]);
debug(LOG_DEBUG, "first 20 %s", temp);
int fin = !!(*data & 0x80);
int opcode = *data & 0x0F;
int mask = !!(*(data+1) & 0x80);
Expand Down Expand Up @@ -265,7 +261,23 @@ wsevent_connection_cb(struct bufferevent* b_ws, short events, void *ctx){
debug (LOG_DEBUG,"connect ws server and start web socket request\n");
ws_request(b_ws);
return;
}
} else if(events & (BEV_EVENT_ERROR|BEV_EVENT_EOF)){
debug(LOG_ERR, "ws connection error: %s\n", strerror(errno));
// reconnect ws server
bufferevent_free(b_ws);
b_ws = bufferevent_socket_new(ws_base, -1, BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
bufferevent_setcb(b_ws, ws_read_cb, NULL, wsevent_connection_cb, NULL);
bufferevent_enable(b_ws, EV_READ|EV_WRITE);
t_auth_serv *auth_server = get_auth_server();
if (!auth_server->authserv_use_ssl) {
bufferevent_socket_connect_hostname(b_ws, ws_dnsbase, AF_INET,
auth_server->authserv_hostname, auth_server->authserv_http_port);
} else {
bufferevent_socket_connect_hostname(b_ws, ws_dnsbase, AF_INET,
auth_server->authserv_hostname, auth_server->authserv_ssl_port);
}
upgraded = false;
}
}

/*
Expand Down
13 changes: 13 additions & 0 deletions src/ws_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
#ifndef _WS_THREAD_H_
#define _WS_THREAD_H_

#include <stdint.h>

struct ws_header {
uint8_t fin; // Final fragment flag
uint8_t rsv1; // Reserved flag 1
uint8_t rsv2; // Reserved flag 2
uint8_t rsv3; // Reserved flag 3
uint8_t opcode; // Opcode
uint8_t mask; // Masking flag
uint64_t payload_length; // Payload length
uint8_t masking_key[4]; // Masking key
};

void start_ws_thread(void *arg);

void stop_ws_thread();
Expand Down

0 comments on commit 7f34d77

Please sign in to comment.