提交 a21c68d1 编写于 作者: T tanghai

actor模型测试通过,客户端发送一个actor_test消息到gate,gate转发给map上的unit(actor),unit收到Actor_test消...

actor模型测试通过,客户端发送一个actor_test消息到gate,gate转发给map上的unit(actor),unit收到Actor_test消息回发给gatesession(actor),gatesession将收到的actor_test消息回发给client端
消息路径如下:
client->gate->map->gate->client
上级 009d24c4
......@@ -9,6 +9,8 @@ namespace Hotfix
protected override async Task<bool> Run(Unit unit, Actor_Test message)
{
Log.Info(message.Info);
unit.GetComponent<UnitGateComponent>().GetActorProxy().Send(message);
return true;
}
}
......
......@@ -22,11 +22,12 @@ namespace Hotfix
Player player = ObjectFactory.Create<Player, string>(account);
Game.Scene.GetComponent<PlayerComponent>().Add(player);
session.AddComponent<SessionPlayerComponent>().Player = player;
session.AddComponent<ActorComponent, IEntityActorHandler>(new GateSessionEntityActorHandler());
// 在map服务器上创建战斗Unit
string mapAddress = Game.Scene.GetComponent<StartConfigComponent>().MapConfig.GetComponent<InnerConfig>().Address;
Session mapSession = Game.Scene.GetComponent<NetInnerComponent>().Get(mapAddress);
M2G_CreateUnit createUnit = await mapSession.Call<M2G_CreateUnit>(new G2M_CreateUnit() { PlayerId = player.Id });
M2G_CreateUnit createUnit = await mapSession.Call<M2G_CreateUnit>(new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.Id });
player.UnitId = createUnit.UnitId;
response.PlayerId = player.Id;
......
......@@ -13,6 +13,7 @@ namespace Hotfix
{
Unit unit = ObjectFactory.Create<Unit, UnitType>(UnitType.Hero);
unit.AddComponent<ActorComponent, IEntityActorHandler>(new CommonEntityActorHandler());
unit.AddComponent<UnitGateComponent, long>(message.GateSessionId);
Game.Scene.GetComponent<UnitComponent>().Add(unit);
response.UnitId = unit.Id;
reply(response);
......
namespace Model
{
[ObjectEvent]
public class UnitGateComponentEvent : ObjectEvent<UnitGateComponent>, IAwake<long>
{
public void Awake(long gateSessionId)
{
this.Get().Awake(gateSessionId);
}
}
public class UnitGateComponent : Component
{
public long GateSessionId;
public void Awake(long gateSessionId)
{
this.GateSessionId = gateSessionId;
}
public ActorProxy GetActorProxy()
{
return Game.Scene.GetComponent<ActorProxyComponent>().Get(this.GateSessionId);
}
}
}
\ No newline at end of file
......@@ -230,12 +230,14 @@ namespace Model
public void Send(AActorMessage message)
{
message.Id = this.Id;
ActorMessageTask task = new ActorMessageTask(this, message);
this.Add(task);
}
public Task<Response> Call<Response>(AActorRequest request)where Response : AActorResponse
{
request.Id = this.Id;
ActorRpcTask<Response> task = new ActorRpcTask<Response>(this, request);
this.Add(task);
return task.Tcs.Task;
......
......@@ -56,4 +56,4 @@ namespace Model { [Message(Opcode.M2A_Reload)] [BsonIgnoreExtraElements] publ
[Message(Opcode.ObjectGetRequest)] [BsonIgnoreExtraElements] public class ObjectGetRequest : ARequest { public long Key { get; set; } }
[Message(Opcode.ObjectGetResponse)] [BsonIgnoreExtraElements] public class ObjectGetResponse : AResponse { public int AppId { get; set; } } [Message(Opcode.R2G_GetLoginKey)] [BsonIgnoreExtraElements] public class R2G_GetLoginKey : ARequest { public string Account; } [Message(Opcode.G2R_GetLoginKey)] [BsonIgnoreExtraElements] public class G2R_GetLoginKey : AResponse { public long Key; public G2R_GetLoginKey() { } public G2R_GetLoginKey(long key) { this.Key = key; } } [Message(Opcode.G2M_CreateUnit)] [BsonIgnoreExtraElements] public class G2M_CreateUnit : ARequest { public long PlayerId; } [Message(Opcode.M2G_CreateUnit)] [BsonIgnoreExtraElements] public class M2G_CreateUnit : AResponse { public long UnitId; }}
\ No newline at end of file
[Message(Opcode.ObjectGetResponse)] [BsonIgnoreExtraElements] public class ObjectGetResponse : AResponse { public int AppId { get; set; } } [Message(Opcode.R2G_GetLoginKey)] [BsonIgnoreExtraElements] public class R2G_GetLoginKey : ARequest { public string Account; } [Message(Opcode.G2R_GetLoginKey)] [BsonIgnoreExtraElements] public class G2R_GetLoginKey : AResponse { public long Key; public G2R_GetLoginKey() { } public G2R_GetLoginKey(long key) { this.Key = key; } } [Message(Opcode.G2M_CreateUnit)] [BsonIgnoreExtraElements] public class G2M_CreateUnit : ARequest { public long PlayerId; public long GateSessionId; } [Message(Opcode.M2G_CreateUnit)] [BsonIgnoreExtraElements] public class M2G_CreateUnit : AResponse { public long UnitId; }}
\ No newline at end of file
......
......@@ -4,11 +4,13 @@ namespace Model
{
public interface IActorMessage
{
[BsonIgnoreIfDefault]
long Id { get; set; }
}
public abstract class AActorMessage: ARequest, IActorMessage
{
[BsonIgnoreIfDefault]
public long Id { get; set; }
}
......
namespace Model
using MongoDB.Bson.Serialization.Attributes;
namespace Model
{
public abstract class AMessage
{
......@@ -6,6 +8,7 @@
public abstract class ARequest: AMessage
{
[BsonIgnoreIfDefault]
public uint RpcId;
}
......
......@@ -69,6 +69,7 @@
</Compile>
<Compile Include="Component\ActorProxyComponent.cs" />
<Compile Include="Component\PlayerComponent.cs" />
<Compile Include="Component\Unit\UnitGateComponent.cs" />
<Compile Include="Entity\Player.cs" />
<Compile Include="Entity\Location.cs" />
<Compile Include="Helper\ObjectFactory.cs" />
......
using System;
namespace Model
{
public abstract class AMHandler<Message> : IMHandler where Message : AMessage
{
protected abstract void Run(Message message);
public void Handle(object msg)
{
Message message = msg as Message;
if (message == null)
{
Log.Error($"消息类型转换错误: {msg.GetType().Name} to {typeof(Message).Name}");
}
this.Run(message);
}
public Type GetMessageType()
{
return typeof(Message);
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 51f27baf9bcf4588a008ab171d42ffd7
timeCreated: 1503744375
\ No newline at end of file
......@@ -16,6 +16,10 @@ namespace Model
{
Game.Scene.GetComponent<CrossComponent>().Run(CrossIdType.MessageDeserializeFinish, messageInfo);
}
else
{
Game.Scene.GetComponent<MessageDispatherComponent>().Handle(messageInfo);
}
return;
}
......
using System;
namespace Model
{
public interface IMHandler
{
void Handle(object message);
Type GetMessageType();
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 6081581f6dde45a0889b8593bdedf860
timeCreated: 1503744375
\ No newline at end of file
using System;
using System.Collections.Generic;
namespace Model
{
/// <summary>
/// 消息分发组件
/// </summary>
[ObjectEvent(EntityEventId.MessageDispatherComponent)]
public class MessageDispatherComponent : Component, IAwake, ILoad
{
private Dictionary<ushort, List<IMHandler>> handlers;
public void Awake()
{
this.Load();
}
public void Load()
{
handlers = new Dictionary<ushort, List<IMHandler>>();
Type[] types = DllHelper.GetMonoTypes();
foreach (Type type in types)
{
object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
if (attrs.Length == 0)
{
continue;
}
MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
{
this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMHandler>());
}
this.handlers[messageHandlerAttribute.Opcode].Add(iMHandler);
}
}
public void Handle(MessageInfo messageInfo)
{
List<IMHandler> actions;
if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
{
Log.Error($"消息 {messageInfo.Opcode} 没有处理");
return;
}
foreach (IMHandler ev in actions)
{
try
{
ev.Handle(messageInfo.Message);
}
catch (Exception e)
{
Log.Error(e.ToString());
}
}
}
public override void Dispose()
{
if (this.Id == 0)
{
return;
}
base.Dispose();
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: f6a1591f4b7a4121a01428af98aed2c1
timeCreated: 1503745497
\ No newline at end of file
......@@ -11,6 +11,6 @@
public const ushort G2C_GetPlayerInfo = 1007;
public const ushort C2M_Reload = 1008;
public const ushort Actor_Test = 1009;
public const ushort Actor_Test = 2001;
}
}
fileFormatVersion: 2
guid: 1760c4c295484ca7ba64936681612d21
timeCreated: 1503744277
\ No newline at end of file
namespace Model
{
[MessageHandler(Opcode.Actor_Test)]
public class Actor_TestHandler : AMHandler<Actor_Test>
{
protected override void Run(Actor_Test message)
{
Log.Debug(message.Info);
}
}
}
fileFormatVersion: 2
guid: ddd6230b24c24bb3b358f5de9ba82bb5
timeCreated: 1503744322
\ No newline at end of file
using System;
using Model;
namespace Model
namespace Hotfix
{
public abstract class AMHandler<Message> : IMHandler where Message : AMessage
{
......
namespace Hotfix
{
public abstract class AMessage
{
}
public abstract class ARequest: AMessage
{
public uint RpcId;
}
/// <summary>
/// 服务端回的RPC消息需要继承这个抽象类
/// </summary>
public abstract class AResponse
{
public uint RpcId;
public int Error = 0;
public string Message = "";
}
public abstract class AFrameMessage : AMessage
{
public long Id;
}
}
\ No newline at end of file
using System;
namespace Model
namespace Hotfix
{
public interface IMHandler
{
......
......@@ -11,6 +11,7 @@ namespace Hotfix
{
Hotfix.Scene.ModelScene = Game.Scene;
Hotfix.Scene.ModelScene.AddComponent<OpcodeTypeComponent>();
Hotfix.Scene.ModelScene.AddComponent<Model.MessageDispatherComponent>();
Hotfix.Scene.ModelScene.AddComponent<NetOuterComponent>();
Hotfix.Scene.ModelScene.AddComponent<ResourcesComponent>();
Hotfix.Scene.ModelScene.AddComponent<BehaviorTreeComponent>();
......
......@@ -32,7 +32,7 @@ namespace Hotfix
Log.Info("登陆gate成功!");
// 发送一个actor消息
gateSession.Send(new Actor_Test() { Info = "send to actor" });
gateSession.Send(new Actor_Test() { Info = "message client->gate->map->gate->client" });
}
catch (Exception e)
{
......
......@@ -56,6 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Base\Helper\ExceptionHelper.cs" />
<Compile Include="Base\Message\AMessage.cs" />
<Compile Include="Base\Message\AMHandler.cs" />
<Compile Include="Base\Message\IMHandler.cs" />
<Compile Include="Component\MessageDispatherComponent.cs" />
......
......@@ -12,12 +12,15 @@
<ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkProfile></TargetFrameworkProfile>
<CompilerResponseFile></CompilerResponseFile>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<CompilerResponseFile>
</CompilerResponseFile>
<UnityProjectType>Game:1</UnityProjectType>
<UnityBuildTarget>StandaloneWindows64:19</UnityBuildTarget>
<UnityVersion>2017.1.0f3</UnityVersion>
<RootNamespace></RootNamespace>
<RootNamespace>
</RootNamespace>
<LangVersion>6</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
......@@ -371,11 +374,13 @@
<Compile Include="Assets\Scripts\Base\LogType.cs" />
<Compile Include="Assets\Scripts\Base\Message\AActorMessage.cs" />
<Compile Include="Assets\Scripts\Base\Message\AMessage.cs" />
<Compile Include="Assets\Scripts\Base\Message\AMHandler.cs" />
<Compile Include="Assets\Scripts\Base\Message\AppType.cs" />
<Compile Include="Assets\Scripts\Base\Message\ClientDispatcher.cs" />
<Compile Include="Assets\Scripts\Base\Message\ErrorCode.cs" />
<Compile Include="Assets\Scripts\Base\Message\IMessageDispatcher.cs" />
<Compile Include="Assets\Scripts\Base\Message\IMessagePacker.cs" />
<Compile Include="Assets\Scripts\Base\Message\IMHandler.cs" />
<Compile Include="Assets\Scripts\Base\Message\JsondotnetPacker.cs" />
<Compile Include="Assets\Scripts\Base\Message\MessageAttribute.cs" />
<Compile Include="Assets\Scripts\Base\Message\MessageHandlerAttribute.cs" />
......@@ -486,6 +491,7 @@
<Compile Include="Assets\Scripts\Component\Config\VersionConfig.cs" />
<Compile Include="Assets\Scripts\Component\CrossComponent.cs" />
<Compile Include="Assets\Scripts\Component\EventComponent.cs" />
<Compile Include="Assets\Scripts\Component\MessageDispatherComponent.cs" />
<Compile Include="Assets\Scripts\Component\NetOuterComponent.cs" />
<Compile Include="Assets\Scripts\Component\NetworkComponent.cs" />
<Compile Include="Assets\Scripts\Component\OpcodeTypeComponent.cs" />
......@@ -501,6 +507,7 @@
<Compile Include="Assets\Scripts\Entity\Scene.cs" />
<Compile Include="Assets\Scripts\Entity\Session.cs" />
<Compile Include="Assets\Scripts\Entity\WWWAsync.cs" />
<Compile Include="Assets\Scripts\Handler\Actor_TestHandler.cs" />
<Compile Include="Assets\Scripts\Helper\ActionHelper.cs" />
<Compile Include="Assets\Scripts\Helper\GameObjectHelper.cs" />
<Compile Include="Assets\Scripts\Helper\JsonHelper.cs" />
......@@ -529,4 +536,4 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="GenerateTargetFrameworkMonikerAttribute" />
</Project>
</Project>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册