vendor: add golib/msg

This commit is contained in:
fatedier 2018-05-11 16:42:08 +08:00
parent a0fe2fc2c2
commit d9aadab4cb
4 changed files with 208 additions and 2 deletions

5
Gopkg.lock generated
View File

@ -25,11 +25,12 @@
"crypto",
"errors",
"io",
"msg/json",
"net",
"net/mux",
"pool"
]
revision = "c0c98fef672bc2ef02359dddc03e05dce3737640"
revision = "354693cdd7fd9fa4f207c2f91ec2534615d3e5e5"
[[projects]]
branch = "frp"
@ -167,6 +168,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "d4f8f1e8dd5c07302832144eed2288be10b8061eb60712540ff0c569774e216f"
inputs-digest = "d934d16928772cfb22c55a39964c7ca364d02fe1ab680a90cdb5c3c8be256273"
solver-name = "gps-cdcl"
solver-version = 1

50
vendor/github.com/fatedier/golib/msg/json/msg.go generated vendored Normal file
View File

@ -0,0 +1,50 @@
// Copyright 2018 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package json
import (
"reflect"
)
var (
defaultMaxMsgLength int64 = 10240
)
// Message wraps socket packages for communicating between frpc and frps.
type Message interface{}
type MsgCtl struct {
typeMap map[byte]reflect.Type
typeByteMap map[reflect.Type]byte
maxMsgLength int64
}
func NewMsgCtl() *MsgCtl {
return &MsgCtl{
typeMap: make(map[byte]reflect.Type),
typeByteMap: make(map[reflect.Type]byte),
maxMsgLength: defaultMaxMsgLength,
}
}
func (msgCtl *MsgCtl) RegisterMsg(typeByte byte, msg interface{}) {
msgCtl.typeMap[typeByte] = reflect.TypeOf(msg)
msgCtl.typeByteMap[reflect.TypeOf(msg)] = typeByte
}
func (msgCtl *MsgCtl) SetMaxMsgLength(length int64) {
msgCtl.maxMsgLength = length
}

66
vendor/github.com/fatedier/golib/msg/json/pack.go generated vendored Normal file
View File

@ -0,0 +1,66 @@
// Copyright 2018 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package json
import (
"bytes"
"encoding/binary"
"encoding/json"
"reflect"
)
func (msgCtl *MsgCtl) unpack(typeByte byte, buffer []byte, msgIn Message) (msg Message, err error) {
if msgIn == nil {
t, ok := msgCtl.typeMap[typeByte]
if !ok {
err = ErrMsgType
return
}
msg = reflect.New(t).Interface().(Message)
} else {
msg = msgIn
}
err = json.Unmarshal(buffer, &msg)
return
}
func (msgCtl *MsgCtl) UnPackInto(buffer []byte, msg Message) (err error) {
_, err = msgCtl.unpack(' ', buffer, msg)
return
}
func (msgCtl *MsgCtl) UnPack(typeByte byte, buffer []byte) (msg Message, err error) {
return msgCtl.unpack(typeByte, buffer, nil)
}
func (msgCtl *MsgCtl) Pack(msg Message) ([]byte, error) {
typeByte, ok := msgCtl.typeByteMap[reflect.TypeOf(msg).Elem()]
if !ok {
return nil, ErrMsgType
}
content, err := json.Marshal(msg)
if err != nil {
return nil, err
}
buffer := bytes.NewBuffer(nil)
buffer.WriteByte(typeByte)
binary.Write(buffer, binary.BigEndian, int64(len(content)))
buffer.Write(content)
return buffer.Bytes(), nil
}

89
vendor/github.com/fatedier/golib/msg/json/process.go generated vendored Normal file
View File

@ -0,0 +1,89 @@
// Copyright 2018 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package json
import (
"encoding/binary"
"errors"
"io"
)
var (
ErrMsgType = errors.New("message type error")
ErrMaxMsgLength = errors.New("message length exceed the limit")
ErrMsgFormat = errors.New("message format error")
)
func (msgCtl *MsgCtl) readMsg(c io.Reader) (typeByte byte, buffer []byte, err error) {
buffer = make([]byte, 1)
_, err = c.Read(buffer)
if err != nil {
return
}
typeByte = buffer[0]
if _, ok := msgCtl.typeMap[typeByte]; !ok {
err = ErrMsgType
return
}
var length int64
err = binary.Read(c, binary.BigEndian, &length)
if err != nil {
return
}
if length > msgCtl.maxMsgLength {
err = ErrMaxMsgLength
return
}
buffer = make([]byte, length)
n, err := io.ReadFull(c, buffer)
if err != nil {
return
}
if int64(n) != length {
err = ErrMsgFormat
}
return
}
func (msgCtl *MsgCtl) ReadMsg(c io.Reader) (msg Message, err error) {
typeByte, buffer, err := msgCtl.readMsg(c)
if err != nil {
return
}
return msgCtl.UnPack(typeByte, buffer)
}
func (msgCtl *MsgCtl) ReadMsgInto(c io.Reader, msg Message) (err error) {
_, buffer, err := msgCtl.readMsg(c)
if err != nil {
return
}
return msgCtl.UnPackInto(buffer, msg)
}
func (msgCtl *MsgCtl) WriteMsg(c io.Writer, msg interface{}) (err error) {
buffer, err := msgCtl.Pack(msg)
if err != nil {
return
}
if _, err = c.Write(buffer); err != nil {
return
}
return nil
}