0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-14 23:55:07 +00:00
SmoOnlineServer/Server/Client.cs

75 lines
2.5 KiB
C#
Raw Normal View History

using System.Buffers;
using System.Collections.Concurrent;
2022-04-27 06:43:11 +00:00
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using Shared;
2022-02-10 08:42:35 +00:00
using Shared.Packet;
2022-02-08 22:46:12 +00:00
using Shared.Packet.Packets;
2021-11-29 04:04:34 +00:00
2022-02-10 01:44:50 +00:00
namespace Server;
2021-11-29 04:04:34 +00:00
public class Client : IDisposable {
2022-03-15 07:23:37 +00:00
public readonly ConcurrentDictionary<string, object?> Metadata = new ConcurrentDictionary<string, object?>(); // can be used to store any information about a player
2022-02-09 21:56:57 +00:00
public bool Connected = false;
public CostumePacket? CurrentCostume = null; // required for proper client sync
public string Name {
get => Logger.Name;
set => Logger.Name = value;
}
2022-02-10 01:44:50 +00:00
public Guid Id;
public Socket? Socket;
2022-02-10 08:42:35 +00:00
public Server Server { get; init; }
public Logger Logger { get; }
2022-02-10 01:44:50 +00:00
public Client(Socket socket) {
Socket = socket;
Logger = new Logger("Unknown User");
}
2022-02-10 01:44:50 +00:00
public void Dispose() {
if (Socket?.Connected is true)
Socket.Disconnect(false);
2022-02-10 01:44:50 +00:00
}
2021-11-29 04:04:34 +00:00
2022-02-16 00:35:38 +00:00
public async Task Send<T>(T packet, Client? sender = null) where T : struct, IPacket {
IMemoryOwner<byte> memory = MemoryPool<byte>.Shared.RentZero(Constants.HeaderSize + packet.Size);
2022-04-27 05:48:13 +00:00
PacketAttribute packetAttribute = Constants.PacketMap[typeof(T)];
2022-04-27 20:09:50 +00:00
try {
2022-04-27 20:17:02 +00:00
Server.FillPacket(new PacketHeader {
2022-04-27 20:09:50 +00:00
Id = sender?.Id ?? Id,
Type = packetAttribute.Type,
PacketSize = packet.Size
2022-04-27 20:17:02 +00:00
}, packet, memory.Memory);
2022-04-27 20:09:50 +00:00
}
catch (Exception e) {
2022-04-28 03:32:52 +00:00
Logger.Error($"Failed to serialize {packetAttribute.Type}");
Logger.Error(e);
2022-04-27 20:09:50 +00:00
}
2022-04-27 19:43:03 +00:00
await Socket!.SendAsync(memory.Memory[..(Constants.HeaderSize + packet.Size)], SocketFlags.None);
memory.Dispose();
}
2022-04-27 20:02:26 +00:00
public async Task Send(Memory<byte> data, Client? sender) {
PacketHeader header = new PacketHeader();
header.Deserialize(data.Span);
2022-04-27 05:58:43 +00:00
if (!Connected && header.Type is not PacketType.Connect) {
Server.Logger.Error($"Didn't send {header.Type} to {Id} because they weren't connected yet");
2022-02-10 08:42:35 +00:00
return;
}
2022-04-27 05:58:43 +00:00
await Socket!.SendAsync(data[..(Constants.HeaderSize + header.PacketSize)], SocketFlags.None);
2021-11-29 04:04:34 +00:00
}
2022-02-10 01:44:50 +00:00
public static bool operator ==(Client? left, Client? right) {
return left is { } leftClient && right is { } rightClient && leftClient.Id == rightClient.Id;
2021-11-29 04:04:34 +00:00
}
2022-02-10 01:44:50 +00:00
public static bool operator !=(Client? left, Client? right) {
return !(left == right);
}
2021-11-29 04:04:34 +00:00
}