mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-13 19:59:31 +08:00
扩展pbprocessor参数
This commit is contained in:
2
go.mod
2
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module github.com/duanhf2012/origin
|
module github.com/duanhf2012/origin
|
||||||
|
|
||||||
go 1.15
|
go 1.17
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-sql-driver/mysql v1.5.0
|
github.com/go-sql-driver/mysql v1.5.0
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type ConnectHandler func(clientId uint64)
|
|||||||
type UnknownMessageHandler func(clientId uint64, msg []byte)
|
type UnknownMessageHandler func(clientId uint64, msg []byte)
|
||||||
|
|
||||||
const MsgTypeSize = 2
|
const MsgTypeSize = 2
|
||||||
|
|
||||||
type PBProcessor struct {
|
type PBProcessor struct {
|
||||||
mapMsg map[uint16]MessageInfo
|
mapMsg map[uint16]MessageInfo
|
||||||
LittleEndian bool
|
LittleEndian bool
|
||||||
@@ -53,19 +54,19 @@ func (slf *PBPackInfo) GetMsg() proto.Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (pbProcessor *PBProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
func (pbProcessor *PBProcessor) MsgRoute(clientId uint64, msg interface{}) error {
|
||||||
pPackInfo := msg.(*PBPackInfo)
|
pPackInfo := msg.(*PBPackInfo)
|
||||||
v, ok := pbProcessor.mapMsg[pPackInfo.typ]
|
v, ok := pbProcessor.mapMsg[pPackInfo.typ]
|
||||||
if ok == false {
|
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(userdata.(uint64),pPackInfo.msg)
|
v.msgHandler(clientId, pPackInfo.msg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (pbProcessor *PBProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
func (pbProcessor *PBProcessor) Unmarshal(clientId uint64, data []byte) (interface{}, error) {
|
||||||
defer pbProcessor.ReleaseByteSlice(data)
|
defer pbProcessor.ReleaseByteSlice(data)
|
||||||
var msgType uint16
|
var msgType uint16
|
||||||
if pbProcessor.LittleEndian == true {
|
if pbProcessor.LittleEndian == true {
|
||||||
@@ -89,7 +90,7 @@ func (pbProcessor *PBProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (pbProcessor *PBProcessor ) Marshal(msg interface{}) ([]byte, error){
|
func (pbProcessor *PBProcessor) Marshal(clientId uint64, msg interface{}) ([]byte, error) {
|
||||||
pMsg := msg.(*PBPackInfo)
|
pMsg := msg.(*PBPackInfo)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
@@ -127,17 +128,17 @@ func (pbProcessor *PBProcessor) MakeRawMsg(msgType uint16,msg []byte) *PBPackInf
|
|||||||
return &PBPackInfo{typ: msgType, rawMsg: msg}
|
return &PBPackInfo{typ: msgType, rawMsg: msg}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pbProcessor *PBProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
func (pbProcessor *PBProcessor) UnknownMsgRoute(clientId uint64, msg interface{}) {
|
||||||
pbProcessor.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
pbProcessor.unknownMessageHandler(clientId, msg.([]byte))
|
||||||
}
|
}
|
||||||
|
|
||||||
// connect event
|
// connect event
|
||||||
func (pbProcessor *PBProcessor) ConnectedRoute(userData interface{}){
|
func (pbProcessor *PBProcessor) ConnectedRoute(clientId uint64) {
|
||||||
pbProcessor.connectHandler(userData.(uint64))
|
pbProcessor.connectHandler(clientId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pbProcessor *PBProcessor) DisConnectedRoute(userData interface{}){
|
func (pbProcessor *PBProcessor) DisConnectedRoute(clientId uint64) {
|
||||||
pbProcessor.disconnectHandler(userData.(uint64))
|
pbProcessor.disconnectHandler(clientId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pbProcessor *PBProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageHandler) {
|
func (pbProcessor *PBProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageHandler) {
|
||||||
@@ -151,6 +152,3 @@ func (pbProcessor *PBProcessor) RegisterConnected(connectHandler ConnectHandler)
|
|||||||
func (pbProcessor *PBProcessor) RegisterDisConnected(disconnectHandler ConnectHandler) {
|
func (pbProcessor *PBProcessor) RegisterDisConnected(disconnectHandler ConnectHandler) {
|
||||||
pbProcessor.disconnectHandler = disconnectHandler
|
pbProcessor.disconnectHandler = disconnectHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user