Typereaders finished, cleanup

This commit is contained in:
Master Kwoth
2017-10-10 00:04:02 +02:00
parent 3d3871f903
commit 0bacb1f780
19 changed files with 436 additions and 274 deletions

View File

@ -3,26 +3,26 @@ using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NadekoBot.Services;
using StackExchange.Redis;
namespace NadekoBot.Common.ShardCom
{
public class ShardComClient
{
private int port;
private readonly IDataCache _cache;
public ShardComClient(int port)
public ShardComClient(IDataCache cache)
{
this.port = port;
_cache = cache;
}
public async Task Send(ShardComMessage data)
{
var sub = _cache.Redis.GetSubscriber();
var msg = JsonConvert.SerializeObject(data);
using (var client = new UdpClient())
{
var bytes = Encoding.UTF8.GetBytes(msg);
await client.SendAsync(bytes, bytes.Length, IPAddress.Loopback.ToString(), port).ConfigureAwait(false);
}
await sub.PublishAsync("shardcoord_send", msg).ConfigureAwait(false);
}
}
}

View File

@ -4,35 +4,26 @@ using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NadekoBot.Services;
namespace NadekoBot.Common.ShardCom
{
public class ShardComServer : IDisposable
public class ShardComServer
{
private readonly UdpClient _client;
private readonly IDataCache _cache;
public ShardComServer(int port)
public ShardComServer(IDataCache cache)
{
_client = new UdpClient(port);
_cache = cache;
}
public void Start()
{
Task.Run(async () =>
var sub = _cache.Redis.GetSubscriber();
sub.SubscribeAsync("shardcoord_send", (ch, data) =>
{
var ip = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
var recv = await _client.ReceiveAsync();
var data = Encoding.UTF8.GetString(recv.Buffer);
var _ = OnDataReceived(JsonConvert.DeserializeObject<ShardComMessage>(data));
}
});
}
public void Dispose()
{
_client.Dispose();
var _ = OnDataReceived(JsonConvert.DeserializeObject<ShardComMessage>(data));
}, StackExchange.Redis.CommandFlags.FireAndForget);
}
public event Func<ShardComMessage, Task> OnDataReceived = delegate { return Task.CompletedTask; };

View File

@ -9,7 +9,7 @@ using Discord.WebSocket;
namespace NadekoBot.Common.TypeReaders
{
public class CommandTypeReader : NadekoTypeReader
public class CommandTypeReader : NadekoTypeReader<CommandInfo>
{
public CommandTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{
@ -35,10 +35,14 @@ namespace NadekoBot.Common.TypeReaders
}
}
public class CommandOrCrTypeReader : CommandTypeReader
public class CommandOrCrTypeReader : NadekoTypeReader<CommandOrCrInfo>
{
private readonly DiscordSocketClient _client;
private readonly CommandService _cmds;
public CommandOrCrTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{
_client = client;
_cmds = cmds;
}
public override async Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
@ -63,7 +67,7 @@ namespace NadekoBot.Common.TypeReaders
}
}
var cmd = await base.Read(context, input, services);
var cmd = await new CommandTypeReader(_client, _cmds).Read(context, input, services);
if (cmd.IsSuccess)
{
return TypeReaderResult.FromSuccess(new CommandOrCrInfo(((CommandInfo)cmd.Values.First().Value).Name));

View File

@ -7,7 +7,7 @@ using Discord.WebSocket;
namespace NadekoBot.Common.TypeReaders
{
public class GuildDateTimeTypeReader : NadekoTypeReader
public class GuildDateTimeTypeReader : NadekoTypeReader<GuildDateTime>
{
public GuildDateTimeTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{

View File

@ -4,10 +4,11 @@ using System.Threading.Tasks;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Core.Common.TypeReaders;
using Discord;
namespace NadekoBot.Common.TypeReaders
{
public class GuildTypeReader : NadekoTypeReader
public class GuildTypeReader : NadekoTypeReader<IGuild>
{
private readonly DiscordSocketClient _client;

View File

@ -8,7 +8,7 @@ using Discord.WebSocket;
namespace NadekoBot.Common.TypeReaders
{
public class ModuleTypeReader : NadekoTypeReader
public class ModuleTypeReader : NadekoTypeReader<ModuleInfo>
{
private readonly CommandService _cmds;
@ -28,7 +28,7 @@ namespace NadekoBot.Common.TypeReaders
}
}
public class ModuleOrCrTypeReader : NadekoTypeReader
public class ModuleOrCrTypeReader : NadekoTypeReader<ModuleOrCrInfo>
{
private readonly CommandService _cmds;

View File

@ -3,7 +3,8 @@ using Discord.WebSocket;
namespace NadekoBot.Core.Common.TypeReaders
{
public abstract class NadekoTypeReader : TypeReader
public abstract class NadekoTypeReader<T> : TypeReader where
T : class
{
private readonly DiscordSocketClient _client;
private readonly CommandService _cmds;

View File

@ -10,7 +10,7 @@ namespace NadekoBot.Common.TypeReaders
/// <summary>
/// Used instead of bool for more flexible keywords for true/false only in the permission module
/// </summary>
public class PermissionActionTypeReader : NadekoTypeReader
public class PermissionActionTypeReader : NadekoTypeReader<PermissionAction>
{
public PermissionActionTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{