diff --git a/Unity/Assets/Hotfix/Module/Message/Session.cs b/Unity/Assets/Hotfix/Module/Message/Session.cs index d56d56c2529cd18e1d8b32e98a52b20445cc2425..f4b6e40246489756b5b01e7b78aacacf488cccb2 100644 --- a/Unity/Assets/Hotfix/Module/Message/Session.cs +++ b/Unity/Assets/Hotfix/Module/Message/Session.cs @@ -15,7 +15,7 @@ namespace ETHotfix { self.session = session; SessionCallbackComponent sessionComponent = self.session.AddComponent(); - sessionComponent.MessageCallback = (s, flag, opcode, memoryStream) => { self.Run(s, flag, opcode, memoryStream); }; + sessionComponent.MessageCallback = (s, opcode, memoryStream) => { self.Run(s, opcode, memoryStream); }; sessionComponent.DisposeCallback = s => { self.Dispose(); }; } } @@ -49,7 +49,7 @@ namespace ETHotfix this.session.Dispose(); } - public void Run(ETModel.Session s, byte flag, ushort opcode, MemoryStream memoryStream) + public void Run(ETModel.Session s, ushort opcode, MemoryStream memoryStream) { OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent(); object instance = opcodeTypeComponent.GetInstance(opcode); @@ -60,46 +60,36 @@ namespace ETHotfix Log.Msg(message); } - if ((flag & 0x01) > 0) + IResponse response = message as IResponse; + if (response == null) { - IResponse response = message as IResponse; - if (response == null) - { - throw new Exception($"flag is response, but hotfix message is not! {opcode}"); - } - - Action action; - if (!this.requestCallback.TryGetValue(response.RpcId, out action)) - { - return; - } - this.requestCallback.Remove(response.RpcId); - - action(response); + Game.Scene.GetComponent().Handle(session, new MessageInfo(opcode, message)); return; } + + Action 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); - Game.Scene.GetComponent().Handle(session, new MessageInfo(opcode, message)); + action(response); } public void Send(IMessage message) - { - Send(0x00, message); - } - - public void Send(byte flag, IMessage message) { ushort opcode = Game.Scene.GetComponent().GetOpcode(message.GetType()); - this.Send(flag, opcode, message); + this.Send(opcode, message); } - public void Send(byte flag, ushort opcode, IMessage message) + public void Send(ushort opcode, IMessage message) { if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { Log.Msg(message); } - session.Send(flag, opcode, message); + session.Send(opcode, message); } public ETTask Call(IRequest request) @@ -126,7 +116,7 @@ namespace ETHotfix request.RpcId = rpcId; - this.Send(0x00, request); + this.Send(request); return tcs.Task; } @@ -156,7 +146,7 @@ namespace ETHotfix request.RpcId = rpcId; - this.Send(0x00, request); + this.Send(request); return tcs.Task; } } diff --git a/Unity/Assets/Model/Base/Helper/StringHelper.cs b/Unity/Assets/Model/Base/Helper/StringHelper.cs index 30e62445d19fce99d2c134d56e4c5a3ae5a0b2f5..596f1e418e8ec285a9da150da1706552dfbbfa6b 100644 --- a/Unity/Assets/Model/Base/Helper/StringHelper.cs +++ b/Unity/Assets/Model/Base/Helper/StringHelper.cs @@ -58,5 +58,14 @@ namespace ETModel } return sb.ToString(); } + + public static string MessageToStr(object message) + { +#if SERVER + return MongoHelper.ToJson(message); +#else + return Dumper.DumpAsString(message); +#endif + } } } \ No newline at end of file diff --git a/Unity/Assets/Model/ILBinding/CLRBindings.cs b/Unity/Assets/Model/ILBinding/CLRBindings.cs index 1167a58ce284442b35648eff41c5dc176b8ebb9f..c6f861447ae01b352b655b97d8afdcf1bd932a88 100644 --- a/Unity/Assets/Model/ILBinding/CLRBindings.cs +++ b/Unity/Assets/Model/ILBinding/CLRBindings.cs @@ -140,6 +140,7 @@ namespace ILRuntime.Runtime.Generated ETModel_Component_Binding.Register(app); ETModel_IMessagePacker_Binding.Register(app); ETModel_OpcodeHelper_Binding.Register(app); + ETModel_StringHelper_Binding.Register(app); ETModel_ETTaskCompletionSource_1_Google_Protobuf_Adapt_IMessage_Binding_Adaptor_Binding.Register(app); System_Threading_CancellationToken_Binding.Register(app); ETModel_ErrorCode_Binding.Register(app); diff --git a/Unity/Assets/Model/ILBinding/ETModel_SessionCallbackComponent_Binding.cs b/Unity/Assets/Model/ILBinding/ETModel_SessionCallbackComponent_Binding.cs index e444417c2500d1378426596c97502e00f6d6b575..71060687ef981e903bd437abf97c9db19e526da3 100644 --- a/Unity/Assets/Model/ILBinding/ETModel_SessionCallbackComponent_Binding.cs +++ b/Unity/Assets/Model/ILBinding/ETModel_SessionCallbackComponent_Binding.cs @@ -41,7 +41,7 @@ namespace ILRuntime.Runtime.Generated } static void set_MessageCallback_0(ref object o, object v) { - ((ETModel.SessionCallbackComponent)o).MessageCallback = (System.Action)v; + ((ETModel.SessionCallbackComponent)o).MessageCallback = (System.Action)v; } static object get_DisposeCallback_1(ref object o) { diff --git a/Unity/Assets/Model/ILBinding/ETModel_Session_Binding.cs b/Unity/Assets/Model/ILBinding/ETModel_Session_Binding.cs index 007de5e5d3058998fae509949d2d2888301f7276..c22fa0922f5aeafc5bd692a0c1b9ce7c4b1b5967 100644 --- a/Unity/Assets/Model/ILBinding/ETModel_Session_Binding.cs +++ b/Unity/Assets/Model/ILBinding/ETModel_Session_Binding.cs @@ -34,7 +34,7 @@ namespace ILRuntime.Runtime.Generated args = new Type[]{}; method = type.GetMethod("get_Network", flag, null, args, null); app.RegisterCLRMethodRedirection(method, get_Network_3); - args = new Type[]{typeof(System.Byte), typeof(System.UInt16), typeof(System.Object)}; + args = new Type[]{typeof(System.UInt16), typeof(System.Object)}; method = type.GetMethod("Send", flag, null, args, null); app.RegisterCLRMethodRedirection(method, Send_4); @@ -126,7 +126,7 @@ namespace ILRuntime.Runtime.Generated { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject* ptr_of_this_method; - StackObject* __ret = ILIntepreter.Minus(__esp, 4); + StackObject* __ret = ILIntepreter.Minus(__esp, 3); ptr_of_this_method = ILIntepreter.Minus(__esp, 1); System.Object @message = (System.Object)typeof(System.Object).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); @@ -136,13 +136,10 @@ namespace ILRuntime.Runtime.Generated System.UInt16 @opcode = (ushort)ptr_of_this_method->Value; ptr_of_this_method = ILIntepreter.Minus(__esp, 3); - System.Byte @flag = (byte)ptr_of_this_method->Value; - - ptr_of_this_method = ILIntepreter.Minus(__esp, 4); ETModel.Session instance_of_this_method = (ETModel.Session)typeof(ETModel.Session).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); - instance_of_this_method.Send(@flag, @opcode, @message); + instance_of_this_method.Send(@opcode, @message); return __ret; } diff --git a/Unity/Assets/Model/ILBinding/ETModel_StringHelper_Binding.cs b/Unity/Assets/Model/ILBinding/ETModel_StringHelper_Binding.cs new file mode 100644 index 0000000000000000000000000000000000000000..11338233cf8ffa0606fad7daaabd8224ea3f0705 --- /dev/null +++ b/Unity/Assets/Model/ILBinding/ETModel_StringHelper_Binding.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Runtime.InteropServices; + +using ILRuntime.CLR.TypeSystem; +using ILRuntime.CLR.Method; +using ILRuntime.Runtime.Enviorment; +using ILRuntime.Runtime.Intepreter; +using ILRuntime.Runtime.Stack; +using ILRuntime.Reflection; +using ILRuntime.CLR.Utils; + +namespace ILRuntime.Runtime.Generated +{ + unsafe class ETModel_StringHelper_Binding + { + public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app) + { + BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; + MethodBase method; + Type[] args; + Type type = typeof(ETModel.StringHelper); + args = new Type[]{typeof(System.Object)}; + method = type.GetMethod("MessageToStr", flag, null, args, null); + app.RegisterCLRMethodRedirection(method, MessageToStr_0); + + + } + + + static StackObject* MessageToStr_0(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) + { + ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; + StackObject* ptr_of_this_method; + StackObject* __ret = ILIntepreter.Minus(__esp, 1); + + ptr_of_this_method = ILIntepreter.Minus(__esp, 1); + System.Object @message = (System.Object)typeof(System.Object).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); + __intp.Free(ptr_of_this_method); + + + var result_of_this_method = ETModel.StringHelper.MessageToStr(@message); + + return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method); + } + + + + } +} diff --git a/Unity/Assets/Model/ILBinding/ETModel_StringHelper_Binding.cs.meta b/Unity/Assets/Model/ILBinding/ETModel_StringHelper_Binding.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..011051aefd5485810bd892d29759d4952e36d386 --- /dev/null +++ b/Unity/Assets/Model/ILBinding/ETModel_StringHelper_Binding.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 14da91b78333e45b19f13aff42c1b0f3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Unity/Assets/Model/ILBinding/System_String_Binding.cs b/Unity/Assets/Model/ILBinding/System_String_Binding.cs index cbf443fc84473debbc0ebd05f6a02e877e27a06d..a6607821269e57edd7b7f5e8ae003546ec757370 100644 --- a/Unity/Assets/Model/ILBinding/System_String_Binding.cs +++ b/Unity/Assets/Model/ILBinding/System_String_Binding.cs @@ -28,9 +28,9 @@ namespace ILRuntime.Runtime.Generated args = new Type[]{typeof(System.Int32)}; method = type.GetMethod("get_Chars", flag, null, args, null); app.RegisterCLRMethodRedirection(method, get_Chars_1); - args = new Type[]{typeof(System.String), typeof(System.String), typeof(System.String)}; - method = type.GetMethod("Concat", flag, null, args, null); - app.RegisterCLRMethodRedirection(method, Concat_2); + args = new Type[]{typeof(System.String), typeof(System.Object)}; + method = type.GetMethod("Format", flag, null, args, null); + app.RegisterCLRMethodRedirection(method, Format_2); args = new Type[]{typeof(System.String), typeof(System.String)}; method = type.GetMethod("Concat", flag, null, args, null); app.RegisterCLRMethodRedirection(method, Concat_3); @@ -40,21 +40,15 @@ namespace ILRuntime.Runtime.Generated args = new Type[]{}; method = type.GetMethod("get_Length", flag, null, args, null); app.RegisterCLRMethodRedirection(method, get_Length_5); - args = new Type[]{typeof(System.String), typeof(System.Object)}; - method = type.GetMethod("Format", flag, null, args, null); - app.RegisterCLRMethodRedirection(method, Format_6); args = new Type[]{typeof(System.String[]), typeof(System.StringSplitOptions)}; method = type.GetMethod("Split", flag, null, args, null); - app.RegisterCLRMethodRedirection(method, Split_7); + app.RegisterCLRMethodRedirection(method, Split_6); args = new Type[]{}; method = type.GetMethod("Trim", flag, null, args, null); - app.RegisterCLRMethodRedirection(method, Trim_8); + app.RegisterCLRMethodRedirection(method, Trim_7); args = new Type[]{typeof(System.String), typeof(System.String)}; method = type.GetMethod("op_Equality", flag, null, args, null); - app.RegisterCLRMethodRedirection(method, op_Equality_9); - args = new Type[]{typeof(System.String), typeof(System.String), typeof(System.String), typeof(System.String)}; - method = type.GetMethod("Concat", flag, null, args, null); - app.RegisterCLRMethodRedirection(method, Concat_10); + app.RegisterCLRMethodRedirection(method, op_Equality_8); app.RegisterCLRCreateArrayInstance(type, s => new System.String[s]); @@ -103,26 +97,22 @@ namespace ILRuntime.Runtime.Generated return __ret + 1; } - static StackObject* Concat_2(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) + static StackObject* Format_2(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject* ptr_of_this_method; - StackObject* __ret = ILIntepreter.Minus(__esp, 3); + StackObject* __ret = ILIntepreter.Minus(__esp, 2); ptr_of_this_method = ILIntepreter.Minus(__esp, 1); - System.String @str2 = (System.String)typeof(System.String).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); + System.Object @arg0 = (System.Object)typeof(System.Object).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); ptr_of_this_method = ILIntepreter.Minus(__esp, 2); - System.String @str1 = (System.String)typeof(System.String).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); - __intp.Free(ptr_of_this_method); - - ptr_of_this_method = ILIntepreter.Minus(__esp, 3); - System.String @str0 = (System.String)typeof(System.String).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); + System.String @format = (System.String)typeof(System.String).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); - var result_of_this_method = System.String.Concat(@str0, @str1, @str2); + var result_of_this_method = System.String.Format(@format, @arg0); return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method); } @@ -188,27 +178,7 @@ namespace ILRuntime.Runtime.Generated return __ret + 1; } - static StackObject* Format_6(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) - { - ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; - StackObject* ptr_of_this_method; - StackObject* __ret = ILIntepreter.Minus(__esp, 2); - - ptr_of_this_method = ILIntepreter.Minus(__esp, 1); - System.Object @arg0 = (System.Object)typeof(System.Object).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); - __intp.Free(ptr_of_this_method); - - ptr_of_this_method = ILIntepreter.Minus(__esp, 2); - System.String @format = (System.String)typeof(System.String).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); - __intp.Free(ptr_of_this_method); - - - var result_of_this_method = System.String.Format(@format, @arg0); - - return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method); - } - - static StackObject* Split_7(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) + static StackObject* Split_6(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject* ptr_of_this_method; @@ -231,7 +201,7 @@ namespace ILRuntime.Runtime.Generated return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method); } - static StackObject* Trim_8(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) + static StackObject* Trim_7(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject* ptr_of_this_method; @@ -246,7 +216,7 @@ namespace ILRuntime.Runtime.Generated return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method); } - static StackObject* op_Equality_9(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) + static StackObject* op_Equality_8(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject* ptr_of_this_method; @@ -268,34 +238,6 @@ namespace ILRuntime.Runtime.Generated return __ret + 1; } - static StackObject* Concat_10(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) - { - ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; - StackObject* ptr_of_this_method; - StackObject* __ret = ILIntepreter.Minus(__esp, 4); - - ptr_of_this_method = ILIntepreter.Minus(__esp, 1); - System.String @str3 = (System.String)typeof(System.String).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); - __intp.Free(ptr_of_this_method); - - ptr_of_this_method = ILIntepreter.Minus(__esp, 2); - System.String @str2 = (System.String)typeof(System.String).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); - __intp.Free(ptr_of_this_method); - - ptr_of_this_method = ILIntepreter.Minus(__esp, 3); - System.String @str1 = (System.String)typeof(System.String).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); - __intp.Free(ptr_of_this_method); - - ptr_of_this_method = ILIntepreter.Minus(__esp, 4); - System.String @str0 = (System.String)typeof(System.String).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); - __intp.Free(ptr_of_this_method); - - - var result_of_this_method = System.String.Concat(@str0, @str1, @str2, @str3); - - return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method); - } - } diff --git a/Unity/Assets/Model/Module/Message/ErrorCode.cs b/Unity/Assets/Model/Module/Message/ErrorCode.cs index e6dda39ad3f6d9548792ba0385117ad7431977cb..b3e6dd3aecf35417936859415445f42ee3bde2bc 100644 --- a/Unity/Assets/Model/Module/Message/ErrorCode.cs +++ b/Unity/Assets/Model/Module/Message/ErrorCode.cs @@ -5,43 +5,41 @@ namespace ETModel public const int ERR_Success = 0; // 1-11004 是SocketError请看SocketError定义 - + //----------------------------------- // 100000 以上,避免跟SocketError冲突 public const int ERR_MyErrorCode = 100000; + public const int ERR_ActorNoMailBoxComponent = 100003; + public const int ERR_ActorRemove = 100004; + public const int ERR_PacketParserError = 100005; + + public const int ERR_KcpCantConnect = 102005; + public const int ERR_KcpChannelTimeout = 102006; + public const int ERR_KcpRemoteDisconnect = 102007; + public const int ERR_PeerDisconnect = 102008; + public const int ERR_SocketCantSend = 102009; + public const int ERR_SocketError = 102010; + public const int ERR_KcpWaitSendSizeTooLarge = 102011; - // 小于这个Rpc会抛异常 + public const int ERR_WebsocketPeerReset = 103001; + public const int ERR_WebsocketMessageTooBig = 103002; + public const int ERR_WebsocketError = 103003; + public const int ERR_WebsocketConnectError = 103004; + public const int ERR_WebsocketSendError = 103005; + public const int ERR_WebsocketRecvError = 103006; + + public const int ERR_RpcFail = 102001; + public const int ERR_ReloadFail = 102003; + public const int ERR_ConnectGateKeyError = 100105; + public const int ERR_ActorLocationNotFound = 102004; + //----------------------------------- + // 小于这个Rpc会抛异常,大于这个异常的error需要自己判断处理,也就是说需要处理的错误应该要大于该值 public const int ERR_Exception = 200000; public const int ERR_NotFoundActor = 200002; - public const int ERR_ActorNoMailBoxComponent = 200003; - public const int ERR_ActorRemove = 200004; - public const int ERR_PacketParserError = 200005; - + public const int ERR_AccountOrPasswordError = 200102; - public const int ERR_SessionActorError = 200103; - public const int ERR_NotFoundUnit = 200104; - public const int ERR_ConnectGateKeyError = 200105; - - public const int ERR_RpcFail = 202001; - public const int ERR_SocketDisconnected = 202002; - public const int ERR_ReloadFail = 202003; - public const int ERR_ActorLocationNotFound = 202004; - public const int ERR_KcpCantConnect = 202005; - public const int ERR_KcpChannelTimeout = 202006; - public const int ERR_KcpRemoteDisconnect = 202007; - public const int ERR_PeerDisconnect = 202008; - public const int ERR_SocketCantSend = 202009; - public const int ERR_SocketError = 202010; - public const int ERR_KcpWaitSendSizeTooLarge = 202011; - - public const int ERR_WebsocketPeerReset = 203001; - public const int ERR_WebsocketMessageTooBig = 203002; - public const int ERR_WebsocketError = 203003; - public const int ERR_WebsocketConnectError = 203004; - public const int ERR_WebsocketSendError = 203005; - public const int ERR_WebsocketRecvError = 203006; - + //----------------------------------- public static bool IsRpcNeedThrowException(int error) { if (error == 0) diff --git a/Unity/Assets/Model/Module/Message/Network/TCP/PacketParser.cs b/Unity/Assets/Model/Module/Message/Network/TCP/PacketParser.cs index 94087a9c7d233c40976bf9754de1fbbd7fc328dc..50a9dfb182839d137a1fe15c53419fc0a1dcae61 100644 --- a/Unity/Assets/Model/Module/Message/Network/TCP/PacketParser.cs +++ b/Unity/Assets/Model/Module/Message/Network/TCP/PacketParser.cs @@ -13,9 +13,8 @@ namespace ETModel { public const int PacketSizeLength2 = 2; public const int PacketSizeLength4 = 4; - public const int FlagIndex = 0; - public const int OpcodeIndex = 1; - public const int MessageIndex = 3; + public const int OpcodeIndex = 0; + public const int MessageIndex = 2; } public class PacketParser @@ -74,7 +73,6 @@ namespace ETModel default: throw new Exception("packet size byte count must be 2 or 4!"); } - this.state = ParserState.PacketBody; } break; diff --git a/Unity/Assets/Model/Module/Message/Session.cs b/Unity/Assets/Model/Module/Message/Session.cs index 745bd44392d0bd3d3a74453378df21d59ddb4daf..43bf3f4a34e35ed0917a019830e2c9a34c4b1af0 100644 --- a/Unity/Assets/Model/Module/Message/Session.cs +++ b/Unity/Assets/Model/Module/Message/Session.cs @@ -23,7 +23,7 @@ namespace ETModel private AChannel channel; private readonly Dictionary> requestCallback = new Dictionary>(); - private readonly List byteses = new List() { new byte[1], new byte[2] }; + private readonly byte[] opcodeBytes = new byte[2]; public NetworkComponent Network { @@ -128,13 +128,12 @@ namespace ETModel 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().MessageCallback.Invoke(this, flag, opcode, memoryStream); + this.GetComponent().MessageCallback.Invoke(this, opcode, memoryStream); return; } #endif @@ -159,33 +158,28 @@ namespace ETModel 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}"); + this.Network.MessageDispatcher.Dispatch(this, opcode, message); + return; } + Action action; if (!this.requestCallback.TryGetValue(response.RpcId, out action)) { - return; + throw new Exception($"not found rpc, response message: {StringHelper.MessageToStr(response)}"); } this.requestCallback.Remove(response.RpcId); action(response); } - public Task Call(IRequest request) + public ETTask Call(IRequest request) { int rpcId = ++RpcId; - var tcs = new TaskCompletionSource(); + var tcs = new ETTaskCompletionSource(); this.requestCallback[rpcId] = (response) => { @@ -205,14 +199,14 @@ namespace ETModel }; request.RpcId = rpcId; - this.Send(0x00, request); + this.Send(request); return tcs.Task; } - public Task Call(IRequest request, CancellationToken cancellationToken) + public ETTask Call(IRequest request, CancellationToken cancellationToken) { int rpcId = ++RpcId; - var tcs = new TaskCompletionSource(); + var tcs = new ETTaskCompletionSource(); this.requestCallback[rpcId] = (response) => { @@ -234,15 +228,10 @@ namespace ETModel cancellationToken.Register(() => this.requestCallback.Remove(rpcId)); request.RpcId = rpcId; - this.Send(0x00, request); + this.Send(request); return tcs.Task; } - public void Send(IMessage message) - { - this.Send(0x00, message); - } - public void Reply(IResponse message) { if (this.IsDisposed) @@ -250,18 +239,18 @@ namespace ETModel throw new Exception("session已经被Dispose了"); } - this.Send(0x01, message); + this.Send(message); } - public void Send(byte flag, IMessage message) + public void Send(IMessage message) { OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent(); ushort opcode = opcodeTypeComponent.GetOpcode(message.GetType()); - Send(flag, opcode, message); + Send(opcode, message); } - public void Send(byte flag, ushort opcode, object message) + public void Send(ushort opcode, object message) { if (this.IsDisposed) { @@ -288,14 +277,8 @@ namespace ETModel this.Network.MessagePacker.SerializeTo(message, stream); stream.Seek(0, SeekOrigin.Begin); - 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; - } + opcodeBytes.WriteTo(0, opcode); + Array.Copy(opcodeBytes, 0, stream.GetBuffer(), 0, opcodeBytes.Length); #if SERVER // 如果是allserver,内部消息不走网络,直接转给session,方便调试时看到整体堆栈 diff --git a/Unity/Assets/Model/Module/Message/SessionCallbackComponent.cs b/Unity/Assets/Model/Module/Message/SessionCallbackComponent.cs index ebad4ac0cc5506663527266613e8ed87ee648633..6cfbbb00e2514a54268a9fcc415a2d8d8e8c6ab6 100644 --- a/Unity/Assets/Model/Module/Message/SessionCallbackComponent.cs +++ b/Unity/Assets/Model/Module/Message/SessionCallbackComponent.cs @@ -5,7 +5,7 @@ namespace ETModel { public class SessionCallbackComponent: Component { - public Action MessageCallback; + public Action MessageCallback; public Action DisposeCallback; public override void Dispose() diff --git a/Unity/Unity.Model.csproj b/Unity/Unity.Model.csproj index fe9f421a9c53c1b7f6f8c5a4e77f3852fe467bde..a097d182d3c440b645f1dad310016cbd0e75cdf0 100644 --- a/Unity/Unity.Model.csproj +++ b/Unity/Unity.Model.csproj @@ -205,6 +205,7 @@ + diff --git a/Unity/Unity.sln b/Unity/Unity.sln index 9fb0d3879b2b9f4d8a98d86b374e28fc20b54075..946328dda340303c9792d8266dec75a8fa1d98e7 100644 --- a/Unity/Unity.sln +++ b/Unity/Unity.sln @@ -5,10 +5,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Model", "Unity.Model. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.ThirdParty", "Unity.ThirdParty.csproj", "{E15BADD2-3A26-309A-AB0F-DC5B08044350}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Hotfix", "Unity.Hotfix.csproj", "{1066F652-6A89-D1C4-9881-1A19DF7AB80E}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Editor", "Unity.Editor.csproj", "{CD311104-1830-B119-81B6-5DBEE2467FFB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Hotfix", "Unity.Hotfix.csproj", "{1066F652-6A89-D1C4-9881-1A19DF7AB80E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -23,14 +23,14 @@ Global {E15BADD2-3A26-309A-AB0F-DC5B08044350}.Debug|Any CPU.Build.0 = Debug|Any CPU {E15BADD2-3A26-309A-AB0F-DC5B08044350}.Release|Any CPU.ActiveCfg = Release|Any CPU {E15BADD2-3A26-309A-AB0F-DC5B08044350}.Release|Any CPU.Build.0 = Release|Any CPU - {1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Release|Any CPU.Build.0 = Release|Any CPU {CD311104-1830-B119-81B6-5DBEE2467FFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {CD311104-1830-B119-81B6-5DBEE2467FFB}.Debug|Any CPU.Build.0 = Debug|Any CPU {CD311104-1830-B119-81B6-5DBEE2467FFB}.Release|Any CPU.ActiveCfg = Release|Any CPU {CD311104-1830-B119-81B6-5DBEE2467FFB}.Release|Any CPU.Build.0 = Release|Any CPU + {1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE