TH1/Unity/Assets/Scripts/Hotfix/Server/Demo/Gate/NetComponentOnReadInvoker_Gate.cs
2025-07-17 18:26:28 +08:00

78 lines
3.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
namespace ET.Server
{
[Invoke((long)SceneType.Gate)]
public class NetComponentOnReadInvoker_Gate: AInvokeHandler<NetComponentOnRead>
{
public override void Handle(NetComponentOnRead args)
{
HandleAsync(args).Coroutine();
}
private async ETTask HandleAsync(NetComponentOnRead args)
{
Session session = args.Session;
object message = args.Message;
Scene root = args.Session.Root();
// 根据消息接口判断是不是Actor消息不同的接口做不同的处理,比如需要转发给Chat Scene可以做一个IChatMessage接口
switch (message)
{
case ISessionMessage:
{
MessageSessionDispatcher.Instance.Handle(session, message);
break;
}
case FrameMessage frameMessage:
{
Player player = session.GetComponent<SessionPlayerComponent>().Player;
ActorId roomActorId = player.GetComponent<PlayerRoomComponent>().RoomActorId;
frameMessage.PlayerId = player.Id;
root.GetComponent<MessageSender>().Send(roomActorId, frameMessage);
break;
}
case IRoomMessage actorRoom:
{
Player player = session.GetComponent<SessionPlayerComponent>().Player;
ActorId roomActorId = player.GetComponent<PlayerRoomComponent>().RoomActorId;
actorRoom.PlayerId = player.Id;
root.GetComponent<MessageSender>().Send(roomActorId, actorRoom);
break;
}
case ILocationMessage actorLocationMessage:
{
long unitId = session.GetComponent<SessionPlayerComponent>().Player.Id;
root.GetComponent<MessageLocationSenderComponent>().Get(LocationType.Unit).Send(unitId, actorLocationMessage);
break;
}
case ILocationRequest actorLocationRequest: // gate session收到actor rpc消息先向actor 发送rpc请求再将请求结果返回客户端
{
long unitId = session.GetComponent<SessionPlayerComponent>().Player.Id;
int rpcId = actorLocationRequest.RpcId; // 这里要保存客户端的rpcId
long instanceId = session.InstanceId;
IResponse iResponse = await root.GetComponent<MessageLocationSenderComponent>().Get(LocationType.Unit).Call(unitId, actorLocationRequest);
iResponse.RpcId = rpcId;
// session可能已经断开了所以这里需要判断
if (session.InstanceId == instanceId)
{
session.Send(iResponse);
}
break;
}
case IRequest actorRequest: // 分发IActorRequest消息目前没有用到需要的自己添加
{
break;
}
case IMessage actorMessage: // 分发IActorMessage消息目前没有用到需要的自己添加
{
break;
}
default:
{
throw new Exception($"not found handler: {message}");
}
}
}
}
}