提交 b2d76405 编写于 作者: T tanghai

简化了actor代码

上级 ad822d06
......@@ -60,7 +60,7 @@ namespace ETHotfix
while (self.WaitingTasks.Count > 0)
{
ActorTask actorTask = self.WaitingTasks.Dequeue();
actorTask.RunFail(self.Error);
actorTask.Tcs?.SetException(new RpcException(self.Error, ""));
}
self.LastSendTime = 0;
......@@ -139,7 +139,11 @@ namespace ETHotfix
private static async Task RunTask(this ActorMessageSender self, ActorTask task)
{
IResponse response = await task.Run();
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.Address);
task.ActorMessage.ActorId = self.ActorId;
IResponse response = await session.Call(task.ActorMessage);
// 发送成功
switch (response.Error)
......@@ -149,6 +153,9 @@ namespace ETHotfix
self.FailTimes = 0;
self.WaitingTasks.Dequeue();
task.Tcs?.SetResult(response);
return;
case ErrorCode.ERR_NotFoundActor:
// 如果没找到Actor,重试
......@@ -182,13 +189,14 @@ namespace ETHotfix
public static void Send(this ActorMessageSender self, IActorMessage message)
{
ActorTask task = new ActorTask { message = message, MessageSender = self };
ActorTask task = new ActorTask(message);
self.Add(task);
}
public static Task<IResponse> Call(this ActorMessageSender self, IActorRequest request)
{
ActorTask task = new ActorTask { message = request, MessageSender = self, Tcs = new TaskCompletionSource<IResponse>() };
TaskCompletionSource<IResponse> tcs = new TaskCompletionSource<IResponse>();
ActorTask task = new ActorTask(request, tcs);
self.Add(task);
return task.Tcs.Task;
}
......@@ -198,7 +206,7 @@ namespace ETHotfix
string s = "";
foreach (ActorTask task in tasks)
{
s += $" {task.message.GetType().Name}";
s += $" {task.ActorMessage.GetType().Name}";
}
return s;
......
......@@ -6,7 +6,7 @@ namespace ETHotfix
[MessageHandler(AppType.Gate)]
public class C2G_LoginGateHandler : AMRpcHandler<C2G_LoginGate, G2C_LoginGate>
{
protected override async void Run(Session session, C2G_LoginGate message, Action<G2C_LoginGate> reply)
protected override void Run(Session session, C2G_LoginGate message, Action<G2C_LoginGate> reply)
{
G2C_LoginGate response = new G2C_LoginGate();
try
......
namespace ETModel
namespace ETHotfix
{
public static partial class HotfixOpcode
{
......
namespace ETModel
namespace ETHotfix
{
public static partial class InnerOpcode
{
......
using System;
using System.Collections.Generic;
using ETModel;
namespace ETModel
namespace ETHotfix
{
[ObjectSystem]
public class MessageDispatherComponentAwakeSystem : AwakeSystem<MessageDispatherComponent>
......
namespace ETModel
namespace ETHotfix
{
public static partial class OuterOpcode
{
......
......@@ -4,32 +4,20 @@ namespace ETModel
{
public struct ActorTask
{
public ActorMessageSender MessageSender;
public IActorMessage message;
public IActorMessage ActorMessage;
public TaskCompletionSource<IResponse> Tcs;
public async Task<IResponse> Run()
public ActorTask(IActorMessage actorMessage)
{
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.MessageSender.Address);
this.message.ActorId = this.MessageSender.ActorId;
IResponse response = await session.Call(message);
if (response.Error != ErrorCode.ERR_NotFoundActor)
{
if (this.Tcs != null)
{
this.Tcs?.SetResult(response);
}
}
return response;
this.ActorMessage = actorMessage;
this.Tcs = null;
}
public void RunFail(int error)
public ActorTask(IActorMessage actorMessage, TaskCompletionSource<IResponse> tcs)
{
this.Tcs?.SetException(new RpcException(error, ""));
this.ActorMessage = actorMessage;
this.Tcs = tcs;
}
}
}
\ No newline at end of file
......@@ -36,22 +36,22 @@ namespace ETEditor
{
msgOpcode.Clear();
Proto2CS("ETModel", "OuterMessage.proto", clientMessagePath, "OuterOpcode", 100, HeadFlag.Proto);
GenerateOpcode("OuterOpcode", clientMessagePath);
GenerateOpcode("ETModel", "OuterOpcode", clientMessagePath);
Proto2CS("ETHotfix", "OuterMessage.proto", serverMessagePath, "OuterOpcode", 100, HeadFlag.Proto | HeadFlag.Bson, false);
GenerateOpcode("OuterOpcode", serverMessagePath);
GenerateOpcode("ETHotfix", "OuterOpcode", serverMessagePath);
msgOpcode.Clear();
Proto2CS("ETHotfix", "HotfixMessage.proto", hotfixMessagePath, "HotfixOpcode", 10000, HeadFlag.None);
GenerateOpcode("HotfixOpcode", hotfixMessagePath);
GenerateOpcode("ETHotfix", "HotfixOpcode", hotfixMessagePath);
msgOpcode.Clear();
Proto2CS("ETHotfix", "HotfixMessage.proto", serverMessagePath, "HotfixOpcode", 10000, HeadFlag.Bson, false);
GenerateOpcode("HotfixOpcode", serverMessagePath);
GenerateOpcode("ETHotfix", "HotfixOpcode", serverMessagePath);
msgOpcode.Clear();
Proto2CS("ETHotfix", "InnerMessage.proto", serverMessagePath, "InnerOpcode", 1000, HeadFlag.Bson, false);
GenerateOpcode("InnerOpcode", serverMessagePath);
GenerateOpcode("ETHotfix", "InnerOpcode", serverMessagePath);
AssetDatabase.Refresh();
}
......@@ -229,10 +229,10 @@ namespace ETEditor
}
}
private static void GenerateOpcode(string outputFileName, string outputPath)
private static void GenerateOpcode(string ns, string outputFileName, string outputPath)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("namespace ETModel");
sb.AppendLine($"namespace {ns}");
sb.AppendLine("{");
sb.AppendLine($"\tpublic static partial class {outputFileName}");
sb.AppendLine("\t{");
......
namespace ETModel
namespace ETHotfix
{
public static partial class HotfixOpcode
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册