lib9p

Go 9P library.
Log | Files | Refs | LICENSE

conn.go (908B)


      1 package testfs
      2 
      3 import (
      4 	"context"
      5 	"io"
      6 
      7 	"git.mtkn.jp/lib9p"
      8 	"git.mtkn.jp/lib9p/client"
      9 )
     10 
     11 type Conn struct {
     12 	S      *lib9p.Server
     13 	C      *client.Client
     14 	cr, sr *io.PipeReader
     15 	cw, sw *io.PipeWriter
     16 	cancel context.CancelFunc
     17 }
     18 
     19 // SetupTestConn setups a connection between a server and a client and
     20 // returns those.
     21 func SetupConn() *Conn {
     22 	const (
     23 		mSize = 8 * 1024
     24 		uname = "kenji"
     25 	)
     26 	cr, sw := io.Pipe()
     27 	sr, cw := io.Pipe()
     28 	// TODO: fix the inconsistency of server and client api.
     29 	s := lib9p.NewServer(Fsys)
     30 	clnt := client.NewClient(mSize, uname, cr, cw)
     31 	ctx, cancel := context.WithCancel(context.Background())
     32 	go s.Serve(ctx, sr, sw)
     33 	return &Conn{
     34 		S:      s,
     35 		C:      clnt,
     36 		cr:     cr,
     37 		cw:     cw,
     38 		sr:     sr,
     39 		sw:     sw,
     40 		cancel: cancel,
     41 	}
     42 }
     43 
     44 func (conn *Conn) Close() {
     45 	conn.cancel()
     46 	conn.C.Stop()
     47 	conn.cw.Close()
     48 	conn.cr.Close()
     49 	conn.sw.Close()
     50 	conn.sr.Close()
     51 }