0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-22 11:15:15 +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 System.Net.Sockets;
using Shared; using Shared;
using Shared.Packet;
using Shared.Packet.Packets; using Shared.Packet.Packets;
namespace Server; namespace Server;
@ -15,13 +16,18 @@ public class Client : IDisposable {
public Guid Id; public Guid Id;
public Socket? Socket; public Socket? Socket;
public Server Server { get; init; }
public void Dispose() { public void Dispose() {
Socket?.Disconnect(false); Socket?.Disconnect(false);
} }
public async Task Send(ReadOnlyMemory<byte> data) { public async Task Send(ReadOnlyMemory<byte> data, Client? other) {
if (!Connected) return; 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); 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="data">Memory owner to dispose once done</param>
/// <param name="sender">Optional sender to not broadcast data to</param> /// <param name="sender">Optional sender to not broadcast data to</param>
public async Task Broadcast(IMemoryOwner<byte> data, Client? sender = null) { public async Task Broadcast(IMemoryOwner<byte> data, Client? sender = null) {
await Task.WhenAll(Clients.Where(c => c.Connected && c != sender).Select(client => { await Task.WhenAll(Clients.Where(c => c.Connected && c != sender).Select(client => client.Send(data.Memory, sender)));
Logger.Info($"Sending {(PacketType) data.Memory.Span[16]} to {client.Id} from {sender.Id}");
return client.Send(data.Memory);
}));
data.Dispose(); data.Dispose();
} }
@ -73,7 +70,7 @@ public class Server {
/// <param name="data">Memory to send to the clients</param> /// <param name="data">Memory to send to the clients</param>
/// <param name="sender">Optional sender to not broadcast data to</param> /// <param name="sender">Optional sender to not broadcast data to</param>
public async void Broadcast(Memory<byte> data, Client? sender = null) { 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) { public Client? FindExistingClient(Guid id) {
@ -82,7 +79,7 @@ public class Server {
private async void HandleSocket(Socket socket) { private async void HandleSocket(Socket socket) {
Client client = new Client { Socket = socket }; Client client = new Client { Socket = socket, Server = this };
IMemoryOwner<byte> memory = null!; IMemoryOwner<byte> memory = null!;
bool first = true; bool first = true;
try { try {
@ -152,7 +149,7 @@ public class Server {
ConnectionType = ConnectionTypes.FirstConnection // doesn't matter what it is :) ConnectionType = ConnectionTypes.FirstConnection // doesn't matter what it is :)
}; };
MemoryMarshal.Write(connectBuffer.Memory.Span, ref connectPacket); MemoryMarshal.Write(connectBuffer.Memory.Span, ref connectPacket);
await client.Send(connectBuffer.Memory); await client.Send(connectBuffer.Memory, null);
connectBuffer.Dispose(); connectBuffer.Dispose();
}); });