2015-11-19 13:55:47 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets-test-server - libwebsockets test implementation
|
|
|
|
*
|
2016-02-08 08:46:05 +08:00
|
|
|
* Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
|
2015-11-19 13:55:47 +08:00
|
|
|
*
|
2016-02-08 08:46:05 +08:00
|
|
|
* This file is made available under the Creative Commons CC0 1.0
|
|
|
|
* Universal Public Domain Dedication.
|
2015-11-19 13:55:47 +08:00
|
|
|
*
|
2016-02-08 08:46:05 +08:00
|
|
|
* The person who associated a work with this deed has dedicated
|
|
|
|
* the work to the public domain by waiving all of his or her rights
|
|
|
|
* to the work worldwide under copyright law, including all related
|
|
|
|
* and neighboring rights, to the extent allowed by law. You can copy,
|
|
|
|
* modify, distribute and perform the work, even for commercial purposes,
|
|
|
|
* all without asking permission.
|
2015-11-19 13:55:47 +08:00
|
|
|
*
|
2016-02-08 08:46:05 +08:00
|
|
|
* The test apps are intended to be adapted for use in your code, which
|
|
|
|
* may be proprietary. So unlike the library itself, they are licensed
|
|
|
|
* Public Domain.
|
2015-11-19 13:55:47 +08:00
|
|
|
*/
|
|
|
|
#include "test-server.h"
|
|
|
|
|
|
|
|
/* dumb_increment protocol */
|
|
|
|
|
|
|
|
int
|
2015-12-17 07:54:44 +08:00
|
|
|
callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
|
2015-11-19 13:55:47 +08:00
|
|
|
void *user, void *in, size_t len)
|
|
|
|
{
|
|
|
|
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
|
|
|
|
LWS_SEND_BUFFER_POST_PADDING];
|
|
|
|
struct per_session_data__dumb_increment *pss =
|
|
|
|
(struct per_session_data__dumb_increment *)user;
|
|
|
|
unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
|
|
|
|
int n, m;
|
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
|
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
|
|
|
pss->number = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
|
|
|
n = sprintf((char *)p, "%d", pss->number++);
|
2015-12-04 08:43:54 +08:00
|
|
|
m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
|
2015-11-19 13:55:47 +08:00
|
|
|
if (m < n) {
|
|
|
|
lwsl_err("ERROR %d writing to di socket\n", n);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (close_testing && pss->number == 50) {
|
|
|
|
lwsl_info("close tesing limit, closing\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_RECEIVE:
|
|
|
|
if (len < 6)
|
|
|
|
break;
|
|
|
|
if (strcmp((const char *)in, "reset\n") == 0)
|
|
|
|
pss->number = 0;
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* this just demonstrates how to use the protocol filter. If you won't
|
|
|
|
* study and reject connections based on header content, you don't need
|
|
|
|
* to handle this callback
|
|
|
|
*/
|
|
|
|
case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
|
|
|
|
dump_handshake_info(wsi);
|
|
|
|
/* you could return non-zero here and kill the connection */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|