ustream: add example code
[project/libubox.git] / examples / ustream-example.c
1 #include <sys/socket.h>
2 #include <netinet/in.h>
3
4 #include <stdio.h>
5 #include <getopt.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "ustream.h"
10 #include "uloop.h"
11 #include "usock.h"
12
13 static struct uloop_fd server;
14 static const char *port = "10000";
15 struct client *next_client = NULL;
16
17 struct client {
18 struct sockaddr_in sin;
19
20 struct ustream_fd s;
21 int ctr;
22 };
23
24 static void client_read_cb(struct ustream *s, int bytes)
25 {
26 struct client *cl = container_of(s, struct client, s);
27 struct ustream_buf *buf = s->r.head;
28 char *newline, *str;
29
30 do {
31 str = ustream_get_read_buf(s, NULL);
32 if (!str)
33 break;
34
35 newline = strchr(buf->data, '\n');
36 if (!newline)
37 break;
38
39 *newline = 0;
40 ustream_printf(s, "%s\n", str);
41 ustream_consume(s, newline + 1 - str);
42 cl->ctr += newline + 1 - str;
43 } while(1);
44
45 if (s->w.data_bytes > 256 && ustream_read_blocked(s)) {
46 fprintf(stderr, "Block read, bytes: %d\n", s->w.data_bytes);
47 ustream_set_read_blocked(s, true);
48 }
49 }
50
51 static void client_close(struct ustream *s)
52 {
53 struct client *cl = container_of(s, struct client, s);
54
55 fprintf(stderr, "Connection closed\n");
56 ustream_free(s);
57 close(cl->s.fd.fd);
58 free(cl);
59 }
60
61 static void client_notify_write(struct ustream *s, int bytes)
62 {
63 fprintf(stderr, "Wrote %d bytes, pending: %d\n", bytes, s->w.data_bytes);
64
65 if (s->w.data_bytes < 128 && ustream_read_blocked(s)) {
66 fprintf(stderr, "Unblock read\n");
67 ustream_set_read_blocked(s, false);
68 }
69 }
70
71 static void client_notify_state(struct ustream *s)
72 {
73 struct client *cl = container_of(s, struct client, s);
74
75 if (!s->eof)
76 return;
77
78 fprintf(stderr, "eof!, pending: %d, total: %d\n", s->w.data_bytes, cl->ctr);
79 if (!s->w.data_bytes)
80 return client_close(s);
81
82 }
83
84 static void server_cb(struct uloop_fd *fd, unsigned int events)
85 {
86 struct client *cl;
87 unsigned int sl = sizeof(struct sockaddr_in);
88 int sfd;
89
90 if (!next_client)
91 next_client = calloc(1, sizeof(*next_client));
92
93 cl = next_client;
94 sfd = accept(server.fd, (struct sockaddr *) &cl->sin, &sl);
95 if (sfd < 0) {
96 fprintf(stderr, "Accept failed\n");
97 return;
98 }
99
100 cl->s.stream.string_data = true;
101 cl->s.stream.notify_read = client_read_cb;
102 cl->s.stream.notify_state = client_notify_state;
103 cl->s.stream.notify_write = client_notify_write;
104 ustream_fd_init(&cl->s, sfd);
105 next_client = NULL;
106 fprintf(stderr, "New connection\n");
107 }
108
109 static int run_server(void)
110 {
111
112 server.cb = server_cb;
113 server.fd = usock(USOCK_TCP | USOCK_SERVER | USOCK_IPV4ONLY | USOCK_NUMERIC, "127.0.0.1", port);
114 if (server.fd < 0) {
115 perror("usock");
116 return 1;
117 }
118
119 uloop_init();
120 uloop_fd_add(&server, ULOOP_READ);
121 uloop_run();
122
123 return 0;
124 }
125
126 static int usage(const char *name)
127 {
128 fprintf(stderr, "Usage: %s -p <port>\n", name);
129 return 1;
130 }
131
132 int main(int argc, char **argv)
133 {
134 int ch;
135
136 while ((ch = getopt(argc, argv, "p:")) != -1) {
137 switch(ch) {
138 case 'p':
139 port = optarg;
140 break;
141 default:
142 return usage(argv[0]);
143 }
144 }
145
146 return run_server();
147 }