lib9p

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

commit 25d431a6c23ee49bd0d0fe11748caa3a5e8e72ed
parent b7a49256f55c187854ff8a3ab55b47d53bdb3d19
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Thu, 26 Oct 2023 07:35:21 +0900

add comment

Diffstat:
Mserver.go | 35+++++++++++++++++++++++++++++++----
1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/server.go b/server.go @@ -69,7 +69,8 @@ type Server struct { Auth func(context.Context, *Req) } - +// NewServer creates a Server and runs listener and speaker goroutines. +// It reads incoming messages from r and writes responses to w. func NewServer(fsys FS, mSize uint32, r io.Reader, w io.Writer) *Server { s := &Server{ fs: fsys, @@ -83,20 +84,29 @@ func NewServer(fsys FS, mSize uint32, r io.Reader, w io.Writer) *Server { return s } +// Chatty enables the server's log messages of 9P messages. func (s *Server) Chatty() { s.chatty9P = true } +// mSize reads the maximum message size of the server. func (s *Server) mSize() uint32 { s.mSizeLock.Lock() defer s.mSizeLock.Unlock() return s.msize } +// setMSize changes the server's maximum message size. func (s *Server) setMSize(mSize uint32) { s.mSizeLock.Lock() defer s.mSizeLock.Unlock() s.msize = mSize } +// runListener runs the listener goroutine. +// Listener goroutine reads 9P messages from r and allocats Req for each +// of them, and sends it to the returned chan of *Req. +// It reports any error to the returned chan of error. +// TODO: pass context.Context and stop the goroutine with the Context being +// canceled. func (s *Server) runListener(r io.Reader) (<-chan *Req, <-chan error) { rc := make(chan *Req) ec := make(chan error) @@ -114,12 +124,17 @@ func (s *Server) runListener(r io.Reader) (<-chan *Req, <-chan error) { return rc, ec } +// runSpeaker runs the speaker goroutine. +// Speaker goroutine wait for reply Requests from the returned channel, +// and marshalls each of them into 9P messages and writes it to w. +// TODO: pass context.Context and stop the goroutine with the Context being +// canceled. func (s *Server) runSpeaker(w io.Writer) (chan<- *Req, <-chan error) { rc := make(chan *Req, 3) // TODO: buffer size? ec := make(chan error) go func() { // TODO: close rc anywhere - defer close(ec) // I think this will never be called. + defer close(ec) for { r := <-rc _, err := w.Write(r.ofcall.marshal()) @@ -177,7 +192,14 @@ func getReq(r io.Reader, s *Server) (*Req, error) { return req, nil } -// TODO: abort all outstanding I/O on the same connection. +// sVersion processes Tversion message. +// It checks if the version string is "9P2000", and if the version differs from +// that, it replies with version string "unknown". +// It also checks the msize of the message and if it is bigger than that of the +// server, it replies with msize being the server's mSize, otherwise it sets the +// server's mSize to the message's one. +// TODO: abort all outstanding I/O on the same connection before +// serving new Tversion. func sVersion(ctx context.Context, s *Server, r *Req) { ifcall := r.ifcall.(*TVersion) version := ifcall.Version() @@ -197,6 +219,8 @@ func sVersion(ctx context.Context, s *Server, r *Req) { respond(ctx, r, nil) } +// rVersion confirms that err is nil, and sets the server's msize to the +// appropreate one. func rVersion(r *Req, err error) { if err != nil { panic(fmt.Errorf("rVersion err: %w", err)) @@ -204,10 +228,10 @@ func rVersion(r *Req, err error) { r.srv.setMSize(r.ofcall.(*RVersion).MSize()) } +// sAuth serves Tauth message. func sAuth(ctx context.Context, s *Server, r *Req) { ifcall := r.ifcall.(*TAuth) var err error - // TODO: this call can block. need ctx? r.afid, err = s.fPool.add(ifcall.AFid()) if err != nil { respond(ctx, r, ErrDupFid) @@ -220,6 +244,8 @@ func sAuth(ctx context.Context, s *Server, r *Req) { } } +// rAuth checks if err is nil, and if not, it deletes the +// allocated fid from fPool. func rAuth(r *Req, err error) { if err != nil { r.srv.fPool.delete(r.ifcall.(*TAuth).AFid()) @@ -883,6 +909,7 @@ func rWStat(r *Req, err error) { } } +// Serve serves 9P conversation. func (s *Server) Serve(ctx context.Context) { L: for {