stop being dum
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
using System.Collections.Concurrent;
|
using MareSynchronosShared.Protos;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -8,6 +11,13 @@ namespace MareSynchronosServices.Identity;
|
|||||||
internal class IdentityHandler
|
internal class IdentityHandler
|
||||||
{
|
{
|
||||||
private readonly ConcurrentDictionary<string, ServerIdentity> cachedIdentities = new();
|
private readonly ConcurrentDictionary<string, ServerIdentity> cachedIdentities = new();
|
||||||
|
private readonly ConcurrentDictionary<string, ConcurrentQueue<IdentChange>> identChanges = new();
|
||||||
|
private readonly ILogger<IdentityHandler> _logger;
|
||||||
|
|
||||||
|
public IdentityHandler(ILogger<IdentityHandler> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
internal Task<string> GetUidForCharacterIdent(string ident, string serverId)
|
internal Task<string> GetUidForCharacterIdent(string ident, string serverId)
|
||||||
{
|
{
|
||||||
@@ -64,6 +74,33 @@ internal class IdentityHandler
|
|||||||
cachedIdentities.TryRemove(identity.Key, out _);
|
cachedIdentities.TryRemove(identity.Key, out _);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void EnqueueIdentChange(IdentChange identchange)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Enqueued " + identchange.UidWithIdent.Uid.Uid + ":" + identchange.IsOnline + " from " + identchange.UidWithIdent.Ident.ServerId);
|
||||||
|
|
||||||
|
foreach (var k in identChanges.Keys)
|
||||||
|
{
|
||||||
|
if (string.Equals(k, identchange.UidWithIdent.Ident.ServerId, System.StringComparison.Ordinal)) continue;
|
||||||
|
identChanges[k].Enqueue(identchange);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal bool DequeueIdentChange(string server, out IdentChange? cur)
|
||||||
|
{
|
||||||
|
if (!(identChanges.ContainsKey(server) && identChanges[server].TryDequeue(out cur)))
|
||||||
|
{
|
||||||
|
cur = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void RegisterServerForQueue(string serverId)
|
||||||
|
{
|
||||||
|
identChanges[serverId] = new ConcurrentQueue<IdentChange>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal record ServerIdentity
|
internal record ServerIdentity
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
using Grpc.Core;
|
using Grpc.Core;
|
||||||
using MareSynchronosShared.Protos;
|
using MareSynchronosShared.Protos;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Runtime.ConstrainedExecution;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MareSynchronosServices.Identity;
|
namespace MareSynchronosServices.Identity;
|
||||||
@@ -12,7 +9,6 @@ internal class IdentityService : IdentificationService.IdentificationServiceBase
|
|||||||
{
|
{
|
||||||
private readonly ILogger<IdentityService> _logger;
|
private readonly ILogger<IdentityService> _logger;
|
||||||
private readonly IdentityHandler _handler;
|
private readonly IdentityHandler _handler;
|
||||||
private readonly ConcurrentDictionary<string, ConcurrentQueue<IdentChange>> identChanges = new();
|
|
||||||
|
|
||||||
public IdentityService(ILogger<IdentityService> logger, IdentityHandler handler)
|
public IdentityService(ILogger<IdentityService> logger, IdentityHandler handler)
|
||||||
{
|
{
|
||||||
@@ -73,13 +69,14 @@ internal class IdentityService : IdentificationService.IdentificationServiceBase
|
|||||||
await requestStream.MoveNext();
|
await requestStream.MoveNext();
|
||||||
var server = requestStream.Current.Server;
|
var server = requestStream.Current.Server;
|
||||||
if (server == null) throw new System.Exception("First message needs to be server message");
|
if (server == null) throw new System.Exception("First message needs to be server message");
|
||||||
|
_handler.RegisterServerForQueue(server.ServerId);
|
||||||
_logger.LogInformation("Registered Server " + server.ServerId + " input stream");
|
_logger.LogInformation("Registered Server " + server.ServerId + " input stream");
|
||||||
identChanges[server.ServerId] = new ConcurrentQueue<IdentChange>();
|
|
||||||
while (await requestStream.MoveNext().ConfigureAwait(false))
|
while (await requestStream.MoveNext().ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
var cur = requestStream.Current.IdentChange;
|
var cur = requestStream.Current.IdentChange;
|
||||||
if (cur == null) throw new System.Exception("Expected client ident change");
|
if (cur == null) throw new System.Exception("Expected client ident change");
|
||||||
EnqueueIdentChange(cur);
|
_handler.EnqueueIdentChange(cur);
|
||||||
|
|
||||||
if (cur.IsOnline)
|
if (cur.IsOnline)
|
||||||
{
|
{
|
||||||
@@ -105,17 +102,9 @@ internal class IdentityService : IdentificationService.IdentificationServiceBase
|
|||||||
{
|
{
|
||||||
while (!context.CancellationToken.IsCancellationRequested)
|
while (!context.CancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
if (identChanges.ContainsKey(server))
|
while (_handler.DequeueIdentChange(server, out var cur))
|
||||||
{
|
{
|
||||||
if (identChanges[server].TryDequeue(out var cur))
|
await responseStream.WriteAsync(cur).ConfigureAwait(false);
|
||||||
{
|
|
||||||
_logger.LogInformation("Sending " + cur.UidWithIdent.Uid.Uid + " to " + server);
|
|
||||||
await responseStream.WriteAsync(cur).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Nothing to send to " + server);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.Delay(250).ConfigureAwait(false);
|
await Task.Delay(250).ConfigureAwait(false);
|
||||||
@@ -151,20 +140,9 @@ internal class IdentityService : IdentificationService.IdentificationServiceBase
|
|||||||
return Task.FromResult(response);
|
return Task.FromResult(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void EnqueueIdentChange(IdentChange identchange)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Enqueued " + identchange.UidWithIdent.Uid.Uid + ":" + identchange.IsOnline + " from " + identchange.UidWithIdent.Ident.ServerId);
|
|
||||||
|
|
||||||
foreach (var k in identChanges.Keys)
|
|
||||||
{
|
|
||||||
if (string.Equals(k, identchange.UidWithIdent.Ident.ServerId, System.StringComparison.Ordinal)) continue;
|
|
||||||
identChanges[k].Enqueue(identchange);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EnqueueIdentOnline(UidWithIdent ident)
|
private void EnqueueIdentOnline(UidWithIdent ident)
|
||||||
{
|
{
|
||||||
EnqueueIdentChange(new IdentChange()
|
_handler.EnqueueIdentChange(new IdentChange()
|
||||||
{
|
{
|
||||||
IsOnline = true,
|
IsOnline = true,
|
||||||
UidWithIdent = ident
|
UidWithIdent = ident
|
||||||
@@ -173,7 +151,7 @@ internal class IdentityService : IdentificationService.IdentificationServiceBase
|
|||||||
|
|
||||||
private void EnqueueIdentOffline(UidWithIdent ident)
|
private void EnqueueIdentOffline(UidWithIdent ident)
|
||||||
{
|
{
|
||||||
EnqueueIdentChange(new IdentChange()
|
_handler.EnqueueIdentChange(new IdentChange()
|
||||||
{
|
{
|
||||||
IsOnline = false,
|
IsOnline = false,
|
||||||
UidWithIdent = ident
|
UidWithIdent = ident
|
||||||
|
|||||||
Reference in New Issue
Block a user