-
Notifications
You must be signed in to change notification settings - Fork 8
/
parser.go
62 lines (52 loc) · 1.91 KB
/
parser.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
package socketio
import (
"errors"
"io"
"reflect"
"github.com/zyxar/socketio/engine"
)
var (
// ErrUnknownPacket indicates packet invalid or unknown when parser encoding/decoding data
ErrUnknownPacket = errors.New("unknown packet")
// DefaultParser is default parser implementation for socket.io, compatible with `socket.io-parser`.
DefaultParser Parser = &defaultParser{}
// MsgpackParser is msgpack parser implementation for socket.io, compatible with `socket.io-msgpack-parser`.
MsgpackParser Parser = &msgpackParser{}
)
type üWriter interface {
io.Writer
io.ByteWriter
}
// Encoder encodes a Packet into byte format
type Encoder interface {
Encode(p *Packet) ([]byte, [][]byte, error)
}
// Decoder decodes data into a Packet
type Decoder interface {
Add(msgType MessageType, data []byte) error
Decoded() <-chan *Packet
ParseData(p *Packet) (string, []byte, [][]byte, error)
ArgsUnmarshaler
}
// ArgsUnmarshaler unmarshals func arguments `args` from data and binary (bin, if exists).
// Decoder should implement ArgsUnmarshaler.
// For `DefaultParser`, data denotes the data in the 1st Packet (w/ type string), while bin denotes binary data
// in following packets if available;
// For `MsgpackParser`, bin is not used since all data are packed in a single Packet;
// args are acquired from reflection, usually by calling `newCallback(func)`
type ArgsUnmarshaler interface {
UnmarshalArgs(args []reflect.Type, data []byte, bin [][]byte) ([]reflect.Value, error)
}
// Parser provides Encoder and Decoder instance, like a factory
type Parser interface {
Encoder() Encoder
Decoder() Decoder
}
// MessageType is alias of engine.MessageType
type MessageType = engine.MessageType
const (
// MessageTypeString is alias of engine.MessageTypeString
MessageTypeString MessageType = engine.MessageTypeString
// MessageTypeBinary is alias of engine.MessageTypeBinary
MessageTypeBinary MessageType = engine.MessageTypeBinary
)