Example #1
0
// 注册连接消息
func RegisterSessionMessage(eq cellnet.EventQueue, msgIns interface{}, userHandler func(interface{}, cellnet.Session)) *cellnet.MessageMeta {

	msgMeta := cellnet.NewMessageMeta(msgIns)

	// 将消息注册到mapper中, 提供反射用
	MapNameID(msgMeta.Name, msgMeta.ID)

	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.Printf("[cellnet] unmarshaling error:\n", err)
				return
			}

			userHandler(rawMsg, ev.Ses)

		}

	})

	return msgMeta
}
Example #2
0
// 注册连接消息
func RegisterSessionMessage(msgIns interface{}, userHandler func(interface{}, cellnet.Session, int64)) {

	msgMeta := cellnet.NewMessageMeta(msgIns)

	// 将消息注册到mapper中, 提供反射用
	socket.MapNameID(msgMeta.Name, msgMeta.ID)

	for _, conn := range gateConnArray {

		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("[gate] unmarshaling error:\n", err)
					return
				}

				userHandler(rawMsg, ev.Ses, ev.ClientID)

			}

		})
	}

}
Example #3
0
// 注册连接消息
func RegisterMessage(eq cellnet.EventQueue, msgIns interface{}, userHandler func(Response, interface{})) {

	msgMeta := cellnet.NewMessageMeta(msgIns)

	// 将消息注册到mapper中, 提供反射用
	socket.MapNameID(msgMeta.Name, msgMeta.ID)

	eq.RegisterCallback(msgMeta.ID, func(data interface{}) {

		if ev, ok := data.(*response); ok {

			rawMsg, err := cellnet.ParsePacket(&cellnet.Packet{
				MsgID: ev.req.GetMsgID(),
				Data:  ev.req.Data,
			}, msgMeta.Type)

			if err != nil {
				log.Errorln("[cellnet] unmarshaling error:\n", err)
				return
			}

			userHandler(ev, rawMsg)

		}

	})

}
Example #4
0
// 注册连接消息
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
}
Example #5
0
// 将PB消息解析封装到闭包中
func RegisterMessage(disp *PacketDispatcher, msgIns interface{}, userHandler func(cellnet.CellID, interface{})) {

	msgType := reflect.TypeOf(msgIns)

	msgName := msgType.String()

	msgID := cellnet.Name2ID(msgName)

	// 将消息注册到mapper中, 提供反射用
	addMapper(msgName, msgID)

	//log.Printf("[dispatcher] #regmsg %s(%d 0x%x)", msgName, msgID, msgID)

	disp.RegisterCallback(msgID, func(ses cellnet.CellID, pkt *cellnet.Packet) {

		rawMsg, err := cellnet.ParsePacket(pkt, msgType)

		if err != nil {
			log.Printf("[cellnet] unmarshaling error:\n", err)
			return
		}

		userHandler(ses, rawMsg)
	})
}
Example #6
0
// 注册连接消息
func RegisterSessionMessage(eq cellnet.EventQueue, msgIns interface{}, userHandler func(interface{}, cellnet.Session)) *cellnet.MessageMeta {

	msgMeta := cellnet.NewMessageMeta(msgIns)

	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
			}

			log.Debugf("#recv(%s) sid: %d %s(%d)|%s", ev.Ses.FromPeer().Name(), ev.Ses.ID(), msgMeta.Name, len(ev.Packet.Data), rawMsg.(proto.Message).String())

			userHandler(rawMsg, ev.Ses)

		}

	})

	return msgMeta
}
Example #7
0
// 注册连接消息
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)

		}

	})

}
Example #8
0
func (self *MessageLogInfo) MsgString() string {
	if self.meta == nil {
		return fmt.Sprintf("%v", self.pkt.Data)
	}

	rawMsg, err := cellnet.ParsePacket(self.pkt, self.meta.Type)
	if err != nil {
		return err.Error()
	}

	return rawMsg.(proto.Message).String()
}
Example #9
0
func (self *request) done(msg *coredef.RemoteCallACK) {

	rawType, err := cellnet.ParsePacket(&cellnet.Packet{
		MsgID: msg.GetMsgID(),
		Data:  msg.Data,
	}, self.replyType)

	defer removeCall(self.id)

	if err != nil {
		log.Errorln(err)
		return
	}

	self.callback.Call([]reflect.Value{reflect.ValueOf(rawType)})
}
Example #10
0
// 注册连接消息
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)

		}

	})

}
Example #11
0
// 注册连接消息
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
}
Example #12
0
func (self *request) done(msg *gamedef.RemoteCallACK) {

	rawType, err := cellnet.ParsePacket(&cellnet.Packet{
		MsgID: msg.MsgID,
		Data:  msg.Data,
	}, self.replyType)

	defer removeCall(self.id)

	if err != nil {
		log.Errorln(err)
		return
	}

	// 这里的反射, 会影响非常少的效率, 但因为外部写法简单, 就算了
	self.callback.Call([]reflect.Value{reflect.ValueOf(rawType)})

	if self.recvied != nil {
		self.recvied <- true
	}

}
Example #13
0
// 注册从网关接收到的消息
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)

			}

		})
	}

}