0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-22 03:05:16 +00:00

Currently cleaning up transport related stuff

This commit is contained in:
Sanae 2022-02-09 22:29:10 -06:00
parent 691086fdb9
commit 6cae63ca38
5 changed files with 19 additions and 10 deletions

View file

@ -1,4 +1,5 @@
using System.Net.Sockets; using System.Net.Sockets;
using Shared;
using Shared.Packet.Packets; using Shared.Packet.Packets;
namespace Server; namespace Server;
@ -19,9 +20,9 @@ public class Client : IDisposable {
Socket?.Disconnect(false); Socket?.Disconnect(false);
} }
public async Task Send(Memory<byte> data) { public async Task Send(ReadOnlyMemory<byte> data) {
if (!Connected) return; if (!Connected) return;
await Socket!.SendAsync(data, SocketFlags.None); await Socket!.SendAsync(data[..Constants.MaxPacketSize], SocketFlags.None);
} }
public static bool operator ==(Client? left, Client? right) { public static bool operator ==(Client? left, Client? right) {

View file

@ -48,7 +48,7 @@ public class Server {
PacketHeader header = new PacketHeader { PacketHeader header = new PacketHeader {
Id = sender?.Id ?? Guid.Empty, Id = sender?.Id ?? Guid.Empty,
Type = Constants.Packets[typeof(T)].Type Type = Constants.PacketMap[typeof(T)].Type
}; };
FillPacket(header, packet, memory.Memory); FillPacket(header, packet, memory.Memory);
await Broadcast(memory, sender); await Broadcast(memory, sender);
@ -147,7 +147,7 @@ public class Server {
Id = other.Id, Id = other.Id,
Type = PacketType.Connect Type = PacketType.Connect
}; };
MemoryMarshal.Write(connectBuffer.Memory.Span, ref connectHeader); MemoryMarshal.Write(connectBuffer.Memory.Span[Constants.HeaderSize..], 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 :)
}; };

View file

@ -11,11 +11,16 @@ public static class Constants {
public const int CostumeNameSize = 0x20; public const int CostumeNameSize = 0x20;
// dictionary of packet types to packet // dictionary of packet types to packet
public static readonly Dictionary<Type, PacketAttribute> Packets = Assembly public static readonly Dictionary<Type, PacketAttribute> PacketMap = Assembly
.GetExecutingAssembly() .GetExecutingAssembly()
.GetTypes() .GetTypes()
.Where(type => type.IsAssignableTo(typeof(IPacket))) .Where(type => type.IsAssignableTo(typeof(IPacket)))
.ToDictionary(type => type, type => type.GetCustomAttribute<PacketAttribute>()!); .ToDictionary(type => type, type => type.GetCustomAttribute<PacketAttribute>()!);
public static readonly Dictionary<PacketType, Type> PacketIdMap = Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(type => type.IsAssignableTo(typeof(IPacket)))
.ToDictionary(type => type.GetCustomAttribute<PacketAttribute>()!.Type, type => type);
public static int HeaderSize { get; } = Marshal.SizeOf<PacketHeader>(); public static int HeaderSize { get; } = Marshal.SizeOf<PacketHeader>();
public static int PacketDataSize { get; } = MaxPacketSize - HeaderSize; public static int PacketDataSize { get; } = MaxPacketSize - HeaderSize;

View file

@ -8,7 +8,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0"/> <PackageReference Include="ObjectDumper.NET" Version="3.3.13" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -7,7 +7,8 @@ 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 otherId = Guid.Empty; Guid otherId = Guid.Empty;
Logger logger = new Logger("Client"); Logger logger = new Logger("Client");
NetworkStream stream = client.GetStream(); NetworkStream stream = client.GetStream();
@ -32,10 +33,13 @@ 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 (reboundPackets.All(x => x != type)) continue; if (reboundPackets.All(x => x != type)) continue;
header.Id = ownId; header.Id = ownId;
MemoryMarshal.Write(owner.Memory.Span, ref header); MemoryMarshal.Write(owner.Memory.Span, ref header);
await stream.WriteAsync(owner.Memory); await stream.WriteAsync(owner.Memory[..Constants.MaxPacketSize]);
} }
} }
@ -50,7 +54,5 @@ ConnectPacket connect = new ConnectPacket {
}; };
MemoryMarshal.Write(owner.Memory.Span[Constants.HeaderSize..Constants.MaxPacketSize], ref connect); MemoryMarshal.Write(owner.Memory.Span[Constants.HeaderSize..Constants.MaxPacketSize], ref connect);
await stream.WriteAsync(owner.Memory); await stream.WriteAsync(owner.Memory);
coolHeader.Type = PacketType.Player;
MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader);
logger.Info("Connected"); logger.Info("Connected");
await S(); await S();