Ejemplo n.º 1
0
// 封包转字符串, 调试用
func (self *pbCodec) PacketToString(pkt *netdef.Packet) string {

	if pkt == nil {
		return ""
	}

	meta := getProtoMetaByID(pkt.MsgID)
	if meta == nil {
		return fmt.Sprintf("[0x%x] size: %d", pkt.MsgID, pkt.Size)
	}

	c := netdef.NewPacketContext(pkt, nil)

	// 注入消息适配器
	c.Invoke(meta.Adapter)

	// 使用消息适配器翻译消息
	var final string
	c.Invoke(func(msg proto.Message) {

		final = msg.String()

	})

	return fmt.Sprintf("%s size: %d|%s", meta.GetName(), pkt.Size, final)
}
Ejemplo n.º 2
0
func (self *rpcComponent) Start(p netfw.IPeer) {

	// 应答方
	p.RegisterMessage("coredef.RemoteCallREQ", func(msg *coredef.RemoteCallREQ, ses *netdef.Session) {

		userpkt := &netdef.Packet{
			MsgID: msg.GetUserMsgID(),
			Size:  uint16(len(msg.GetUserMsg())),
			Data:  msg.GetUserMsg(),
		}

		userc := netdef.NewPacketContext(userpkt, ses)

		injectResponseHandler(userc, p.GetCodec(), ses, msg)

		p.CallHandlers(int(userpkt.MsgID), userc)

	})

	p.RegisterMessage("coredef.RemoteCallACK", func(msg *coredef.RemoteCallACK) {

		// 获得callid对应的调用上下文
		rcd := self.getCallData(msg.GetCallID())

		defer self.removeCallData(msg.GetCallID())

		if rcd == nil {
			rcd.fail(lostCallDataError)
			return
		}

		err := proto.Unmarshal(msg.GetUserMsg(), rcd.Reply.(proto.Message))
		if err != nil {
			rcd.fail(replyUnmarshalingError)
			return
		}

		// 回应请求方, 请求方解除阻塞
		rcd.done()

	})
}
Ejemplo n.º 3
0
// Chan队列消息发送
func (self *Peer) SendPacketLoop(ses *netdef.Session) {
	for {
		ev := <-ses.OutputChan

		c := netdef.NewPacketContext(ev.Pkt, ev.Ses)

		// 发出事件
		self.CallHandlers(int(ev.Method), c)

		// 注意 Send事件在独立线程, 和Recv事件不在一个线程
		switch ev.Method {
		case netdef.EventSendPacket:
			ses.PStream.Write(ev.Pkt)
		case netdef.EventEnd:
			goto exit
		}

	}
exit:
	//log.Println("send loop exit")
}

// 派发消息到回调
func (self *Peer) DispatchLoop(ses *netdef.Session) {

	for {
		ev := <-ses.InputChan

		if self.OnBeginRecv != nil {
			self.OnBeginRecv()
		}

		c := netdef.NewPacketContext(ev.Pkt, ev.Ses)

		// 派发事件
		self.CallHandlers(int(ev.Method), c)

		// 派发消息
		switch ev.Method {
		case netdef.EventRecvPacket, netdef.EventPostPacket:

			self.CallHandlers(int(ev.Pkt.MsgID), c)

		case netdef.EventEnd:
			if self.OnEndRecv != nil {
				self.OnEndRecv()
			}
			goto exit
		}

		if self.OnEndRecv != nil {
			self.OnEndRecv()
		}

	}
exit:
	//log.Println("dispatch loop exit")
}

// 被实现手动设置进来
func (self *Peer) Init(p IPeer) {

	self.ComponentManager = NewComponentManager(self, p)

	// 调试选项开启时, 注入调试句柄
	if SvcConfig.GetDebugRoute().GetEnable() {

		injectDebugHandler(self)
	}
}

func (self *Peer) GetDefine() *coredef.PeerDefine {
	return self.Define
}

func NewPeerData(def *coredef.PeerDefine, codec netdef.IPacketCodec) *Peer {

	return &Peer{
		Define:          def,
		Ready:           false,
		Codec:           codec,
		EventDispatcher: NewEventDispatcher(def.GetCapturePanic(), codec),
	}
}