public void Send(ushort opcode, object message) { if (this.IsDisposed) { throw new Exception("session已经被Dispose了"); } this.LastSendTime = TimeHelper.Now(); if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { Log.Msg(message); } MemoryStream stream = this.Stream; stream.Seek(Packet.MessageIndex, SeekOrigin.Begin); stream.SetLength(Packet.MessageIndex); this.Network.MessagePacker.SerializeTo(message, stream); stream.Seek(0, SeekOrigin.Begin); opcodeBytes.WriteTo(0, opcode); Array.Copy(opcodeBytes, 0, stream.GetBuffer(), 0, opcodeBytes.Length); this.Send(stream); }
private void Run(MemoryStream memoryStream) { memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin); ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex); object message; try { OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>(); object instance = opcodeTypeComponent.GetInstance(opcode); message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream); if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { Log.Msg(message); } } catch (Exception e) { // 出现任何消息解析异常都要断开Session,防止客户端伪造消息 Log.Error($"opcode: {opcode} {this.Network.Count} {e}, ip: {this.RemoteAddress}"); this.Error = ErrorCode.ERR_PacketParserError; this.Network.Remove(this.Id); return; } RunMessage(opcode, message); }
public void Run(Session s, byte flag, ushort opcode, MemoryStream memoryStream) { OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent <OpcodeTypeComponent>(); object instance = opcodeTypeComponent.GetInstance(opcode); object message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream); if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { Log.Msg(message); } if ((flag & 0x01) > 0) { IResponse response = message as IResponse; if (response == null) { throw new Exception($"flag is response, but hotfix message is not! {opcode}"); } Action <IResponse> action; if (!this.requestCallback.TryGetValue(response.RpcId, out action)) { return; } this.requestCallback.Remove(response.RpcId); action(response); return; } Game.Scene.GetComponent <MessageDispatcherComponent>().Handle(s, new MessageInfo(opcode, message)); }
private void Run(MemoryStream memoryStream) { memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin); byte flag = memoryStream.GetBuffer()[Packet.OpcodeIndex]; ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex); // #if !SERVER // if (OpcodeHelper.IsClientHotfixMessage(opcode)) // { // this.GetComponent<SessionCallbackComponent>().MessageCallback.Invoke(this, opcode, memoryStream); // return; // } // #endif object message; try { OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>(); object instance = opcodeTypeComponent.GetInstance(opcode); message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream); if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { Log.Msg(message); } } catch (Exception e) { // 出现任何消息解析异常都要断开Session,防止客户端伪造消息 Log.Error($"opcode: {opcode} {this.Network.Count} {e}, ip: {this.RemoteAddress}"); this.Error = ErrorCode.ERR_PacketParserError; this.Network.Remove(this.Id); return; } if (!(message is IResponse response)) { this.Network.MessageDispatcher.Dispatch(this, opcode, message); return; } // flag第一位为1表示这是rpc返回消息,否则交由MessageDispatcher分发 //if (flag == 109) //{ // this.Network.MessageDispatcher.Dispatch(this, opcode, message); // return; //} Action <IResponse> action; if (!this.requestCallback.TryGetValue(response.RpcId, out action)) { throw new Exception($"not found rpc, response message: {StringHelper.MessageToStr(response)}"); } this.requestCallback.Remove(response.RpcId); action(response); }
/// <summary> /// 读取消息 /// </summary> /// <param name="memoryStream">包体数据</param> private void Run(MemoryStream memoryStream) { memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin); //读取操作码 操作码也是属于包体的一部分 ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex); #if !SERVER //如果是热更模块的操作码 >10000以上的 if (OpcodeHelper.IsClientHotfixMessage(opcode)) { //提交到热更层Session对象,在其内部注册的方法去处理 this.GetComponent <SessionCallbackComponent>().MessageCallback.Invoke(this, opcode, memoryStream); return; } #endif object message; try { //通过操作码 获取到实例 OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>(); object instance = opcodeTypeComponent.GetInstance(opcode); //反序列化 将内存流剩下的数据反序列化成proto实体 message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream); //需要调试日志的信息 就打印出来 if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { Log.Msg(message); } } catch (Exception e) { // 出现任何消息解析异常都要断开Session,防止客户端伪造消息 Log.Error($"opcode: {opcode} {this.Network.Count} {e}, ip: {this.RemoteAddress}"); this.Error = ErrorCode.ERR_PacketParserError; this.Network.Remove(this.Id); return; } //如果消息不是响应类型的消息 进行派发处理 if (!(message is IResponse response)) { //进行派发处理 this.Network.MessageDispatcher.Dispatch(this, opcode, message); return; } //如果是响应类型的消息 //从requestCallback缓存取出要执行的方法 将message压入到方法中 然后执行该方法 这涉及到ETTask的封装 等待了解 Action <IResponse> action; if (!this.requestCallback.TryGetValue(response.RpcId, out action)) { throw new Exception($"not found rpc, response message: {StringHelper.MessageToStr(response)}"); } this.requestCallback.Remove(response.RpcId); action(response); }
private void Run(MemoryStream memoryStream) { memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin); byte flag = memoryStream.GetBuffer()[Packet.FlagIndex]; ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex); #if !SERVER if (OpcodeHelper.IsClientHotfixMessage(opcode)) { this.GetComponent <SessionCallbackComponent>().MessageCallback.Invoke(this, flag, opcode, memoryStream); return; } #endif object message; try { OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>(); object instance = opcodeTypeComponent.GetInstance(opcode); message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream); //Log.Debug($"recv: {JsonHelper.ToJson(message)}"); if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { Log.Msg(message); } } catch (Exception e) { // 出现任何消息解析异常都要断开Session,防止客户端伪造消息 Log.Error($"opcode: {opcode} {this.Network.Count} {e} "); this.Error = ErrorCode.ERR_PacketParserError; this.Network.Remove(this.Id); return; } // flag第一位为1表示这是rpc返回消息,否则交由MessageDispatcher分发 if ((flag & 0x01) == 0) { this.Network.MessageDispatcher.Dispatch(this, opcode, message); return; } IResponse response = message as IResponse; if (response == null) { throw new Exception($"flag is response, but message is not! {opcode}"); } Action <IResponse> action; if (!this.requestCallback.TryGetValue(response.RpcId, out action)) { return; } this.requestCallback.Remove(response.RpcId); action(response); }
public void Send(byte flag, ushort opcode, object message) { if (this.IsDisposed) { throw new Exception("session已经被Dispose了"); } if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { #if !SERVER if (OpcodeHelper.IsClientHotfixMessage(opcode)) { } else #endif { Log.Msg(message); } } MemoryStream stream = this.Stream; stream.Seek(Packet.MessageIndex, SeekOrigin.Begin); stream.SetLength(Packet.MessageIndex); this.Network.MessagePacker.SerializeTo(message, stream); stream.Seek(0, SeekOrigin.Begin); if (stream.Length > ushort.MaxValue) { Log.Error($"message too large: {stream.Length}, opcode: {opcode}"); return; } this.byteses[0][0] = flag; this.byteses[1].WriteTo(0, opcode); int index = 0; foreach (var bytes in this.byteses) { Array.Copy(bytes, 0, stream.GetBuffer(), index, bytes.Length); index += bytes.Length; } #if SERVER // 如果是allserver,内部消息不走网络,直接转给session,方便调试时看到整体堆栈 if (this.Network.AppType == AppType.AllServer) { Session session = this.Network.Entity.GetComponent <NetInnerComponent>().Get(this.RemoteAddress); session.Run(stream); return; } #endif this.Send(stream); }
public void Send(ushort opcode, object message) { if (this.IsDisposed) { throw new Exception("session已经被Dispose了"); } MemoryStream stream = this.Stream; stream.Seek(Packet.MessageIndex, SeekOrigin.Begin); stream.SetLength(Packet.MessageIndex); this.Network.MessagePacker.SerializeTo(message, stream); stream.Seek(0, SeekOrigin.Begin); opcodeBytes.WriteTo(0, opcode); Array.Copy(opcodeBytes, 0, stream.GetBuffer(), 0, opcodeBytes.Length); /* * if (this.channel.RemoteAddress.Port != 20002 && opcode == HotfixOpcode.Actor_CountDown_Ntt) * { * int temp = 0; * temp++; * } */ #if SERVER // 如果是allserver,内部消息不走网络,直接转给session,方便调试时看到整体堆栈 if (this.Network.AppType == AppType.AllServer) { Session session = this.Network.Entity.GetComponent <NetInnerComponent>().Get(this.RemoteAddress); session.Run(stream); return; } #endif if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { #if !SERVER if (OpcodeHelper.IsClientHotfixMessage(opcode)) { } else #endif { //屏蔽心跳日志输出 10009 //屏蔽广播玩家下注日志 10050 if (opcode != 10009 && opcode != 10050 && opcode != 1012 && opcode != 1011 && opcode != 1010 && opcode != 1009) { Log.Msg(message); } } } this.Send(stream); }
public void Send(ushort opcode, object message) { if (this.IsDisposed) { throw new Exception("session已经被Dispose了"); } if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { #if !SERVER if (OpcodeHelper.IsClientHotfixMessage(opcode)) { } else #endif { Log.Msg(message); } } MemoryStream stream = this.Stream; stream.Seek(Packet.MessageIndex, SeekOrigin.Begin); stream.SetLength(Packet.MessageIndex); this.Network.MessagePacker.SerializeTo(message, stream); stream.Seek(0, SeekOrigin.Begin); opcodeBytes.WriteTo(0, opcode); Array.Copy(opcodeBytes, 0, stream.GetBuffer(), 0, opcodeBytes.Length); #if SERVER // 如果是allserver,内部消息不走网络,直接转给session,方便调试时看到整体堆栈 if (this.Network.AppType == AppType.AllServer) { Session session = this.Network.Entity.GetComponent <NetInnerComponent>().Get(this.RemoteAddress); session.Run(stream); return; } #endif //这里是走网络层的发送 this.Send(stream); }
/// <summary> /// 发送逻辑2-将操作码和协议都写入到内存中 /// </summary> /// <param name="opcode"></param> /// <param name="message"></param> public void Send(ushort opcode, object message) { if (this.IsDisposed) { throw new Exception("session已经被Dispose了"); } //需要打印消息 就打印出来 if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { #if !SERVER if (OpcodeHelper.IsClientHotfixMessage(opcode)) { } else #endif { Log.Msg(message); } } MemoryStream stream = this.Stream; //将数据写入到内存流中 stream.Seek(Packet.MessageIndex, SeekOrigin.Begin); //设置写入的位置为2 因为留2个字节写入操作码 stream.SetLength(Packet.MessageIndex); //写入的长度 2 tcp this.Network.MessagePacker.SerializeTo(message, stream); //开始写入proto序列化后的数组 stream.Seek(0, SeekOrigin.Begin); //重设写入位置为0 opcodeBytes.WriteTo(0, opcode); //将操作码opcode转化为opcodeBytes 操作码也就是协议号 Array.Copy(opcodeBytes, 0, stream.GetBuffer(), 0, opcodeBytes.Length); //将操作码复制到内存流中 #if SERVER // 如果是allserver,内部消息不走网络,直接转给session,方便调试时看到整体堆栈 if (this.Network.AppType == AppType.AllServer) { Session session = this.Network.Entity.GetComponent <NetInnerComponent>().Get(this.RemoteAddress); session.Run(stream); return; } #endif this.Send(stream); //调用下一个发送接口 }
//------------//[MyAppend end]------------ private void Run(MemoryStream memoryStream) { memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin); ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex); //------------//[MyAppend begin]------------ if (!checkOpcode(opcode)) { myDebug(opcode); this.Error = ErrorCode.ERR_PacketParserError; this.Network.Remove(this.Id); return; } //------------//[MyAppend end]------------ #if !SERVER if (OpcodeHelper.IsClientHotfixMessage(opcode)) { this.GetComponent <SessionCallbackComponent>().MessageCallback.Invoke(this, opcode, memoryStream); return; } #endif object message; try { OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>(); object instance = opcodeTypeComponent.GetInstance(opcode); message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream); } catch (Exception e) { // 出现任何消息解析异常都要断开Session,防止客户端伪造消息 // Log.Error($"opcode: {opcode} {this.Network.Count} {e} " + myDebug(memoryStream)); //Log.Error(e + ", opcode: " + opcode + ", this.Network.Count: " + this.Network.Count + ", " + myDebug(memoryStream)); this.Error = ErrorCode.ERR_PacketParserError; this.Network.Remove(this.Id); return; } IResponse response = message as IResponse; if (response == null) { //屏蔽心跳日志输出 10008 //屏蔽广播玩家下注日志 10050 //屏蔽广播推送的消息 request //1011数据存储 if (OpcodeHelper.IsNeedDebugLogMessage(opcode) && opcode != 10008 && opcode != 10050 && opcode != 1011 && opcode != 1012 && opcode != 1009 && opcode != 1010 && (message as IActorMessage) == null ) { Log.Msg(message); } this.Network.MessageDispatcher.Dispatch(this, opcode, message); return; } Action <IResponse> action; if (!this.requestCallback.TryGetValue(response.RpcId, out action)) { throw new Exception($"not found rpc, response message: {StringHelper.MessageToStr(response)}"); } this.requestCallback.Remove(response.RpcId); action(response); //屏蔽心跳日志输出 10008 //屏蔽广播玩家下注日志 10050 if (OpcodeHelper.IsNeedDebugLogMessage(opcode) && opcode != 10008 && opcode != 10050 && opcode != 1011 && opcode != 1012 && opcode != 1010 && opcode != 1009) { Log.Msg(message); } }