~rbn/neinp

ref: 280ca11dc4f314d4bb506de2f219eb70dd959227 neinp/message/attach.go -rw-r--r-- 1.8 KiB
280ca11dRuben Schuller update go.mod 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package message

import (
	"git.sr.ht/~rbn/neinp/basic"
	"git.sr.ht/~rbn/neinp/fid"
	"git.sr.ht/~rbn/neinp/qid"
	"io"
)

/*TAttach establishes a new connection.

Fid will be mapped to the root of the file system.
Afid specifies a fid gained from a previous TAuth.
Uname identifies the user accessing the file system, Aname
selecting the file tree to access.

See also: http://man.cat-v.org/plan_9/5/attach
*/
type TAttach struct {
	Fid   fid.Fid
	Afid  fid.Fid
	Uname string
	Aname string
}

func (m *TAttach) encode(w io.Writer) (int64, error) {
	n1, err := basic.Uint32Encode(w, uint32(m.Fid))
	if err != nil {
		return n1, err
	}

	n2, err := basic.Uint32Encode(w, uint32(m.Afid))
	if err != nil {
		return n1 + n2, err
	}

	n3, err := basic.StringEncode(w, m.Uname)
	if err != nil {
		return n1 + n2 + n3, err
	}

	n4, err := basic.StringEncode(w, m.Aname)
	if err != nil {
		return n1 + n2 + n3 + n4, err
	}

	return n1 + n2 + n3 + n4, nil
}

func (m *TAttach) decode(r io.Reader) (int64, error) {
	f, n1, err := basic.Uint32Decode(r)
	if err != nil {
		return n1, err
	}

	af, n2, err := basic.Uint32Decode(r)
	if err != nil {
		return n1 + n2, err
	}

	uname, n3, err := basic.StringDecode(r)
	if err != nil {
		return n1 + n2 + n3, err
	}

	aname, n4, err := basic.StringDecode(r)
	if err != nil {
		return n1 + n2 + n3 + n4, err
	}

	m.Fid = fid.Fid(f)
	m.Afid = fid.Fid(af)
	m.Uname = uname
	m.Aname = aname

	return n1 + n2 + n3 + n4, err
}

/*RAttach is the answer to an attach request.

Qid is the servers representation of the file trees root.

See also: http://man.cat-v.org/plan_9/5/attach
*/
type RAttach struct {
	Qid qid.Qid
}

func (m *RAttach) encode(w io.Writer) (int64, error) {
	return m.Qid.Encode(w)
}

func (m *RAttach) decode(r io.Reader) (int64, error) {
	return m.Qid.Decode(r)
}