plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

conn.c (1308B)


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