2022-02-10 01:44:50 +00:00
|
|
|
|
using System.Net.Sockets;
|
2022-02-10 04:29:10 +00:00
|
|
|
|
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-02-10 01:44:50 +00:00
|
|
|
|
public readonly Dictionary<string, object> Metadata = new Dictionary<string, object>(); // can be used to store any information about a player
|
2022-02-09 21:56:57 +00:00
|
|
|
|
public bool Connected = false;
|
2022-02-10 01:44:50 +00:00
|
|
|
|
|
2022-02-12 23:37:26 +00:00
|
|
|
|
public CostumePacket? CurrentCostume = null;
|
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; }
|
2022-02-10 01:44:50 +00:00
|
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
Socket?.Disconnect(false);
|
|
|
|
|
}
|
2021-11-29 04:04:34 +00:00
|
|
|
|
|
2022-02-10 08:42:35 +00:00
|
|
|
|
public async Task Send(ReadOnlyMemory<byte> data, Client? other) {
|
|
|
|
|
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"}");
|
2022-02-10 04:29:10 +00:00
|
|
|
|
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
|
|
|
|
}
|