send.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <venti.h>
  4. #include "queue.h"
  5. long ventisendbytes, ventisendpackets;
  6. long ventirecvbytes, ventirecvpackets;
  7. static int
  8. _vtsend(VtConn *z, Packet *p)
  9. {
  10. IOchunk ioc;
  11. int n, tot;
  12. uchar buf[2];
  13. if(z->state != VtStateConnected) {
  14. werrstr("session not connected");
  15. return -1;
  16. }
  17. /* add framing */
  18. n = packetsize(p);
  19. if(n >= (1<<16)) {
  20. werrstr("packet too large");
  21. packetfree(p);
  22. return -1;
  23. }
  24. buf[0] = n>>8;
  25. buf[1] = n;
  26. packetprefix(p, buf, 2);
  27. ventisendbytes += n+2;
  28. ventisendpackets++;
  29. tot = 0;
  30. for(;;){
  31. n = packetfragments(p, &ioc, 1, 0);
  32. if(n == 0)
  33. break;
  34. if(write(z->outfd, ioc.addr, ioc.len) < ioc.len){
  35. vtlog(VtServerLog, "<font size=-1>%T %s:</font> sending packet %p: %r<br>\n", z->addr, p);
  36. packetfree(p);
  37. return -1;
  38. }
  39. packetconsume(p, nil, ioc.len);
  40. tot += ioc.len;
  41. }
  42. vtlog(VtServerLog, "<font size=-1>%T %s:</font> sent packet %p (%d bytes)<br>\n", z->addr, p, tot);
  43. packetfree(p);
  44. return 1;
  45. }
  46. static int
  47. interrupted(void)
  48. {
  49. char e[ERRMAX];
  50. rerrstr(e, sizeof e);
  51. return strstr(e, "interrupted") != nil;
  52. }
  53. static Packet*
  54. _vtrecv(VtConn *z)
  55. {
  56. uchar buf[10], *b;
  57. int n;
  58. Packet *p;
  59. int size, len;
  60. if(z->state != VtStateConnected) {
  61. werrstr("session not connected");
  62. return nil;
  63. }
  64. p = z->part;
  65. /* get enough for head size */
  66. size = packetsize(p);
  67. while(size < 2) {
  68. b = packettrailer(p, 2);
  69. assert(b != nil);
  70. if(0) fprint(2, "%d read hdr\n", getpid());
  71. n = read(z->infd, b, 2);
  72. if(0) fprint(2, "%d got %d (%r)\n", getpid(), n);
  73. if(n==0 || (n<0 && !interrupted()))
  74. goto Err;
  75. size += n;
  76. packettrim(p, 0, size);
  77. }
  78. if(packetconsume(p, buf, 2) < 0)
  79. goto Err;
  80. len = (buf[0] << 8) | buf[1];
  81. size -= 2;
  82. while(size < len) {
  83. n = len - size;
  84. if(n > MaxFragSize)
  85. n = MaxFragSize;
  86. b = packettrailer(p, n);
  87. if(0) fprint(2, "%d read body %d\n", getpid(), n);
  88. n = read(z->infd, b, n);
  89. if(0) fprint(2, "%d got %d (%r)\n", getpid(), n);
  90. if(n > 0)
  91. size += n;
  92. packettrim(p, 0, size);
  93. if(n==0 || (n<0 && !interrupted()))
  94. goto Err;
  95. }
  96. ventirecvbytes += len;
  97. ventirecvpackets++;
  98. p = packetsplit(p, len);
  99. vtlog(VtServerLog, "<font size=-1>%T %s:</font> read packet %p len %d<br>\n", z->addr, p, len);
  100. return p;
  101. Err:
  102. vtlog(VtServerLog, "<font size=-1>%T %s:</font> error reading packet: %r<br>\n", z->addr);
  103. return nil;
  104. }
  105. /*
  106. * If you fork off two procs running vtrecvproc and vtsendproc,
  107. * then vtrecv/vtsend (and thus vtrpc) will never block except on
  108. * rendevouses, which is nice when it's running in one thread of many.
  109. */
  110. void
  111. vtrecvproc(void *v)
  112. {
  113. Packet *p;
  114. VtConn *z;
  115. Queue *q;
  116. z = v;
  117. q = _vtqalloc();
  118. qlock(&z->lk);
  119. z->readq = q;
  120. qlock(&z->inlk);
  121. rwakeup(&z->rpcfork);
  122. qunlock(&z->lk);
  123. while((p = _vtrecv(z)) != nil)
  124. if(_vtqsend(q, p) < 0){
  125. packetfree(p);
  126. break;
  127. }
  128. qunlock(&z->inlk);
  129. qlock(&z->lk);
  130. _vtqhangup(q);
  131. while((p = _vtnbqrecv(q)) != nil)
  132. packetfree(p);
  133. _vtqdecref(q);
  134. z->readq = nil;
  135. rwakeup(&z->rpcfork);
  136. qunlock(&z->lk);
  137. vthangup(z);
  138. }
  139. void
  140. vtsendproc(void *v)
  141. {
  142. Queue *q;
  143. Packet *p;
  144. VtConn *z;
  145. z = v;
  146. q = _vtqalloc();
  147. qlock(&z->lk);
  148. z->writeq = q;
  149. qlock(&z->outlk);
  150. rwakeup(&z->rpcfork);
  151. qunlock(&z->lk);
  152. while((p = _vtqrecv(q)) != nil)
  153. if(_vtsend(z, p) < 0)
  154. break;
  155. qunlock(&z->outlk);
  156. qlock(&z->lk);
  157. _vtqhangup(q);
  158. while((p = _vtnbqrecv(q)) != nil)
  159. packetfree(p);
  160. _vtqdecref(q);
  161. z->writeq = nil;
  162. rwakeup(&z->rpcfork);
  163. qunlock(&z->lk);
  164. return;
  165. }
  166. Packet*
  167. vtrecv(VtConn *z)
  168. {
  169. Packet *p;
  170. Queue *q;
  171. qlock(&z->lk);
  172. if(z->state != VtStateConnected){
  173. werrstr("not connected");
  174. qunlock(&z->lk);
  175. return nil;
  176. }
  177. if(z->readq){
  178. q = _vtqincref(z->readq);
  179. qunlock(&z->lk);
  180. p = _vtqrecv(q);
  181. _vtqdecref(q);
  182. return p;
  183. }
  184. qlock(&z->inlk);
  185. qunlock(&z->lk);
  186. p = _vtrecv(z);
  187. qunlock(&z->inlk);
  188. if(!p)
  189. vthangup(z);
  190. return p;
  191. }
  192. int
  193. vtsend(VtConn *z, Packet *p)
  194. {
  195. Queue *q;
  196. qlock(&z->lk);
  197. if(z->state != VtStateConnected){
  198. packetfree(p);
  199. werrstr("not connected");
  200. qunlock(&z->lk);
  201. return -1;
  202. }
  203. if(z->writeq){
  204. q = _vtqincref(z->writeq);
  205. qunlock(&z->lk);
  206. if(_vtqsend(q, p) < 0){
  207. _vtqdecref(q);
  208. packetfree(p);
  209. return -1;
  210. }
  211. _vtqdecref(q);
  212. return 0;
  213. }
  214. qlock(&z->outlk);
  215. qunlock(&z->lk);
  216. if(_vtsend(z, p) < 0){
  217. qunlock(&z->outlk);
  218. vthangup(z);
  219. return -1;
  220. }
  221. qunlock(&z->outlk);
  222. return 0;
  223. }