Server overhaul, might rewrite though

This commit is contained in:
Sanae 2022-02-04 03:45:38 -06:00
parent cdad20ddd2
commit de16e4a787
16 changed files with 469 additions and 359 deletions

View File

@ -1,4 +1,5 @@
using System.Buffers; using System.Buffers;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Shared; using Shared;
@ -12,12 +13,16 @@ public class Server {
public readonly List<Client> Clients = new List<Client>(); public readonly List<Client> Clients = new List<Client>();
public readonly Logger Logger = new Logger("Server"); public readonly Logger Logger = new Logger("Server");
public async Task Listen(ushort port) { public async Task Listen(ushort port) {
TcpListener listener = TcpListener.Create(port); Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Loopback, port));
serverSocket.Listen();
listener.Start(); Logger.Info($"Listening on port {port}");
while (true) { while (true) {
Socket socket = await listener.AcceptSocketAsync(); Socket socket = await serverSocket.AcceptAsync();
Logger.Warn("ok");
if (Clients.Count > Constants.MaxClients) { if (Clients.Count > Constants.MaxClients) {
Logger.Warn("Turned away client due to max clients"); Logger.Warn("Turned away client due to max clients");
@ -37,13 +42,12 @@ public class Server {
} }
// broadcast packets to all clients // broadcast packets to all clients
public async void Broadcast<T>(T packet, Client? sender = null) where T : unmanaged, IPacket { public async Task Broadcast<T>(T packet, Client? sender = null) where T : unmanaged, IPacket {
IMemoryOwner<byte> memory = memoryPool.Rent(Marshal.SizeOf<T>() + Constants.HeaderSize); IMemoryOwner<byte> memory = memoryPool.Rent(Marshal.SizeOf<T>() + Constants.HeaderSize);
PacketHeader header = new PacketHeader { PacketHeader header = new PacketHeader {
Id = sender?.Id ?? Guid.Empty, Id = sender?.Id ?? Guid.Empty,
Type = Constants.Packets[typeof(T)].Type, Type = Constants.Packets[typeof(T)].Type,
Sender = PacketSender.Server // todo maybe use client?
}; };
FillPacket(header, packet, memory.Memory); FillPacket(header, packet, memory.Memory);
await Broadcast(memory, sender); await Broadcast(memory, sender);
@ -98,17 +102,10 @@ public class Server {
ConnectPacket connect = MemoryMarshal.Read<ConnectPacket>(memory.Memory.Span[Constants.HeaderSize..size]); ConnectPacket connect = MemoryMarshal.Read<ConnectPacket>(memory.Memory.Span[Constants.HeaderSize..size]);
lock (Clients) { lock (Clients) {
bool firstConn = false;
switch (connect.ConnectionType) { switch (connect.ConnectionType) {
case ConnectionTypes.FirstConnection: { case ConnectionTypes.FirstConnection: {
// do any cleanup required when it comes to new clients firstConn = true;
List<Client> toDisconnect = Clients.FindAll(c => c.Id == header.Id && c.Connected && c.Socket != null);
Clients.RemoveAll(c => c.Id == header.Id);
client.Id = header.Id;
Clients.Add(client);
foreach (Client c in toDisconnect) c.Socket!.DisconnectAsync(false);
// done disconnecting and removing stale clients with the same id
break; break;
} }
case ConnectionTypes.Reconnecting: { case ConnectionTypes.Reconnecting: {
@ -117,32 +114,37 @@ public class Server {
newClient.Socket = client.Socket; newClient.Socket = client.Socket;
client = newClient; client = newClient;
} else { } else {
// TODO MAJOR: IF CLIENT COULD NOT BE FOUND, SERVER SHOULD GRACEFULLY TELL CLIENT THAT ON DISCONNECT firstConn = true;
// can be done via disconnect packet when that gets more data?
throw new Exception("Could not find a suitable client to reconnect as");
} }
break; break;
} }
default: default:
throw new Exception($"Invalid connection type {connect.ConnectionType}"); throw new Exception($"Invalid connection type {connect.ConnectionType}");
} }
if (firstConn) {
// do any cleanup required when it comes to new clients
List<Client> toDisconnect = Clients.FindAll(c => c.Id == header.Id && c.Connected && c.Socket != null);
Clients.RemoveAll(c => c.Id == header.Id);
client.Id = header.Id;
Clients.Add(client);
Parallel.ForEachAsync(toDisconnect, (c, token) => c.Socket!.DisconnectAsync(false, token));
// Broadcast(connect, client);
// done disconnecting and removing stale clients with the same id
}
} }
Logger.Info($"Client {socket.RemoteEndPoint} connected."); Logger.Info($"Client {socket.RemoteEndPoint} connected.");
continue; // continue;
} }
// todo support variable length packets when they show up // todo support variable length packets if they show up
if (header.Sender == PacketSender.Client) { // Logger.Warn($"broadcasting {header.Type}");
Logger.Warn($"broadcasting {header.Type}");
await Broadcast(memory, client); await Broadcast(memory, client);
} }
else {
//todo handle server packets :)
}
}
} }
catch (Exception e) { catch (Exception e) {
if (e is SocketException {SocketErrorCode: SocketError.ConnectionReset}) { if (e is SocketException {SocketErrorCode: SocketError.ConnectionReset}) {
@ -154,7 +156,11 @@ public class Server {
memory?.Dispose(); memory?.Dispose();
} }
Clients.Remove(client);
Broadcast(new DisconnectPacket(), client).ContinueWith(_ => {
client.Dispose(); client.Dispose();
});
} }
private static PacketHeader GetHeader(Span<byte> data) { private static PacketHeader GetHeader(Span<byte> data) {

View File

@ -8,7 +8,7 @@ namespace Shared;
public static class Constants { public static class Constants {
public const int MaxPacketSize = 256; public const int MaxPacketSize = 256;
public const int MaxClients = 4; public const int MaxClients = 4;
public static int HeaderSize => Marshal.SizeOf<PacketHeader>(); public static int HeaderSize { get; } = Marshal.SizeOf<PacketHeader>();
// dictionary of packet types to packet // dictionary of packet types to packet
public static readonly Dictionary<Type, PacketAttribute> Packets = Assembly public static readonly Dictionary<Type, PacketAttribute> Packets = Assembly

View File

@ -1,10 +1,21 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Shared.Packet.Packets;
namespace Shared.Packet; namespace Shared.Packet;
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct PacketHeader { public struct PacketHeader : IPacket {
// public int Length;
public Guid Id; public Guid Id;
public PacketType Type; public PacketType Type;
public PacketSender Sender; public void Serialize(Span<byte> data) {
// MemoryMarshal.Write(data, ref Length);
MemoryMarshal.Write(data, ref Id);
MemoryMarshal.Write(data[16..], ref Type);
}
public void Deserialize(Span<byte> data) {
Id = MemoryMarshal.Read<Guid>(data);
Type = MemoryMarshal.Read<PacketType>(data[16..]);
}
} }

View File

@ -1,6 +0,0 @@
namespace Shared.Packet;
public enum PacketSender {
Server,
Client
}

View File

@ -3,7 +3,9 @@
public enum PacketType { public enum PacketType {
Unknown, Unknown,
Player, Player,
Cap,
Game, Game,
Tag,
Connect, Connect,
Disconnect, Disconnect,
Costume, Costume,

View File

@ -0,0 +1,20 @@
using System.Runtime.InteropServices;
using Shared.Packet.Packets;
namespace Shared.Packet;
public static class PacketUtils {
public static void SerializeHeaded<T>(Span<byte> data, PacketHeader header, T t) where T : struct, IPacket {
header.Serialize(data);
t.Serialize(data[Constants.HeaderSize..]);
}
public static T Deserialize<T>(Span<byte> data) where T : IPacket, new() {
T packet = new T();
packet.Deserialize(data);
return packet;
}
public static int SizeOf<T>() where T : struct, IPacket {
return Constants.HeaderSize + Marshal.SizeOf<T>();
}
}

View File

@ -2,6 +2,7 @@
[Packet(PacketType.Command)] [Packet(PacketType.Command)]
public struct CommandPacket : IPacket { public struct CommandPacket : IPacket {
//todo: implement something for this
public void Serialize(Span<byte> data) { public void Serialize(Span<byte> data) {
} }

View File

@ -6,7 +6,9 @@ namespace Shared.Packet.Packets;
public struct CostumePacket : IPacket { public struct CostumePacket : IPacket {
public const int CostumeNameSize = 0x20; public const int CostumeNameSize = 0x20;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CostumeNameSize)]
public string BodyName; public string BodyName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CostumeNameSize)]
public string CapName; public string CapName;
public void Serialize(Span<byte> data) { public void Serialize(Span<byte> data) {
Span<char> strData = MemoryMarshal.Cast<byte, char>(data); Span<char> strData = MemoryMarshal.Cast<byte, char>(data);

View File

@ -2,6 +2,7 @@
[Packet(PacketType.Disconnect)] [Packet(PacketType.Disconnect)]
public struct DisconnectPacket : IPacket { public struct DisconnectPacket : IPacket {
//empty packet
public void Serialize(Span<byte> data) { public void Serialize(Span<byte> data) {
} }

View File

@ -0,0 +1,19 @@
using System.Runtime.InteropServices;
namespace Shared.Packet.Packets;
public class GamePacket : IPacket {
public int ScenarioNum;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = PlayerPacket.NameSize)]
public string Stage;
public void Serialize(Span<byte> data) {
MemoryMarshal.Write(data, ref ScenarioNum);
Stage.CopyTo(MemoryMarshal.Cast<byte, char>(data[4..]));
}
public void Deserialize(Span<byte> data) {
ScenarioNum = MemoryMarshal.Read<int>(data);
Stage = new string(MemoryMarshal.Cast<byte, char>(data[4..]));
}
}

View File

@ -1,4 +1,6 @@
namespace Shared.Packet.Packets; using System.Runtime.InteropServices;
namespace Shared.Packet.Packets;
// Packet interface for type safety // Packet interface for type safety
public interface IPacket { public interface IPacket {

View File

@ -0,0 +1,8 @@
namespace Shared.Packet.Packets;
[Flags]
public enum MovementFlags : byte {
IsFlat,
IsCapThrown,
IsSeeker
}

View File

@ -1,5 +1,6 @@
using System.Numerics; using System.Numerics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
namespace Shared.Packet.Packets; namespace Shared.Packet.Packets;
@ -9,14 +10,18 @@ public struct PlayerPacket : IPacket {
public Vector3 Position; public Vector3 Position;
public Quaternion Rotation; public Quaternion Rotation;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public float[] AnimationBlendWeights; public float[] AnimationBlendWeights;
public float AnimationRate; public float AnimationRate;
public bool Flat; public bool Is2d;
public bool ThrowingCap; public bool ThrowingCap;
public bool Seeker; public bool IsIt;
public int ScenarioNum; public int ScenarioNum;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NameSize)]
public string Stage; public string Stage;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NameSize)]
public string Act; public string Act;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NameSize)]
public string SubAct; public string SubAct;
public void Serialize(Span<byte> data) { public void Serialize(Span<byte> data) {
@ -26,16 +31,19 @@ public struct PlayerPacket : IPacket {
MemoryMarshal.Write(data[offset..], ref Rotation); MemoryMarshal.Write(data[offset..], ref Rotation);
offset += Marshal.SizeOf<Quaternion>(); offset += Marshal.SizeOf<Quaternion>();
AnimationBlendWeights.CopyTo(MemoryMarshal.Cast<byte, float>(data[offset..(offset += 4 * 6)])); AnimationBlendWeights.CopyTo(MemoryMarshal.Cast<byte, float>(data[offset..(offset += 4 * 6)]));
MemoryMarshal.Write(data[(offset += 4)..], ref AnimationRate); MemoryMarshal.Write(data[offset..], ref AnimationRate);
offset += 4; offset += 4;
MemoryMarshal.Write(data[offset++..], ref Flat); MemoryMarshal.Write(data[offset++..], ref Is2d);
MemoryMarshal.Write(data[offset++..], ref ThrowingCap); MemoryMarshal.Write(data[offset++..], ref ThrowingCap);
MemoryMarshal.Write(data[offset++..], ref Seeker); MemoryMarshal.Write(data[offset++..], ref IsIt);
MemoryMarshal.Write(data[(offset += 4)..], ref ScenarioNum); MemoryMarshal.Write(data[offset..], ref ScenarioNum);
Span<char> strData = MemoryMarshal.Cast<byte, char>(data[offset..]); offset += 5;
Stage.CopyTo(strData[..NameSize]); // Span<char> strData = MemoryMarshal.Cast<byte, char>(data[offset..]);
Act.CopyTo(strData[NameSize..(2 * NameSize)]); Encoding.UTF8.GetBytes(Stage).CopyTo(data[offset..(offset + NameSize)]);
SubAct.CopyTo(strData[(2 * NameSize)..(3 * NameSize)]); offset += NameSize;
Encoding.UTF8.GetBytes(Act).CopyTo(data[offset..(offset + NameSize)]);
offset += NameSize;
Encoding.UTF8.GetBytes(SubAct).CopyTo(data[offset..]);
} }
public void Deserialize(Span<byte> data) { public void Deserialize(Span<byte> data) {
@ -44,17 +52,18 @@ public struct PlayerPacket : IPacket {
offset += Marshal.SizeOf<Vector3>(); offset += Marshal.SizeOf<Vector3>();
Rotation = MemoryMarshal.Read<Quaternion>(data[offset..]); Rotation = MemoryMarshal.Read<Quaternion>(data[offset..]);
offset += Marshal.SizeOf<Quaternion>(); offset += Marshal.SizeOf<Quaternion>();
AnimationBlendWeights = MemoryMarshal.Cast<byte, float>(data[offset..(offset + 4 * 6)]).ToArray(); AnimationBlendWeights = MemoryMarshal.Cast<byte, float>(data[offset..(offset += 4 * 6)]).ToArray();
offset += 4 * 6; AnimationRate = MemoryMarshal.Read<float>(data[offset..(offset += 4)]);
AnimationRate = MemoryMarshal.Read<float>(data[(offset += 4)..]); Is2d = MemoryMarshal.Read<bool>(data[offset++..]);
offset += 4;
Flat = MemoryMarshal.Read<bool>(data[offset++..]);
ThrowingCap = MemoryMarshal.Read<bool>(data[offset++..]); ThrowingCap = MemoryMarshal.Read<bool>(data[offset++..]);
Seeker = MemoryMarshal.Read<bool>(data[offset++..]); IsIt = MemoryMarshal.Read<bool>(data[offset++..]);
ScenarioNum = MemoryMarshal.Read<int>(data[(offset += 4)..]); // offset++; // padding
Span<char> strData = MemoryMarshal.Cast<byte, char>(data[offset..]); ScenarioNum = MemoryMarshal.Read<int>(data[offset..]);
Stage = new string(strData[..NameSize].TrimEnd('\0')); offset += 5;
Act = new string(strData[NameSize..(2 * NameSize)].TrimEnd('\0')); Stage = new string(Encoding.UTF8.GetString(data[offset..(offset + NameSize)]).TrimEnd('\0'));
SubAct = new string(strData[(2 * NameSize)..(3 * NameSize)].TrimEnd('\0')); offset += NameSize;
Act = new string(Encoding.UTF8.GetString(data[offset..(offset + NameSize)]).TrimEnd('\0'));
offset += NameSize;
SubAct = new string(Encoding.UTF8.GetString(data[offset..(offset + NameSize)]).TrimEnd('\0'));
} }
} }

View File

@ -8,18 +8,48 @@ using Shared.Packet;
using Shared.Packet.Packets; using Shared.Packet.Packets;
TcpClient client = new TcpClient("127.0.0.1", 1027); TcpClient client = new TcpClient("127.0.0.1", 1027);
Guid ownId = new Guid(); Guid ownId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Guid otherId = Guid.Empty; Guid otherId = Guid.Empty;
Logger logger = new Logger("Client"); Logger logger = new Logger("Client");
NetworkStream stream = client.GetStream(); NetworkStream stream = client.GetStream();
PacketHeader coolHeader = new PacketHeader {
Type = PacketType.Connect,
Sender = PacketSender.Client,
Id = ownId
};
int e = 0; int e = 0;
double d = 0; double d = 0;
Vector3 basePoint = Vector3.Zero;
PlayerPacket? playerPacket = null;
async void Funny() {
Memory<byte> memory = new Memory<byte>(new byte[256]);
{
PacketHeader header = new PacketHeader {
Id = ownId,
Type = PacketType.Player
};
MemoryMarshal.Write(memory.Span, ref header);
}
while (true) {
d += Math.PI / 32;
if (playerPacket == null) {
// logger.Warn($"Waiting...");
await Task.Delay(300);
continue;
}
PlayerPacket packet = playerPacket.Value;
Vector3 pos = basePoint;
pos.X += 100f * (float) Math.Cos(d);
pos.Y += 300f;
pos.Z += 100f * (float) Math.Sin(d);
packet.Position = pos;
packet.Serialize(memory.Span[Constants.HeaderSize..]);
logger.Warn($"Current strs:{packet.Stage}-{packet.Act}-{packet.SubAct} {packet.Is2d} {packet.ThrowingCap} {packet.IsIt}");
await stream.WriteAsync(memory);
await Task.Delay(50);
}
}
async Task S() { async Task S() {
IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(256); IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(256);
@ -34,23 +64,26 @@ async Task S() {
continue; continue;
} }
d += Math.PI / 180; header.Id = ownId;
MemoryMarshal.Write(owner.Memory.Span, ref header);
unsafe { unsafe {
MemoryMarshal.Write(owner.Memory.Span, ref coolHeader); fixed (byte* data = owner.Memory.Span[Constants.HeaderSize..]) {
// unbelievably shitty way to marshal playerpacket logger.Error($"{Marshal.OffsetOf<PlayerPacket>(nameof(PlayerPacket.AnimationBlendWeights))} {Marshal.OffsetOf<PlayerPacket>(nameof(PlayerPacket.AnimationRate))}");
fixed (byte* basePtr = owner.Memory.Span) { PlayerPacket packet = Marshal.PtrToStructure<PlayerPacket>((IntPtr) data);
byte* dataPtr = basePtr + Constants.HeaderSize; playerPacket = packet;
Vector3 pos = Unsafe.Read<Vector3>(dataPtr); basePoint = packet.Position;
pos.X += 1000f * (float)Math.Cos(d);
pos.Z += 1000f * (float)Math.Sin(d);
Unsafe.Write(dataPtr, pos);
} }
} }
Console.WriteLine($"aargh {coolHeader.Id} {owner.Memory.Span[..256].Hex()}");
await stream.WriteAsync(owner.Memory); // packet.SubAct = "";
} }
} }
} }
PacketHeader coolHeader = new PacketHeader {
Type = PacketType.Connect,
Id = ownId
};
IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(256); IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(256);
MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader); MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader);
ConnectPacket connect = new ConnectPacket { ConnectPacket connect = new ConnectPacket {
@ -60,4 +93,6 @@ MemoryMarshal.Write(owner.Memory.Span[Constants.HeaderSize..256], ref connect);
await stream.WriteAsync(owner.Memory); await stream.WriteAsync(owner.Memory);
coolHeader.Type = PacketType.Player; coolHeader.Type = PacketType.Player;
MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader); MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader);
logger.Info("Connected");
Task.Run(Funny);
await S(); await S();