lib9p

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

commit b7a49256f55c187854ff8a3ab55b47d53bdb3d19
parent 1cf5e4d64da123409ed0a48d08e62d285692c9a4
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 25 Oct 2023 11:53:58 +0900

add comment to Server struct

Diffstat:
Mauth.go | 16+++++++++++++++-
Mserver.go | 36+++++++++++++++++++++++++++++++++---
2 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/auth.go b/auth.go @@ -1,10 +1,12 @@ package lib9p import ( + "fmt" "io" "io/fs" ) +// AuthFile is a file allocated by Tauth messages. type AuthFile struct { Qid Qid Uname string @@ -30,6 +32,18 @@ func (af *AuthFile) Stat() (*FileInfo, error) { return &FileInfo{Stat: s}, nil } -func (af *AuthFile) Close() error { return nil } +func (af *AuthFile) Close() error { + var we, re error + if w, ok := af.W.(io.Closer); ok { + we = w.Close() + } + if r, ok := af.R.(io.Closer); ok { + re = r.Close() + } + if we != nil || re != nil { + return fmt.Errorf("close writer: %v, close reader: %v", we, re) + } + return nil +} func (af *AuthFile) Read(p []byte) (int, error) { return af.R.Read(p) } func (af *AuthFile) Write(p []byte) (int, error) { return af.W.Write(p) } diff --git a/server.go b/server.go @@ -28,21 +28,48 @@ func setError(r *Req, err error) { } } +// Server is a 9P server type Server struct { + // If true, the server prints the transcript of the 9P session + // to os.Stderr chatty9P bool + + // The file system to export via 9P. fs FS + + // Maximum length in byte of 9P messages. msize uint32 + // Mutex to change msize. + // TODO: should be RWMutex? mSizeLock *sync.Mutex + + // FidPool of the Server. fPool *FidPool + + // Pending Requests the server is dealing with. rPool *ReqPool + + // The channel from which incoming requests are delivered by the + // listener goroutine. listenChan <-chan *Req + // The error channel the listener goroutine reports any error. listenErrChan <-chan error + + // The channel to which outgoing replies are sent to the speaker + // goroutine. speakChan chan<- *Req + // The error channel the speaker goroutine reports any error. speakErrChan <-chan error + // Auth is called when Tauth message arrives. + // If authentication is desired, the Auth functions should + // set Req.Afid.Qid and Req.ofcall.Qid. + // TODO: export Req.ofcall. + // If this is nil, no authentication is performed. Auth func(context.Context, *Req) } + func NewServer(fsys FS, mSize uint32, r io.Reader, w io.Writer) *Server { s := &Server{ fs: fsys, @@ -53,7 +80,6 @@ func NewServer(fsys FS, mSize uint32, r io.Reader, w io.Writer) *Server { } s.listenChan, s.listenErrChan = s.runListener(r) s.speakChan, s.speakErrChan = s.runSpeaker(w) - return s } @@ -684,7 +710,11 @@ func sClunk(ctx context.Context, s *Server, r *Req) { respond(ctx, r, nil) } -func rClunk(r *Req, err error) {} +func rClunk(r *Req, err error) { + if err != nil { + panic("err in clunk: %v", err) + } +} func sRemove(ctx context.Context, s *Server, r *Req) { ifcall := r.ifcall.(*TRemove) @@ -862,7 +892,7 @@ L: log.Printf("speak: %v", err) continue L case err := <-s.listenErrChan: - log.Printf("getReq: %v", err) + log.Printf("listen: %v", err) if err == io.EOF { break L }