event.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <event.h>
  13. #include "plumb.h"
  14. typedef struct EQueue EQueue;
  15. struct EQueue
  16. {
  17. int id;
  18. char *buf;
  19. int nbuf;
  20. EQueue *next;
  21. };
  22. static EQueue *equeue;
  23. static Lock eqlock;
  24. static
  25. int
  26. partial(int id, Event *e, uint8_t *b, int n)
  27. {
  28. EQueue *eq, *p;
  29. int nmore;
  30. lock(&eqlock);
  31. for(eq = equeue; eq != nil; eq = eq->next)
  32. if(eq->id == id)
  33. break;
  34. unlock(&eqlock);
  35. if(eq == nil)
  36. return 0;
  37. /* partial message exists for this id */
  38. eq->buf = realloc(eq->buf, eq->nbuf+n);
  39. if(eq->buf == nil)
  40. drawerror(display, "eplumb: cannot allocate buffer");
  41. memmove(eq->buf+eq->nbuf, b, n);
  42. eq->nbuf += n;
  43. e->v = plumbunpackpartial((char*)eq->buf, eq->nbuf, &nmore);
  44. if(nmore == 0){ /* no more to read in this message */
  45. lock(&eqlock);
  46. if(eq == equeue)
  47. equeue = eq->next;
  48. else{
  49. for(p = equeue; p!=nil && p->next!=eq; p = p->next)
  50. ;
  51. if(p == nil)
  52. drawerror(display, "eplumb: bad event queue");
  53. p->next = eq->next;
  54. }
  55. unlock(&eqlock);
  56. free(eq->buf);
  57. free(eq);
  58. }
  59. return 1;
  60. }
  61. static
  62. void
  63. addpartial(int id, char *b, int n)
  64. {
  65. EQueue *eq;
  66. eq = malloc(sizeof(EQueue));
  67. if(eq == nil)
  68. return;
  69. eq->id = id;
  70. eq->nbuf = n;
  71. eq->buf = malloc(n);
  72. if(eq->buf == nil){
  73. free(eq);
  74. return;
  75. }
  76. memmove(eq->buf, b, n);
  77. lock(&eqlock);
  78. eq->next = equeue;
  79. equeue = eq;
  80. unlock(&eqlock);
  81. }
  82. static
  83. int
  84. plumbevent(int id, Event *e, uint8_t *b, int n)
  85. {
  86. int nmore;
  87. if(partial(id, e, b, n) == 0){
  88. /* no partial message already waiting for this id */
  89. e->v = plumbunpackpartial((char*)b, n, &nmore);
  90. if(nmore > 0) /* incomplete message */
  91. addpartial(id, (char*)b, n);
  92. }
  93. if(e->v == nil)
  94. return 0;
  95. return id;
  96. }
  97. int
  98. eplumb(int key, char *port)
  99. {
  100. int fd;
  101. fd = plumbopen(port, OREAD|OCEXEC);
  102. if(fd < 0)
  103. return -1;
  104. return estartfn(key, fd, 8192, plumbevent);
  105. }