mirror of
https://github.com/lammertb/libhttp.git
synced 2025-07-31 08:24:23 +03:00
Updated indentation
This commit is contained in:
@ -54,8 +54,7 @@ static void *ws_server_thread(void *parm)
|
|||||||
fprintf(stderr, "ws_server_thread %d\n", wsd);
|
fprintf(stderr, "ws_server_thread %d\n", wsd);
|
||||||
|
|
||||||
/* Send initial meter updates */
|
/* Send initial meter updates */
|
||||||
for (i=0; meter[i].period != 0; i++)
|
for (i=0; meter[i].period != 0; i++) {
|
||||||
{
|
|
||||||
if (meter[i].value >= meter[i].limit)
|
if (meter[i].value >= meter[i].limit)
|
||||||
meter[i].value = 0;
|
meter[i].value = 0;
|
||||||
if (meter[i].value >= meter[i].limit)
|
if (meter[i].value >= meter[i].limit)
|
||||||
@ -66,18 +65,14 @@ static void *ws_server_thread(void *parm)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* While the connection is open, send periodic updates */
|
/* While the connection is open, send periodic updates */
|
||||||
while(!ws_conn[wsd].closing)
|
while(!ws_conn[wsd].closing) {
|
||||||
{
|
|
||||||
usleep(100000); /* 0.1 second */
|
usleep(100000); /* 0.1 second */
|
||||||
timer++;
|
timer++;
|
||||||
|
|
||||||
/* Send meter updates */
|
/* Send meter updates */
|
||||||
if (ws_conn[wsd].update)
|
if (ws_conn[wsd].update) {
|
||||||
{
|
for (i=0; meter[i].period != 0; i++) {
|
||||||
for (i=0; meter[i].period != 0; i++)
|
if (timer%meter[i].period == 0) {
|
||||||
{
|
|
||||||
if (timer%meter[i].period == 0)
|
|
||||||
{
|
|
||||||
if (meter[i].value >= meter[i].limit)
|
if (meter[i].value >= meter[i].limit)
|
||||||
meter[i].value = 0;
|
meter[i].value = 0;
|
||||||
else
|
else
|
||||||
@ -85,8 +80,7 @@ static void *ws_server_thread(void *parm)
|
|||||||
if (meter[i].value >= meter[i].limit)
|
if (meter[i].value >= meter[i].limit)
|
||||||
meter[i].value = meter[i].limit;
|
meter[i].value = meter[i].limit;
|
||||||
// if we are closing, server should not send new data
|
// if we are closing, server should not send new data
|
||||||
if (!ws_conn[wsd].closing)
|
if (!ws_conn[wsd].closing) {
|
||||||
{
|
|
||||||
sprintf(tstr, "meter%d:%d,%d", i+1,
|
sprintf(tstr, "meter%d:%d,%d", i+1,
|
||||||
meter[i].value, meter[i].limit);
|
meter[i].value, meter[i].limit);
|
||||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr));
|
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr));
|
||||||
@ -114,15 +108,14 @@ static void *ws_server_thread(void *parm)
|
|||||||
// On new client connection, find next available server connection and store
|
// On new client connection, find next available server connection and store
|
||||||
// new connection information. If no more server connections are available
|
// new connection information. If no more server connections are available
|
||||||
// tell civetweb to not accept the client request.
|
// tell civetweb to not accept the client request.
|
||||||
static int websocket_connect_handler(const struct mg_connection *conn) {
|
static int websocket_connect_handler(const struct mg_connection *conn)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
fprintf(stderr, "connect handler\n");
|
fprintf(stderr, "connect handler\n");
|
||||||
|
|
||||||
for(i=0; i < CONNECTIONS; ++i)
|
for(i=0; i < CONNECTIONS; ++i) {
|
||||||
{
|
if (ws_conn[i].conn == NULL) {
|
||||||
if (ws_conn[i].conn == NULL)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "...prep for server %d\n", i);
|
fprintf(stderr, "...prep for server %d\n", i);
|
||||||
ws_conn[i].conn = (struct mg_connection *)conn;
|
ws_conn[i].conn = (struct mg_connection *)conn;
|
||||||
ws_conn[i].closing = 0;
|
ws_conn[i].closing = 0;
|
||||||
@ -130,8 +123,7 @@ static int websocket_connect_handler(const struct mg_connection *conn) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i >= CONNECTIONS)
|
if (i >= CONNECTIONS) {
|
||||||
{
|
|
||||||
fprintf(stderr, "Refused connection: Max connections exceeded\n");
|
fprintf(stderr, "Refused connection: Max connections exceeded\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -141,15 +133,14 @@ static int websocket_connect_handler(const struct mg_connection *conn) {
|
|||||||
|
|
||||||
// websocket_ready_handler()
|
// websocket_ready_handler()
|
||||||
// Once websocket negotiation is complete, start a server for the connection
|
// Once websocket negotiation is complete, start a server for the connection
|
||||||
static void websocket_ready_handler(struct mg_connection *conn) {
|
static void websocket_ready_handler(struct mg_connection *conn)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
fprintf(stderr, "ready handler\n");
|
fprintf(stderr, "ready handler\n");
|
||||||
|
|
||||||
for(i=0; i < CONNECTIONS; ++i)
|
for(i=0; i < CONNECTIONS; ++i) {
|
||||||
{
|
if (ws_conn[i].conn == conn) {
|
||||||
if (ws_conn[i].conn == conn)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "...start server %d\n", i);
|
fprintf(stderr, "...start server %d\n", i);
|
||||||
mg_start_thread(ws_server_thread, (void *)(long)i);
|
mg_start_thread(ws_server_thread, (void *)(long)i);
|
||||||
break;
|
break;
|
||||||
@ -159,15 +150,14 @@ static void websocket_ready_handler(struct mg_connection *conn) {
|
|||||||
|
|
||||||
// websocket_close_handler()
|
// websocket_close_handler()
|
||||||
// When websocket is closed, tell the associated server to shut down
|
// When websocket is closed, tell the associated server to shut down
|
||||||
static void websocket_close_handler(struct mg_connection *conn) {
|
static void websocket_close_handler(struct mg_connection *conn)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
//fprintf(stderr, "close handler\n"); /* called for every close, not just websockets */
|
//fprintf(stderr, "close handler\n"); /* called for every close, not just websockets */
|
||||||
|
|
||||||
for(i=0; i < CONNECTIONS; ++i)
|
for(i=0; i < CONNECTIONS; ++i) {
|
||||||
{
|
if (ws_conn[i].conn == conn) {
|
||||||
if (ws_conn[i].conn == conn)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "...close server %d\n", i);
|
fprintf(stderr, "...close server %d\n", i);
|
||||||
ws_conn[i].closing = 1;
|
ws_conn[i].closing = 1;
|
||||||
}
|
}
|
||||||
@ -179,44 +169,37 @@ static void websocket_close_handler(struct mg_connection *conn) {
|
|||||||
// http://tools.ietf.org/html/rfc6455, section 5.2
|
// http://tools.ietf.org/html/rfc6455, section 5.2
|
||||||
// data, data_len: payload data. Mask, if any, is already applied.
|
// data, data_len: payload data. Mask, if any, is already applied.
|
||||||
static int websocket_data_handler(struct mg_connection *conn, int flags,
|
static int websocket_data_handler(struct mg_connection *conn, int flags,
|
||||||
char *data, size_t data_len) {
|
char *data, size_t data_len)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
int wsd;
|
int wsd;
|
||||||
|
|
||||||
for(i=0; i < CONNECTIONS; ++i)
|
for(i=0; i < CONNECTIONS; ++i) {
|
||||||
{
|
if (ws_conn[i].conn == conn) {
|
||||||
if (ws_conn[i].conn == conn)
|
|
||||||
{
|
|
||||||
wsd = i;
|
wsd = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i >= CONNECTIONS)
|
if (i >= CONNECTIONS) {
|
||||||
{
|
|
||||||
fprintf(stderr, "Received websocket data from unknown connection\n");
|
fprintf(stderr, "Received websocket data from unknown connection\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & 0x80)
|
if (flags & 0x80) {
|
||||||
{
|
|
||||||
flags &= 0x7f;
|
flags &= 0x7f;
|
||||||
switch (flags)
|
switch (flags) {
|
||||||
{
|
|
||||||
case WEBSOCKET_OPCODE_CONTINUATION:
|
case WEBSOCKET_OPCODE_CONTINUATION:
|
||||||
fprintf(stderr, "CONTINUATION...\n");
|
fprintf(stderr, "CONTINUATION...\n");
|
||||||
break;
|
break;
|
||||||
case WEBSOCKET_OPCODE_TEXT:
|
case WEBSOCKET_OPCODE_TEXT:
|
||||||
fprintf(stderr, "TEXT: %-.*s\n", (int)data_len, data);
|
fprintf(stderr, "TEXT: %-.*s\n", (int)data_len, data);
|
||||||
/*** interpret data as commands here ***/
|
/*** interpret data as commands here ***/
|
||||||
if (strncmp("update on", data, data_len)== 0)
|
if (strncmp("update on", data, data_len)== 0) {
|
||||||
{
|
|
||||||
/* turn on updates */
|
/* turn on updates */
|
||||||
ws_conn[wsd].update = 1;
|
ws_conn[wsd].update = 1;
|
||||||
/* echo back */
|
/* echo back */
|
||||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
|
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
|
||||||
}
|
} else if (strncmp("update off", data, data_len)== 0) {
|
||||||
else if (strncmp("update off", data, data_len)== 0)
|
|
||||||
{
|
|
||||||
/* turn off updates */
|
/* turn off updates */
|
||||||
ws_conn[wsd].update = 0;
|
ws_conn[wsd].update = 0;
|
||||||
/* echo back */
|
/* echo back */
|
||||||
@ -229,8 +212,7 @@ static int websocket_data_handler(struct mg_connection *conn, int flags,
|
|||||||
case WEBSOCKET_OPCODE_CONNECTION_CLOSE:
|
case WEBSOCKET_OPCODE_CONNECTION_CLOSE:
|
||||||
fprintf(stderr, "CLOSE...\n");
|
fprintf(stderr, "CLOSE...\n");
|
||||||
/* If client initiated close, respond with close message in acknowlegement */
|
/* If client initiated close, respond with close message in acknowlegement */
|
||||||
if (!ws_conn[wsd].closing)
|
if (!ws_conn[wsd].closing) {
|
||||||
{
|
|
||||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_CONNECTION_CLOSE, data, data_len);
|
mg_websocket_write(conn, WEBSOCKET_OPCODE_CONNECTION_CLOSE, data, data_len);
|
||||||
ws_conn[wsd].closing = 1; /* we should not send addional messages when close requested/acknowledged */
|
ws_conn[wsd].closing = 1; /* we should not send addional messages when close requested/acknowledged */
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user