Beispiel #1
0
func ExampleDecoder() {
	l, err := net.Listen("tcp", ":564")
	if err != nil {
		log.Fatal(err)
	}
	rwc, err := l.Accept()
	if err != nil {
		log.Fatal(err)
	}

	d := styxproto.NewDecoder(rwc)
	e := styxproto.NewEncoder(rwc)
	for d.Next() {
		switch msg := d.Msg().(type) {
		case styxproto.Tversion:
			log.Printf("Client wants version %s", msg.Version())
			e.Rversion(8192, "9P2000")
		case styxproto.Tread:
			e.Rread(msg.Tag(), []byte("data data"))
		case styxproto.Twrite:
			log.Printf("Receiving %d bytes from client", msg.Count())
			io.Copy(ioutil.Discard, msg)
		}
	}
}
Beispiel #2
0
func newConn(srv *Server, rwc io.ReadWriteCloser) *conn {
	var msize int64 = styxproto.DefaultMaxSize
	if srv.MaxSize > 0 {
		if srv.MaxSize > styxproto.MinBufSize {
			msize = srv.MaxSize
		} else {
			msize = styxproto.MinBufSize
		}
	}
	var enc *styxproto.Encoder
	var dec *styxproto.Decoder
	if srv.TraceLog != nil {
		enc = tracing.Encoder(rwc, func(m styxproto.Msg) {
			srv.TraceLog.Printf("← %03d %s", m.Tag(), m)
		})
		dec = tracing.Decoder(rwc, func(m styxproto.Msg) {
			srv.TraceLog.Printf("→ %03d %s", m.Tag(), m)
		})
	} else {
		enc = styxproto.NewEncoder(rwc)
		dec = styxproto.NewDecoder(rwc)
	}
	return &conn{
		Decoder:    dec,
		Encoder:    enc,
		srv:        srv,
		rwc:        rwc,
		cx:         context.TODO(),
		msize:      msize,
		sessionFid: util.NewMap(),
		pendingReq: util.NewMap(),
		qidpool:    qidpool.New(),
	}
}
Beispiel #3
0
// Encoder creates a new styxproto.Encoder that traces messages
// before writing them to w.
func Encoder(w io.Writer, fn Func) *styxproto.Encoder {
	rd, wr := io.Pipe()
	encoder := styxproto.NewEncoder(wr)
	decoder := styxproto.NewDecoderSize(rd, 8*kilobyte)
	go func() {
		for decoder.Next() {
			fn(decoder.Msg())
			styxproto.Write(w, decoder.Msg())
		}
	}()
	return encoder
}
Beispiel #4
0
func (s testServer) runMsg(fn func(*styxproto.Encoder)) {
	rd, wr := io.Pipe()
	e := styxproto.NewEncoder(wr)
	go func() {
		e.Tversion(styxproto.DefaultMaxSize, "9P2000")
		e.Tattach(0, 0, styxproto.NoFid, "", "")
		fn(e)
		e.Flush()
		wr.Close()
	}()
	s.run(rd)
}