test-server-status: increase tx size to avoid WRITEABLE loops
[platform/upstream/libwebsockets.git] / test-server / test-server-echogen.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 /* echogen protocol
23  *
24  * if you connect to him using his protocol, he'll send you a file chopped
25  * up in various frame sizes repeated until he reaches a limit.
26  */
27
28 #define TOTAL 993840
29
30 int
31 callback_lws_echogen(struct lws *wsi, enum lws_callback_reasons reason,
32                         void *user, void *in, size_t len)
33 {
34         unsigned char buf[LWS_PRE + 8192];
35         struct per_session_data__echogen *pss =
36                         (struct per_session_data__echogen *)user;
37         unsigned char *p = &buf[LWS_PRE];
38         int n, m;
39
40         switch (reason) {
41
42         case LWS_CALLBACK_ESTABLISHED:
43                 pss->total = TOTAL;
44                 pss->fragsize = 2048;
45                 pss->total_rx = 0;
46                 sprintf((char *)buf, "%s/test.html", resource_path);
47                 pss->fd = open((char *)buf, LWS_O_RDONLY);
48                 if (pss->fd < 0) {
49                         lwsl_err("Failed to open %s\n", buf);
50                         return -1;
51                 }
52                 pss->wr = LWS_WRITE_TEXT | LWS_WRITE_NO_FIN;
53                 lws_callback_on_writable(wsi);
54                 break;
55
56         case LWS_CALLBACK_CLOSED:
57                 if (pss->fd >= 0)
58                         close(pss->fd);
59                 break;
60
61         case LWS_CALLBACK_SERVER_WRITEABLE:
62
63 //              pss->fragsize += 16;
64 //              if (pss->fragsize >= 4096)
65 //                      pss->fragsize = 32;
66
67                 lwsl_err("%s: cb writeable, total left %ld\n", __func__, (long)pss->total);
68                 m = pss->fragsize;
69                 if ((size_t)m >=  pss->total) {
70                         m = (int)pss->total;
71                         pss->wr = LWS_WRITE_CONTINUATION; /* ie, FIN */
72                 }
73                 n = read(pss->fd, p, m);
74                 if (n < 0) {
75                         lwsl_err("failed read\n");
76                         return -1;
77                 }
78                 if (n < m) {
79                         lseek(pss->fd, 0, SEEK_SET);
80                         m = read(pss->fd, p + n, m - n);
81                         if (m < 0)
82                                 return -1;
83                 } else
84                         m = 0;
85                 pss->total -= n + m;
86                 m = lws_write(wsi, p, n + m, pss->wr);
87                 if (m < n) {
88                         lwsl_err("ERROR %d writing to di socket\n", n);
89                         return -1;
90                 }
91                 if (!pss->total) {
92                         lwsl_err("Completed OK\n");
93                         break;
94                 }
95                 pss->wr = LWS_WRITE_CONTINUATION | LWS_WRITE_NO_FIN;
96                 lws_callback_on_writable(wsi);
97                 break;
98
99         case LWS_CALLBACK_RECEIVE:
100                 pss->total_rx += len;
101                 lwsl_err("rx %ld\n", (long)pss->total_rx);
102                 if (pss->total_rx == TOTAL) {
103                         lws_close_reason(wsi, LWS_CLOSE_STATUS_NORMAL,
104                                          (unsigned char *)"done", 4);
105                         return -1;
106                 }
107                 break;
108
109         case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
110                 lwsl_notice("LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: len %lu\n",
111                             (unsigned long)len);
112                 for (n = 0; n < (int)len; n++)
113                         lwsl_notice(" %d: 0x%02X\n", n,
114                                     ((unsigned char *)in)[n]);
115                 break;
116
117         default:
118                 break;
119         }
120
121         return 0;
122 }