2 * libwebsockets-test-server - libwebsockets test implementation
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
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.
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
20 #include "test-server.h"
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.
31 callback_lws_echogen(struct lws *wsi, enum lws_callback_reasons reason,
32 void *user, void *in, size_t len)
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];
42 case LWS_CALLBACK_ESTABLISHED:
46 sprintf((char *)buf, "%s/test.html", resource_path);
47 pss->fd = open((char *)buf, LWS_O_RDONLY);
49 lwsl_err("Failed to open %s\n", buf);
52 pss->wr = LWS_WRITE_TEXT | LWS_WRITE_NO_FIN;
53 lws_callback_on_writable(wsi);
56 case LWS_CALLBACK_CLOSED:
61 case LWS_CALLBACK_SERVER_WRITEABLE:
63 // pss->fragsize += 16;
64 // if (pss->fragsize >= 4096)
65 // pss->fragsize = 32;
67 lwsl_err("%s: cb writeable, total left %ld\n", __func__, (long)pss->total);
69 if ((size_t)m >= pss->total) {
71 pss->wr = LWS_WRITE_CONTINUATION; /* ie, FIN */
73 n = read(pss->fd, p, m);
75 lwsl_err("failed read\n");
79 lseek(pss->fd, 0, SEEK_SET);
80 m = read(pss->fd, p + n, m - n);
86 m = lws_write(wsi, p, n + m, pss->wr);
88 lwsl_err("ERROR %d writing to di socket\n", n);
92 lwsl_err("Completed OK\n");
95 pss->wr = LWS_WRITE_CONTINUATION | LWS_WRITE_NO_FIN;
96 lws_callback_on_writable(wsi);
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);
109 case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
110 lwsl_notice("LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: len %d\n",
112 for (n = 0; n < (int)len; n++)
113 lwsl_notice(" %d: 0x%02X\n", n,
114 ((unsigned char *)in)[n]);