mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] 接入ET8服务端
[+] 接入ET8服务端
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
[ComponentOf(typeof(Scene))]
|
||||
public class ActorOuterComponent: Entity, IAwake<IPEndPoint>, IUpdate, IDestroy
|
||||
{
|
||||
public const long TIMEOUT_TIME = 40 * 1000;
|
||||
|
||||
public int RpcId;
|
||||
|
||||
public readonly Dictionary<int, ActorMessageSender> requestCallback = new();
|
||||
|
||||
public AService AService;
|
||||
|
||||
public NetworkProtocol InnerProtocol = NetworkProtocol.KCP;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 11d2abb5f986c5b44b5b58bfae58908f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
[ComponentOf(typeof(Scene))]
|
||||
public class ActorSenderComponent: Entity, IAwake, IDestroy
|
||||
{
|
||||
public const long TIMEOUT_TIME = 40 * 1000;
|
||||
|
||||
public int RpcId;
|
||||
|
||||
public readonly Dictionary<int, ActorMessageSender> requestCallback = new();
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ecc41a2013820624bbe9d11dc8940dc8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
public abstract class MessageHandler<Request, Response>: IMHandler where Request : MessageObject, IRequest where Response : MessageObject, IResponse
|
||||
{
|
||||
protected abstract ETTask Run(Session session, Request request, Response response);
|
||||
|
||||
public void Handle(Session session, object message)
|
||||
{
|
||||
HandleAsync(session, message).Coroutine();
|
||||
}
|
||||
|
||||
private async ETTask HandleAsync(Session session, object message)
|
||||
{
|
||||
try
|
||||
{
|
||||
Request request = message as Request;
|
||||
if (request == null)
|
||||
{
|
||||
throw new Exception($"消息类型转换错误: {message.GetType().FullName} to {typeof (Request).FullName}");
|
||||
}
|
||||
|
||||
int rpcId = request.RpcId;
|
||||
long instanceId = session.InstanceId;
|
||||
|
||||
Response response = ObjectPool.Instance.Fetch<Response>();
|
||||
|
||||
try
|
||||
{
|
||||
await this.Run(session, request, response);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception);
|
||||
response.Error = ErrorCore.ERR_RpcFail;
|
||||
response.Message = exception.ToString();
|
||||
}
|
||||
|
||||
// 等回调回来,session可以已经断开了,所以需要判断session InstanceId是否一样
|
||||
if (session.InstanceId != instanceId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
response.RpcId = rpcId; // 在这里设置rpcId是为了防止在Run中不小心修改rpcId字段
|
||||
session.Send(response);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"解释消息失败: {message.GetType().FullName}", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Type GetMessageType()
|
||||
{
|
||||
return typeof (Request);
|
||||
}
|
||||
|
||||
public Type GetResponseType()
|
||||
{
|
||||
return typeof (Response);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e3881ad9cfc7dc343a107f25d5659fb1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,16 @@
|
||||
using System.Net;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
public struct NetServerComponentOnRead
|
||||
{
|
||||
public Session Session;
|
||||
public object Message;
|
||||
}
|
||||
|
||||
[ComponentOf(typeof(Scene))]
|
||||
public class NetServerComponent: Entity, IAwake<IPEndPoint>, IDestroy, IUpdate
|
||||
{
|
||||
public AService AService;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d26c818710bffb459fecf60387869b6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user