File: conn_out.go

package info (click to toggle)
golang-github-linuxdeepin-go-x11-client 0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,656 kB
  • sloc: python: 944; sh: 38; makefile: 17
file content (297 lines) | stat: -rw-r--r-- 5,618 bytes parent folder | download | duplicates (2)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package x

import (
	"bufio"
	"errors"
	"io"
	"math"
)

func (c *Conn) Flush() (err error) {
	c.ioMu.Lock()
	err = c.out.flushTo(c.out.request)
	c.ioMu.Unlock()
	if err != nil {
		c.Close()
	}
	return
}

func (c *Conn) sendSync() {
	header := RequestHeader{
		Opcode: GetInputFocusOpcode,
		Length: 4,
	}
	logPrintln("sendSync")
	c.sendRequest(false, 0, RequestDiscardReply,
		header.toBytes(), nil)
}

func (c *Conn) SendSync() {
	c.ioMu.Lock()
	c.sendSync()
	c.ioMu.Unlock()
}

// sequence number
type SeqNum uint64

const (
	seqNumConnClosed    SeqNum = 0
	seqNumExtNotPresent SeqNum = math.MaxUint64 - iota
	seqNumQueryExtErr
)

var errExtNotPresent = errors.New("extension not present")
var errQueryExtErr = errors.New("query extension error")

func (n SeqNum) err() error {
	switch n {
	case seqNumConnClosed:
		return errConnClosed
	case seqNumExtNotPresent:
		return errExtNotPresent
	case seqNumQueryExtErr:
		return errQueryExtErr
	default:
		return nil
	}
}

type out struct {
	request        SeqNum
	requestWritten SeqNum
	bw             *bufio.Writer
}

func (o *out) flushTo(request SeqNum) error {
	if !(request <= o.request) {
		panic("assert request < o.request failed")
	}

	if o.requestWritten >= request {
		return nil
	}

	logPrintln("flushTo", request)
	err := o.bw.Flush()
	if err != nil {
		return err
	}
	o.requestWritten = o.request
	return nil
}

func (c *Conn) writeRequest(header []byte, body RequestBody) error {
	_, err := c.out.bw.Write(header)
	if err != nil {
		return err
	}
	var emptyBuf [3]byte
	for _, data := range body {
		_, err = c.out.bw.Write(data)
		if err != nil {
			return err
		}

		pad := Pad(len(data))
		if pad > 0 {
			_, err = c.out.bw.Write(emptyBuf[:pad])
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func (c *Conn) sendRequest(noReply bool, workaround uint, flags uint, header []byte, body RequestBody) {
	if c.isClosed() {
		return
	}
	c.out.request++
	if !noReply {
		// has reply
		c.in.requestExpected = c.out.request
	}
	logPrintln("sendRequest seq:", c.out.request)

	if workaround != 0 || flags != 0 {
		c.in.expectReply(c.out.request, workaround, flags)
	}

	err := c.writeRequest(header, body)
	if err != nil {
		logPrintln("write error:", err)
		c.Close()
		return
	}

	if c.out.bw.Buffered() == 0 {
		// write all the data of request to c.conn
		c.out.requestWritten = c.out.request
	}
}

type ProtocolRequest struct {
	Ext     *Extension
	NoReply bool
	Header  RequestHeader
	Body    RequestBody
}

// return sequence id
func (c *Conn) SendRequest(flags uint, req *ProtocolRequest) SeqNum {
	if c.isClosed() {
		return seqNumConnClosed
	}

	// process data auto field
	// set the major opcode, and the minor opcode for extensions
	if req.Ext != nil {
		extension := c.GetExtensionData(req.Ext)
		if extension == nil {
			return seqNumQueryExtErr
		}
		if !extension.Present {
			return seqNumExtNotPresent
		}

		req.Header.Opcode = extension.MajorOpcode
	}

	var requestLen uint64
	requestLen = 4
	for _, data := range req.Body {
		requestLen += uint64(len(data))
		requestLen += uint64(Pad(len(data)))
	}
	req.Header.Length = requestLen

	header := req.Header.toBytes()

	c.ioMu.Lock()
	c.sendRequest(req.NoReply, 0, flags, header, req.Body)
	request := c.out.request
	c.ioMu.Unlock()
	return request
}

type RequestHeader struct {
	Opcode uint8  // major opcode
	Data   uint8  // data or minor opcode
	Length uint64 // unit is byte
}

func (rh RequestHeader) toBytes() []byte {
	b := make([]byte, 4)
	b[0] = rh.Opcode
	b[1] = rh.Data

	if rh.Length&3 != 0 {
		panic("length is not a multiple of 4")
	}

	length := uint16(rh.Length)
	length >>= 2
	b[2] = byte(length)
	b[3] = byte(length >> 8)
	return b
}

type FixedSizeBuf struct {
	data   []byte
	offset int
}

func (b *FixedSizeBuf) Write1b(v uint8) *FixedSizeBuf {
	b.data[b.offset] = v
	b.offset++
	return b
}

// byte order: least significant byte first

func (b *FixedSizeBuf) Write2b(v uint16) *FixedSizeBuf {
	b.data[b.offset] = byte(v)
	b.data[b.offset+1] = byte(v >> 8)
	b.offset += 2
	return b
}

func (b *FixedSizeBuf) Write4b(v uint32) *FixedSizeBuf {
	b.data[b.offset] = byte(v)
	b.data[b.offset+1] = byte(v >> 8)
	b.data[b.offset+2] = byte(v >> 16)
	b.data[b.offset+3] = byte(v >> 24)
	b.offset += 4
	return b
}

func (b *FixedSizeBuf) Write8b(v uint64) *FixedSizeBuf {
	b.data[b.offset] = byte(v)
	b.data[b.offset+1] = byte(v >> 8)
	b.data[b.offset+2] = byte(v >> 16)
	b.data[b.offset+3] = byte(v >> 24)
	b.data[b.offset+4] = byte(v >> 32)
	b.data[b.offset+5] = byte(v >> 40)
	b.data[b.offset+6] = byte(v >> 48)
	b.data[b.offset+7] = byte(v >> 56)
	b.offset += 8
	return b
}

func (b *FixedSizeBuf) WritePad(n int) *FixedSizeBuf {
	b.offset += n
	return b
}

func (b *FixedSizeBuf) WriteString(s string) *FixedSizeBuf {
	n := copy(b.data[b.offset:], s)
	b.offset += n
	if n != len(s) {
		panic(io.ErrShortWrite)
	}
	return b
}

func (b *FixedSizeBuf) WriteBytes(p []byte) *FixedSizeBuf {
	n := copy(b.data[b.offset:], p)
	b.offset += n
	if n != len(p) {
		panic(io.ErrShortWrite)
	}
	return b
}

func (b *FixedSizeBuf) WriteBool(v bool) *FixedSizeBuf {
	return b.Write1b(BoolToUint8(v))
}

func (b *FixedSizeBuf) End() {
	if len(b.data) != b.offset {
		panic("not end")
	}
}

func (b *FixedSizeBuf) Bytes() []byte {
	return b.data
}

func NewFixedSizeBuf(size int) *FixedSizeBuf {
	return &FixedSizeBuf{
		data: make([]byte, size),
	}
}

type RequestBody [][]byte

func (rb *RequestBody) AddBlock(n int) *FixedSizeBuf {
	b := NewFixedSizeBuf(n * 4)
	*rb = append(*rb, b.data)
	return b
}

func (rb *RequestBody) AddBytes(data []byte) {
	*rb = append(*rb, data)
}