Fixed wsvpn server to work with latest Mongoose
This commit is contained in:
parent
022cb0cde8
commit
23447e7ed9
50
wsvpn.c
50
wsvpn.c
|
@ -65,10 +65,10 @@ struct client {
|
|||
|
||||
struct client clients[MAX_CLIENTS] = { 0 };
|
||||
|
||||
static void handle_client(struct mg_connection *connection, int event_type, void *ev_data, void *fn_data);
|
||||
static void on_ws_connect(struct mg_connection *connection, struct mg_http_message *message, void *data);
|
||||
static void on_ws_message(struct mg_connection *connection, struct mg_ws_message *message, void *data);
|
||||
static void on_ws_disconnect(struct mg_connection *connection, void *data);
|
||||
static void handle_client(struct mg_connection *connection, int event_type, void *ev_data);
|
||||
static void on_ws_connect(struct mg_connection *connection, struct mg_http_message *message);
|
||||
static void on_ws_message(struct mg_connection *connection, struct mg_ws_message *message);
|
||||
static void on_ws_disconnect(struct mg_connection *connection);
|
||||
|
||||
bool client_is_open(struct client *client, uint16_t channel);
|
||||
static void modem_open(struct client *client, uint16_t request_id, uint16_t channel);
|
||||
|
@ -101,15 +101,15 @@ int main(void) {
|
|||
mg_mgr_free(&manager);
|
||||
}
|
||||
|
||||
static void handle_client(struct mg_connection *connection, int event_type, void *event_data, void *fn_data) {
|
||||
static void handle_client(struct mg_connection *connection, int event_type, void *event_data) {
|
||||
if (event_type == MG_EV_OPEN) {
|
||||
if (connection->rem.port == 0) return;
|
||||
memset(connection->data, 0, 32);
|
||||
} else if (event_type == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *http_message = (struct mg_http_message *) event_data;
|
||||
if (mg_http_match_uri(http_message, "/open")) {
|
||||
if (mg_match(http_message->uri, mg_str_s("/open"), 0)) {
|
||||
mg_ws_upgrade(connection, http_message, NULL);
|
||||
} else if (mg_http_match_uri(http_message, "/metrics")) {
|
||||
} else if (mg_match(http_message->uri, mg_str_s("/metrics"), 0)) {
|
||||
mg_printf(connection, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||
mg_http_printf_chunk(connection, "# HELP ws_bytes_sent_total Number of bytes sent to clients\n");
|
||||
mg_http_printf_chunk(connection, "# TYPE ws_bytes_sent_total counter\n");
|
||||
|
@ -147,13 +147,13 @@ static void handle_client(struct mg_connection *connection, int event_type, void
|
|||
}
|
||||
} else if (event_type == MG_EV_WS_OPEN) {
|
||||
struct mg_http_message *http_message = (struct mg_http_message *) event_data;
|
||||
on_ws_connect(connection, http_message, fn_data);
|
||||
on_ws_connect(connection, http_message);
|
||||
} else if (event_type == MG_EV_WS_MSG) {
|
||||
struct mg_ws_message *ws_message = (struct mg_ws_message *)event_data;
|
||||
on_ws_message(connection, ws_message, fn_data);
|
||||
on_ws_message(connection, ws_message);
|
||||
} else if (event_type == MG_EV_CLOSE) {
|
||||
if (connection->is_websocket) {
|
||||
on_ws_disconnect(connection, fn_data);
|
||||
on_ws_disconnect(connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -211,9 +211,8 @@ void ws_respond(struct client *client, uint16_t request_id, void *data, uint32_t
|
|||
mg_ws_send(client->connection, buffer, size + 3, WEBSOCKET_OP_BINARY);
|
||||
}
|
||||
|
||||
static void on_ws_connect(struct mg_connection *connection, struct mg_http_message *message, void *data) {
|
||||
static void on_ws_connect(struct mg_connection *connection, struct mg_http_message *message) {
|
||||
(void)message;
|
||||
(void)data;
|
||||
struct client *client = malloc(sizeof(struct client));
|
||||
memcpy(&connection->data[0], &client, sizeof(struct client *));
|
||||
client->connection = connection;
|
||||
|
@ -226,9 +225,7 @@ static void on_ws_connect(struct mg_connection *connection, struct mg_http_messa
|
|||
mg_ws_send(connection, buffer, 2 + buffer[1], WEBSOCKET_OP_BINARY);
|
||||
}
|
||||
|
||||
static void on_ws_message(struct mg_connection *connection, struct mg_ws_message *message, void *data) {
|
||||
(void)data;
|
||||
|
||||
static void on_ws_message(struct mg_connection *connection, struct mg_ws_message *message) {
|
||||
if ((message->flags & 15) != WEBSOCKET_OP_BINARY) {
|
||||
const char *err_str = "This server only works in binary mode. Sorry!";
|
||||
mg_ws_send(connection, err_str, strlen(err_str), WEBSOCKET_OP_TEXT);
|
||||
|
@ -244,15 +241,15 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message
|
|||
|
||||
if (message->data.len == 0) return;
|
||||
|
||||
uint16_t request_id = ntohs(*(uint16_t*)&message->data.ptr[1]);
|
||||
uint16_t request_id = ntohs(*(uint16_t*)&message->data.buf[1]);
|
||||
|
||||
switch (message->data.ptr[0]) {
|
||||
switch (message->data.buf[0]) {
|
||||
case 'I': // info. We can safely ignore that message
|
||||
break;
|
||||
case 'O': // open
|
||||
{
|
||||
metrics.method_calls[0]++;
|
||||
uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]);
|
||||
uint16_t channel = ntohs(*(uint16_t*)&message->data.buf[3]);
|
||||
printf("%p[%04x] modem.open(%d)\n", (void*)client, request_id, channel);
|
||||
modem_open(client, request_id, channel);
|
||||
}
|
||||
|
@ -260,7 +257,7 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message
|
|||
case 'o': // isOpen
|
||||
{
|
||||
metrics.method_calls[1]++;
|
||||
uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]);
|
||||
uint16_t channel = ntohs(*(uint16_t*)&message->data.buf[3]);
|
||||
printf("%p[%04x] modem.isOpen(%d)\n", (void*)client, request_id, channel);
|
||||
modem_isOpen(client, request_id, channel);
|
||||
}
|
||||
|
@ -268,7 +265,7 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message
|
|||
case 'c': // close
|
||||
{
|
||||
metrics.method_calls[2]++;
|
||||
uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]);
|
||||
uint16_t channel = ntohs(*(uint16_t*)&message->data.buf[3]);
|
||||
printf("%p[%04x] modem.close(%d)\n", (void*)client, request_id, channel);
|
||||
modem_close(client, request_id, channel);
|
||||
}
|
||||
|
@ -283,21 +280,20 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message
|
|||
case 'T': // transmit
|
||||
{
|
||||
metrics.method_calls[4]++;
|
||||
uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]);
|
||||
uint16_t reply_channel = ntohs(*(uint16_t*)&message->data.ptr[5]);
|
||||
uint16_t data_length = ntohs(*(uint16_t*)&message->data.ptr[7]);
|
||||
modem_transmit(client, request_id, channel, reply_channel, (void*)&message->data.ptr[9], data_length);
|
||||
uint16_t channel = ntohs(*(uint16_t*)&message->data.buf[3]);
|
||||
uint16_t reply_channel = ntohs(*(uint16_t*)&message->data.buf[5]);
|
||||
uint16_t data_length = ntohs(*(uint16_t*)&message->data.buf[7]);
|
||||
modem_transmit(client, request_id, channel, reply_channel, (void*)&message->data.buf[9], data_length);
|
||||
}
|
||||
return;
|
||||
default:
|
||||
ws_send_error(client, request_id, "Unknown opcode: 0x%02x", message->data.ptr[0]);
|
||||
ws_send_error(client, request_id, "Unknown opcode: 0x%02x", message->data.buf[0]);
|
||||
connection->is_draining = 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void on_ws_disconnect(struct mg_connection *connection, void *data) {
|
||||
(void)data;
|
||||
static void on_ws_disconnect(struct mg_connection *connection) {
|
||||
|
||||
struct client *client = *(struct client **)&connection->data[0];
|
||||
if (client->connection == connection) {
|
||||
|
|
Loading…
Reference in New Issue