6 #include "ev_signal_exit.h"
8 #define JSON_EMPTY "{\"type\":\"empty\"}"
10 static const struct lws_http_mount mount = {
11 /* .mount_next */ NULL, /* linked-list "next" */
12 /* .mountpoint */ "/", /* mountpoint URL */
13 /* .origin */ "/usr/share/mt-server", /* serve from dir */
14 /* .def */ "index.html", /* default filename */
17 /* .extra_mimetypes */ NULL,
18 /* .interpret */ NULL,
20 /* .cache_max_age */ 0,
22 /* .cache_reusable */ 0,
23 /* .cache_revalidate */ 0,
24 /* .cache_intermediaries */ 0,
25 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
26 /* .mountpoint_len */ 1, /* char count */
27 /* .basic_auth_login_file */ NULL,
30 typedef struct per_vhost_data__dumb_increment {
31 struct lws_context *context;
32 struct lws_vhost *vhost;
33 const struct lws_protocols *protocol;
34 } per_vhost_data__dumb_increment;
36 typedef struct per_session_data__dumb_increment {
42 } per_session_data__dumb_increment;
45 PROTOCOL_HTTP = 0, // always first
46 PROTOCOL_MERICA_TERMINAL
49 static int callback_merica_terminal(struct lws *wsi,
50 enum lws_callback_reasons reason,
51 void *user, void *in, size_t len);
53 // list of supported protocols and callbacks
54 static struct lws_protocols protocols[] = {
55 /* first protocol must always be HTTP handler */
56 {"http", lws_callback_http_dummy, 0, 0},
58 "dumb-increment-protocol",
59 callback_merica_terminal,
60 sizeof(struct per_session_data__dumb_increment),
61 128, /* rx buf size must be >= permessage-deflate rx size
62 * dumb-increment only sends very small packets, so we set
63 * this accordingly. If your protocol will send bigger
64 * things, adjust this to match */
66 {NULL, NULL, 0, 0} /* terminator */
69 static int callback_merica_terminal(struct lws *wsi,
70 enum lws_callback_reasons reason,
71 void *user, void *in, size_t len)
73 struct lws_vhost *vhost = lws_get_vhost(wsi);
74 const struct lws_protocols *prot = lws_get_protocol(wsi);
75 per_session_data__dumb_increment *pss =
76 (per_session_data__dumb_increment *)user;
77 per_vhost_data__dumb_increment *vhd =
78 (per_vhost_data__dumb_increment *)
79 lws_protocol_vh_priv_get(vhost, prot);
83 char *line = (char *)prot->user;
86 case LWS_CALLBACK_PROTOCOL_INIT:
87 vhd = lws_protocol_vh_priv_zalloc(
89 sizeof(struct per_vhost_data__dumb_increment)
91 vhd->context = lws_get_context(wsi);
96 case LWS_CALLBACK_PROTOCOL_DESTROY:
102 case LWS_CALLBACK_ESTABLISHED:
105 case LWS_CALLBACK_SERVER_WRITEABLE:
107 m = lws_write(wsi, line, n, LWS_WRITE_TEXT);
109 printf("ERROR %d writing to di socket\n", n);
114 case LWS_CALLBACK_RECEIVE:
115 if (strcmp((const char *)in, "reset") == 0) {
116 strcpy(line, JSON_EMPTY);
117 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
118 &protocols[PROTOCOL_MERICA_TERMINAL]);
119 } else if (strcmp((const char *)in, "close") == 0) {
120 printf("dumb_inc: closing as requested\n");
121 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
122 (unsigned char *)"seeya", 5);
134 static void fd_cb(EV_P_ ev_io *w_, int revents)
136 ev_io_ws *w = (ev_io_ws *)w_;
138 char *pos = w->pos++;
140 read(w->w.fd, pos, 1);
145 lws_callback_on_writable_all_protocol(w->context,
146 &protocols[PROTOCOL_MERICA_TERMINAL]);
150 int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
152 struct lws_context_creation_info info;
154 memset(&info, 0, sizeof(info));
156 info.mounts = &mount;
157 info.protocols = protocols;
158 info.max_http_header_pool = 1;
159 info.options |= LWS_SERVER_OPTION_LIBEV;
161 struct lws_context *context = lws_create_context(&info);
163 fprintf(stderr, "lws_create_context failed\n");
167 self->context = context;
169 ev_io_ws *w = &(self->fd_watcher);
170 w->context = context;
171 w->text = (char *)malloc(512*sizeof(char));
176 strcpy(w->text, JSON_EMPTY);
178 protocols[PROTOCOL_MERICA_TERMINAL].user = (void *)w->text;
179 ev_io_init(&(w->w), fd_cb, fd, EV_READ);
180 ev_io_start(loop, (ev_io *)w);
182 return lws_ev_initloop(context, loop, 0);
185 void mt_server_deinit(mt_server_t *self)
187 //free(self->fd_watcher.text);
188 //protocols[PROTOCOL_MERICA_TERMINAL].user = NULL;
189 lws_context_destroy(self->context);
193 int main(int argc, const char **argv)
195 struct ev_loop *loop = EV_DEFAULT;
198 set_signal_exit(loop);
200 if (mt_server_init(&server, loop, STDIN_FILENO) == -1) {
206 mt_server_deinit(&server);