mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-13 07:04:44 +08:00
优化工程结构
This commit is contained in:
@@ -18,45 +18,36 @@ const RawMsgTypeSize = 2
|
||||
type PBRawProcessor struct {
|
||||
msgHandler RawMessageHandler
|
||||
LittleEndian bool
|
||||
|
||||
unknownMessageHandler UnknownRawMessageHandler
|
||||
connectHandler RawConnectHandler
|
||||
disconnectHandler RawConnectHandler
|
||||
}
|
||||
|
||||
func NewPBRawProcessor() *PBRawProcessor {
|
||||
processor := &PBRawProcessor{}
|
||||
return processor
|
||||
}
|
||||
|
||||
func (p *PBRawProcessor) SetByteOrder(littleEndian bool) {
|
||||
p.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
type PBRawPackInfo struct {
|
||||
typ uint16
|
||||
rawMsg []byte
|
||||
}
|
||||
|
||||
func (slf *PBRawPackInfo) GetPackType() uint16 {
|
||||
return slf.typ
|
||||
func NewPBRawProcessor() *PBRawProcessor {
|
||||
processor := &PBRawProcessor{}
|
||||
return processor
|
||||
}
|
||||
|
||||
func (slf *PBRawPackInfo) GetMsg() []byte {
|
||||
return slf.rawMsg
|
||||
func (pbRawProcessor *PBRawProcessor) SetByteOrder(littleEndian bool) {
|
||||
pbRawProcessor.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *PBRawProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
func (pbRawProcessor *PBRawProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
pPackInfo := msg.(*PBRawPackInfo)
|
||||
slf.msgHandler(userdata.(uint64),pPackInfo.typ,pPackInfo.rawMsg)
|
||||
pbRawProcessor.msgHandler(userdata.(uint64),pPackInfo.typ,pPackInfo.rawMsg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *PBRawProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
||||
func (pbRawProcessor *PBRawProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
||||
var msgType uint16
|
||||
if slf.LittleEndian == true {
|
||||
if pbRawProcessor.LittleEndian == true {
|
||||
msgType = binary.LittleEndian.Uint16(data[:2])
|
||||
}else{
|
||||
msgType = binary.BigEndian.Uint16(data[:2])
|
||||
@@ -66,11 +57,11 @@ func (slf *PBRawProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *PBRawProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
func (pbRawProcessor *PBRawProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
pMsg := msg.(*PBRawPackInfo)
|
||||
|
||||
buff := make([]byte, 2, len(pMsg.rawMsg)+RawMsgTypeSize)
|
||||
if slf.LittleEndian == true {
|
||||
if pbRawProcessor.LittleEndian == true {
|
||||
binary.LittleEndian.PutUint16(buff[:2],pMsg.typ)
|
||||
}else{
|
||||
binary.BigEndian.PutUint16(buff[:2],pMsg.typ)
|
||||
@@ -80,40 +71,46 @@ func (slf *PBRawProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
return buff,nil
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) SetRawMsgHandler(handle RawMessageHandler) {
|
||||
slf.msgHandler = handle
|
||||
func (pbRawProcessor *PBRawProcessor) SetRawMsgHandler(handle RawMessageHandler) {
|
||||
pbRawProcessor.msgHandler = handle
|
||||
}
|
||||
|
||||
|
||||
func (slf *PBRawProcessor) MakeRawMsg(msgType uint16,msg []byte) *PBRawPackInfo {
|
||||
func (pbRawProcessor *PBRawProcessor) MakeRawMsg(msgType uint16,msg []byte) *PBRawPackInfo {
|
||||
return &PBRawPackInfo{typ:msgType,rawMsg:msg}
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
if slf.unknownMessageHandler == nil {
|
||||
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
if pbRawProcessor.unknownMessageHandler == nil {
|
||||
return
|
||||
}
|
||||
slf.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
pbRawProcessor.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
}
|
||||
|
||||
// connect event
|
||||
func (slf *PBRawProcessor) ConnectedRoute(userData interface{}){
|
||||
slf.connectHandler(userData.(uint64))
|
||||
func (pbRawProcessor *PBRawProcessor) ConnectedRoute(userData interface{}){
|
||||
pbRawProcessor.connectHandler(userData.(uint64))
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) DisConnectedRoute(userData interface{}){
|
||||
slf.disconnectHandler(userData.(uint64))
|
||||
func (pbRawProcessor *PBRawProcessor) DisConnectedRoute(userData interface{}){
|
||||
pbRawProcessor.disconnectHandler(userData.(uint64))
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) SetUnknownMsgHandler(unknownMessageHandler UnknownRawMessageHandler){
|
||||
slf.unknownMessageHandler = unknownMessageHandler
|
||||
func (pbRawProcessor *PBRawProcessor) SetUnknownMsgHandler(unknownMessageHandler UnknownRawMessageHandler){
|
||||
pbRawProcessor.unknownMessageHandler = unknownMessageHandler
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) SetConnectedHandler(connectHandler RawConnectHandler){
|
||||
slf.connectHandler = connectHandler
|
||||
func (pbRawProcessor *PBRawProcessor) SetConnectedHandler(connectHandler RawConnectHandler){
|
||||
pbRawProcessor.connectHandler = connectHandler
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) SetDisConnectedHandler(disconnectHandler RawConnectHandler){
|
||||
slf.disconnectHandler = disconnectHandler
|
||||
func (pbRawProcessor *PBRawProcessor) SetDisConnectedHandler(disconnectHandler RawConnectHandler){
|
||||
pbRawProcessor.disconnectHandler = disconnectHandler
|
||||
}
|
||||
|
||||
func (slf *PBRawPackInfo) GetPackType() uint16 {
|
||||
return slf.typ
|
||||
}
|
||||
|
||||
func (slf *PBRawPackInfo) GetMsg() []byte {
|
||||
return slf.rawMsg
|
||||
}
|
||||
Reference in New Issue
Block a user