ustream-example.c 2.9 KB

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