forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcodec.go
37 lines (31 loc) · 993 Bytes
/
codec.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
// Copyright 2019 Andy Pan. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gnet
// CRLFByte represents a byte of CRLF.
var CRLFByte = byte('\n')
type (
// ICodec is the interface of gnet codec.
ICodec interface {
// Encode encodes frames upon server responses into TCP stream.
Encode(c Conn, buf []byte) ([]byte, error)
// Decode decodes frames from TCP stream via specific implementation.
Decode(c Conn) ([]byte, error)
}
// BuiltInFrameCodec is the built-in codec which will be assigned to gnet server when customized codec is not set up.
BuiltInFrameCodec struct {
}
)
// Encode ...
func (cc *BuiltInFrameCodec) Encode(c Conn, buf []byte) ([]byte, error) {
return buf, nil
}
// Decode ...
func (cc *BuiltInFrameCodec) Decode(c Conn) ([]byte, error) {
buf := c.Read()
if len(buf) == 0 {
return nil, nil
}
c.ResetBuffer()
return buf, nil
}