-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
403 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/common/Edelstein.Common.Services.Dispatch/DispatchService.Send.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/common/Edelstein.Common.Services.Dispatch/DispatchService.Subscribe.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/common/Edelstein.Common.Services.Dispatch/DispatchService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/common/Edelstein.Common.Services.Dispatch/DispatchServiceEntry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
6 changes: 6 additions & 0 deletions
6
src/common/Edelstein.Common.Services.Dispatch/DispatchServiceEntryRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
8 changes: 8 additions & 0 deletions
8
src/common/Edelstein.Common.Services.Dispatch/Edelstein.Common.Services.Dispatch.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.