Initial split of the modules
This commit is contained in:
13
NadekoBot.Core/Common/ShardCom/IShardComMessage.cs
Normal file
13
NadekoBot.Core/Common/ShardCom/IShardComMessage.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using Discord;
|
||||
|
||||
namespace NadekoBot.Common.ShardCom
|
||||
{
|
||||
public class ShardComMessage
|
||||
{
|
||||
public int ShardId { get; set; }
|
||||
public ConnectionState ConnectionState { get; set; }
|
||||
public int Guilds { get; set; }
|
||||
public DateTime Time { get; set; }
|
||||
}
|
||||
}
|
28
NadekoBot.Core/Common/ShardCom/ShardComClient.cs
Normal file
28
NadekoBot.Core/Common/ShardCom/ShardComClient.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Common.ShardCom
|
||||
{
|
||||
public class ShardComClient
|
||||
{
|
||||
private int port;
|
||||
|
||||
public ShardComClient(int port)
|
||||
{
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public async Task Send(ShardComMessage data)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
40
NadekoBot.Core/Common/ShardCom/ShardComServer.cs
Normal file
40
NadekoBot.Core/Common/ShardCom/ShardComServer.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Common.ShardCom
|
||||
{
|
||||
public class ShardComServer : IDisposable
|
||||
{
|
||||
private readonly UdpClient _client;
|
||||
|
||||
public ShardComServer(int port)
|
||||
{
|
||||
_client = new UdpClient(port);
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
public event Func<ShardComMessage, Task> OnDataReceived = delegate { return Task.CompletedTask; };
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user