mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-21 18:55:17 +00:00
Updated test client to send varsize packets, 8p test
This commit is contained in:
parent
15960a8d5a
commit
3974b7022e
2 changed files with 84 additions and 72 deletions
|
@ -13,14 +13,14 @@ public struct PacketHeader : IPacket {
|
|||
public short Size => 20;
|
||||
|
||||
public void Serialize(Span<byte> data) {
|
||||
// MemoryMarshal.Write(data, ref Length);
|
||||
MemoryMarshal.Write(data, ref Id);
|
||||
MemoryMarshal.Write(data[16..], ref Type);
|
||||
MemoryMarshal.Write(data[18..], ref PacketSize);
|
||||
}
|
||||
|
||||
public void Deserialize(Span<byte> data) {
|
||||
Id = MemoryMarshal.Read<Guid>(data);
|
||||
Type = MemoryMarshal.Read<PacketType>(data[16..]);
|
||||
Type = MemoryMarshal.Read<PacketType>(data[16..]);
|
||||
PacketSize = MemoryMarshal.Read<short>(data[16..]);
|
||||
}
|
||||
}
|
|
@ -6,73 +6,85 @@ using Shared;
|
|||
using Shared.Packet;
|
||||
using Shared.Packet.Packets;
|
||||
|
||||
// TcpClient client = new TcpClient(args[0], 1027);
|
||||
Guid ownId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
// Guid ownId = Guid.NewGuid();
|
||||
// Guid otherId = Guid.Parse("d5feae62-2e71-1000-88fd-597ea147ae88");
|
||||
// Logger logger = new Logger("Client");
|
||||
// NetworkStream stream = client.GetStream();
|
||||
//
|
||||
// Vector3 basePoint = Vector3.Zero;
|
||||
//
|
||||
// PacketType[] reboundPackets = {
|
||||
// PacketType.Player,
|
||||
// PacketType.Cap,
|
||||
// PacketType.Capture,
|
||||
// PacketType.Costume,
|
||||
// PacketType.Tag,
|
||||
// PacketType.Shine
|
||||
// };
|
||||
//
|
||||
// string lastCapture = "";
|
||||
//
|
||||
// async Task S() {
|
||||
// IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(Constants.MaxPacketSize);
|
||||
// while (true) {
|
||||
// await stream.ReadAsync(owner.Memory);
|
||||
// PacketHeader header = MemoryMarshal.Read<PacketHeader>(owner.Memory.Span);
|
||||
// PacketType type = header.Type;
|
||||
// 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..]);
|
||||
// logger.Info(playerPacket.Hack);
|
||||
// if (playerPacket.Hack != lastCapture) logger.Info($"Changed to hack: {lastCapture = playerPacket.Hack}");
|
||||
// // 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;
|
||||
// header.Id = ownId;
|
||||
// MemoryMarshal.Write(owner.Memory.Span, ref header);
|
||||
// await stream.WriteAsync(owner.Memory[..Constants.MaxPacketSize]);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// PacketHeader coolHeader = new PacketHeader {
|
||||
// Type = PacketType.Connect,
|
||||
// Id = ownId
|
||||
// };
|
||||
// IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.RentZero(Constants.MaxPacketSize);
|
||||
// MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader);
|
||||
// ConnectPacket connect = new ConnectPacket {
|
||||
// ConnectionType = ConnectionTypes.Reconnecting,
|
||||
// ClientName = "Test Sanae"
|
||||
// };
|
||||
// connect.Serialize(owner.Memory.Span[Constants.HeaderSize..Constants.MaxPacketSize]);
|
||||
// await stream.WriteAsync(owner.Memory);
|
||||
// logger.Info("Connected");
|
||||
// await S();
|
||||
// Guid ownId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
Guid baseOtherId = Guid.Parse("8ca3fcdd-2940-1000-b5f8-579301fcbfbb");
|
||||
|
||||
PacketType[] reboundPackets = {
|
||||
PacketType.Player,
|
||||
PacketType.Cap,
|
||||
PacketType.Capture,
|
||||
PacketType.Costume,
|
||||
PacketType.Tag,
|
||||
PacketType.Game,
|
||||
// PacketType.Shine
|
||||
};
|
||||
|
||||
string lastCapture = "";
|
||||
|
||||
async Task S(string n, Guid otherId, Guid ownId) {
|
||||
Logger logger = new Logger($"Client ({n})");
|
||||
TcpClient client = new TcpClient(args[0], 1027);
|
||||
NetworkStream stream = client.GetStream();
|
||||
logger.Info("Connected!");
|
||||
async Task<bool> Read(Memory<byte> readMem, int readSize, int readOffset) {
|
||||
readSize += readOffset;
|
||||
while (readOffset < readSize) {
|
||||
int size = await stream.ReadAsync(readMem[readOffset..readSize]);
|
||||
if (size == 0) {
|
||||
// treat it as a disconnect and exit
|
||||
logger.Info($"Socket {client.Client.RemoteEndPoint} disconnected.");
|
||||
return false;
|
||||
}
|
||||
|
||||
readOffset += size;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
{
|
||||
ConnectPacket connect = new ConnectPacket {
|
||||
ConnectionType = ConnectPacket.ConnectionTypes.FirstConnection,
|
||||
ClientName = n
|
||||
};
|
||||
PacketHeader coolHeader = new PacketHeader {
|
||||
Type = PacketType.Connect,
|
||||
Id = ownId,
|
||||
PacketSize = connect.Size,
|
||||
};
|
||||
IMemoryOwner<byte> connectOwner = MemoryPool<byte>.Shared.RentZero(Constants.HeaderSize + connect.Size);
|
||||
// coolHeader.Serialize(connectOwner.Memory.Span[..Constants.HeaderSize]);
|
||||
MemoryMarshal.Write(connectOwner.Memory.Span[..Constants.HeaderSize], ref coolHeader);
|
||||
connect.Serialize(connectOwner.Memory.Span[Constants.HeaderSize..(Constants.HeaderSize + connect.Size)]);
|
||||
await stream.WriteAsync(connectOwner.Memory[..(Constants.HeaderSize + connect.Size)]);
|
||||
connectOwner.Dispose();
|
||||
}
|
||||
|
||||
while (true) {
|
||||
IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.RentZero(0xFF);
|
||||
if (!await Read(owner.Memory, Constants.HeaderSize, 0)) return;
|
||||
PacketHeader header = MemoryMarshal.Read<PacketHeader>(owner.Memory.Span);
|
||||
if (header.Size > 0) {
|
||||
if (!await Read(owner.Memory, header.PacketSize, Constants.HeaderSize)) return;
|
||||
}
|
||||
PacketType type = header.Type;
|
||||
if (header.Id != otherId) continue;
|
||||
if (reboundPackets.All(x => x != type)) continue;
|
||||
if (type == PacketType.Player) {
|
||||
|
||||
}
|
||||
header.Id = ownId;
|
||||
MemoryMarshal.Write(owner.Memory.Span[..Constants.HeaderSize], ref header);
|
||||
await stream.WriteAsync(owner.Memory[..(Constants.HeaderSize + header.PacketSize)]);
|
||||
owner.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Guid temp = baseOtherId;
|
||||
IEnumerable<Task> stuff = Enumerable.Range(0, 7).Select(i => {
|
||||
Guid newOwnId = Guid.NewGuid();
|
||||
Task task = S($"Sussy {i}", temp, newOwnId);
|
||||
temp = newOwnId;
|
||||
return task;
|
||||
});
|
||||
await Task.WhenAll(stuff.ToArray());
|
Loading…
Reference in a new issue