0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-25 12:45:18 +00:00

More logs

This commit is contained in:
Sanae 2022-02-10 02:42:35 -06:00
parent 6cae63ca38
commit 8ac53f15f6
2 changed files with 12 additions and 9 deletions

View file

@ -1,5 +1,6 @@
using System.Net.Sockets;
using Shared;
using Shared.Packet;
using Shared.Packet.Packets;
namespace Server;
@ -15,13 +16,18 @@ public class Client : IDisposable {
public Guid Id;
public Socket? Socket;
public Server Server { get; init; }
public void Dispose() {
Socket?.Disconnect(false);
}
public async Task Send(ReadOnlyMemory<byte> data) {
if (!Connected) return;
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;
}
Server.Logger.Info($"Sending {(PacketType) data.Span[16]} to {Id} from {other?.Id.ToString() ?? "server"}");
await Socket!.SendAsync(data[..Constants.MaxPacketSize], SocketFlags.None);
}

View file

@ -60,10 +60,7 @@ public class Server {
/// <param name="data">Memory owner to dispose once done</param>
/// <param name="sender">Optional sender to not broadcast data to</param>
public async Task Broadcast(IMemoryOwner<byte> data, Client? sender = null) {
await Task.WhenAll(Clients.Where(c => c.Connected && c != sender).Select(client => {
Logger.Info($"Sending {(PacketType) data.Memory.Span[16]} to {client.Id} from {sender.Id}");
return client.Send(data.Memory);
}));
await Task.WhenAll(Clients.Where(c => c.Connected && c != sender).Select(client => client.Send(data.Memory, sender)));
data.Dispose();
}
@ -73,7 +70,7 @@ public class Server {
/// <param name="data">Memory to send to the clients</param>
/// <param name="sender">Optional sender to not broadcast data to</param>
public async void Broadcast(Memory<byte> data, Client? sender = null) {
await Task.WhenAll(Clients.Where(c => c.Connected && c != sender).Select(client => client.Send(data)));
await Task.WhenAll(Clients.Where(c => c.Connected && c != sender).Select(client => client.Send(data, sender)));
}
public Client? FindExistingClient(Guid id) {
@ -82,7 +79,7 @@ public class Server {
private async void HandleSocket(Socket socket) {
Client client = new Client { Socket = socket };
Client client = new Client { Socket = socket, Server = this };
IMemoryOwner<byte> memory = null!;
bool first = true;
try {
@ -152,7 +149,7 @@ public class Server {
ConnectionType = ConnectionTypes.FirstConnection // doesn't matter what it is :)
};
MemoryMarshal.Write(connectBuffer.Memory.Span, ref connectPacket);
await client.Send(connectBuffer.Memory);
await client.Send(connectBuffer.Memory, null);
connectBuffer.Dispose();
});