conn.c 895 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <venti.h>
  4. #include "queue.h"
  5. int chattyventi;
  6. VtConn*
  7. vtconn(int infd, int outfd)
  8. {
  9. VtConn *z;
  10. NetConnInfo *nci;
  11. z = vtmallocz(sizeof(VtConn));
  12. z->tagrend.l = &z->lk;
  13. z->rpcfork.l = &z->lk;
  14. z->infd = infd;
  15. z->outfd = outfd;
  16. z->part = packetalloc();
  17. nci = getnetconninfo(nil, infd);
  18. if(nci == nil)
  19. snprint(z->addr, sizeof z->addr, "/dev/fd/%d", infd);
  20. else{
  21. strecpy(z->addr, z->addr+sizeof z->addr, nci->raddr);
  22. freenetconninfo(nci);
  23. }
  24. return z;
  25. }
  26. void
  27. vtfreeconn(VtConn *z)
  28. {
  29. vthangup(z);
  30. qlock(&z->lk);
  31. /*
  32. * Wait for send and recv procs to notice
  33. * the hangup and clear out the queues.
  34. */
  35. while(z->readq || z->writeq){
  36. if(z->readq)
  37. _vtqhangup(z->readq);
  38. if(z->writeq)
  39. _vtqhangup(z->writeq);
  40. rsleep(&z->rpcfork);
  41. }
  42. packetfree(z->part);
  43. vtfree(z->version);
  44. vtfree(z->sid);
  45. qunlock(&z->lk);
  46. vtfree(z);
  47. }