// 注册连接消息 func RegisterMessage(eq cellnet.EventQueue, msgName string, userHandler func(Response, interface{})) { msgMeta := cellnet.MessageMetaByName(msgName) eq.RegisterCallback(msgMeta.ID, func(data interface{}) { if ev, ok := data.(*response); ok { rawMsg, err := cellnet.ParsePacket(&cellnet.Packet{ MsgID: ev.req.MsgID, Data: ev.req.Data, }, msgMeta.Type) if err != nil { log.Errorln("unmarshaling error:", err) return } userHandler(ev, rawMsg) } }) }
// 注册连接消息 func RegisterSessionMessage(eq cellnet.EventQueue, msgName string, userHandler func(interface{}, cellnet.Session)) *cellnet.MessageMeta { msgMeta := cellnet.MessageMetaByName(msgName) if msgMeta == nil { log.Errorf("message register failed, %s", msgName) return nil } eq.RegisterCallback(msgMeta.ID, func(data interface{}) { if ev, ok := data.(*SessionEvent); ok { rawMsg, err := cellnet.ParsePacket(ev.Packet, msgMeta.Type) if err != nil { log.Errorln("unmarshaling error:\n", err) return } userHandler(rawMsg, ev.Ses) } }) return msgMeta }
// 注册消息路由方法 func RelayMessage(targetSvcName string, messageName string) { meta := cellnet.MessageMetaByName(messageName) if meta == nil { log.Errorf("relay message not found: %s, target: %s", messageName, targetSvcName) return } routerMap[meta.ID] = &routeInfo{name: targetSvcName} }
func BlockMessageLog(msgName string) { meta := cellnet.MessageMetaByName(msgName) if meta == nil { log.Errorf("msg log block not found: %s", msgName) return } msgMetaByID[meta.ID] = meta }
// 注册连接消息 func RegisterMessage(eq cellnet.EventQueue, msgName string, userHandler func(interface{}, Response)) { if needRegisterServer { // 服务端 socket.RegisterSessionMessage(eq, "gamedef.RemoteCallREQ", func(content interface{}, ses cellnet.Session) { msg := content.(*gamedef.RemoteCallREQ) eq.CallData(&response{ ses: ses, req: msg, }) }) needRegisterServer = false } msgMeta := cellnet.MessageMetaByName(msgName) eq.RegisterCallback(msgMeta.ID, func(data interface{}) { if ev, ok := data.(*response); ok { rawMsg, err := cellnet.ParsePacket(&cellnet.Packet{ MsgID: ev.req.MsgID, Data: ev.req.Data, }, msgMeta.Type) if err != nil { log.Errorln("unmarshaling error:", err) return } userHandler(rawMsg, ev) } }) }
// 注册连接消息 func RegisterSessionMessage(eq cellnet.EventQueue, msgName string, userHandler func(interface{}, cellnet.Session)) *cellnet.MessageMeta { msgMeta := cellnet.MessageMetaByName(msgName) if msgMeta == nil { log.Errorf("message register failed, %s", msgName) return nil } eq.RegisterCallback(msgMeta.ID, func(data interface{}) { if ev, ok := data.(*SessionEvent); ok { rawMsg, err := cellnet.ParsePacket(ev.Packet, msgMeta.Type) if err != nil { log.Errorln("unmarshaling error:\n", err) return } if EnableMessageLog { msgLog(&MessageLogInfo{ Dir: "recv", PeerName: ev.Ses.FromPeer().Name(), SessionID: ev.Ses.ID(), Name: msgMeta.Name, ID: msgMeta.ID, Size: int32(len(ev.Packet.Data)), Data: rawMsg.(proto.Message).String(), }) } userHandler(rawMsg, ev.Ses) } }) return msgMeta }
// 注册连接消息 func RegisterPeerMessage(eq cellnet.EventQueue, msgName string, userHandler func(interface{}, cellnet.Peer)) *cellnet.MessageMeta { msgMeta := cellnet.MessageMetaByName(msgName) if msgMeta == nil { log.Errorf("message register failed, %s", msgName) return nil } eq.RegisterCallback(msgMeta.ID, func(data interface{}) { if ev, ok := data.(*PeerEvent); ok { rawMsg := reflect.New(msgMeta.Type).Interface() userHandler(rawMsg, ev.P) } }) return msgMeta }
// 注册从网关接收到的消息 func RegisterMessage(msgName string, userHandler func(interface{}, cellnet.Session, int64)) { msgMeta := cellnet.MessageMetaByName(msgName) if msgMeta == nil { log.Errorf("message register failed, %s", msgName) return } for _, conn := range routerConnArray { conn.RegisterCallback(msgMeta.ID, func(data interface{}) { if ev, ok := data.(*relayEvent); ok { rawMsg, err := cellnet.ParsePacket(ev.Packet, msgMeta.Type) if err != nil { log.Errorln("unmarshaling error:\n", err) return } msgContent := rawMsg.(interface { String() string }).String() log.Debugf("router->backend clientid: %d %s(%d) size: %d|%s", ev.ClientID, getMsgName(ev.MsgID), ev.MsgID, len(ev.Packet.Data), msgContent) userHandler(rawMsg, ev.Ses, ev.ClientID) } }) } }
package socket import ( "fmt" _ "github.com/davyxu/cellnet/proto/gamedef" "github.com/davyxu/cellnet" ) var ( Event_SessionConnected = uint32(cellnet.MessageMetaByName("gamedef.SessionConnected").ID) Event_SessionClosed = uint32(cellnet.MessageMetaByName("gamedef.SessionClosed").ID) Event_SessionAccepted = uint32(cellnet.MessageMetaByName("gamedef.SessionAccepted").ID) Event_PeerInit = uint32(cellnet.MessageMetaByName("gamedef.PeerInit").ID) Event_PeerStart = uint32(cellnet.MessageMetaByName("gamedef.PeerStart").ID) Event_PeerStop = uint32(cellnet.MessageMetaByName("gamedef.PeerStop").ID) ) // 会话事件 type SessionEvent struct { *cellnet.Packet Ses cellnet.Session } func (self SessionEvent) String() string { return fmt.Sprintf("SessionEvent msgid: %d data: %v", self.MsgID, self.Data) } func NewSessionEvent(msgid uint32, s cellnet.Session, data []byte) *SessionEvent { return &SessionEvent{