mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-13 07:04:44 +08:00
优化工程结构
This commit is contained in:
@@ -11,13 +11,11 @@ type MessageJsonInfo struct {
|
||||
msgHandler MessageJsonHandler
|
||||
}
|
||||
|
||||
type MessageJsonHandler func(clientid uint64,msg interface{})
|
||||
type ConnectJsonHandler func(clientid uint64)
|
||||
type UnknownMessageJsonHandler func(clientid uint64,msg []byte)
|
||||
type MessageJsonHandler func(clientId uint64,msg interface{})
|
||||
type ConnectJsonHandler func(clientId uint64)
|
||||
type UnknownMessageJsonHandler func(clientId uint64,msg []byte)
|
||||
|
||||
type JsonProcessor struct {
|
||||
//SetByteOrder(littleEndian bool)
|
||||
//SetMsgLen(lenMsgLen int, minMsgLen uint32, maxMsgLen uint32)
|
||||
mapMsg map[uint16]MessageJsonInfo
|
||||
LittleEndian bool
|
||||
|
||||
@@ -26,33 +24,25 @@ type JsonProcessor struct {
|
||||
disconnectHandler ConnectJsonHandler
|
||||
}
|
||||
|
||||
func NewJsonProcessor() *JsonProcessor {
|
||||
processor := &JsonProcessor{mapMsg:map[uint16]MessageJsonInfo{}}
|
||||
return processor
|
||||
}
|
||||
|
||||
func (p *JsonProcessor) SetByteOrder(littleEndian bool) {
|
||||
p.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
type JsonPackInfo struct {
|
||||
typ uint16
|
||||
msg interface{}
|
||||
rawMsg []byte
|
||||
}
|
||||
|
||||
func (slf *JsonPackInfo) GetPackType() uint16 {
|
||||
return slf.typ
|
||||
func NewJsonProcessor() *JsonProcessor {
|
||||
processor := &JsonProcessor{mapMsg:map[uint16]MessageJsonInfo{}}
|
||||
return processor
|
||||
}
|
||||
|
||||
func (slf *JsonPackInfo) GetMsg() interface{} {
|
||||
return slf.msg
|
||||
func (jsonProcessor *JsonProcessor) SetByteOrder(littleEndian bool) {
|
||||
jsonProcessor.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *JsonProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
func (jsonProcessor *JsonProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
pPackInfo := msg.(*JsonPackInfo)
|
||||
v,ok := slf.mapMsg[pPackInfo.typ]
|
||||
v,ok := jsonProcessor.mapMsg[pPackInfo.typ]
|
||||
if ok == false {
|
||||
return fmt.Errorf("cannot find msgtype %d is register!",pPackInfo.typ)
|
||||
}
|
||||
@@ -61,18 +51,19 @@ func (slf *JsonProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) Unmarshal(data []byte) (interface{}, error) {
|
||||
typeStrcut := struct {Type int `json:"typ"`}{}
|
||||
err := json.Unmarshal(data, &typeStrcut)
|
||||
func (jsonProcessor *JsonProcessor) Unmarshal(data []byte) (interface{}, error) {
|
||||
typeStruct := struct {Type int `json:"typ"`}{}
|
||||
err := json.Unmarshal(data, &typeStruct)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
msgType := uint16(typeStrcut.Type)
|
||||
|
||||
info,ok := slf.mapMsg[msgType]
|
||||
msgType := uint16(typeStruct.Type)
|
||||
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)
|
||||
}
|
||||
|
||||
msg := reflect.New(info.msgType.Elem()).Interface()
|
||||
err = json.Unmarshal(data, msg)
|
||||
if err != nil {
|
||||
@@ -82,8 +73,7 @@ func (slf *JsonProcessor) Unmarshal(data []byte) (interface{}, error) {
|
||||
return &JsonPackInfo{typ:msgType,msg:msg},nil
|
||||
}
|
||||
|
||||
|
||||
func (slf *JsonProcessor) Marshal(msg interface{}) ([]byte, error) {
|
||||
func (jsonProcessor *JsonProcessor) Marshal(msg interface{}) ([]byte, error) {
|
||||
rawMsg,err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return nil,err
|
||||
@@ -92,43 +82,50 @@ func (slf *JsonProcessor) Marshal(msg interface{}) ([]byte, error) {
|
||||
return rawMsg,nil
|
||||
}
|
||||
|
||||
func (slf *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
|
||||
slf.mapMsg[msgtype] = info
|
||||
jsonProcessor.mapMsg[msgtype] = info
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) MakeMsg(msgType uint16,msg interface{}) *JsonPackInfo {
|
||||
func (jsonProcessor *JsonProcessor) MakeMsg(msgType uint16,msg interface{}) *JsonPackInfo {
|
||||
return &JsonPackInfo{typ:msgType,msg:msg}
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) MakeRawMsg(msgType uint16,msg []byte) *JsonPackInfo {
|
||||
func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16,msg []byte) *JsonPackInfo {
|
||||
return &JsonPackInfo{typ:msgType,rawMsg:msg}
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
slf.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
jsonProcessor.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
}
|
||||
|
||||
// connect event
|
||||
func (slf *JsonProcessor) ConnectedRoute(userData interface{}){
|
||||
slf.connectHandler(userData.(uint64))
|
||||
func (jsonProcessor *JsonProcessor) ConnectedRoute(userData interface{}){
|
||||
jsonProcessor.connectHandler(userData.(uint64))
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) DisConnectedRoute(userData interface{}){
|
||||
slf.disconnectHandler(userData.(uint64))
|
||||
func (jsonProcessor *JsonProcessor) DisConnectedRoute(userData interface{}){
|
||||
jsonProcessor.disconnectHandler(userData.(uint64))
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageJsonHandler){
|
||||
slf.unknownMessageHandler = unknownMessageHandler
|
||||
func (jsonProcessor *JsonProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageJsonHandler){
|
||||
jsonProcessor.unknownMessageHandler = unknownMessageHandler
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) RegisterConnected(connectHandler ConnectJsonHandler){
|
||||
slf.connectHandler = connectHandler
|
||||
func (jsonProcessor *JsonProcessor) RegisterConnected(connectHandler ConnectJsonHandler){
|
||||
jsonProcessor.connectHandler = connectHandler
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) RegisterDisConnected(disconnectHandler ConnectJsonHandler){
|
||||
slf.disconnectHandler = disconnectHandler
|
||||
}
|
||||
func (jsonProcessor *JsonProcessor) RegisterDisConnected(disconnectHandler ConnectJsonHandler){
|
||||
jsonProcessor.disconnectHandler = disconnectHandler
|
||||
}
|
||||
|
||||
func (slf *JsonPackInfo) GetPackType() uint16 {
|
||||
return slf.typ
|
||||
}
|
||||
|
||||
func (slf *JsonPackInfo) GetMsg() interface{} {
|
||||
return slf.msg
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user