lib9p

Go 9P library.
Log | Files | Refs

commit b776bbca81c3e1903c221091cb5e2527394e5238
parent a594aeb30977473f6adfbbfec7812f5ebd43233a
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 29 Jul 2023 13:06:07 +0900

change RAttach to struct

Diffstat:
Mfcall.go | 44+++++++++++++++++++++++++++++---------------
Mparse.go | 2+-
Mserver.go | 11+++++------
3 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/fcall.go b/fcall.go @@ -363,21 +363,35 @@ func (msg TAttach) String() string { msg.Tag(), fid, afid, msg.UName(), msg.AName()) } -type RAttach []byte - -func (msg RAttach) Size() uint32 { return gbit32(msg[0:4]) } -func (msg RAttach) SetSize(s uint32) { pbit32(msg[0:4], s) } -func (msg RAttach) Type() MsgType { return MsgType(msg[4]) } -func (msg RAttach) SetType(t MsgType) { msg[4] = byte(t) } -func (msg RAttach) Tag() uint16 { return gbit16(msg[5:7]) } -func (msg RAttach) SetTag(t uint16) { pbit16(msg[5:7], t) } -func (msg RAttach) Qid() *Qid { return newQid(msg[7:20]) } -func (msg RAttach) SetQid(q *Qid) { - msg[7] = uint8(q.Type()) - pbit32(msg[8:12], q.Vers()) - pbit64(msg[12:20], q.Path()) -} -func (msg RAttach) conv2M() []byte { return []byte(msg)[:msg.Size()] } +type RAttach struct { + tag uint16 + qid *Qid +} + +func newRAttach(buf []byte) *RAttach { panic("not implemented") } +func (msg RAttach) Size() uint32 { return 4+1+2+13 } +func (msg RAttach) Type() MsgType { return Rattach } +func (msg RAttach) Tag() uint16 { return msg.tag } +func (msg RAttach) Qid() *Qid { return msg.qid } +func (msg RAttach) conv2M() []byte { + cur := 0 + buf := make([]byte, msg.Size()) + pbit32(buf[cur:cur+4], msg.Size()) + cur += 4 + buf[cur] = uint8(Rattach) + cur += 1 + pbit16(buf[cur:cur+2], msg.Tag()) + cur += 2 + qid := msg.Qid().conv2M() + for i := 0; i < len(qid); i++ { + buf[cur+i] = qid[i] + } + cur += len(qid) + if cur != len(buf) { + panic("length of buf and cursor position don't match") + } + return buf +} func (msg RAttach) String() string { return fmt.Sprintf("Rattach tag %d qid %v", msg.Tag(), msg.Qid()) diff --git a/parse.go b/parse.go @@ -45,7 +45,7 @@ func unmarshal(buf []byte) (Msg, error) { case Tattach: return newTAttach(buf), nil case Rattach: - return RAttach(buf), nil + return newRAttach(buf), nil case Rerror: return RError(buf), nil case Twalk: diff --git a/server.go b/server.go @@ -146,11 +146,10 @@ func sAttach(s *Server, r *Req) { fid.OMode = OREAD fid.Qid = fid.File.qid - ofcall := RAttach(make([]byte, 4+1+2+13)) - ofcall.SetSize(uint32(len(ofcall))) - ofcall.SetType(Rattach) - ofcall.SetTag(ifcall.Tag()) - ofcall.SetQid(fid.Qid) + ofcall := &RAttach{ + tag: ifcall.Tag(), + qid: fid.Qid, + } r.ofcall = ofcall respond(r, nil) @@ -264,7 +263,7 @@ func respond(r *Req, err error) { rVersion(r, err) case *RAuth: rAuth(r, err) - case RAttach: + case *RAttach: rAttach(r, err) case *RWalk: rWalk(r, err)