/// <summary> /// 创建一个新Session /// </summary> public virtual NetworkSession Create(IPEndPoint rIpEndPoint) { try { AChannel rChannel = this.mService.ConnectChannel(rIpEndPoint); NetworkSession rSession = new NetworkSession(this, rChannel); rSession.Parent = this; rChannel.ErrorCallback += (c, e) => { rSession.Error = e; this.Remove(rSession.Id); }; this.mSessions.Add(rSession.Id, rSession); return(rSession); } catch (Exception e) { Debug.LogError(e); return(null); } }
public void Dispatch(NetworkSession rSession, Packet rPacket) { object rMessage; try { if (NetworkOpcodeTypes.IsClientHotfixMessage(rPacket.Opcode)) { // 处理热更新的消息分发 //if (mHotfixDispatchObject != null) //{ // mHotfixDispatchObject.Invoke("Dispatch", rSession, rPacket); //} return; } NetworkOpcodeTypes rOpcodeTypes = rSession.Parent.OpcodeTypes; Type rResponseType = rOpcodeTypes.GetType(rPacket.Opcode); rMessage = rSession.Parent.MessagePacker.DeserializeFrom(rResponseType, rPacket.Bytes, Packet.Index, rPacket.Length - Packet.Index); } catch (Exception e) { // 出现任何解析消息异常都要断开Session,防止客户端伪造消息 Debug.LogError(e); rSession.Error = NetworkErrorCode.ERR_PacketParserError; rSession.Parent.Remove(rSession.Id); return; } // 如果是帧同步消息,交给ClientFrameComponent处理 //FrameMessage frameMessage = rMessage as FrameMessage; //if (frameMessage != null) //{ // Game.Scene.GetComponent<ClientFrameComponent>().Add(session, frameMessage); // return; //} // 普通消息或者是Rpc请求消息 EventManager.Instance.Distribute(rPacket.Opcode, rMessage); }