mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-11 21:54:50 +08:00
优化代码规范
This commit is contained in:
@@ -13,29 +13,29 @@ type MessageJsonInfo struct {
|
||||
msgHandler MessageJsonHandler
|
||||
}
|
||||
|
||||
type MessageJsonHandler func(clientId string,msg interface{})
|
||||
type MessageJsonHandler func(clientId string, msg interface{})
|
||||
type ConnectJsonHandler func(clientId string)
|
||||
type UnknownMessageJsonHandler func(clientId string,msg []byte)
|
||||
type UnknownMessageJsonHandler func(clientId string, msg []byte)
|
||||
|
||||
type JsonProcessor struct {
|
||||
mapMsg map[uint16]MessageJsonInfo
|
||||
mapMsg map[uint16]MessageJsonInfo
|
||||
LittleEndian bool
|
||||
|
||||
unknownMessageHandler UnknownMessageJsonHandler
|
||||
connectHandler ConnectJsonHandler
|
||||
disconnectHandler ConnectJsonHandler
|
||||
bytespool.IBytesMempool
|
||||
connectHandler ConnectJsonHandler
|
||||
disconnectHandler ConnectJsonHandler
|
||||
bytespool.IBytesMemPool
|
||||
}
|
||||
|
||||
type JsonPackInfo struct {
|
||||
typ uint16
|
||||
msg interface{}
|
||||
typ uint16
|
||||
msg interface{}
|
||||
rawMsg []byte
|
||||
}
|
||||
|
||||
func NewJsonProcessor() *JsonProcessor {
|
||||
processor := &JsonProcessor{mapMsg:map[uint16]MessageJsonInfo{}}
|
||||
processor.IBytesMempool = bytespool.NewMemAreaPool()
|
||||
processor := &JsonProcessor{mapMsg: map[uint16]MessageJsonInfo{}}
|
||||
processor.IBytesMemPool = bytespool.NewMemAreaPool()
|
||||
|
||||
return processor
|
||||
}
|
||||
@@ -44,99 +44,101 @@ func (jsonProcessor *JsonProcessor) SetByteOrder(littleEndian bool) {
|
||||
jsonProcessor.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (jsonProcessor *JsonProcessor ) MsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)) error{
|
||||
// MsgRoute must goroutine safe
|
||||
func (jsonProcessor *JsonProcessor) MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error {
|
||||
pPackInfo := msg.(*JsonPackInfo)
|
||||
defer recyclerReaderBytes(pPackInfo.rawMsg)
|
||||
|
||||
v,ok := jsonProcessor.mapMsg[pPackInfo.typ]
|
||||
v, ok := jsonProcessor.mapMsg[pPackInfo.typ]
|
||||
if ok == false {
|
||||
return fmt.Errorf("cannot find msgtype %d is register!",pPackInfo.typ)
|
||||
return fmt.Errorf("cannot find msgtype %d is register", pPackInfo.typ)
|
||||
}
|
||||
|
||||
v.msgHandler(clientId,pPackInfo.msg)
|
||||
v.msgHandler(clientId, pPackInfo.msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) Unmarshal(clientId string,data []byte) (interface{}, error) {
|
||||
typeStruct := struct {Type int `json:"typ"`}{}
|
||||
func (jsonProcessor *JsonProcessor) Unmarshal(clientId string, data []byte) (interface{}, error) {
|
||||
typeStruct := struct {
|
||||
Type int `json:"typ"`
|
||||
}{}
|
||||
err := json.Unmarshal(data, &typeStruct)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
msgType := uint16(typeStruct.Type)
|
||||
info,ok := jsonProcessor.mapMsg[msgType]
|
||||
info, ok := jsonProcessor.mapMsg[msgType]
|
||||
if ok == false {
|
||||
return nil,fmt.Errorf("Cannot find register %d msgType!",msgType)
|
||||
return nil, fmt.Errorf("cannot find register %d msgType", msgType)
|
||||
}
|
||||
|
||||
msgData := reflect.New(info.msgType.Elem()).Interface()
|
||||
err = json.Unmarshal(data, msgData)
|
||||
if err != nil {
|
||||
return nil,err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &JsonPackInfo{typ:msgType,msg:msgData,rawMsg: data},nil
|
||||
return &JsonPackInfo{typ: msgType, msg: msgData, rawMsg: data}, nil
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) Marshal(clientId string,msg interface{}) ([]byte, error) {
|
||||
rawMsg,err := json.Marshal(msg)
|
||||
func (jsonProcessor *JsonProcessor) Marshal(clientId string, msg interface{}) ([]byte, error) {
|
||||
rawMsg, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return nil,err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rawMsg,nil
|
||||
return rawMsg, nil
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) Register(msgtype uint16,msg interface{},handle MessageJsonHandler) {
|
||||
func (jsonProcessor *JsonProcessor) Register(msgType uint16, msg interface{}, handle MessageJsonHandler) {
|
||||
var info MessageJsonInfo
|
||||
|
||||
info.msgType = reflect.TypeOf(msg)
|
||||
info.msgHandler = handle
|
||||
jsonProcessor.mapMsg[msgtype] = info
|
||||
jsonProcessor.mapMsg[msgType] = info
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) MakeMsg(msgType uint16,msg interface{}) *JsonPackInfo {
|
||||
return &JsonPackInfo{typ:msgType,msg:msg}
|
||||
func (jsonProcessor *JsonProcessor) MakeMsg(msgType uint16, msg interface{}) *JsonPackInfo {
|
||||
return &JsonPackInfo{typ: msgType, msg: msg}
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16,msg []byte) *JsonPackInfo {
|
||||
return &JsonPackInfo{typ:msgType,rawMsg:msg}
|
||||
func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16, msg []byte) *JsonPackInfo {
|
||||
return &JsonPackInfo{typ: msgType, rawMsg: msg}
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)){
|
||||
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) {
|
||||
defer recyclerReaderBytes(msg.([]byte))
|
||||
if jsonProcessor.unknownMessageHandler==nil {
|
||||
log.Debug("Unknown message",log.String("clientId",clientId))
|
||||
if jsonProcessor.unknownMessageHandler == nil {
|
||||
log.Debug("Unknown message", log.String("clientId", clientId))
|
||||
return
|
||||
}
|
||||
|
||||
jsonProcessor.unknownMessageHandler(clientId,msg.([]byte))
|
||||
jsonProcessor.unknownMessageHandler(clientId, msg.([]byte))
|
||||
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) ConnectedRoute(clientId string){
|
||||
func (jsonProcessor *JsonProcessor) ConnectedRoute(clientId string) {
|
||||
if jsonProcessor.connectHandler != nil {
|
||||
jsonProcessor.connectHandler(clientId)
|
||||
}
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) DisConnectedRoute(clientId string){
|
||||
func (jsonProcessor *JsonProcessor) DisConnectedRoute(clientId string) {
|
||||
if jsonProcessor.disconnectHandler != nil {
|
||||
jsonProcessor.disconnectHandler(clientId)
|
||||
}
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageJsonHandler){
|
||||
func (jsonProcessor *JsonProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageJsonHandler) {
|
||||
jsonProcessor.unknownMessageHandler = unknownMessageHandler
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) RegisterConnected(connectHandler ConnectJsonHandler){
|
||||
func (jsonProcessor *JsonProcessor) RegisterConnected(connectHandler ConnectJsonHandler) {
|
||||
jsonProcessor.connectHandler = connectHandler
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) RegisterDisConnected(disconnectHandler ConnectJsonHandler){
|
||||
func (jsonProcessor *JsonProcessor) RegisterDisConnected(disconnectHandler ConnectJsonHandler) {
|
||||
jsonProcessor.disconnectHandler = disconnectHandler
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user