diff --git a/Server/App/Program.cs b/Server/App/Program.cs index 15b5c53a58b2cd7f7271edcc1b30eb5f39200a81..7f506ef2e4bccc7de4a61cd33876d133f6c2fef1 100644 --- a/Server/App/Program.cs +++ b/Server/App/Program.cs @@ -33,8 +33,10 @@ namespace App switch (options.AppType) { case "Realm": + Game.Scene.AddComponent(); break; case "Gate": + Game.Scene.AddComponent(); break; default: throw new Exception($"命令行参数没有设置正确的AppType: {options.AppType}"); diff --git a/Server/Controller/C2S_LoginHandler.cs b/Server/Controller/C2S_LoginHandler.cs deleted file mode 100644 index 0cee38fd934d9151803eb682d7abbecc2766b319..0000000000000000000000000000000000000000 --- a/Server/Controller/C2S_LoginHandler.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using Base; -using Model; - -namespace Controller -{ - [MessageHandler(AppType.Realm)] - public class C2S_LoginHandler: AMRpcEvent - { - protected override void Run(Entity scene, C2S_Login message, Action reply) - { - Log.Info(MongoHelper.ToJson(message)); - - reply(new S2C_Login()); - } - } -} \ No newline at end of file diff --git a/Server/Controller/C2S_SubscribeLogHandler.cs b/Server/Controller/C2S_SubscribeLogHandler.cs deleted file mode 100644 index 61c9b4fa029af22ee84238118de690bc4120c4af..0000000000000000000000000000000000000000 --- a/Server/Controller/C2S_SubscribeLogHandler.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using Base; -using Model; - -namespace Controller -{ - [MessageHandler(AppType.Realm)] - public class C2S_SubscribeLogHandler : AMRpcEvent - { - protected override void Run(Entity entity, C2S_SubscribeLog message, Action reply) - { - Log.Info(MongoHelper.ToJson(message)); - - entity.AddComponent(); - - reply(new S2C_SubscribeLog()); - } - } -} \ No newline at end of file diff --git a/Server/Controller/Message/C2G_LoginGateHandler.cs b/Server/Controller/Message/C2G_LoginGateHandler.cs new file mode 100644 index 0000000000000000000000000000000000000000..3466b024b173ae12d322968e534288db665c4cc8 --- /dev/null +++ b/Server/Controller/Message/C2G_LoginGateHandler.cs @@ -0,0 +1,22 @@ +using System; +using Base; +using Model; + +namespace Controller +{ + [MessageHandler(AppType.Gate)] + public class C2G_LoginGateHandler : AMRpcEvent + { + protected override void Run(Entity scene, C2G_LoginGate message, Action reply) + { + bool isCheckOK = Game.Scene.GetComponent().Check(message.Key); + G2C_LoginGate g2CLoginGate = new G2C_LoginGate(); + if (!isCheckOK) + { + g2CLoginGate.Error = ErrorCode.ERR_ConnectGateKeyError; + g2CLoginGate.Message = "Gate key验证失败!"; + } + reply(g2CLoginGate); + } + } +} \ No newline at end of file diff --git a/Server/Controller/Message/C2R_LoginHandler.cs b/Server/Controller/Message/C2R_LoginHandler.cs new file mode 100644 index 0000000000000000000000000000000000000000..2928bf60fbb882b1628773e160ee193a11bf77b9 --- /dev/null +++ b/Server/Controller/Message/C2R_LoginHandler.cs @@ -0,0 +1,31 @@ +using System; +using Base; +using Model; + +namespace Controller +{ + [MessageHandler(AppType.Realm)] + public class C2R_LoginHandler: AMRpcEvent + { + protected override async void Run(Entity session, C2R_Login message, Action reply) + { + R2C_Login r2CLogin; + if (message.Account != "abcdef" || message.Password != "111111") + { + r2CLogin = new R2C_Login {Error = ErrorCode.ERR_AccountOrPasswordError, Message = "账号名或者密码错误!"}; + reply(r2CLogin); + return; + } + + // 随机分配一个Gate + string gateAddress = Game.Scene.GetComponent().GetAddress(); + Entity gateSession = Game.Scene.GetComponent().Get(gateAddress); + + // 向gate请求一个key,客户端可以拿着这个key连接gate + G2R_GetLoginKey g2RGetLoginKey = await gateSession.GetComponent().Call(new R2G_GetLoginKey()); + + r2CLogin = new R2C_Login {Address = gateAddress, Key = g2RGetLoginKey.Key}; + reply(r2CLogin); + } + } +} \ No newline at end of file diff --git a/Server/Controller/Message/C2R_SubscribeLogHandler.cs b/Server/Controller/Message/C2R_SubscribeLogHandler.cs new file mode 100644 index 0000000000000000000000000000000000000000..d544f78d820b8e16a217178520ebd693e329ebe4 --- /dev/null +++ b/Server/Controller/Message/C2R_SubscribeLogHandler.cs @@ -0,0 +1,19 @@ +using System; +using Base; +using Model; + +namespace Controller +{ + [MessageHandler(AppType.Realm)] + public class C2R_SubscribeLogHandler : AMRpcEvent + { + protected override void Run(Entity entity, C2R_SubscribeLog message, Action reply) + { + Log.Info(MongoHelper.ToJson(message)); + + //entity.AddComponent(); + + reply(new R2C_SubscribeLog()); + } + } +} \ No newline at end of file diff --git a/Server/Controller/Message/R2G_GetLoginKeyHandler.cs b/Server/Controller/Message/R2G_GetLoginKeyHandler.cs new file mode 100644 index 0000000000000000000000000000000000000000..9a9efb6c97af857a470c906fb420064166349067 --- /dev/null +++ b/Server/Controller/Message/R2G_GetLoginKeyHandler.cs @@ -0,0 +1,17 @@ +using System; +using Base; +using Model; + +namespace Controller +{ + [MessageHandler(AppType.Gate)] + public class R2G_GetLoginKeyHandler : AMRpcEvent + { + protected override void Run(Entity scene, R2G_GetLoginKey message, Action reply) + { + long key = Game.Scene.GetComponent().Get(); + G2R_GetLoginKey g2RGetLoginKey = new G2R_GetLoginKey(key); + reply(g2RGetLoginKey); + } + } +} \ No newline at end of file diff --git a/Server/Controller/Server.Controller.csproj b/Server/Controller/Server.Controller.csproj index 22ebd4e8563d7fe6e1228c57e32f84be6f1d5967..58dea9e028db0d15322970bee53543474af9ac38 100644 --- a/Server/Controller/Server.Controller.csproj +++ b/Server/Controller/Server.Controller.csproj @@ -34,9 +34,11 @@ - + + + - + diff --git a/Server/Model/Component/GateSessionKeyComponent.cs b/Server/Model/Component/GateSessionKeyComponent.cs new file mode 100644 index 0000000000000000000000000000000000000000..b7e845e3890597720a0c1f08e0d8b0253fbe660f --- /dev/null +++ b/Server/Model/Component/GateSessionKeyComponent.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using Base; + +namespace Model +{ + [ObjectEvent] + public class GateSessionKeyComponentEvent : ObjectEvent, IAwake + { + public void Awake() + { + this.GetValue().Awake(); + } + } + + public class GateSessionKeyComponent : Component + { + private TimerComponent timerComponent; + + private readonly HashSet sessionKey = new HashSet(); + + public void Awake() + { + this.timerComponent = Game.Scene.GetComponent(); + } + + public long Get() + { + long key = RandomHelper.RandInt64(); + this.sessionKey.Add(key); + this.TimeoutRemoveKey(key); + return key; + } + + public bool Check(long key) + { + bool ret = this.sessionKey.Contains(key); + if (ret) + { + this.sessionKey.Remove(key); + } + return ret; + } + + public void Remove(long key) + { + this.sessionKey.Remove(key); + } + + private async void TimeoutRemoveKey(long key) + { + await this.timerComponent.WaitAsync(20000); + this.sessionKey.Remove(key); + } + } +} diff --git a/Server/Model/Component/LogToClientComponent.cs b/Server/Model/Component/LogToClientComponent.cs index ca49100d34813383b845824590f5970732916623..30993dd5487782483ea58f9568c4cc4cf9ae1e61 100644 --- a/Server/Model/Component/LogToClientComponent.cs +++ b/Server/Model/Component/LogToClientComponent.cs @@ -14,10 +14,12 @@ namespace Model public class LogToClientComponent : Component { private string appType; + private int appId; public void Awake() { this.appType = Game.Scene.GetComponent().Options.AppType; + this.appId = Game.Scene.GetComponent().Options.Id; Log.Callback.Add(this.Id, this.LogToClient); } @@ -27,7 +29,7 @@ namespace Model { return; } - this.GetComponent().Send(new S2C_ServerLog { AppType = this.appType, Type = type, Log = message }); + this.GetComponent().Send(new R2C_ServerLog { AppType = this.appType, AppId = this.appId, Type = type, Log = message }); } public override void Dispose() diff --git a/Server/Model/Component/RealmGateAddressComponent.cs b/Server/Model/Component/RealmGateAddressComponent.cs new file mode 100644 index 0000000000000000000000000000000000000000..0d17bf0fa79acd4c4ede41a488c25e0c2e0247b2 --- /dev/null +++ b/Server/Model/Component/RealmGateAddressComponent.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Base; + +namespace Model +{ + [ObjectEvent] + public class RealmGateAddressComponentEvent : ObjectEvent, IAwake + { + public void Awake() + { + this.GetValue().Awake(); + } + } + + public class RealmGateAddressComponent : Component + { + private readonly List GateAddress = new List(); + + public void Awake() + { + string s = File.ReadAllText("./CommandLineConfig.txt"); + CommandLines commandLines = MongoHelper.FromJson(s); + foreach (CommandLine commandLine in commandLines.Commands) + { + if (commandLine.Options.AppType != "Gate") + { + continue; + } + this.GateAddress.Add($"{commandLine.Options.Host}:{commandLine.Options.Port}"); + } + } + + public string GetAddress() + { + int n = RandomHelper.RandomNumber(0, this.GateAddress.Count); + return this.GateAddress[n]; + } + } +} diff --git a/Server/Model/Server.Model.csproj b/Server/Model/Server.Model.csproj index e192f2517fa4929635bb44f425626a934049b07c..dcbd51c05dd6bfa5535712fe6b7d9cb62bef9abb 100644 --- a/Server/Model/Server.Model.csproj +++ b/Server/Model/Server.Model.csproj @@ -74,10 +74,15 @@ Message\OpcodeHelper.cs + + Other\CommandLine.cs + Other\Options.cs + + diff --git a/Unity/Assets/Editor/ServerCommandLineEditor/UI/ServerCommandLineEditor.cs b/Unity/Assets/Editor/ServerCommandLineEditor/UI/ServerCommandLineEditor.cs index 42ac0d2cb4b25e0ca2d1ff3be11052b267ce6d60..184e3153db2c8bcbc6507af6292fe3b338fe3dd0 100644 --- a/Unity/Assets/Editor/ServerCommandLineEditor/UI/ServerCommandLineEditor.cs +++ b/Unity/Assets/Editor/ServerCommandLineEditor/UI/ServerCommandLineEditor.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.IO; using Base; +using Model; using UnityEditor; using UnityEngine; diff --git a/Unity/Assets/Plugins/Base/Helper/RandomHelper.cs b/Unity/Assets/Plugins/Base/Helper/RandomHelper.cs index e29bc3318fcb176eca54ca8e1ad1912da06625d0..549e5966968292475da2cd0c84174ae9a61ec9d4 100644 --- a/Unity/Assets/Plugins/Base/Helper/RandomHelper.cs +++ b/Unity/Assets/Plugins/Base/Helper/RandomHelper.cs @@ -12,6 +12,14 @@ namespace Base return BitConverter.ToUInt64(bytes, 0); } + public static Int64 RandInt64() + { + var bytes = new byte[8]; + Random random = new Random(); + random.NextBytes(bytes); + return BitConverter.ToInt64(bytes, 0); + } + /// /// 获取lower与Upper之间的随机数 /// diff --git a/Unity/Assets/Plugins/Base/Message/AMessage.cs b/Unity/Assets/Plugins/Base/Message/AMessage.cs index 9c68e2cde9e24cc34c7b3d0b39581605da9fa768..b2018160ca93a1a9a45ad5b78119bbca25757a77 100644 --- a/Unity/Assets/Plugins/Base/Message/AMessage.cs +++ b/Unity/Assets/Plugins/Base/Message/AMessage.cs @@ -13,7 +13,7 @@ /// public abstract class AResponse: AMessage { - public int ErrorCode = 0; + public int Error = 0; public string Message = ""; } } diff --git a/Unity/Assets/Plugins/Base/Message/RpcException.cs b/Unity/Assets/Plugins/Base/Message/RpcException.cs index c9d9b609f812a534a87130f1e28390d5aca9cb4e..420031b594ed520b324ca84c132f01db584db06f 100644 --- a/Unity/Assets/Plugins/Base/Message/RpcException.cs +++ b/Unity/Assets/Plugins/Base/Message/RpcException.cs @@ -10,12 +10,12 @@ namespace Base { public int Error { get; private set; } - public RpcException(int error, string message) : base($"{error} : {message}") + public RpcException(int error, string message) : base($"Error: {error} Message: {message}") { this.Error = error; } - public RpcException(int error, string message, Exception e) : base($"{error} : {message}", e) + public RpcException(int error, string message, Exception e) : base($"Error: {error} Message: {message}", e) { this.Error = error; } diff --git a/Unity/Assets/Scripts/Component/MessageComponent.cs b/Unity/Assets/Scripts/Component/MessageComponent.cs index f5965d8354db370e9a434dc0b2305cc2dd97d1e0..f6d939a4fd334b05cfcde9658f350bae54800fc9 100644 --- a/Unity/Assets/Scripts/Component/MessageComponent.cs +++ b/Unity/Assets/Scripts/Component/MessageComponent.cs @@ -140,9 +140,9 @@ namespace Model try { Response response = MongoHelper.FromBson(bytes, offset, count); - if (response.ErrorCode != 0) + if (response.Error != 0) { - tcs.SetException(new RpcException(response.ErrorCode, response.Message)); + tcs.SetException(new RpcException(response.Error, response.Message)); return; } tcs.SetResult(response); @@ -172,9 +172,9 @@ namespace Model try { Response response = MongoHelper.FromBson(bytes, offset, count); - if (response.ErrorCode != 0) + if (response.Error != 0) { - tcs.SetException(new RpcException(response.ErrorCode, response.Message)); + tcs.SetException(new RpcException(response.Error, response.Message)); return; } tcs.SetResult(response); @@ -219,7 +219,7 @@ namespace Model } byte[] seqBytes = BitConverter.GetBytes(rpcId); - + channel.Send(new List { opcodeBytes, seqBytes, messageBytes }); } diff --git a/Unity/Assets/Scripts/Message/ErrorCode.cs b/Unity/Assets/Scripts/Message/ErrorCode.cs index e9c8781f82966759c6fff4064a1cb22648a1d1a2..db74a8939184f5c53a3805f3f7d19916ce683a2a 100644 --- a/Unity/Assets/Scripts/Message/ErrorCode.cs +++ b/Unity/Assets/Scripts/Message/ErrorCode.cs @@ -3,5 +3,7 @@ namespace Model public static class ErrorCode { public const int ERR_Success = 0; + public const int ERR_AccountOrPasswordError = 1; + public const int ERR_ConnectGateKeyError = 2; } } diff --git a/Unity/Assets/Scripts/Message/Message.cs b/Unity/Assets/Scripts/Message/Message.cs index 40de5633ebb8a2c095fe9b0d99b06d290c091ba5..a81e302704976c774fc11737b2caf4a123d0b446 100644 --- a/Unity/Assets/Scripts/Message/Message.cs +++ b/Unity/Assets/Scripts/Message/Message.cs @@ -5,7 +5,7 @@ namespace Model { [Message(1)] [BsonIgnoreExtraElements] - public class C2S_Login: ARequest + public class C2R_Login: ARequest { public string Account; public string Password; @@ -13,30 +13,67 @@ namespace Model [Message(2)] [BsonIgnoreExtraElements] - public class S2C_Login: AResponse + public class R2C_Login: AResponse { - public string Host; - public int Port; + public string Address { get; set; } + public long Key { get; set; } } [Message(3)] [BsonIgnoreExtraElements] - public class S2C_ServerLog: AMessage + public class R2C_ServerLog: AMessage { - public string AppType; - public LogType Type; - public string Log; + public string AppType { get; set; } + public int AppId { get; set; } + public LogType Type { get; set; } + public string Log { get; set; } } [Message(4)] [BsonIgnoreExtraElements] - public class C2S_SubscribeLog: ARequest + public class C2R_SubscribeLog: ARequest { } [Message(5)] [BsonIgnoreExtraElements] - public class S2C_SubscribeLog: AResponse + public class R2C_SubscribeLog: AResponse + { + } + + [Message(6)] + [BsonIgnoreExtraElements] + public class R2G_GetLoginKey : ARequest + { + } + + [Message(7)] + [BsonIgnoreExtraElements] + public class G2R_GetLoginKey : AResponse + { + public long Key; + + public G2R_GetLoginKey(long key) + { + this.Key = key; + } + } + + [Message(8)] + [BsonIgnoreExtraElements] + public class C2G_LoginGate : ARequest + { + public long Key; + + public C2G_LoginGate(long key) + { + this.Key = key; + } + } + + [Message(9)] + [BsonIgnoreExtraElements] + public class G2C_LoginGate : AResponse { } } diff --git a/Unity/Assets/Editor/ServerCommandLineEditor/Component/CommandLine.cs b/Unity/Assets/Scripts/Other/CommandLine.cs similarity index 95% rename from Unity/Assets/Editor/ServerCommandLineEditor/Component/CommandLine.cs rename to Unity/Assets/Scripts/Other/CommandLine.cs index 8f6d5a4f12607a304c6a7370bf3554f032bd7c22..c881b03ac386a30cac9d371b01332c4ed8acaf6a 100644 --- a/Unity/Assets/Editor/ServerCommandLineEditor/Component/CommandLine.cs +++ b/Unity/Assets/Scripts/Other/CommandLine.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using Base; using Model; -namespace MyEditor +namespace Model { public class CommandLine: ICloneable { diff --git a/Unity/Assets/Editor/ServerCommandLineEditor/Component/CommandLine.cs.meta b/Unity/Assets/Scripts/Other/CommandLine.cs.meta similarity index 75% rename from Unity/Assets/Editor/ServerCommandLineEditor/Component/CommandLine.cs.meta rename to Unity/Assets/Scripts/Other/CommandLine.cs.meta index c2188700b09c306697e080dcf973283b740b8095..af3fea2430fc29ebdc9982cdadb728b4b660b13e 100644 --- a/Unity/Assets/Editor/ServerCommandLineEditor/Component/CommandLine.cs.meta +++ b/Unity/Assets/Scripts/Other/CommandLine.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 4f4981289b5c3cc4ca1604a6ba39f3a8 -timeCreated: 1476674012 +guid: c181dc38f15288746960812611a7767e +timeCreated: 1476862821 licenseType: Pro MonoImporter: serializedVersion: 2 diff --git a/Unity/Controller/Event/InitSceneStartEvent_InitGame.cs b/Unity/Controller/Event/InitSceneStartEvent_InitGame.cs index 6441854be38e734ee1b5ad4b3f7f497b69c0224b..9a2aac767dc149bb98320f9c4713cd2b6a3b504d 100644 --- a/Unity/Controller/Event/InitSceneStartEvent_InitGame.cs +++ b/Unity/Controller/Event/InitSceneStartEvent_InitGame.cs @@ -19,9 +19,13 @@ namespace Controller try { // 订阅服务端日志, 服务端收到这个消息会将之后的日志转发给客户端 - await session.GetComponent().Call(new C2S_SubscribeLog()); - S2C_Login s2CLogin = await session.GetComponent().Call(new C2S_Login {Account = "tanghai", Password = "1111111"}); - Log.Info(MongoHelper.ToJson(s2CLogin)); + await session.GetComponent().Call(new C2R_SubscribeLog()); + R2C_Login s2CLogin = await session.GetComponent().Call(new C2R_Login {Account = "111", Password = "111111"}); + + // 连接Gate + Entity gateSession = networkComponent.Get(s2CLogin.Address); + await gateSession.GetComponent().Call(new C2G_LoginGate(s2CLogin.Key)); + Log.Info("连接Gate验证成功!"); } catch (RpcException e) { diff --git a/Unity/Controller/Message/R2C_ServerLogHandler.cs b/Unity/Controller/Message/R2C_ServerLogHandler.cs new file mode 100644 index 0000000000000000000000000000000000000000..653557c32097babcf5ec6e373ed1563993441f71 --- /dev/null +++ b/Unity/Controller/Message/R2C_ServerLogHandler.cs @@ -0,0 +1,14 @@ +using Base; +using Model; + +namespace Controller +{ + [MessageHandler(AppType.Client)] + public class R2C_ServerLogHandler: AMEvent + { + protected override void Run(Entity scene, R2C_ServerLog message) + { + Log.Debug($"[{message.AppType}][{message.AppId}] [{message.Type}] {message.Log}"); + } + } +} diff --git a/Unity/Controller/Message/S2C_ServerLogHandler.cs b/Unity/Controller/Message/S2C_ServerLogHandler.cs deleted file mode 100644 index 3717331832c974f34eb1c1d4416e7036e6b4e12f..0000000000000000000000000000000000000000 --- a/Unity/Controller/Message/S2C_ServerLogHandler.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Base; -using Model; - -namespace Controller -{ - [MessageHandler(AppType.Client)] - public class S2C_ServerLogHandler: AMEvent - { - protected override void Run(Entity scene, S2C_ServerLog message) - { - Log.Debug($"[{message.AppType}] [{message.Type}] {message.Log}"); - } - } -} diff --git a/Unity/Controller/Unity.Controller.csproj b/Unity/Controller/Unity.Controller.csproj index 347c3141583cfa97387ecaef30cf4803a3b0ed9a..14ad30926efc645c7a29381197a49553af125d12 100644 --- a/Unity/Controller/Unity.Controller.csproj +++ b/Unity/Controller/Unity.Controller.csproj @@ -54,7 +54,7 @@ - + diff --git a/Unity/Unity.CSharp.Editor.csproj b/Unity/Unity.CSharp.Editor.csproj index 8b87525c9cc8694ae5c0c8ad5557008a842f7a1d..761f6af013bfed9a6abd2730aa2d54afcdad7370 100644 --- a/Unity/Unity.CSharp.Editor.csproj +++ b/Unity/Unity.CSharp.Editor.csproj @@ -13,11 +13,13 @@ .NETFramework v3.5 Unity Full v3.5 - + + Editor:5 StandaloneWindows:5 5.4.2f1 - + + pdbonly @@ -112,7 +114,6 @@ - diff --git a/Unity/Unity.CSharp.csproj b/Unity/Unity.CSharp.csproj index 9544b092185af72027b3073c931e20ce70c93e52..61d457119f2c11463f77e7f18bbeaf079d17f6c3 100644 --- a/Unity/Unity.CSharp.csproj +++ b/Unity/Unity.CSharp.csproj @@ -102,6 +102,7 @@ +