feat: implement IPacket based Send function
This commit is contained in:
parent
adaa74470e
commit
72c37b5c7a
1 changed files with 43 additions and 1 deletions
|
@ -1,8 +1,11 @@
|
|||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using MassTransit.Mediator;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetCoreServer;
|
||||
using Server.Packets;
|
||||
using Buffer = System.Buffer;
|
||||
|
||||
namespace Server;
|
||||
|
||||
|
@ -18,6 +21,45 @@ public class AuthSession : TcpSession
|
|||
_logger = logger;
|
||||
}
|
||||
|
||||
public override long Send(byte[] buffer)
|
||||
{
|
||||
_logger.LogInformation("Data being sent is: {Data}", BitConverter.ToString(buffer));
|
||||
return base.Send(buffer);
|
||||
}
|
||||
|
||||
public void Send(IPacket packet)
|
||||
{
|
||||
var type = packet.GetType();
|
||||
_logger.LogTrace("Packet of type {Type} is being serialized", type.Name);
|
||||
var packetIdAttribute = type.GetCustomAttribute<PacketId>();
|
||||
if (packetIdAttribute == null) return;
|
||||
var opcode = packetIdAttribute.Code;
|
||||
|
||||
Span<byte> packetData = packet.Serialize();
|
||||
ushort length = (ushort)(packetData.Length + 8);
|
||||
|
||||
Span<byte> buffer = stackalloc byte[length];
|
||||
buffer.Clear();
|
||||
packetData.CopyTo(buffer.Slice(8, length - 8));
|
||||
|
||||
var bytesOfLength = BitConverter.GetBytes(length);
|
||||
var bytesOfOpcode = BitConverter.GetBytes((ushort)opcode);
|
||||
for (int i = 0; i < bytesOfLength.Length || i < 2; i++)
|
||||
{
|
||||
buffer[i] = bytesOfLength[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < bytesOfOpcode.Length || i < 2; i++)
|
||||
{
|
||||
buffer[2 + i] = bytesOfOpcode[i];
|
||||
}
|
||||
|
||||
_logger.LogTrace("Packet data being parsed is: {Data}", BitConverter.ToString(packetData.ToArray()));
|
||||
_logger.LogTrace("Packet being parsed is: {Data}", BitConverter.ToString(buffer.ToArray()));
|
||||
|
||||
Send(buffer);
|
||||
}
|
||||
|
||||
protected override void OnReceived(byte[] buffer, long offset, long size)
|
||||
{
|
||||
Console.WriteLine($"Length: {size} & offset: {offset}");
|
||||
|
|
Loading…
Reference in a new issue