Add ZMQ for dashboard

This commit is contained in:
thesecretmaster 2022-03-22 10:28:57 -05:00
parent 27d2d9acd9
commit aa39386fef
No known key found for this signature in database
GPG Key ID: 22ABA4F624BE98D5
3 changed files with 83 additions and 3 deletions

View File

@ -22,6 +22,10 @@ public class Client : IDisposable {
public Server Server { get; init; }
public Logger Logger { get; }
public (int, int, int) Xyz = (0,0,0);
public bool IsIt = false;
public (ushort, byte) Time = (0, 0);
public Client(Socket socket) {
Socket = socket;
Logger = new Logger("Unknown User");
@ -66,4 +70,4 @@ public class Client : IDisposable {
public static bool operator !=(Client? left, Client? right) {
return !(left == right);
}
}
}

View File

@ -5,6 +5,11 @@ using System.Runtime.InteropServices;
using Shared;
using Shared.Packet;
using Shared.Packet.Packets;
using NetMQ.Sockets;
using NetMQ;
using MsgPack.Serialization;
using MsgPack;
using System.Text;
namespace Server;
@ -23,10 +28,12 @@ public class Server {
Logger.Info($"Listening on {serverSocket.LocalEndPoint}");
Task.Run(() => HandleZMQ());
try {
while (true) {
Socket socket = token.HasValue ? await serverSocket.AcceptAsync(token.Value) : await serverSocket.AcceptAsync();
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}");
@ -255,6 +262,17 @@ public class Server {
// if (header.Type is not PacketType.Cap and not PacketType.Player) client.Logger.Warn($"lol {header.Type}");
IPacket packet = (IPacket) Activator.CreateInstance(Constants.PacketIdMap[header.Type])!;
packet.Deserialize(memory.Memory.Span[Constants.HeaderSize..(Constants.HeaderSize + packet.Size)]);
if (header.Type == PacketType.Player) {
PlayerPacket pp = (PlayerPacket)packet;
client.Xyz = ((Int32)pp.Position.X, (Int32)pp.Position.Y, (Int32)pp.Position.Z);
} else if (header.Type == PacketType.Tag) {
TagPacket tp = (TagPacket)packet;
if (tp.UpdateType == TagPacket.TagUpdate.State) {
client.IsIt = tp.IsIt;
} else if (tp.UpdateType == TagPacket.TagUpdate.Time) {
client.Time = (tp.Minutes, tp.Seconds);
}
}
if (PacketHandler?.Invoke(client, packet) is false) {
memory.Dispose();
continue;
@ -291,4 +309,60 @@ public class Server {
//no need to error check, the client will disconnect when the packet is invalid :)
return MemoryMarshal.Read<PacketHeader>(data);
}
}
public class ClientSerializable {
[MessagePackMember(0)]
public byte[] Guid { get; set; }
[MessagePackMember(1)]
public byte[] Name { get; set; }
[MessagePackMember(2)]
public bool IsIt {get; set; }
[MessagePackMember(3)]
public Int32 Seconds {get; set; }
[MessagePackMember(4)]
public (Int32, Int32, Int32) Xyz { get; set; }
}
private async Task ZMQServerAsync() {
// *:5555 for all bind
using (var responseSocket = new ResponseSocket("tcp://localhost:5555"))
{
var serializer = MessagePackSerializer.Get<List<ClientSerializable>>();
while (true) {
var (message, more) = await responseSocket.ReceiveFrameStringAsync();
switch (message) {
case "WHOWHERE":
Console.WriteLine("GOT WHO");
List<ClientSerializable> clientsSerialized = new List<ClientSerializable>();
foreach (Client c in Clients.FindAll(c => c.Connected)) {
ClientSerializable cs = new ClientSerializable {
Guid = Encoding.ASCII.GetBytes(c.Id.ToString("N")),
Name = Encoding.ASCII.GetBytes(c.Name),
IsIt = c.IsIt,
Seconds = ((int)c.Time.Item1 * 60) + c.Time.Item2,
Xyz = c.Xyz
};
clientsSerialized.Add(cs);
}
var stream = new MemoryStream();
serializer.Pack(stream, clientsSerialized);
var rawObject = Unpacking.UnpackObject(stream.GetBuffer());
Console.WriteLine(rawObject.Value.AsList());
Console.WriteLine("REP WHO");
responseSocket.SendFrame(stream.GetBuffer());
break;
case "WHOSTATS":
responseSocket.SendFrame("World");
break;
}
}
}
}
private async void HandleZMQ() {
using (var runtime = new NetMQRuntime()) {
runtime.Run(ZMQServerAsync());
}
}
}

View File

@ -12,6 +12,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="MsgPack.Cli" Version="1.0.1" />
<PackageReference Include="NetMQ" Version="4.0.1.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>