0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-29 06:33:06 +00:00
SmoOnlineServer/Server/Client.cs

55 lines
1.9 KiB
C#
Raw Normal View History

using System.Buffers;
using System.Collections.Concurrent;
using System.Net.Sockets;
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 {
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; init; } = new Logger("Unknown User");
2022-02-10 01:44:50 +00:00
public void Dispose() {
Socket?.Disconnect(false);
}
2021-11-29 04:04:34 +00:00
public async Task Send<T>(T packet, Client? sender = null) where T : unmanaged, IPacket {
IMemoryOwner<byte> memory = MemoryPool<byte>.Shared.Rent(Constants.MaxPacketSize);
PacketHeader header = new PacketHeader {
Id = sender?.Id ?? Guid.Empty,
Type = Constants.PacketMap[typeof(T)].Type
};
Server.FillPacket(header, packet, memory.Memory);
}
public async Task Send(ReadOnlyMemory<byte> data, Client? sender) {
2022-02-10 08:42:35 +00:00
if (!Connected) {
Server.Logger.Info($"Didn't send {(PacketType) data.Span[16]} to {Id} because they weren't connected yet");
return;
}
2022-02-11 04:25:47 +00:00
// Server.Logger.Info($"Sending {(PacketType) data.Span[16]} to {Id} from {other?.Id.ToString() ?? "server"}");
await Socket!.SendAsync(data[..Constants.MaxPacketSize], 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
}