forked from nielsAD/gowarcraft3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.go
159 lines (128 loc) · 3.63 KB
/
serialization.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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
package bncs
import (
"io"
"github.com/nielsAD/gowarcraft3/protocol"
)
// Encoder keeps amortized allocs at 0 for repeated Packet.Serialize calls.
type Encoder struct {
Encoding
buf protocol.Buffer
}
// NewEncoder initialization
func NewEncoder(e Encoding) *Encoder {
return &Encoder{
Encoding: e,
}
}
// Serialize packet and returns its byte representation.
// Result is valid until the next Serialize() call.
func (enc *Encoder) Serialize(p Packet) ([]byte, error) {
enc.buf.Truncate()
if err := p.Serialize(&enc.buf, &enc.Encoding); err != nil {
return nil, err
}
return enc.buf.Bytes, nil
}
// Write serializes p and writes it to w.
func (enc *Encoder) Write(w io.Writer, p Packet) (int, error) {
b, err := enc.Serialize(p)
if err != nil {
return 0, err
}
return w.Write(b)
}
// Decoder keeps amortized allocs at 0 for repeated Packet.Deserialize calls.
type Decoder struct {
Encoding
PacketFactory
bufRaw protocol.Buffer
bufDes protocol.Buffer
}
// NewDecoder initialization
func NewDecoder(e Encoding, f PacketFactory) *Decoder {
return &Decoder{
PacketFactory: f,
Encoding: e,
}
}
// Deserialize reads exactly one packet from b and returns it in the proper (deserialized) packet type.
func (dec *Decoder) Deserialize(b []byte) (Packet, int, error) {
dec.bufDes.Reset(b)
var size = dec.bufDes.Size()
if size < 4 || b[0] != ProtocolSig {
return nil, 0, ErrNoProtocolSig
}
var fac = dec.PacketFactory
if fac == nil {
fac = DefaultFactory
}
var pkt = fac.NewPacket(b[1], &dec.Encoding)
if pkt == nil {
return nil, 0, ErrNoFactory
}
var err = pkt.Deserialize(&dec.bufDes, &dec.Encoding)
var n = size - dec.bufDes.Size()
if err != nil {
return nil, n, err
}
return pkt, n, nil
}
// ReadRaw reads exactly one packet from r and returns its raw bytes.
// Result is valid until the next ReadRaw() call.
func (dec *Decoder) ReadRaw(r io.Reader) ([]byte, int, error) {
dec.bufRaw.Truncate()
if n, err := dec.bufRaw.ReadSizeFrom(r, 4); err != nil {
if err == io.ErrUnexpectedEOF {
err = ErrNoProtocolSig
}
return nil, int(n), err
}
if dec.bufRaw.Bytes[0] != ProtocolSig {
return nil, 4, ErrNoProtocolSig
}
var size = int(uint16(dec.bufRaw.Bytes[3])<<8 | uint16(dec.bufRaw.Bytes[2]))
if size < 4 {
return nil, 4, ErrNoProtocolSig
}
if n, err := dec.bufRaw.ReadSizeFrom(r, size-4); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return nil, int(n) + 4, err
}
return dec.bufRaw.Bytes, size, nil
}
// ReadClient exactly one packet from r and returns it in the proper (deserialized) packet type.
func (dec *Decoder) Read(r io.Reader) (Packet, int, error) {
b, n, err := dec.ReadRaw(r)
if err != nil {
return nil, n, err
}
p, m, err := dec.Deserialize(b)
if err != nil {
return nil, n, err
}
if m != n {
return nil, n, ErrInvalidPacketSize
}
return p, n, nil
}
// Serialize serializes p and returns its byte representation.
func Serialize(p Packet, e Encoding) ([]byte, error) {
return NewEncoder(e).Serialize(p)
}
// Deserialize reads exactly one packet from b and returns it in the proper (deserialized) packet type.
func Deserialize(b []byte, e Encoding) (Packet, int, error) {
return NewDecoder(e, nil).Deserialize(b)
}
// Read exactly one packet from r and returns it in the proper (deserialized) packet type.
func Read(r io.Reader, e Encoding) (Packet, int, error) {
return NewDecoder(e, nil).Read(r)
}
// Write serializes p and writes it to w.
func Write(w io.Writer, p Packet, e Encoding) (int, error) {
return NewEncoder(e).Write(w, p)
}