forked from AmarnathCJD/gogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor_w.go
executable file
·159 lines (130 loc) · 3.84 KB
/
cursor_w.go
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
// Copyright (c) 2024 RoseLoverX
package tl
import (
"encoding/binary"
"fmt"
"io"
"math"
)
type Encoder struct {
w io.Writer
// this error is last unsuccessful write into w. if this err != nil,
// write() method will not write enay data
err error
}
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: w}
}
func (e *Encoder) write(b []byte) {
if e.err != nil {
return
}
n, err := e.w.Write(b)
if err != nil {
e.err = err
return
}
if n != len(b) {
e.err = &ErrorPartialWrite{Has: n, Want: len(b)}
}
}
// CheckErr must call after encoding has been finished. if this func returns not nil value, encoding has
// failed, and you shouldn't use its result
func (e *Encoder) CheckErr() error {
return e.err
}
// PutBool very specific type, т.к. There is a separate constructor under true and false,
// then we can calculate that these are two crc constants
func (e *Encoder) PutBool(v bool) {
crc := CrcFalse
if v {
crc = CrcTrue
}
e.PutUint(uint32(crc))
}
func (e *Encoder) PutUint(v uint32) {
buf := make([]byte, WordLen)
binary.LittleEndian.PutUint32(buf, v)
e.write(buf)
}
// PutCRC is an alias for Encoder.PutUint. It uses only for understanding what your code do (like
// self-documented code)
func (e *Encoder) PutCRC(v uint32) {
e.PutUint(v)
}
func (e *Encoder) PutInt(v int32) {
e.PutUint(uint32(v))
}
func (e *Encoder) PutLong(v int64) {
buf := make([]byte, LongLen)
binary.LittleEndian.PutUint64(buf, uint64(v))
e.write(buf)
}
func (e *Encoder) PutDouble(v float64) {
buf := make([]byte, DoubleLen)
binary.LittleEndian.PutUint64(buf, math.Float64bits(v))
e.write(buf)
}
func (e *Encoder) PutMessage(msg []byte) {
if len(msg) < MagicNumber {
e.putTinyBytes(msg)
} else {
e.putLargeBytes(msg)
}
}
func (e *Encoder) PutString(msg string) {
e.PutMessage([]byte(msg))
}
func (e *Encoder) putTinyBytes(msg []byte) {
if len(msg) >= MagicNumber {
// it's panicing, cause, you shouldn' call this func by your
// hands. panic required for internal purposes
panic("tiny messages supports maximum 253 elements")
}
// Here we assume that the length of the resulting message should be divisible by 4
// (32/8 = 4, 4 bytes one word)
// so we create a buf with a size sufficient for storing
// array + 0-3 dot bytes that would have divided the result by 4
realBytesLen := 1 + len(msg) // adding 1, cause we need to store length, realBytesLen doesn't store
factBytesLen := realBytesLen
if factBytesLen%WordLen > 0 {
factBytesLen += WordLen - factBytesLen%WordLen
}
buf := make([]byte, factBytesLen)
buf[0] = byte(len(msg)) // пихаем в первый байт размер сообщения
copy(buf[1:], msg)
e.write(buf)
}
func (e *Encoder) putLargeBytes(msg []byte) {
if len(msg) < MagicNumber {
// it's panicing, cause, you shouldn' call this func by your
// hands. panic required for internal purposes
panic("can't save binary stream with length less than 253 bytes")
}
maxLen := 1 << 24 // 3 bytes 24 bits, the first one is 0xfe, the remaining 3 times the length
if len(msg) > maxLen {
e.err = fmt.Errorf("message entity too large: expect less than %v, got %v", maxLen, len(msg))
return
}
realBytesLen := WordLen + len(msg) // First comes the magic byte and is 3 bytes long
factBytesLen := realBytesLen
if factBytesLen%WordLen > 0 {
factBytesLen += WordLen - factBytesLen%WordLen
}
// FIXME: this thing is uint number too. so, it can decode more simpler
littleEndianLength := make([]byte, 4)
binary.LittleEndian.PutUint32(littleEndianLength, uint32(len(msg)))
buf := make([]byte, factBytesLen)
buf[0] = byte(MagicNumber)
buf[1] = littleEndianLength[0]
buf[2] = littleEndianLength[1]
buf[3] = littleEndianLength[2]
copy(buf[WordLen:], msg)
e.write(buf)
}
func (e *Encoder) PutRawBytes(b []byte) {
e.write(b)
}
func (e *Encoder) PutVector(v any) {
e.encodeVector(sliceToInterfaceSlice(v)...)
}