SCENARIOS 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. Simple:
  2. step1(conn): read(conn), then step2
  3. step2(conn): write(conn), then close
  4. Pass-through:
  5. step1(conn): read(conn), then step2
  6. step2(conn): write(otherconn), then step1
  7. Pass-through-and-connect:
  8. step1(conn): read(conn), then step2
  9. step2(conn): connect(otherconn), then step3
  10. step3(conn): write(otherconn), then step1
  11. Chatroom:
  12. step1(conn): read(conn), then step2
  13. step2(conn): for c in allcons: write(c). goto step1
  14. Simple:
  15. void event(struct io_event *done)
  16. {
  17. char *buf = done->priv;
  18. struct io_event *e;
  19. e = queue_read(done, done->conn, buf, 100);
  20. e = queue_write(e, done->conn, buf, 100);
  21. queue_close(e, done->conn);
  22. }
  23. Pass-through:
  24. struct passthru {
  25. char buf[100];
  26. struct conn *rconn, *wconn;
  27. };
  28. void event(struct io_event *done)
  29. {
  30. struct passthru *p = done->priv;
  31. struct io_event *e;
  32. e = queue_read(done, p->rconn, p->buf, 100);
  33. e = queue_write(e, p->wconn, buf, 100);
  34. queue_event(e, event);
  35. }
  36. Chatroom:
  37. struct list_head clients;
  38. struct buffer {
  39. char buf[100];
  40. unsigned int ref;
  41. };
  42. struct client {
  43. struct list_node list;
  44. struct connection *conn;
  45. struct buffer *rbuf, *wbuf;
  46. };
  47. void broadcast(struct io_event *done)
  48. {
  49. struct client *i, *c = done->conn->priv;
  50. struct io_event *e;
  51. list_for_each(&clients, i, list) {
  52. e = queue_write(done, i->conn, c->buf->buf, 100);
  53. e->priv = c->buf;
  54. c->buf->ref++;
  55. queue_event(e, drop_ref);
  56. }
  57. void event(struct io_event *done)
  58. {
  59. struct client *c = done->conn->priv;
  60. struct io_event *e;
  61. assert(c->conn == done->conn);
  62. c->buf = malloc(sizeof(*c->buf));
  63. c->buf->ref = 0;
  64. e = queue_read(done, c->conn, c->buf->buf, 100);
  65. e = queue_event(e, broadcast);
  66. }
  67. step1(conn): read(conn), then step2
  68. step2(conn): for c in allcons: write(c). goto step1