close: add LWSS_WAITING_TO_SEND_CLOSE_NOTIFICATION allowed to send
[platform/upstream/libwebsockets.git] / test-server / test-server-dumb-increment.c
1 /*
2  * libwebsockets-test-server - libwebsockets test implementation
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * The person who associated a work with this deed has dedicated
10  * the work to the public domain by waiving all of his or her rights
11  * to the work worldwide under copyright law, including all related
12  * and neighboring rights, to the extent allowed by law. You can copy,
13  * modify, distribute and perform the work, even for commercial purposes,
14  * all without asking permission.
15  *
16  * The test apps are intended to be adapted for use in your code, which
17  * may be proprietary.  So unlike the library itself, they are licensed
18  * Public Domain.
19  */
20 #include "test-server.h"
21
22 /* dumb_increment protocol */
23
24 int
25 callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
26                         void *user, void *in, size_t len)
27 {
28         unsigned char buf[LWS_PRE + 512];
29         struct per_session_data__dumb_increment *pss =
30                         (struct per_session_data__dumb_increment *)user;
31         unsigned char *p = &buf[LWS_PRE];
32         int n, m;
33
34         switch (reason) {
35
36         case LWS_CALLBACK_ESTABLISHED:
37                 pss->number = 0;
38                 break;
39
40         case LWS_CALLBACK_SERVER_WRITEABLE:
41                 n = sprintf((char *)p, "%d", pss->number++);
42                 m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
43                 if (m < n) {
44                         lwsl_err("ERROR %d writing to di socket\n", n);
45                         return -1;
46                 }
47                 if (close_testing && pss->number == 50) {
48                         lwsl_info("close tesing limit, closing\n");
49                         return -1;
50                 }
51                 break;
52
53         case LWS_CALLBACK_RECEIVE:
54                 if (len < 6)
55                         break;
56                 if (strcmp((const char *)in, "reset\n") == 0)
57                         pss->number = 0;
58                 if (strcmp((const char *)in, "closeme\n") == 0) {
59                         lwsl_notice("dumb_inc: closing as requested\n");
60                         lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
61                                          (unsigned char *)"seeya", 5);
62                         return -1;
63                 }
64                 break;
65         /*
66          * this just demonstrates how to use the protocol filter. If you won't
67          * study and reject connections based on header content, you don't need
68          * to handle this callback
69          */
70         case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
71                 dump_handshake_info(wsi);
72                 /* you could return non-zero here and kill the connection */
73                 break;
74
75         /*
76          * this just demonstrates how to handle
77          * LWS_CALLBACK_WS_PEER_INITIATED_CLOSE and extract the peer's close
78          * code and auxiliary data.  You can just not handle it if you don't
79          * have a use for this.
80          */
81         case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
82                 lwsl_notice("LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: len %lu\n",
83                             (unsigned long)len);
84                 for (n = 0; n < (int)len; n++)
85                         lwsl_notice(" %d: 0x%02X\n", n,
86                                     ((unsigned char *)in)[n]);
87                 break;
88
89         default:
90                 break;
91         }
92
93         return 0;
94 }