refactor test server
[platform/upstream/libwebsockets.git] / test-server / test-server-dumb-increment.c
1 /*
2  * libwebsockets-test-server - libwebsockets test implementation
3  *
4  * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21 #include "test-server.h"
22
23 /* dumb_increment protocol */
24
25 int
26 callback_dumb_increment(struct libwebsocket_context *context,
27                         struct libwebsocket *wsi,
28                         enum libwebsocket_callback_reasons reason,
29                         void *user, void *in, size_t len)
30 {
31         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
32                                                   LWS_SEND_BUFFER_POST_PADDING];
33         struct per_session_data__dumb_increment *pss =
34                         (struct per_session_data__dumb_increment *)user;
35         unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
36         int n, m;
37
38         switch (reason) {
39
40         case LWS_CALLBACK_ESTABLISHED:
41                 pss->number = 0;
42                 break;
43
44         case LWS_CALLBACK_SERVER_WRITEABLE:
45                 n = sprintf((char *)p, "%d", pss->number++);
46                 m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
47                 if (m < n) {
48                         lwsl_err("ERROR %d writing to di socket\n", n);
49                         return -1;
50                 }
51                 if (close_testing && pss->number == 50) {
52                         lwsl_info("close tesing limit, closing\n");
53                         return -1;
54                 }
55                 break;
56
57         case LWS_CALLBACK_RECEIVE:
58                 if (len < 6)
59                         break;
60                 if (strcmp((const char *)in, "reset\n") == 0)
61                         pss->number = 0;
62                 break;
63         /*
64          * this just demonstrates how to use the protocol filter. If you won't
65          * study and reject connections based on header content, you don't need
66          * to handle this callback
67          */
68         case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
69                 dump_handshake_info(wsi);
70                 /* you could return non-zero here and kill the connection */
71                 break;
72
73         default:
74                 break;
75         }
76
77         return 0;
78 }