mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-22 03:05:16 +00:00
Everything just works™
This commit is contained in:
parent
8ac53f15f6
commit
7bbba0c691
4 changed files with 43 additions and 20 deletions
|
@ -9,10 +9,7 @@ public class Client : IDisposable {
|
||||||
public readonly Dictionary<string, object> Metadata = new Dictionary<string, object>(); // can be used to store any information about a player
|
public readonly Dictionary<string, object> Metadata = new Dictionary<string, object>(); // can be used to store any information about a player
|
||||||
public bool Connected = false;
|
public bool Connected = false;
|
||||||
|
|
||||||
public CostumePacket CurrentCostume = new CostumePacket {
|
public CostumePacket? CurrentCostume;
|
||||||
BodyName = "",
|
|
||||||
CapName = ""
|
|
||||||
};
|
|
||||||
|
|
||||||
public Guid Id;
|
public Guid Id;
|
||||||
public Socket? Socket;
|
public Socket? Socket;
|
||||||
|
@ -27,7 +24,7 @@ public class Client : IDisposable {
|
||||||
Server.Logger.Info($"Didn't send {(PacketType) data.Span[16]} to {Id} because they weren't connected yet");
|
Server.Logger.Info($"Didn't send {(PacketType) data.Span[16]} to {Id} because they weren't connected yet");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Server.Logger.Info($"Sending {(PacketType) data.Span[16]} to {Id} from {other?.Id.ToString() ?? "server"}");
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ public class Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
// broadcast packets to all clients
|
// broadcast packets to all clients
|
||||||
public async Task Broadcast<T>(T packet, Client? sender = null) where T : unmanaged, IPacket {
|
public async Task Broadcast<T>(T packet, Client sender) where T : unmanaged, IPacket {
|
||||||
IMemoryOwner<byte> memory = memoryPool.Rent(Constants.MaxPacketSize);
|
IMemoryOwner<byte> memory = memoryPool.Rent(Constants.MaxPacketSize);
|
||||||
|
|
||||||
PacketHeader header = new PacketHeader {
|
PacketHeader header = new PacketHeader {
|
||||||
|
@ -139,25 +139,35 @@ public class Server {
|
||||||
|
|
||||||
List<Client> otherConnectedPlayers = Clients.FindAll(c => c.Id != header.Id && c.Connected && c.Socket != null);
|
List<Client> otherConnectedPlayers = Clients.FindAll(c => c.Id != header.Id && c.Connected && c.Socket != null);
|
||||||
await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {
|
await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {
|
||||||
IMemoryOwner<byte> connectBuffer = MemoryPool<byte>.Shared.Rent(Constants.MaxPacketSize);
|
IMemoryOwner<byte> tempBuffer = MemoryPool<byte>.Shared.Rent(Constants.MaxPacketSize);
|
||||||
PacketHeader connectHeader = new PacketHeader {
|
PacketHeader connectHeader = new PacketHeader {
|
||||||
Id = other.Id,
|
Id = other.Id,
|
||||||
Type = PacketType.Connect
|
Type = PacketType.Connect
|
||||||
};
|
};
|
||||||
MemoryMarshal.Write(connectBuffer.Memory.Span[Constants.HeaderSize..], ref connectHeader);
|
MemoryMarshal.Write(tempBuffer.Memory.Span, ref connectHeader);
|
||||||
ConnectPacket connectPacket = new ConnectPacket {
|
ConnectPacket connectPacket = new ConnectPacket {
|
||||||
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(tempBuffer.Memory.Span[Constants.HeaderSize..], ref connectPacket);
|
||||||
await client.Send(connectBuffer.Memory, null);
|
await client.Send(tempBuffer.Memory, null);
|
||||||
connectBuffer.Dispose();
|
if (other.CurrentCostume is {} costumePacket) {
|
||||||
|
connectHeader.Type = PacketType.Costume;
|
||||||
|
MemoryMarshal.Write(tempBuffer.Memory.Span, ref connectHeader);
|
||||||
|
costumePacket.Serialize(tempBuffer.Memory.Span[Constants.HeaderSize..]);
|
||||||
|
await client.Send(tempBuffer.Memory, null);
|
||||||
|
}
|
||||||
|
tempBuffer.Dispose();
|
||||||
});
|
});
|
||||||
|
|
||||||
Logger.Info($"Client {socket.RemoteEndPoint} ({client.Id}) connected.");
|
Logger.Info($"Client {socket.RemoteEndPoint} ({client.Id}) connected.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo support variable length packets if they show up
|
// todo support variable length packets if they show up
|
||||||
Logger.Warn($"broadcasting {header.Type} from {client.Id}");
|
// Logger.Warn($"broadcasting {header.Type} from {client.Id}");
|
||||||
|
if (header.Type == PacketType.Costume) {
|
||||||
|
client.CurrentCostume ??= new CostumePacket();
|
||||||
|
client.CurrentCostume.Value.Deserialize(memory.Memory.Span[Constants.HeaderSize..]);
|
||||||
|
}
|
||||||
await Broadcast(memory, client);
|
await Broadcast(memory, client);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -19,7 +19,7 @@ public static class Constants {
|
||||||
public static readonly Dictionary<PacketType, Type> PacketIdMap = Assembly
|
public static readonly Dictionary<PacketType, Type> PacketIdMap = Assembly
|
||||||
.GetExecutingAssembly()
|
.GetExecutingAssembly()
|
||||||
.GetTypes()
|
.GetTypes()
|
||||||
.Where(type => type.IsAssignableTo(typeof(IPacket)))
|
.Where(type => type.IsAssignableTo(typeof(IPacket)) && type.GetCustomAttribute<PacketAttribute>() != null)
|
||||||
.ToDictionary(type => type.GetCustomAttribute<PacketAttribute>()!.Type, type => type);
|
.ToDictionary(type => type.GetCustomAttribute<PacketAttribute>()!.Type, type => type);
|
||||||
|
|
||||||
public static int HeaderSize { get; } = Marshal.SizeOf<PacketHeader>();
|
public static int HeaderSize { get; } = Marshal.SizeOf<PacketHeader>();
|
||||||
|
|
|
@ -7,20 +7,19 @@ using Shared.Packet;
|
||||||
using Shared.Packet.Packets;
|
using Shared.Packet.Packets;
|
||||||
|
|
||||||
TcpClient client = new TcpClient(args[0], 1027);
|
TcpClient client = new TcpClient(args[0], 1027);
|
||||||
// Guid ownId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
Guid ownId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
Guid ownId = Guid.NewGuid();
|
// Guid ownId = Guid.NewGuid();
|
||||||
Guid otherId = Guid.Empty;
|
Guid otherId = Guid.Parse("d5feae62-2e71-1000-88fd-597ea147ae88");
|
||||||
Logger logger = new Logger("Client");
|
Logger logger = new Logger("Client");
|
||||||
NetworkStream stream = client.GetStream();
|
NetworkStream stream = client.GetStream();
|
||||||
|
|
||||||
int e = 0;
|
int e = 0;
|
||||||
double d = 0;
|
double d = 0;
|
||||||
Vector3 basePoint = Vector3.Zero;
|
Vector3 basePoint = Vector3.Zero;
|
||||||
PlayerPacket? playerPacket = null;
|
|
||||||
|
|
||||||
PacketType[] reboundPackets = {
|
PacketType[] reboundPackets = {
|
||||||
PacketType.Player,
|
PacketType.Player,
|
||||||
PacketType.Cap,
|
// PacketType.Cap,
|
||||||
PacketType.Capture,
|
PacketType.Capture,
|
||||||
PacketType.Costume,
|
PacketType.Costume,
|
||||||
PacketType.Tag,
|
PacketType.Tag,
|
||||||
|
@ -33,8 +32,25 @@ async Task S() {
|
||||||
await stream.ReadAsync(owner.Memory);
|
await stream.ReadAsync(owner.Memory);
|
||||||
PacketHeader header = MemoryMarshal.Read<PacketHeader>(owner.Memory.Span);
|
PacketHeader header = MemoryMarshal.Read<PacketHeader>(owner.Memory.Span);
|
||||||
PacketType type = header.Type;
|
PacketType type = header.Type;
|
||||||
if (type is not PacketType.Cap and not PacketType.Player) {
|
if (header.Id != otherId) continue;
|
||||||
|
if (type is PacketType.Player) {
|
||||||
|
CapPacket cap = new CapPacket();
|
||||||
|
PlayerPacket playerPacket = new PlayerPacket();
|
||||||
|
playerPacket.Deserialize(owner.Memory.Span[Constants.HeaderSize..]);
|
||||||
|
cap.Position = playerPacket.Position + Vector3.UnitY * 500f;
|
||||||
|
// cap.Rotation = Quaternion.CreateFromYawPitchRoll(0,0,0);
|
||||||
|
cap.CapAnim = "StayR";
|
||||||
|
playerPacket.Position = new Vector3(1000000f);
|
||||||
|
playerPacket.ThrowingCap = true;
|
||||||
|
header.Id = ownId;
|
||||||
|
MemoryMarshal.Write(owner.Memory.Span, ref header);
|
||||||
|
playerPacket.Serialize(owner.Memory.Span[Constants.HeaderSize..]);
|
||||||
|
await stream.WriteAsync(owner.Memory[..Constants.MaxPacketSize]);
|
||||||
|
header.Type = PacketType.Cap;
|
||||||
|
MemoryMarshal.Write(owner.Memory.Span, ref header);
|
||||||
|
cap.Serialize(owner.Memory.Span[Constants.HeaderSize..]);
|
||||||
|
await stream.WriteAsync(owner.Memory[..Constants.MaxPacketSize]);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (reboundPackets.All(x => x != type)) continue;
|
if (reboundPackets.All(x => x != type)) continue;
|
||||||
header.Id = ownId;
|
header.Id = ownId;
|
||||||
|
|
Loading…
Reference in a new issue