提交 fd0b276f 编写于 作者: T tanghai

Client连接Realm(验证服务器), Realm随机分配一个Gate,并且向Gate请求一个Key,发回给Client, client拿着key连接Gate

调试通过!
上级 fe94aadc
...@@ -33,8 +33,10 @@ namespace App ...@@ -33,8 +33,10 @@ namespace App
switch (options.AppType) switch (options.AppType)
{ {
case "Realm": case "Realm":
Game.Scene.AddComponent<RealmGateAddressComponent>();
break; break;
case "Gate": case "Gate":
Game.Scene.AddComponent<GateSessionKeyComponent>();
break; break;
default: default:
throw new Exception($"命令行参数没有设置正确的AppType: {options.AppType}"); throw new Exception($"命令行参数没有设置正确的AppType: {options.AppType}");
......
using System;
using Base;
using Model;
namespace Controller
{
[MessageHandler(AppType.Realm)]
public class C2S_SubscribeLogHandler : AMRpcEvent<C2S_SubscribeLog, S2C_SubscribeLog>
{
protected override void Run(Entity entity, C2S_SubscribeLog message, Action<S2C_SubscribeLog> reply)
{
Log.Info(MongoHelper.ToJson(message));
entity.AddComponent<LogToClientComponent>();
reply(new S2C_SubscribeLog());
}
}
}
\ No newline at end of file
using System;
using Base;
using Model;
namespace Controller
{
[MessageHandler(AppType.Gate)]
public class C2G_LoginGateHandler : AMRpcEvent<C2G_LoginGate, G2C_LoginGate>
{
protected override void Run(Entity scene, C2G_LoginGate message, Action<G2C_LoginGate> reply)
{
bool isCheckOK = Game.Scene.GetComponent<GateSessionKeyComponent>().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
using System;
using Base;
using Model;
namespace Controller
{
[MessageHandler(AppType.Realm)]
public class C2R_LoginHandler: AMRpcEvent<C2R_Login, R2C_Login>
{
protected override async void Run(Entity session, C2R_Login message, Action<R2C_Login> 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<RealmGateAddressComponent>().GetAddress();
Entity gateSession = Game.Scene.GetComponent<NetworkComponent>().Get(gateAddress);
// 向gate请求一个key,客户端可以拿着这个key连接gate
G2R_GetLoginKey g2RGetLoginKey = await gateSession.GetComponent<MessageComponent>().Call<R2G_GetLoginKey, G2R_GetLoginKey>(new R2G_GetLoginKey());
r2CLogin = new R2C_Login {Address = gateAddress, Key = g2RGetLoginKey.Key};
reply(r2CLogin);
}
}
}
\ No newline at end of file
...@@ -5,13 +5,15 @@ using Model; ...@@ -5,13 +5,15 @@ using Model;
namespace Controller namespace Controller
{ {
[MessageHandler(AppType.Realm)] [MessageHandler(AppType.Realm)]
public class C2S_LoginHandler: AMRpcEvent<C2S_Login, S2C_Login> public class C2R_SubscribeLogHandler : AMRpcEvent<C2R_SubscribeLog, R2C_SubscribeLog>
{ {
protected override void Run(Entity scene, C2S_Login message, Action<S2C_Login> reply) protected override void Run(Entity entity, C2R_SubscribeLog message, Action<R2C_SubscribeLog> reply)
{ {
Log.Info(MongoHelper.ToJson(message)); Log.Info(MongoHelper.ToJson(message));
reply(new S2C_Login()); //entity.AddComponent<LogToClientComponent>();
reply(new R2C_SubscribeLog());
} }
} }
} }
\ No newline at end of file
using System;
using Base;
using Model;
namespace Controller
{
[MessageHandler(AppType.Gate)]
public class R2G_GetLoginKeyHandler : AMRpcEvent<R2G_GetLoginKey, G2R_GetLoginKey>
{
protected override void Run(Entity scene, R2G_GetLoginKey message, Action<G2R_GetLoginKey> reply)
{
long key = Game.Scene.GetComponent<GateSessionKeyComponent>().Get();
G2R_GetLoginKey g2RGetLoginKey = new G2R_GetLoginKey(key);
reply(g2RGetLoginKey);
}
}
}
\ No newline at end of file
...@@ -34,9 +34,11 @@ ...@@ -34,9 +34,11 @@
<Reference Include="System.Core" /> <Reference Include="System.Core" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="C2S_SubscribeLogHandler.cs" /> <Compile Include="Message\C2G_LoginGateHandler.cs" />
<Compile Include="Message\R2G_GetLoginKeyHandler.cs" />
<Compile Include="Message\C2R_SubscribeLogHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="C2S_LoginHandler.cs" /> <Compile Include="Message\C2R_LoginHandler.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Base\Server.Base.csproj"> <ProjectReference Include="..\Base\Server.Base.csproj">
......
using System.Collections.Generic;
using Base;
namespace Model
{
[ObjectEvent]
public class GateSessionKeyComponentEvent : ObjectEvent<GateSessionKeyComponent>, IAwake
{
public void Awake()
{
this.GetValue().Awake();
}
}
public class GateSessionKeyComponent : Component
{
private TimerComponent timerComponent;
private readonly HashSet<long> sessionKey = new HashSet<long>();
public void Awake()
{
this.timerComponent = Game.Scene.GetComponent<TimerComponent>();
}
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);
}
}
}
...@@ -14,10 +14,12 @@ namespace Model ...@@ -14,10 +14,12 @@ namespace Model
public class LogToClientComponent : Component public class LogToClientComponent : Component
{ {
private string appType; private string appType;
private int appId;
public void Awake() public void Awake()
{ {
this.appType = Game.Scene.GetComponent<OptionsComponent>().Options.AppType; this.appType = Game.Scene.GetComponent<OptionsComponent>().Options.AppType;
this.appId = Game.Scene.GetComponent<OptionsComponent>().Options.Id;
Log.Callback.Add(this.Id, this.LogToClient); Log.Callback.Add(this.Id, this.LogToClient);
} }
...@@ -27,7 +29,7 @@ namespace Model ...@@ -27,7 +29,7 @@ namespace Model
{ {
return; return;
} }
this.GetComponent<MessageComponent>().Send(new S2C_ServerLog { AppType = this.appType, Type = type, Log = message }); this.GetComponent<MessageComponent>().Send(new R2C_ServerLog { AppType = this.appType, AppId = this.appId, Type = type, Log = message });
} }
public override void Dispose() public override void Dispose()
......
using System;
using System.Collections.Generic;
using System.IO;
using Base;
namespace Model
{
[ObjectEvent]
public class RealmGateAddressComponentEvent : ObjectEvent<RealmGateAddressComponent>, IAwake
{
public void Awake()
{
this.GetValue().Awake();
}
}
public class RealmGateAddressComponent : Component
{
private readonly List<string> GateAddress = new List<string>();
public void Awake()
{
string s = File.ReadAllText("./CommandLineConfig.txt");
CommandLines commandLines = MongoHelper.FromJson<CommandLines>(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];
}
}
}
...@@ -74,10 +74,15 @@ ...@@ -74,10 +74,15 @@
<Compile Include="..\..\Unity\Assets\Scripts\Message\OpcodeHelper.cs"> <Compile Include="..\..\Unity\Assets\Scripts\Message\OpcodeHelper.cs">
<Link>Message\OpcodeHelper.cs</Link> <Link>Message\OpcodeHelper.cs</Link>
</Compile> </Compile>
<Compile Include="..\..\Unity\Assets\Scripts\Other\CommandLine.cs">
<Link>Other\CommandLine.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Scripts\Other\Options.cs"> <Compile Include="..\..\Unity\Assets\Scripts\Other\Options.cs">
<Link>Other\Options.cs</Link> <Link>Other\Options.cs</Link>
</Compile> </Compile>
<Compile Include="Component\LogToClientComponent.cs" /> <Compile Include="Component\LogToClientComponent.cs" />
<Compile Include="Component\GateSessionKeyComponent.cs" />
<Compile Include="Component\RealmGateAddressComponent.cs" />
<Compile Include="Component\OptionsComponent.cs" /> <Compile Include="Component\OptionsComponent.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using Base; using Base;
using Model;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
......
...@@ -12,6 +12,14 @@ namespace Base ...@@ -12,6 +12,14 @@ namespace Base
return BitConverter.ToUInt64(bytes, 0); 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);
}
/// <summary> /// <summary>
/// 获取lower与Upper之间的随机数 /// 获取lower与Upper之间的随机数
/// </summary> /// </summary>
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
/// </summary> /// </summary>
public abstract class AResponse: AMessage public abstract class AResponse: AMessage
{ {
public int ErrorCode = 0; public int Error = 0;
public string Message = ""; public string Message = "";
} }
} }
...@@ -10,12 +10,12 @@ namespace Base ...@@ -10,12 +10,12 @@ namespace Base
{ {
public int Error { get; private set; } 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; 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; this.Error = error;
} }
......
...@@ -140,9 +140,9 @@ namespace Model ...@@ -140,9 +140,9 @@ namespace Model
try try
{ {
Response response = MongoHelper.FromBson<Response>(bytes, offset, count); Response response = MongoHelper.FromBson<Response>(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; return;
} }
tcs.SetResult(response); tcs.SetResult(response);
...@@ -172,9 +172,9 @@ namespace Model ...@@ -172,9 +172,9 @@ namespace Model
try try
{ {
Response response = MongoHelper.FromBson<Response>(bytes, offset, count); Response response = MongoHelper.FromBson<Response>(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; return;
} }
tcs.SetResult(response); tcs.SetResult(response);
...@@ -219,7 +219,7 @@ namespace Model ...@@ -219,7 +219,7 @@ namespace Model
} }
byte[] seqBytes = BitConverter.GetBytes(rpcId); byte[] seqBytes = BitConverter.GetBytes(rpcId);
channel.Send(new List<byte[]> { opcodeBytes, seqBytes, messageBytes }); channel.Send(new List<byte[]> { opcodeBytes, seqBytes, messageBytes });
} }
......
...@@ -3,5 +3,7 @@ namespace Model ...@@ -3,5 +3,7 @@ namespace Model
public static class ErrorCode public static class ErrorCode
{ {
public const int ERR_Success = 0; public const int ERR_Success = 0;
public const int ERR_AccountOrPasswordError = 1;
public const int ERR_ConnectGateKeyError = 2;
} }
} }
...@@ -5,7 +5,7 @@ namespace Model ...@@ -5,7 +5,7 @@ namespace Model
{ {
[Message(1)] [Message(1)]
[BsonIgnoreExtraElements] [BsonIgnoreExtraElements]
public class C2S_Login: ARequest public class C2R_Login: ARequest
{ {
public string Account; public string Account;
public string Password; public string Password;
...@@ -13,30 +13,67 @@ namespace Model ...@@ -13,30 +13,67 @@ namespace Model
[Message(2)] [Message(2)]
[BsonIgnoreExtraElements] [BsonIgnoreExtraElements]
public class S2C_Login: AResponse public class R2C_Login: AResponse
{ {
public string Host; public string Address { get; set; }
public int Port; public long Key { get; set; }
} }
[Message(3)] [Message(3)]
[BsonIgnoreExtraElements] [BsonIgnoreExtraElements]
public class S2C_ServerLog: AMessage public class R2C_ServerLog: AMessage
{ {
public string AppType; public string AppType { get; set; }
public LogType Type; public int AppId { get; set; }
public string Log; public LogType Type { get; set; }
public string Log { get; set; }
} }
[Message(4)] [Message(4)]
[BsonIgnoreExtraElements] [BsonIgnoreExtraElements]
public class C2S_SubscribeLog: ARequest public class C2R_SubscribeLog: ARequest
{ {
} }
[Message(5)] [Message(5)]
[BsonIgnoreExtraElements] [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
{ {
} }
} }
...@@ -3,7 +3,7 @@ using System.Collections.Generic; ...@@ -3,7 +3,7 @@ using System.Collections.Generic;
using Base; using Base;
using Model; using Model;
namespace MyEditor namespace Model
{ {
public class CommandLine: ICloneable public class CommandLine: ICloneable
{ {
......
fileFormatVersion: 2 fileFormatVersion: 2
guid: 4f4981289b5c3cc4ca1604a6ba39f3a8 guid: c181dc38f15288746960812611a7767e
timeCreated: 1476674012 timeCreated: 1476862821
licenseType: Pro licenseType: Pro
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2
......
...@@ -19,9 +19,13 @@ namespace Controller ...@@ -19,9 +19,13 @@ namespace Controller
try try
{ {
// 订阅服务端日志, 服务端收到这个消息会将之后的日志转发给客户端 // 订阅服务端日志, 服务端收到这个消息会将之后的日志转发给客户端
await session.GetComponent<MessageComponent>().Call<C2S_SubscribeLog, S2C_SubscribeLog>(new C2S_SubscribeLog()); await session.GetComponent<MessageComponent>().Call<C2R_SubscribeLog, R2C_SubscribeLog>(new C2R_SubscribeLog());
S2C_Login s2CLogin = await session.GetComponent<MessageComponent>().Call<C2S_Login, S2C_Login>(new C2S_Login {Account = "tanghai", Password = "1111111"}); R2C_Login s2CLogin = await session.GetComponent<MessageComponent>().Call<C2R_Login, R2C_Login>(new C2R_Login {Account = "111", Password = "111111"});
Log.Info(MongoHelper.ToJson(s2CLogin));
// 连接Gate
Entity gateSession = networkComponent.Get(s2CLogin.Address);
await gateSession.GetComponent<MessageComponent>().Call<C2G_LoginGate, G2C_LoginGate>(new C2G_LoginGate(s2CLogin.Key));
Log.Info("连接Gate验证成功!");
} }
catch (RpcException e) catch (RpcException e)
{ {
......
...@@ -4,11 +4,11 @@ using Model; ...@@ -4,11 +4,11 @@ using Model;
namespace Controller namespace Controller
{ {
[MessageHandler(AppType.Client)] [MessageHandler(AppType.Client)]
public class S2C_ServerLogHandler: AMEvent<S2C_ServerLog> public class R2C_ServerLogHandler: AMEvent<R2C_ServerLog>
{ {
protected override void Run(Entity scene, S2C_ServerLog message) protected override void Run(Entity scene, R2C_ServerLog message)
{ {
Log.Debug($"[{message.AppType}] [{message.Type}] {message.Log}"); Log.Debug($"[{message.AppType}][{message.AppId}] [{message.Type}] {message.Log}");
} }
} }
} }
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Event\InitSceneStartEvent_InitGame.cs" /> <Compile Include="Event\InitSceneStartEvent_InitGame.cs" />
<Compile Include="Message\S2C_ServerLogHandler.cs" /> <Compile Include="Message\R2C_ServerLogHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
...@@ -13,11 +13,13 @@ ...@@ -13,11 +13,13 @@
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier> <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkProfile>Unity Full v3.5</TargetFrameworkProfile> <TargetFrameworkProfile>Unity Full v3.5</TargetFrameworkProfile>
<CompilerResponseFile></CompilerResponseFile> <CompilerResponseFile>
</CompilerResponseFile>
<UnityProjectType>Editor:5</UnityProjectType> <UnityProjectType>Editor:5</UnityProjectType>
<UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget> <UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget>
<UnityVersion>5.4.2f1</UnityVersion> <UnityVersion>5.4.2f1</UnityVersion>
<RootNamespace></RootNamespace> <RootNamespace>
</RootNamespace>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
...@@ -112,7 +114,6 @@ ...@@ -112,7 +114,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Assets\Editor\ReferenceCollectorEditor\ReferenceCollectorEditor.cs" /> <Compile Include="Assets\Editor\ReferenceCollectorEditor\ReferenceCollectorEditor.cs" />
<Compile Include="Assets\Editor\ServerCommandLineEditor\Component\CommandLine.cs" />
<Compile Include="Assets\Editor\ServerCommandLineEditor\UI\ServerCommandLineEditor.cs" /> <Compile Include="Assets\Editor\ServerCommandLineEditor\UI\ServerCommandLineEditor.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
...@@ -102,6 +102,7 @@ ...@@ -102,6 +102,7 @@
<Compile Include="Assets\Scripts\Message\ErrorCode.cs" /> <Compile Include="Assets\Scripts\Message\ErrorCode.cs" />
<Compile Include="Assets\Scripts\Message\Message.cs" /> <Compile Include="Assets\Scripts\Message\Message.cs" />
<Compile Include="Assets\Scripts\Message\OpcodeHelper.cs" /> <Compile Include="Assets\Scripts\Message\OpcodeHelper.cs" />
<Compile Include="Assets\Scripts\Other\CommandLine.cs" />
<Compile Include="Assets\Scripts\Other\EntityType.cs" /> <Compile Include="Assets\Scripts\Other\EntityType.cs" />
<Compile Include="Assets\Scripts\Other\GameException.cs" /> <Compile Include="Assets\Scripts\Other\GameException.cs" />
<Compile Include="Assets\Scripts\Other\Options.cs" /> <Compile Include="Assets\Scripts\Other\Options.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册