|
@@ -7,14 +7,15 @@ using MongoDB.Bson;
|
|
|
|
|
|
|
|
namespace Model
|
|
namespace Model
|
|
|
{
|
|
{
|
|
|
- public sealed class Session: Entity
|
|
|
|
|
|
|
+ public sealed class Session : Entity
|
|
|
{
|
|
{
|
|
|
private static uint RpcId { get; set; }
|
|
private static uint RpcId { get; set; }
|
|
|
private readonly NetworkComponent network;
|
|
private readonly NetworkComponent network;
|
|
|
private readonly Dictionary<uint, Action<byte[], int, int>> requestCallback = new Dictionary<uint, Action<byte[], int, int>>();
|
|
private readonly Dictionary<uint, Action<byte[], int, int>> requestCallback = new Dictionary<uint, Action<byte[], int, int>>();
|
|
|
private readonly AChannel channel;
|
|
private readonly AChannel channel;
|
|
|
|
|
+ private bool isRpc;
|
|
|
|
|
|
|
|
- public Session(NetworkComponent network, AChannel channel): base(EntityType.Session)
|
|
|
|
|
|
|
+ public Session(NetworkComponent network, AChannel channel)
|
|
|
{
|
|
{
|
|
|
this.network = network;
|
|
this.network = network;
|
|
|
this.channel = channel;
|
|
this.channel = channel;
|
|
@@ -39,6 +40,7 @@ namespace Model
|
|
|
|
|
|
|
|
private async void StartRecv()
|
|
private async void StartRecv()
|
|
|
{
|
|
{
|
|
|
|
|
+ TimerComponent timerComponent = Game.Scene.GetComponent<TimerComponent>();
|
|
|
while (true)
|
|
while (true)
|
|
|
{
|
|
{
|
|
|
if (this.Id == 0)
|
|
if (this.Id == 0)
|
|
@@ -49,6 +51,11 @@ namespace Model
|
|
|
byte[] messageBytes;
|
|
byte[] messageBytes;
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
|
|
+ if (this.isRpc)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.isRpc = false;
|
|
|
|
|
+ await timerComponent.WaitAsync(0);
|
|
|
|
|
+ }
|
|
|
messageBytes = await channel.Recv();
|
|
messageBytes = await channel.Recv();
|
|
|
}
|
|
}
|
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
@@ -63,6 +70,7 @@ namespace Model
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
ushort opcode = BitConverter.ToUInt16(messageBytes, 0);
|
|
ushort opcode = BitConverter.ToUInt16(messageBytes, 0);
|
|
|
|
|
+
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
this.Run(opcode, messageBytes);
|
|
this.Run(opcode, messageBytes);
|
|
@@ -121,6 +129,7 @@ namespace Model
|
|
|
public Task<Response> Call<Request, Response>(Request request, CancellationToken cancellationToken) where Request : ARequest
|
|
public Task<Response> Call<Request, Response>(Request request, CancellationToken cancellationToken) where Request : ARequest
|
|
|
where Response : AResponse
|
|
where Response : AResponse
|
|
|
{
|
|
{
|
|
|
|
|
+
|
|
|
this.SendMessage(++RpcId, request);
|
|
this.SendMessage(++RpcId, request);
|
|
|
|
|
|
|
|
var tcs = new TaskCompletionSource<Response>();
|
|
var tcs = new TaskCompletionSource<Response>();
|
|
@@ -135,11 +144,13 @@ namespace Model
|
|
|
tcs.SetException(new RpcException(response.Error, response.Message));
|
|
tcs.SetException(new RpcException(response.Error, response.Message));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+ //Log.Debug($"recv: {response.ToJson()}");
|
|
|
|
|
+ this.isRpc = true;
|
|
|
tcs.SetResult(response);
|
|
tcs.SetResult(response);
|
|
|
}
|
|
}
|
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
|
{
|
|
{
|
|
|
- tcs.SetException(new Exception($"Rpc Error: {typeof (Response).FullName}", e));
|
|
|
|
|
|
|
+ tcs.SetException(new Exception($"Rpc Error: {typeof(Response).FullName}", e));
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -166,11 +177,13 @@ namespace Model
|
|
|
tcs.SetException(new RpcException(response.Error, response.Message));
|
|
tcs.SetException(new RpcException(response.Error, response.Message));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+ //Log.Info($"recv: {response.ToJson()}");
|
|
|
|
|
+ this.isRpc = true;
|
|
|
tcs.SetResult(response);
|
|
tcs.SetResult(response);
|
|
|
}
|
|
}
|
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
|
{
|
|
{
|
|
|
- tcs.SetException(new Exception($"Rpc Error: {typeof (Response).FullName}", e));
|
|
|
|
|
|
|
+ tcs.SetException(new Exception($"Rpc Error: {typeof(Response).FullName}", e));
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -197,6 +210,7 @@ namespace Model
|
|
|
|
|
|
|
|
private void SendMessage(uint rpcId, object message, bool isCall = true)
|
|
private void SendMessage(uint rpcId, object message, bool isCall = true)
|
|
|
{
|
|
{
|
|
|
|
|
+ //Log.Debug($"send: {message.ToJson()}");
|
|
|
ushort opcode = this.network.Owner.GetComponent<MessageDispatherComponent>().GetOpcode(message.GetType());
|
|
ushort opcode = this.network.Owner.GetComponent<MessageDispatherComponent>().GetOpcode(message.GetType());
|
|
|
byte[] opcodeBytes = BitConverter.GetBytes(opcode);
|
|
byte[] opcodeBytes = BitConverter.GetBytes(opcode);
|
|
|
if (!isCall)
|
|
if (!isCall)
|