Skip to content

Commit

Permalink
Add dispatch service implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaioru committed Aug 20, 2024
1 parent dcc3278 commit 90bf31a
Show file tree
Hide file tree
Showing 27 changed files with 403 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Edelstein.sln
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Edelstein.Common.Services.M
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Edelstein.Protocol.Services.Dispatch", "src\protocol\Edelstein.Protocol.Services.Dispatch\Edelstein.Protocol.Services.Dispatch.csproj", "{C9F8DA52-DDA0-4A43-A104-47F38CD6DAE3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Edelstein.Common.Services.Dispatch", "src\common\Edelstein.Common.Services.Dispatch\Edelstein.Common.Services.Dispatch.csproj", "{55B6632F-D547-4133-BDD8-5655B5DD2D64}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -124,6 +126,7 @@ Global
{1E8C9556-7283-4C7A-8E2C-D41072FBCE9E} = {82D7864B-19AD-484C-BD2E-897F05B5852C}
{AEF75B30-6D2E-44F3-A43A-A99EC4942435} = {E50DFCDF-39D5-4D0D-A46E-94D11D795087}
{C9F8DA52-DDA0-4A43-A104-47F38CD6DAE3} = {82D7864B-19AD-484C-BD2E-897F05B5852C}
{55B6632F-D547-4133-BDD8-5655B5DD2D64} = {E50DFCDF-39D5-4D0D-A46E-94D11D795087}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6527D18C-B0E2-49BA-B649-4D5DB6216069}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -232,5 +235,9 @@ Global
{C9F8DA52-DDA0-4A43-A104-47F38CD6DAE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9F8DA52-DDA0-4A43-A104-47F38CD6DAE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9F8DA52-DDA0-4A43-A104-47F38CD6DAE3}.Release|Any CPU.Build.0 = Release|Any CPU
{55B6632F-D547-4133-BDD8-5655B5DD2D64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55B6632F-D547-4133-BDD8-5655B5DD2D64}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55B6632F-D547-4133-BDD8-5655B5DD2D64}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55B6632F-D547-4133-BDD8-5655B5DD2D64}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<ProjectReference Include="..\..\common\Edelstein.Common.Gameplay.Login\Edelstein.Common.Gameplay.Login.csproj" />
<ProjectReference Include="..\..\common\Edelstein.Common.Network.DotNetty\Edelstein.Common.Network.DotNetty.csproj" />
<ProjectReference Include="..\..\common\Edelstein.Common.Plugin\Edelstein.Common.Plugin.csproj" />
<ProjectReference Include="..\..\common\Edelstein.Common.Services.Dispatch\Edelstein.Common.Services.Dispatch.csproj" />
<ProjectReference Include="..\..\common\Edelstein.Common.Services.Migration\Edelstein.Common.Services.Migration.csproj" />
<ProjectReference Include="..\..\common\Edelstein.Common.Services.Server\Edelstein.Common.Services.Server.csproj" />
<ProjectReference Include="..\..\common\Edelstein.Common.Services.Session\Edelstein.Common.Services.Session.csproj" />
Expand Down
3 changes: 3 additions & 0 deletions src/app/Edelstein.Application.Server/ProgramHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Edelstein.Common.Gameplay.Handling;
using Edelstein.Common.Plugin;
using Edelstein.Common.Services.Auth;
using Edelstein.Common.Services.Dispatch;
using Edelstein.Common.Services.Migration;
using Edelstein.Common.Services.Server;
using Edelstein.Common.Services.Session;
Expand All @@ -18,6 +19,7 @@
using Edelstein.Protocol.Gameplay.Login.Contexts;
using Edelstein.Protocol.Plugin;
using Edelstein.Protocol.Services.Auth;
using Edelstein.Protocol.Services.Dispatch;
using Edelstein.Protocol.Services.Migration;
using Edelstein.Protocol.Services.Server;
using Edelstein.Protocol.Services.Session;
Expand Down Expand Up @@ -84,6 +86,7 @@ internal static HostApplicationBuilder CreateBuilder()
.WithSingletonLifetime());

// TODO gRPC
builder.Services.AddSingleton<IDispatchService, DispatchService>();
builder.Services.AddSingleton<IAuthService, AuthService>();
builder.Services.AddSingleton<IServerService, ServerService>();
builder.Services.AddSingleton<ISessionService, SessionService>();
Expand Down
17 changes: 17 additions & 0 deletions src/app/Edelstein.Application.Server/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,15 @@
"EntityFrameworkCore.Exceptions.Common": "[8.1.3, )"
}
},
"edelstein.common.services.dispatch": {
"type": "Project",
"dependencies": {
"Edelstein.Common.Utilities": "[1.0.0, )",
"Edelstein.Protocol.Services.Dispatch": "[1.0.0, )",
"Edelstein.Protocol.Services.Session": "[1.0.0, )",
"Edelstein.Protocol.Utilities": "[1.0.0, )"
}
},
"edelstein.common.services.migration": {
"type": "Project",
"dependencies": {
Expand Down Expand Up @@ -1634,6 +1643,7 @@
"Edelstein.Protocol.Gameplay": "[1.0.0, )",
"Edelstein.Protocol.Network": "[1.0.0, )",
"Edelstein.Protocol.Services.Auth": "[1.0.0, )",
"Edelstein.Protocol.Services.Dispatch": "[1.0.0, )",
"Edelstein.Protocol.Services.Migration": "[1.0.0, )",
"Edelstein.Protocol.Services.Server": "[1.0.0, )",
"Edelstein.Protocol.Services.Session": "[1.0.0, )",
Expand Down Expand Up @@ -1661,6 +1671,13 @@
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.dispatch": {
"type": "Project",
"dependencies": {
"System.ServiceModel.Primitives": "[8.0.0, )",
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.migration": {
"type": "Project",
"dependencies": {
Expand Down
8 changes: 8 additions & 0 deletions src/common/Edelstein.Common.Database.Pgsql/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@
"Edelstein.Protocol.Gameplay": "[1.0.0, )",
"Edelstein.Protocol.Network": "[1.0.0, )",
"Edelstein.Protocol.Services.Auth": "[1.0.0, )",
"Edelstein.Protocol.Services.Dispatch": "[1.0.0, )",
"Edelstein.Protocol.Services.Migration": "[1.0.0, )",
"Edelstein.Protocol.Services.Server": "[1.0.0, )",
"Edelstein.Protocol.Services.Session": "[1.0.0, )",
Expand All @@ -1363,6 +1364,13 @@
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.dispatch": {
"type": "Project",
"dependencies": {
"System.ServiceModel.Primitives": "[8.0.0, )",
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.migration": {
"type": "Project",
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@
"Edelstein.Protocol.Gameplay": "[1.0.0, )",
"Edelstein.Protocol.Network": "[1.0.0, )",
"Edelstein.Protocol.Services.Auth": "[1.0.0, )",
"Edelstein.Protocol.Services.Dispatch": "[1.0.0, )",
"Edelstein.Protocol.Services.Migration": "[1.0.0, )",
"Edelstein.Protocol.Services.Server": "[1.0.0, )",
"Edelstein.Protocol.Services.Session": "[1.0.0, )",
Expand All @@ -1401,6 +1402,13 @@
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.dispatch": {
"type": "Project",
"dependencies": {
"System.ServiceModel.Primitives": "[8.0.0, )",
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.migration": {
"type": "Project",
"dependencies": {
Expand Down
8 changes: 8 additions & 0 deletions src/common/Edelstein.Common.Database/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,7 @@
"Edelstein.Protocol.Gameplay": "[1.0.0, )",
"Edelstein.Protocol.Network": "[1.0.0, )",
"Edelstein.Protocol.Services.Auth": "[1.0.0, )",
"Edelstein.Protocol.Services.Dispatch": "[1.0.0, )",
"Edelstein.Protocol.Services.Migration": "[1.0.0, )",
"Edelstein.Protocol.Services.Server": "[1.0.0, )",
"Edelstein.Protocol.Services.Session": "[1.0.0, )",
Expand All @@ -1114,6 +1115,13 @@
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.dispatch": {
"type": "Project",
"dependencies": {
"System.ServiceModel.Primitives": "[8.0.0, )",
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.migration": {
"type": "Project",
"dependencies": {
Expand Down
8 changes: 8 additions & 0 deletions src/common/Edelstein.Common.Gameplay.Login/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@
"Edelstein.Protocol.Gameplay": "[1.0.0, )",
"Edelstein.Protocol.Network": "[1.0.0, )",
"Edelstein.Protocol.Services.Auth": "[1.0.0, )",
"Edelstein.Protocol.Services.Dispatch": "[1.0.0, )",
"Edelstein.Protocol.Services.Migration": "[1.0.0, )",
"Edelstein.Protocol.Services.Server": "[1.0.0, )",
"Edelstein.Protocol.Services.Session": "[1.0.0, )",
Expand All @@ -1004,6 +1005,13 @@
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.dispatch": {
"type": "Project",
"dependencies": {
"System.ServiceModel.Primitives": "[8.0.0, )",
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.migration": {
"type": "Project",
"dependencies": {
Expand Down
8 changes: 8 additions & 0 deletions src/common/Edelstein.Common.Gameplay/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@
"Edelstein.Protocol.Gameplay": "[1.0.0, )",
"Edelstein.Protocol.Network": "[1.0.0, )",
"Edelstein.Protocol.Services.Auth": "[1.0.0, )",
"Edelstein.Protocol.Services.Dispatch": "[1.0.0, )",
"Edelstein.Protocol.Services.Migration": "[1.0.0, )",
"Edelstein.Protocol.Services.Server": "[1.0.0, )",
"Edelstein.Protocol.Services.Session": "[1.0.0, )",
Expand All @@ -998,6 +999,13 @@
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.dispatch": {
"type": "Project",
"dependencies": {
"System.ServiceModel.Primitives": "[8.0.0, )",
"protobuf-net.Grpc": "[1.1.1, )"
}
},
"edelstein.protocol.services.migration": {
"type": "Project",
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Linq;
using System.Threading.Tasks;
using Edelstein.Protocol.Services.Dispatch;
using Edelstein.Protocol.Services.Dispatch.Contracts;
using Edelstein.Protocol.Services.Session.Contracts;
using ProtoBuf.Grpc;

namespace Edelstein.Common.Services.Dispatch;

public partial class DispatchService
{
public async Task<DispatchServiceResponse> Send(DispatchServiceSendRequest request, CallContext context = default)
{
switch (request.Info.TargetType)
{
case DispatchTarget.All:
await Task.WhenAll(
(await _serverIdIndex.RetrieveAll())
.Select(async e
=> await e.Channel.WriteAsync(request.Info, context.CancellationToken)));
break;
case DispatchTarget.World:
{
var entry = await _worldIdIndex.Retrieve(request.Info.TargetID);

if (entry == null)
return new DispatchServiceResponse
{
Result = DispatchServiceResult.FailedUnknown
};

await entry.Channel.WriteAsync(request.Info, context.CancellationToken);
break;
}
case DispatchTarget.Channel:
{
var entry = await _channelIdIndex.Retrieve(request.Info.TargetID);

if (entry == null)
return new DispatchServiceResponse
{
Result = DispatchServiceResult.FailedUnknown
};

await entry.Channel.WriteAsync(request.Info, context.CancellationToken);
break;
}
case DispatchTarget.Character:
{
var session = await sessions.GetByActiveCharacter(new SessionServiceGetByActiveCharacterRequest
{
CharacterID = request.Info.TargetID
});

if (session.Info != null)
{
var entry = await _serverIdIndex.Retrieve(session.Info.ServerID);

if (entry == null)
return new DispatchServiceResponse
{
Result = DispatchServiceResult.FailedUnknown
};

await entry.Channel.WriteAsync(request.Info, context.CancellationToken);
}
break;
}
default:
return new DispatchServiceResponse
{
Result = DispatchServiceResult.FailedUnknown
};
}

return new DispatchServiceResponse
{
Result = DispatchServiceResult.Success
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Threading.Channels;
using Edelstein.Protocol.Services.Dispatch.Contracts;
using ProtoBuf.Grpc;

namespace Edelstein.Common.Services.Dispatch;

public partial class DispatchService
{
public async IAsyncEnumerable<DispatchInfo> Subscribe(DispatchServiceSubscribeRequest request, CallContext context = default)
{
var channel = Channel.CreateUnbounded<DispatchInfo>();

await _serverIdIndex.Insert(new DispatchServiceEntry<string>(request.ServerID, channel));
if (request.WorldID.HasValue) await _worldIdIndex.Insert(new DispatchServiceEntry<int>(request.WorldID.Value, channel));
if (request.ChannelID.HasValue) await _channelIdIndex.Insert(new DispatchServiceEntry<int>(request.ChannelID.Value, channel));

while (!context.CancellationToken.IsCancellationRequested)
yield return await channel.Reader.ReadAsync(context.CancellationToken);

await _serverIdIndex.Delete(request.ServerID);
if (request.WorldID.HasValue) await _worldIdIndex.Delete(request.WorldID.Value);
if (request.ChannelID.HasValue) await _channelIdIndex.Delete(request.ChannelID.Value);

channel.Writer.Complete();
}
}
13 changes: 13 additions & 0 deletions src/common/Edelstein.Common.Services.Dispatch/DispatchService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Edelstein.Protocol.Services.Dispatch;
using Edelstein.Protocol.Services.Session;

namespace Edelstein.Common.Services.Dispatch;

public partial class DispatchService(
ISessionService sessions
) : IDispatchService
{
private readonly DispatchServiceEntryRepository<string> _serverIdIndex = new();
private readonly DispatchServiceEntryRepository<int> _worldIdIndex = new();
private readonly DispatchServiceEntryRepository<int> _channelIdIndex = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Channels;
using Edelstein.Protocol.Services.Dispatch.Contracts;
using Edelstein.Protocol.Utilities.Repositories;

namespace Edelstein.Common.Services.Dispatch;

public record DispatchServiceEntry<TKey>(
TKey ID,
ChannelWriter<DispatchInfo> Channel
) : IRepositoryEntry<TKey>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Edelstein.Common.Utilities.Repositories;

namespace Edelstein.Common.Services.Dispatch;

public class DispatchServiceEntryRepository<TKey> : Repository<TKey, DispatchServiceEntry<TKey>>
where TKey : notnull;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\protocol\Edelstein.Protocol.Services.Dispatch\Edelstein.Protocol.Services.Dispatch.csproj" />
<ProjectReference Include="..\..\protocol\Edelstein.Protocol.Services.Session\Edelstein.Protocol.Services.Session.csproj" />
<ProjectReference Include="..\..\protocol\Edelstein.Protocol.Utilities\Edelstein.Protocol.Utilities.csproj" />
<ProjectReference Include="..\Edelstein.Common.Utilities\Edelstein.Common.Utilities.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit 90bf31a

Please sign in to comment.