~rbn/neinp

ref: 280ca11dc4f314d4bb506de2f219eb70dd959227 neinp/message/message.go -rw-r--r-- 4.3 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package message

import (
	"git.sr.ht/~rbn/neinp/basic"
	"bytes"
	"fmt"
	"io"
)

//HeaderSize is the length of the size, type and tag headers in bytes.
const HeaderSize uint64 = 4 + 1 + 2

//An encoder writes itself as 9p representation to a io.Writer
type encoder interface {
	encode(w io.Writer) (int64, error)
}

//A decoder reads a 9p representation of itself from a io.Reader
type decoder interface {
	decode(r io.Reader) (int64, error)
}

//Content is implemented by the message types containing the real information.
type Content interface {
	encoder
	decoder
}

/*Message is the general message type.

It wraps implementations of interface Content.
*/
type Message struct {
	typ     messageType
	Tag     uint16
	Content Content
}

// get the matching messageType for a Content
func contentType(c Content) (messageType, error) {
	switch c.(type) {
	case *RVersion:
		return rversion, nil
	case *RAuth:
		return rauth, nil
	case *RAttach:
		return rattach, nil
	case *RError:
		return rerror, nil
	case *RFlush:
		return rflush, nil
	case *RWalk:
		return rwalk, nil
	case *ROpen:
		return ropen, nil
	case *RCreate:
		return rcreate, nil
	case *RRead:
		return rread, nil
	case *RWrite:
		return rwrite, nil
	case *RClunk:
		return rclunk, nil
	case *RRemove:
		return rremove, nil
	case *RStat:
		return rstat, nil
	case *RWstat:
		return rwstat, nil
	case *TVersion:
		return tversion, nil
	case *TAuth:
		return tauth, nil
	case *TAttach:
		return tattach, nil
	case *TFlush:
		return tflush, nil
	case *TWalk:
		return twalk, nil
	case *TOpen:
		return topen, nil
	case *TCreate:
		return tcreate, nil
	case *TRead:
		return tread, nil
	case *TWrite:
		return twrite, nil
	case *TClunk:
		return tclunk, nil
	case *TRemove:
		return tremove, nil
	case *TStat:
		return tstat, nil
	case *TWstat:
		return twstat, nil
	default:
		return 0, fmt.Errorf("unknown content type: %T", c)
	}
}

/*New creates a new Message with a given tag and Content.*/
func New(tag uint16, content Content) (Message, error) {
	typ, err := contentType(content)
	if err != nil {
		return Message{}, err
	}
	return Message{typ: typ, Tag: tag, Content: content}, nil
}

//Response returns a new Message prepared for using as reply.
func (m Message) Response() Message {
	return Message{Tag: m.Tag}
}

/*Decode reads a message from a io.Reader.

It returns the count of read bytes and an error.*/
func (m *Message) Decode(r io.Reader) (int64, error) {
	size, n1, err := basic.Uint32Decode(r)
	if err != nil {
		return n1, err
	}

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

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

	switch messageType(typ) {
	case tversion:
		m.Content = &TVersion{}
	case tauth:
		m.Content = &TAuth{}
	case tattach:
		m.Content = &TAttach{}
	case tflush:
		m.Content = &TFlush{}
	case twalk:
		m.Content = &TWalk{}
	case topen:
		m.Content = &TOpen{}
	case tcreate:
		m.Content = &TCreate{}
	case tread:
		m.Content = &TRead{}
	case twrite:
		m.Content = &TWrite{}
	case tclunk:
		m.Content = &TClunk{}
	case tremove:
		m.Content = &TRemove{}
	case tstat:
		m.Content = &TStat{}
	case twstat:
		m.Content = &TWstat{}
	default:
		return n1 + n2 + n3, fmt.Errorf("unknown message type: %o", typ)
	}

	n4, err := m.Content.decode(r)
	if err != nil {
		return n1 + n2 + n3 + n4, err
	}

	if uint32(n1+n2+n3+n4) != size {
		return n1 + n2 + n3 + n4, fmt.Errorf("short read: %v read, %v expected", n1+n2+n3+n4, size)
	}

	m.typ = messageType(typ)
	m.Tag = tag

	return n1 + n2 + n3 + n4, nil
}

/*Encode writes a Message to an io.Writer.

It returns the count of bytes written and an error.*/
func (m *Message) Encode(w io.Writer) (int64, error) {
	var buf bytes.Buffer

	typ, err := contentType(m.Content)
	if err != nil {
		return 0, err
	}

	_, err = basic.Uint8Encode(&buf, uint8(typ))
	if err != nil {
		return 0, err
	}

	_, err = basic.Uint16Encode(&buf, m.Tag)
	if err != nil {
		return 0, err
	}

	_, err = m.Content.encode(&buf)
	if err != nil {
		return 0, err
	}

	size := uint32(4 + buf.Len())

	n1, err := basic.Uint32Encode(w, size)
	if err != nil {
		return n1, err
	}

	n2, err := buf.WriteTo(w)
	if err != nil {
		return n1 + n2, err
	}

	if uint32(n1+n2) != size {
		return n1 + n2, fmt.Errorf("short write: %v written %v expected", n1+n2, size)
	}

	return n1 + n2, nil
}