commit dce371f63bd86f69e7164ea19cc60540efce050f from: Stephen J Day date: Thu Nov 05 00:07:26 2015 UTC fs/p9p/new: add version to context Signed-off-by: Stephen J Day commit - 3bf22e5860ff26ad641dc222b8121a2bcae316c7 commit + dce371f63bd86f69e7164ea19cc60540efce050f blob - a1b80f10d12ed6dc1acfecf66b41eb22345519e2 blob + 452188d9186f024575da5ff0019c5a295a5dfa61 --- client.go +++ client.go @@ -11,7 +11,7 @@ import ( type client struct { version string - msize uint32 + msize int ctx context.Context transport roundTripper } @@ -20,20 +20,17 @@ type client struct { // a context for out of bad messages, such as flushes, that may be sent by the // session. The session can effectively shutdown with this context. func NewSession(ctx context.Context, conn net.Conn) (Session, error) { - const msize = 64 << 10 - const vers = "9P2000" + ch := newChannel(conn, codec9p{}, DefaultMSize) // sets msize, effectively. - ch := newChannel(conn, codec9p{}, msize) // sets msize, effectively. - // negotiate the protocol version - _, err := clientnegotiate(ctx, ch, vers) + version, err := clientnegotiate(ctx, ch, DefaultVersion) if err != nil { return nil, err } return &client{ - version: vers, - msize: msize, + version: version, + msize: ch.MSize(), ctx: ctx, transport: newTransport(ctx, ch), }, nil @@ -41,7 +38,7 @@ func NewSession(ctx context.Context, conn net.Conn) (S var _ Session = &client{} -func (c *client) Version() (uint32, string) { +func (c *client) Version() (int, string) { return c.msize, c.version } blob - /dev/null blob + ca01956c2100164871d21be450d44c152bb88edb (mode 644) --- /dev/null +++ context.go @@ -0,0 +1,26 @@ +package p9pnew + +import ( + "golang.org/x/net/context" +) + +type contextKey string + +const ( + versionKey contextKey = "9p.version" +) + +func withVersion(ctx context.Context, version string) context.Context { + return context.WithValue(ctx, versionKey, version) +} + +// GetVersion returns the protocol version from the context. If the version is +// not known, an empty string is returned. This is typically set on the +// context passed into function calls in a server implementation. +func GetVersion(ctx context.Context) string { + v, ok := ctx.Value(versionKey).(string) + if !ok { + return "" + } + return v +} blob - 1c2cb5811dd66ef98bf1501ca06ad8ce6b6f6d8a blob + bcb9d3529472a2cb92f44025dfeca20de9d5876d --- server.go +++ server.go @@ -10,11 +10,8 @@ import ( // Serve the 9p session over the provided network connection. func Serve(ctx context.Context, conn net.Conn, session Session) { - const msize = 64 << 10 - const vers = "9P2000" - + msize, version := session.Version() ch := newChannel(conn, codec9p{}, msize) - negctx, cancel := context.WithTimeout(ctx, 1*time.Second) defer cancel() @@ -22,13 +19,15 @@ func Serve(ctx context.Context, conn net.Conn, session // do this outside of this function and then pass in a ready made channel. // We are not really ready to export the channel type yet. - if err := servernegotiate(negctx, ch, vers); err != nil { + if err := servernegotiate(negctx, ch, version); err != nil { // TODO(stevvooe): Need better error handling and retry support here. // For now, we silently ignore the failure. log.Println("error negotiating version:", err) return } + ctx = withVersion(ctx, version) + s := &server{ ctx: ctx, ch: ch, blob - 91e26ec436170573317f400b7da56a6a2971d62b blob + 975a0aad7cfa145559b902deb64d7dfc36c64140 --- session.go +++ session.go @@ -33,7 +33,7 @@ type Session interface { // Version returns the supported version and msize of the session. This // can be affected by negotiating or the level of support provided by the // session implementation. - Version() (msize uint32, version string) + Version() (msize int, version string) } func Dial(ctx context.Context, addr string) (Session, error) { blob - 3eb82c83c1cc430459f85eda47c9ceefcff1f5d6 blob + 61918a21255f3ab026be2362a5ccebb7a78d1780 --- types.go +++ types.go @@ -6,6 +6,11 @@ import ( ) const ( + DefaultMSize = 64 << 10 + DefaultVersion = "9P2000" +) + +const ( NOFID = ^Fid(0) NOTAG = ^Tag(0) )