示例#1
0
文件: req.go 项目: davyxu/cellnet
// 从peer获取rpc使用的session
func getPeerSession(p interface{}) (cellnet.Session, cellnet.EventQueue, error) {

	var ses cellnet.Session

	switch p.(type) {
	case cellnet.Peer:
		if connPeer, ok := p.(interface {
			DefaultSession() cellnet.Session
		}); ok {

			ses = connPeer.DefaultSession()

		} else {

			return nil, nil, errInvalidPeerSession
		}
	case cellnet.Session:
		ses = p.(cellnet.Session)
	}

	if ses == nil {
		return nil, nil, errConnectorSesNotReady
	}

	return ses, ses.FromPeer().(cellnet.EventQueue), nil
}
示例#2
0
// 通知网关关闭客户端连接
func CloseClient(gateSes cellnet.Session, clientid int64) {

	if gateSes == nil {
		return
	}

	// 通知关闭
	gateSes.Send(&coredef.CloseClientACK{
		ClientID: proto.Int64(clientid),
	})
}
示例#3
0
// 通知网关关闭客户端连接
func CloseClient(routerSes cellnet.Session, clientid int64) {

	if routerSes == nil {
		return
	}

	log.Debugf("backend->router clientid: %d CloseClient", clientid)

	// 通知关闭
	routerSes.Send(&coredef.CloseClientACK{
		ClientID: clientid,
	})
}
示例#4
0
// 将消息发送到客户端
func SendToClient(gateSes cellnet.Session, clientid int64, data interface{}) {

	if gateSes == nil {
		return
	}

	userpkt := cellnet.BuildPacket(data)

	gateSes.Send(&coredef.DownstreamACK{
		Data:     userpkt.Data,
		MsgID:    proto.Uint32(userpkt.MsgID),
		ClientID: []int64{clientid},
	})
}
示例#5
0
func sendMessageToBackend(ses cellnet.Session, ev *socket.SessionEvent) {

	// 构建路由封包
	relaypkt, _ := cellnet.BuildPacket(&coredef.UpstreamACK{
		MsgID:    ev.MsgID,
		Data:     ev.Data,
		ClientID: ev.Ses.ID(),
	})

	if DebugMode {
		log.Debugf("client->backend, msg: %s(%d) clientid: %d", getMsgName(ev.MsgID), ev.MsgID, ev.Ses.ID())
	}

	ses.RawSend(relaypkt)
}
示例#6
0
// 将消息发送到客户端
func SendToClient(routerSes cellnet.Session, clientid int64, data interface{}) {

	if routerSes == nil {
		return
	}

	msgContent := data.(interface {
		String() string
	}).String()

	userpkt, _ := cellnet.BuildPacket(data)

	log.Debugf("backend->router clientid: %d %s(%d) size: %d |%s", clientid, getMsgName(userpkt.MsgID), userpkt.MsgID, len(userpkt.Data), msgContent)

	routerSes.Send(&coredef.DownstreamACK{
		Data:     userpkt.Data,
		MsgID:    userpkt.MsgID,
		ClientID: []int64{clientid},
	})
}
示例#7
0
func (self *sessionMgr) Remove(ses cellnet.Session) {
	self.sesMapGuard.Lock()
	delete(self.sesMap, ses.ID())
	self.sesMapGuard.Unlock()
}