// 从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 }
// 通知网关关闭客户端连接 func CloseClient(gateSes cellnet.Session, clientid int64) { if gateSes == nil { return } // 通知关闭 gateSes.Send(&coredef.CloseClientACK{ ClientID: proto.Int64(clientid), }) }
// 通知网关关闭客户端连接 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, }) }
// 将消息发送到客户端 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}, }) }
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) }
// 将消息发送到客户端 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}, }) }
func (self *sessionMgr) Remove(ses cellnet.Session) { self.sesMapGuard.Lock() delete(self.sesMap, ses.ID()) self.sesMapGuard.Unlock() }