rpc-go/codec/codec.go

45 lines
990 B
Go
Raw Normal View History

2018-06-30 16:37:26 +00:00
package codec
import (
css "git.loafle.net/commons/server-go/socket"
)
type Codec interface {
Encode(message []byte) ([]byte, error)
Decode(message []byte) ([]byte, error)
}
type codec struct {
}
func (_ *codec) Encode(message []byte) ([]byte, error) {
return message, nil
}
func (_ *codec) Decode(message []byte) ([]byte, error) {
return message, nil
}
var DefaultCodec = &codec{}
type CodecSelector interface {
Encode(message []byte) (messageType int, encMessage []byte, err error)
Decode(messageType int, message []byte) (decMessage []byte, err error)
}
type codecSelector struct {
}
func (_ *codecSelector) Encode(message []byte) (messageType int, encMessage []byte, err error) {
messageType = css.TextMessage
encMessage, err = DefaultCodec.Encode(message)
return
}
func (_ *codecSelector) Decode(messageType int, message []byte) (decMessage []byte, err error) {
decMessage, err = DefaultCodec.Decode(message)
return
}
var DefaultCodecSelector = &codecSelector{}