Compare commits
5 Commits
4d0743c4aa
...
master
Author | SHA1 | Date | |
---|---|---|---|
c7904f120d | |||
c80f1f2aa0 | |||
07cb396dd7 | |||
de3455ee8a | |||
6cd8997301 |
@ -7,7 +7,7 @@ namespace HermesSocketServer.Requests
|
|||||||
public class CreateConnection : IRequest
|
public class CreateConnection : IRequest
|
||||||
{
|
{
|
||||||
public string Name => "create_connection";
|
public string Name => "create_connection";
|
||||||
public string[] RequiredKeys => ["name", "type", "clientId", "accessToken", "grantType", "scope", "expiration"];
|
public string[] RequiredKeys => ["name", "type", "client_id", "access_token", "grant_type", "scope", "expiration"];
|
||||||
private ILogger _logger;
|
private ILogger _logger;
|
||||||
|
|
||||||
public CreateConnection(ILogger logger)
|
public CreateConnection(ILogger logger)
|
||||||
@ -19,9 +19,9 @@ namespace HermesSocketServer.Requests
|
|||||||
{
|
{
|
||||||
string name = data["name"].ToString()!;
|
string name = data["name"].ToString()!;
|
||||||
string type = data["type"].ToString()!;
|
string type = data["type"].ToString()!;
|
||||||
string clientId = data["clientId"].ToString()!;
|
string clientId = data["client_id"].ToString()!;
|
||||||
string accessToken = data["accessToken"].ToString()!;
|
string accessToken = data["access_token"].ToString()!;
|
||||||
string grantType = data["grantType"].ToString()!;
|
string grantType = data["grant_type"].ToString()!;
|
||||||
string scope = data["scope"].ToString()!;
|
string scope = data["scope"].ToString()!;
|
||||||
if (!DateTime.TryParse(data["expiration"].ToString()!, out var expiresAt))
|
if (!DateTime.TryParse(data["expiration"].ToString()!, out var expiresAt))
|
||||||
return Task.FromResult(RequestResult.Failed("Expiration needs to be a date time string."));
|
return Task.FromResult(RequestResult.Failed("Expiration needs to be a date time string."));
|
||||||
|
@ -31,6 +31,11 @@ namespace HermesSocketServer.Requests
|
|||||||
Allow = allow,
|
Allow = allow,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (channel.GroupPermissions.Get().Values.Any(p => p.GroupId == groupId && p.Path == path))
|
||||||
|
{
|
||||||
|
return Task.FromResult(RequestResult.Failed("Permission exists already."));
|
||||||
|
}
|
||||||
|
|
||||||
bool result = channel.GroupPermissions.Set(id.ToString(), permission);
|
bool result = channel.GroupPermissions.Set(id.ToString(), permission);
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
|
@ -6,7 +6,7 @@ namespace HermesSocketServer.Requests
|
|||||||
public class DeleteConnection : IRequest
|
public class DeleteConnection : IRequest
|
||||||
{
|
{
|
||||||
public string Name => "delete_connection";
|
public string Name => "delete_connection";
|
||||||
public string[] RequiredKeys => ["id"];
|
public string[] RequiredKeys => ["name"];
|
||||||
private ILogger _logger;
|
private ILogger _logger;
|
||||||
|
|
||||||
public DeleteConnection(ILogger logger)
|
public DeleteConnection(ILogger logger)
|
||||||
@ -16,16 +16,16 @@ namespace HermesSocketServer.Requests
|
|||||||
|
|
||||||
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
||||||
{
|
{
|
||||||
var connectionId = data["id"].ToString()!;
|
var connectionName = data["name"].ToString()!;
|
||||||
|
|
||||||
var result = channel.Connections.Remove(connectionId);
|
var result = channel.Connections.Remove(connectionName);
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
_logger.Information($"Deleted a connection by id [connection id: {connectionId}]");
|
_logger.Information($"Deleted a connection by name [connection name: {connectionName}]");
|
||||||
return Task.FromResult(RequestResult.Successful(null));
|
return Task.FromResult(RequestResult.Successful(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Warning($"Connection Id does not exist [connection id: {connectionId}]");
|
_logger.Warning($"Connection Name does not exist [connection name: {connectionName}]");
|
||||||
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
|
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,12 @@ namespace HermesSocketServer.Requests
|
|||||||
{
|
{
|
||||||
var id = data["id"].ToString()!;
|
var id = data["id"].ToString()!;
|
||||||
|
|
||||||
|
var permission = channel.GroupPermissions.Get(id);
|
||||||
var result = channel.GroupPermissions.Remove(id);
|
var result = channel.GroupPermissions.Remove(id);
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
_logger.Information($"Deleted a group permission by id [group permission id: {id}]");
|
_logger.Information($"Deleted a group permission by id [group permission id: {id}]");
|
||||||
return Task.FromResult(RequestResult.Successful(null));
|
return Task.FromResult(RequestResult.Successful(permission));
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Warning($"Group Permission Id does not exist [group permission id: {id}]");
|
_logger.Warning($"Group Permission Id does not exist [group permission id: {id}]");
|
||||||
|
@ -31,6 +31,11 @@ namespace HermesSocketServer.Requests
|
|||||||
Allow = allow,
|
Allow = allow,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (!channel.GroupPermissions.Get().Values.Any(p => p.GroupId == groupId && p.Path == path))
|
||||||
|
{
|
||||||
|
return Task.FromResult(RequestResult.Failed("Permission does not exist."));
|
||||||
|
}
|
||||||
|
|
||||||
bool result = channel.GroupPermissions.Modify(id.ToString(), permission);
|
bool result = channel.GroupPermissions.Modify(id.ToString(), permission);
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
|
@ -14,7 +14,7 @@ namespace HermesSocketServer.Services
|
|||||||
private readonly ServerConfiguration _configuration;
|
private readonly ServerConfiguration _configuration;
|
||||||
private readonly Serilog.ILogger _logger;
|
private readonly Serilog.ILogger _logger;
|
||||||
private readonly IDictionary<string, Channel> _channels;
|
private readonly IDictionary<string, Channel> _channels;
|
||||||
private readonly object _lock;
|
private readonly Mutex _mutex;
|
||||||
|
|
||||||
public ChannelManager(IStore<string, User> users, Database database, IStore<string, TTSVoice> voices, ServerConfiguration configuration, Serilog.ILogger logger)
|
public ChannelManager(IStore<string, User> users, Database database, IStore<string, TTSVoice> voices, ServerConfiguration configuration, Serilog.ILogger logger)
|
||||||
{
|
{
|
||||||
@ -24,7 +24,7 @@ namespace HermesSocketServer.Services
|
|||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_channels = new ConcurrentDictionary<string, Channel>();
|
_channels = new ConcurrentDictionary<string, Channel>();
|
||||||
_lock = new object();
|
_mutex = new Mutex();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -36,15 +36,16 @@ namespace HermesSocketServer.Services
|
|||||||
|
|
||||||
return await Task.Run(() =>
|
return await Task.Run(() =>
|
||||||
{
|
{
|
||||||
lock (_lock)
|
try
|
||||||
{
|
{
|
||||||
|
_mutex.WaitOne();
|
||||||
if (_channels.TryGetValue(userId, out var channel))
|
if (_channels.TryGetValue(userId, out var channel))
|
||||||
return Task.FromResult<Channel?>(channel);
|
return channel;
|
||||||
|
|
||||||
var actionTable = _configuration.Database.Tables["Action"];
|
var actionTable = _configuration.Database.Tables["Action"];
|
||||||
var chatterTable = _configuration.Database.Tables["Chatter"];
|
var chatterTable = _configuration.Database.Tables["Chatter"];
|
||||||
var connectionTable = _configuration.Database.Tables["Connection"];
|
var connectionTable = _configuration.Database.Tables["Connection"];
|
||||||
//var chatterGroupTable = _configuration.Database.Tables["ChatterGroup"];
|
var connectionStateTable = _configuration.Database.Tables["ConnectionState"];
|
||||||
var groupTable = _configuration.Database.Tables["Group"];
|
var groupTable = _configuration.Database.Tables["Group"];
|
||||||
var groupPermissionTable = _configuration.Database.Tables["GroupPermission"];
|
var groupPermissionTable = _configuration.Database.Tables["GroupPermission"];
|
||||||
var policyTable = _configuration.Database.Tables["Policy"];
|
var policyTable = _configuration.Database.Tables["Policy"];
|
||||||
@ -93,22 +94,43 @@ namespace HermesSocketServer.Services
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
_channels.Add(userId, channel);
|
_channels.Add(userId, channel);
|
||||||
return Task.FromResult<Channel?>(channel);
|
return channel;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_mutex.ReleaseMutex();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public Channel? Get(string channelId)
|
public Channel? Get(string channelId)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_mutex.WaitOne();
|
||||||
if (_channels.TryGetValue(channelId, out var channel))
|
if (_channels.TryGetValue(channelId, out var channel))
|
||||||
return channel;
|
return channel;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_mutex.ReleaseMutex();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async Task Save(string userId)
|
public async Task Save(string userId)
|
||||||
{
|
{
|
||||||
if (!_channels.TryGetValue(userId, out var channel))
|
Channel? channel;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_mutex.WaitOne();
|
||||||
|
if (!_channels.TryGetValue(userId, out channel))
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_mutex.ReleaseMutex();
|
||||||
|
}
|
||||||
|
|
||||||
_logger.Debug($"Saving channel data to database [channel id: {channel.Id}][channel name: {channel.User.Name}]");
|
_logger.Debug($"Saving channel data to database [channel id: {channel.Id}][channel name: {channel.User.Name}]");
|
||||||
await Task.WhenAll([
|
await Task.WhenAll([
|
||||||
|
@ -14,7 +14,7 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
private readonly long[] _array;
|
private readonly long[] _array;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
private readonly object _lock;
|
private readonly Mutex _lock;
|
||||||
private int _index;
|
private int _index;
|
||||||
|
|
||||||
public ChatterHandler(Database database, ILogger logger)
|
public ChatterHandler(Database database, ILogger logger)
|
||||||
@ -24,7 +24,7 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
_chatters = new HashSet<long>(CHATTER_BUFFER_SIZE);
|
_chatters = new HashSet<long>(CHATTER_BUFFER_SIZE);
|
||||||
_array = new long[CHATTER_BUFFER_SIZE];
|
_array = new long[CHATTER_BUFFER_SIZE];
|
||||||
_index = -1;
|
_index = -1;
|
||||||
_lock = new object();
|
_lock = new Mutex();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Execute<T>(WebSocketUser sender, T message, HermesSocketManager sockets)
|
public async Task Execute<T>(WebSocketUser sender, T message, HermesSocketManager sockets)
|
||||||
@ -32,8 +32,9 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
if (message is not ChatterMessage data || sender.Id == null)
|
if (message is not ChatterMessage data || sender.Id == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
lock (_lock)
|
try
|
||||||
{
|
{
|
||||||
|
_lock.WaitOne();
|
||||||
if (_chatters.Contains(data.Id))
|
if (_chatters.Contains(data.Id))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -43,11 +44,16 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
_index = -1;
|
_index = -1;
|
||||||
|
|
||||||
var previous = _array[++_index];
|
var previous = _array[++_index];
|
||||||
if (previous != 0) {
|
if (previous != 0)
|
||||||
|
{
|
||||||
_chatters.Remove(previous);
|
_chatters.Remove(previous);
|
||||||
}
|
}
|
||||||
_array[_index] = data.Id;
|
_array[_index] = data.Id;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_lock.ReleaseMutex();
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -14,7 +14,7 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
private readonly HashSet<string> _emotes;
|
private readonly HashSet<string> _emotes;
|
||||||
private readonly string[] _array;
|
private readonly string[] _array;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly object _lock;
|
private readonly Mutex _mutex;
|
||||||
|
|
||||||
private int _index;
|
private int _index;
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
_emotes = new HashSet<string>(EMOTE_BUFFER_SIZE);
|
_emotes = new HashSet<string>(EMOTE_BUFFER_SIZE);
|
||||||
_array = new string[EMOTE_BUFFER_SIZE];
|
_array = new string[EMOTE_BUFFER_SIZE];
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_lock = new object();
|
_mutex = new Mutex();
|
||||||
_index = -1;
|
_index = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,8 +36,9 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
if (data.Emotes == null || !data.Emotes.Any())
|
if (data.Emotes == null || !data.Emotes.Any())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
lock (_lock)
|
try
|
||||||
{
|
{
|
||||||
|
_mutex.WaitOne();
|
||||||
foreach (var entry in data.Emotes)
|
foreach (var entry in data.Emotes)
|
||||||
{
|
{
|
||||||
if (_emotes.Contains(entry.Key))
|
if (_emotes.Contains(entry.Key))
|
||||||
@ -59,6 +60,10 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
_array[_index] = entry.Key;
|
_array[_index] = entry.Key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_mutex.ReleaseMutex();
|
||||||
|
}
|
||||||
|
|
||||||
if (!data.Emotes.Any())
|
if (!data.Emotes.Any())
|
||||||
return;
|
return;
|
||||||
|
@ -13,7 +13,7 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
private readonly HashSet<string> _history;
|
private readonly HashSet<string> _history;
|
||||||
private readonly EmoteUsageMessage[] _array;
|
private readonly EmoteUsageMessage[] _array;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly object _lock;
|
private readonly Mutex _mutex;
|
||||||
|
|
||||||
private int _index;
|
private int _index;
|
||||||
|
|
||||||
@ -21,9 +21,9 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
{
|
{
|
||||||
_database = database;
|
_database = database;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_history = new HashSet<string>(101);
|
_history = new HashSet<string>(1001);
|
||||||
_array = new EmoteUsageMessage[100];
|
_array = new EmoteUsageMessage[1000];
|
||||||
_lock = new object();
|
_mutex = new Mutex();
|
||||||
_index = -1;
|
_index = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
if (message is not EmoteUsageMessage data || sender.Id == null)
|
if (message is not EmoteUsageMessage data || sender.Id == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
lock (_lock)
|
try
|
||||||
{
|
{
|
||||||
if (_history.Contains(data.MessageId))
|
if (_history.Contains(data.MessageId))
|
||||||
{
|
{
|
||||||
@ -50,6 +50,10 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
|
|
||||||
_array[_index] = data;
|
_array[_index] = data;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_mutex.ReleaseMutex();
|
||||||
|
}
|
||||||
|
|
||||||
int rows = 0;
|
int rows = 0;
|
||||||
string sql = "INSERT INTO \"EmoteUsageHistory\" (timestamp, \"broadcasterId\", \"emoteId\", \"chatterId\") VALUES (@time, @broadcaster, @emote, @chatter)";
|
string sql = "INSERT INTO \"EmoteUsageHistory\" (timestamp, \"broadcasterId\", \"emoteId\", \"chatterId\") VALUES (@time, @broadcaster, @emote, @chatter)";
|
||||||
|
@ -17,7 +17,7 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
private readonly Database _database;
|
private readonly Database _database;
|
||||||
private readonly HermesSocketManager _sockets;
|
private readonly HermesSocketManager _sockets;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly object _lock;
|
private readonly SemaphoreSlim _semaphore;
|
||||||
|
|
||||||
public HermesLoginHandler(ChannelManager manager, IStore<string, TTSVoice> voices, ServerConfiguration configuration, Database database, HermesSocketManager sockets, ILogger logger)
|
public HermesLoginHandler(ChannelManager manager, IStore<string, TTSVoice> voices, ServerConfiguration configuration, Database database, HermesSocketManager sockets, ILogger logger)
|
||||||
{
|
{
|
||||||
@ -27,7 +27,7 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
_database = database;
|
_database = database;
|
||||||
_sockets = sockets;
|
_sockets = sockets;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_lock = new object();
|
_semaphore = new SemaphoreSlim(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -46,15 +46,15 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
IEnumerable<WebSocketUser?> recipients = Enumerable.Empty<WebSocketUser?>();
|
IEnumerable<WebSocketUser?> recipients = Enumerable.Empty<WebSocketUser?>();
|
||||||
lock (_lock)
|
try
|
||||||
{
|
{
|
||||||
|
await _semaphore.WaitAsync();
|
||||||
if (sender.Id != null)
|
if (sender.Id != null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
sender.Id = userId;
|
sender.Id = userId;
|
||||||
recipients = _sockets.GetSockets(userId).ToList().Where(s => s.SessionId != sender.SessionId);
|
|
||||||
sender.Slave = data.WebLogin || recipients.Where(r => r != null && !r.WebLogin).Any();
|
sender.Slave = data.WebLogin || recipients.Where(r => r != null && !r.WebLogin).Any();
|
||||||
}
|
recipients = _sockets.GetSockets(userId).ToList().Where(s => s.SessionId != sender.SessionId);
|
||||||
|
|
||||||
sender.ApiKey = data.ApiKey;
|
sender.ApiKey = data.ApiKey;
|
||||||
sender.WebLogin = data.WebLogin;
|
sender.WebLogin = data.WebLogin;
|
||||||
@ -131,5 +131,10 @@ namespace HermesSocketServer.Socket.Handlers
|
|||||||
}
|
}
|
||||||
await Task.WhenAll(tasks);
|
await Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_semaphore.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -30,7 +30,8 @@ namespace HermesSocketServer.Store
|
|||||||
await _database.Execute(sql, data, (reader) =>
|
await _database.Execute(sql, data, (reader) =>
|
||||||
{
|
{
|
||||||
var chatterId = reader.GetInt32(0).ToString();
|
var chatterId = reader.GetInt32(0).ToString();
|
||||||
lock (_lock)
|
_rwls.EnterWriteLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
_store.Add(chatterId, new GroupChatter()
|
_store.Add(chatterId, new GroupChatter()
|
||||||
{
|
{
|
||||||
@ -40,6 +41,10 @@ namespace HermesSocketServer.Store
|
|||||||
ChatterLabel = reader.GetString(1),
|
ChatterLabel = reader.GetString(1),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitWriteLock();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
_logger.Information($"Loaded {_store.Count} group chatters from database [group id: {_groupId}]");
|
_logger.Information($"Loaded {_store.Count} group chatters from database [group id: {_groupId}]");
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ namespace HermesSocketServer.Store
|
|||||||
Default = reader.GetBoolean(7),
|
Default = reader.GetBoolean(7),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
_logger.Information($"Loaded {_store.Count} groups from database.");
|
_logger.Information($"Loaded {_store.Count} connections from database.");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnInitialAdd(string key, Connection value)
|
protected override void OnInitialAdd(string key, Connection value)
|
||||||
|
@ -67,7 +67,7 @@ namespace HermesSocketServer.Store
|
|||||||
{
|
{
|
||||||
List<Task> tasks = _chatters.Values.Select(c => c.Save()).ToList();
|
List<Task> tasks = _chatters.Values.Select(c => c.Save()).ToList();
|
||||||
tasks.Add(base.Save());
|
tasks.Add(base.Save());
|
||||||
await Task.WhenAll(base.Save());
|
await Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnInitialAdd(string key, Group value)
|
protected override void OnInitialAdd(string key, Group value)
|
||||||
|
@ -2,6 +2,7 @@ namespace HermesSocketServer.Store
|
|||||||
{
|
{
|
||||||
public interface IStore<K, V>
|
public interface IStore<K, V>
|
||||||
{
|
{
|
||||||
|
bool Exists(K key);
|
||||||
V? Get(K key);
|
V? Get(K key);
|
||||||
IDictionary<K, V> Get();
|
IDictionary<K, V> Get();
|
||||||
Task Load();
|
Task Load();
|
||||||
|
@ -46,14 +46,27 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
private async Task GenerateQuery(IList<K> keys, Func<int, string> generate, Func<string, IEnumerable<K>, IEnumerable<V>, Task<int>> execute)
|
private async Task GenerateQuery(IList<K> keys, Func<int, string> generate, Func<string, IEnumerable<K>, IEnumerable<V>, Task<int>> execute)
|
||||||
{
|
{
|
||||||
ImmutableList<K>? list = null;
|
ImmutableList<K>? list = null;
|
||||||
lock (_lock)
|
_rwls.EnterUpgradeableReadLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (!keys.Any())
|
if (!keys.Any())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
_rwls.EnterWriteLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
list = keys.ToImmutableList();
|
list = keys.ToImmutableList();
|
||||||
keys.Clear();
|
keys.Clear();
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitUpgradeableReadLock();
|
||||||
|
}
|
||||||
|
|
||||||
var query = generate(list.Count);
|
var query = generate(list.Count);
|
||||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||||
|
@ -51,14 +51,27 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
private async Task GenerateQuery(IList<K> keys, Func<int, string> generate, Func<string, IEnumerable<K>, IEnumerable<V?>, Task<int>> execute)
|
private async Task GenerateQuery(IList<K> keys, Func<int, string> generate, Func<string, IEnumerable<K>, IEnumerable<V?>, Task<int>> execute)
|
||||||
{
|
{
|
||||||
ImmutableList<K>? list = null;
|
ImmutableList<K>? list = null;
|
||||||
lock (_lock)
|
_rwls.EnterUpgradeableReadLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (!keys.Any())
|
if (!keys.Any())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
_rwls.EnterWriteLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
list = keys.ToImmutableList();
|
list = keys.ToImmutableList();
|
||||||
keys.Clear();
|
keys.Clear();
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitUpgradeableReadLock();
|
||||||
|
}
|
||||||
|
|
||||||
var query = generate(list.Count);
|
var query = generate(list.Count);
|
||||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||||
@ -77,16 +90,28 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
private async Task GenerateDeleteQuery(IList<K> keys, IList<V> values, Func<int, string> generate, Func<string, IEnumerable<V>, Task<int>> execute)
|
private async Task GenerateDeleteQuery(IList<K> keys, IList<V> values, Func<int, string> generate, Func<string, IEnumerable<V>, Task<int>> execute)
|
||||||
{
|
{
|
||||||
ImmutableList<V>? list = null;
|
ImmutableList<V>? list = null;
|
||||||
lock (_lock)
|
_rwls.EnterUpgradeableReadLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (!keys.Any() || !values.Any()) {
|
if (!keys.Any() || !values.Any())
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
|
_rwls.EnterWriteLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
list = values.ToImmutableList();
|
list = values.ToImmutableList();
|
||||||
values.Clear();
|
values.Clear();
|
||||||
keys.Clear();
|
keys.Clear();
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitUpgradeableReadLock();
|
||||||
|
}
|
||||||
|
|
||||||
var query = generate(list.Count);
|
var query = generate(list.Count);
|
||||||
int rowsAffected = await execute(query, list);
|
int rowsAffected = await execute(query, list);
|
||||||
|
@ -95,15 +95,14 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
|
|
||||||
var ctp = columns.ToDictionary(c => c, c => _columnPropertyRelations[c]);
|
var ctp = columns.ToDictionary(c => c, c => _columnPropertyRelations[c]);
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
var columnsLower = columns.Select(c => c.ToLower());
|
|
||||||
sb.Append($"INSERT INTO \"{table}\" (\"{string.Join("\", \"", columns)}\") VALUES ");
|
sb.Append($"INSERT INTO \"{table}\" (\"{string.Join("\", \"", columns)}\") VALUES ");
|
||||||
for (var row = 0; row < rows; row++)
|
for (var row = 0; row < rows; row++)
|
||||||
{
|
{
|
||||||
sb.Append("(");
|
sb.Append("(");
|
||||||
foreach (var column in columnsLower)
|
foreach (var column in columns)
|
||||||
{
|
{
|
||||||
sb.Append('@')
|
sb.Append('@')
|
||||||
.Append(column)
|
.Append(column.ToLower())
|
||||||
.Append(row);
|
.Append(row);
|
||||||
|
|
||||||
if (typeMapping.TryGetValue(column, out var type))
|
if (typeMapping.TryGetValue(column, out var type))
|
||||||
|
@ -10,7 +10,7 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
protected readonly IList<K> _added;
|
protected readonly IList<K> _added;
|
||||||
protected readonly IList<K> _modified;
|
protected readonly IList<K> _modified;
|
||||||
protected readonly IList<K> _deleted;
|
protected readonly IList<K> _deleted;
|
||||||
protected readonly object _lock;
|
protected readonly ReaderWriterLockSlim _rwls;
|
||||||
|
|
||||||
|
|
||||||
public GroupSaveStore()
|
public GroupSaveStore()
|
||||||
@ -19,7 +19,7 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
_added = new List<K>();
|
_added = new List<K>();
|
||||||
_modified = new List<K>();
|
_modified = new List<K>();
|
||||||
_deleted = new List<K>();
|
_deleted = new List<K>();
|
||||||
_lock = new object();
|
_rwls = new ReaderWriterLockSlim();
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract Task Load();
|
public abstract Task Load();
|
||||||
@ -28,22 +28,46 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
protected abstract void OnPostRemove(K key, V value);
|
protected abstract void OnPostRemove(K key, V value);
|
||||||
public abstract Task Save();
|
public abstract Task Save();
|
||||||
|
|
||||||
|
|
||||||
|
public bool Exists(K key)
|
||||||
|
{
|
||||||
|
_rwls.EnterReadLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _store.ContainsKey(key);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitReadLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public V? Get(K key)
|
public V? Get(K key)
|
||||||
{
|
{
|
||||||
lock (_lock)
|
_rwls.EnterReadLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (_store.TryGetValue(key, out var value))
|
if (_store.TryGetValue(key, out var value))
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitReadLock();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDictionary<K, V> Get()
|
public IDictionary<K, V> Get()
|
||||||
{
|
{
|
||||||
lock (_lock)
|
_rwls.EnterReadLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return _store.ToImmutableDictionary();
|
return _store.ToImmutableDictionary();
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitReadLock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Modify(K? key, V value)
|
public bool Modify(K? key, V value)
|
||||||
@ -51,9 +75,13 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
if (key == null)
|
if (key == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
lock (_lock)
|
_rwls.EnterUpgradeableReadLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (_store.TryGetValue(key, out V? oldValue))
|
if (_store.TryGetValue(key, out V? oldValue))
|
||||||
|
{
|
||||||
|
_rwls.EnterWriteLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
OnInitialModify(key, oldValue, value);
|
OnInitialModify(key, oldValue, value);
|
||||||
_store[key] = value;
|
_store[key] = value;
|
||||||
@ -63,6 +91,15 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitUpgradeableReadLock();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -72,9 +109,13 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
if (key == null)
|
if (key == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
lock (_lock)
|
_rwls.EnterUpgradeableReadLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (_store.TryGetValue(key, out V? value))
|
if (_store.TryGetValue(key, out V? value))
|
||||||
|
{
|
||||||
|
_rwls.EnterWriteLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
modify(value);
|
modify(value);
|
||||||
if (!_added.Contains(key) && !_modified.Contains(key))
|
if (!_added.Contains(key) && !_modified.Contains(key))
|
||||||
@ -83,6 +124,15 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitUpgradeableReadLock();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -92,12 +142,12 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
if (key == null)
|
if (key == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
lock (_lock)
|
_rwls.EnterWriteLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (_store.TryGetValue(key, out var value))
|
if (_store.TryGetValue(key, out var value))
|
||||||
{
|
{
|
||||||
if (_store.Remove(key))
|
_store.Remove(key);
|
||||||
{
|
|
||||||
OnPostRemove(key, value);
|
OnPostRemove(key, value);
|
||||||
if (!_added.Remove(key))
|
if (!_added.Remove(key))
|
||||||
{
|
{
|
||||||
@ -110,6 +160,9 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitWriteLock();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -119,11 +172,10 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
if (key == null)
|
if (key == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
lock (_lock)
|
_rwls.EnterWriteLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (_store.TryGetValue(key, out V? fetched))
|
if (_store.TryGetValue(key, out V? fetched))
|
||||||
{
|
|
||||||
if (fetched != value)
|
|
||||||
{
|
{
|
||||||
OnInitialModify(key, fetched, value);
|
OnInitialModify(key, fetched, value);
|
||||||
_store[key] = value;
|
_store[key] = value;
|
||||||
@ -133,7 +185,6 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
OnInitialAdd(key, value);
|
OnInitialAdd(key, value);
|
||||||
@ -145,7 +196,10 @@ namespace HermesSocketServer.Store.Internal
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
finally
|
||||||
|
{
|
||||||
|
_rwls.ExitWriteLock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user