07-06-2018, 06:47 PM
Нужна помощь с исправлением приемом и отправкой пакетов.
Не могу понять в чем проблема , иногда идут левые пакеты.
Пробовал написать так же как и в point blank приемку и отправку пакетов, после 1 пакета ничего в ответ не идет.
Структура пакетов:
Длина = 2 байта
Крипт = 1 байт (0-не закриптован/1-закриптован)
Опкод = 2 байта
Тело = Остальные байты
Для логина и гейм одинаковая структура.
Не могу понять в чем проблема , иногда идут левые пакеты.
Пробовал написать так же как и в point blank приемку и отправку пакетов, после 1 пакета ничего в ответ не идет.
Структура пакетов:
Длина = 2 байта
Крипт = 1 байт (0-не закриптован/1-закриптован)
Опкод = 2 байта
Тело = Остальные байты
Для логина и гейм одинаковая структура.
Receive
[SRC="csharp"]
InHeaderSize = 3;
private void BeginReceive()
{
if (_disconnected)
return;
_socket.BeginReceive(_receiveBuffer, 0, _receiveBuffer.Length, SocketFlags.None, Receive, null);
}
private void Receive(IAsyncResult ar)
{
var receivedBytes = 0;
try
{
receivedBytes = _socket.EndReceive(ar);
}
catch (Exception ex)
{
Disconnect($"Receive Exception: {ex}");
}
// Check if the socket was closed.
if (receivedBytes == 0)
{
Disconnect();
return;
}
// Check the message header.
if (receivedBytes < InHeaderSize)
{
Disconnect("The received packets header size is incorrect.");
return;
}
// TODO Properly Process incomming bytes.
using (var stream = new MemoryStream(_receiveBuffer, 0, receivedBytes))
using (var reader = new BinaryReader(stream))
{
var size = reader.ReadUInt16();
var encrypted = reader.ReadByte() == 1;
var body = reader.ReadBytes(size - InHeaderSize);
if (encrypted)
{
body = BDOCrypt.Xor(body, 0, body.Length);
}
using (var messageStream = new MemoryStream(body))
using (var message = new BinaryReader(messageStream))
{
var messageId = message.ReadUInt16(); // MessageId
Console.WriteLine(
$"Received(Size:{size}, Encrypted: {encrypted}, MessageId: {messageId}, 0x{messageId:X2})");
Console.WriteLine(BufferExtensions.ToHex(body.ToArray()));
var handler = MessageTable.FindMessageHandler(messageId);
if (handler != null)
{
handler.Handle(this, message);
}
else
{
Console.WriteLine($"Unknown Message Received: MessageId: {messageId} (0x{messageId:X2})");
}
}
}
// Start receiving more data.
BeginReceive();
}
[/SRC]
InHeaderSize = 3;
private void BeginReceive()
{
if (_disconnected)
return;
_socket.BeginReceive(_receiveBuffer, 0, _receiveBuffer.Length, SocketFlags.None, Receive, null);
}
private void Receive(IAsyncResult ar)
{
var receivedBytes = 0;
try
{
receivedBytes = _socket.EndReceive(ar);
}
catch (Exception ex)
{
Disconnect($"Receive Exception: {ex}");
}
// Check if the socket was closed.
if (receivedBytes == 0)
{
Disconnect();
return;
}
// Check the message header.
if (receivedBytes < InHeaderSize)
{
Disconnect("The received packets header size is incorrect.");
return;
}
// TODO Properly Process incomming bytes.
using (var stream = new MemoryStream(_receiveBuffer, 0, receivedBytes))
using (var reader = new BinaryReader(stream))
{
var size = reader.ReadUInt16();
var encrypted = reader.ReadByte() == 1;
var body = reader.ReadBytes(size - InHeaderSize);
if (encrypted)
{
body = BDOCrypt.Xor(body, 0, body.Length);
}
using (var messageStream = new MemoryStream(body))
using (var message = new BinaryReader(messageStream))
{
var messageId = message.ReadUInt16(); // MessageId
Console.WriteLine(
$"Received(Size:{size}, Encrypted: {encrypted}, MessageId: {messageId}, 0x{messageId:X2})");
Console.WriteLine(BufferExtensions.ToHex(body.ToArray()));
var handler = MessageTable.FindMessageHandler(messageId);
if (handler != null)
{
handler.Handle(this, message);
}
else
{
Console.WriteLine($"Unknown Message Received: MessageId: {messageId} (0x{messageId:X2})");
}
}
}
// Start receiving more data.
BeginReceive();
}
[/SRC]
Send
[SRC="csharp"]
OutHeaderSize = 3;
public void SendBody(ushort opCode, byte[] body, bool encrypted = true)
{
byte[] outputBuffer;
using (var memoryStream = new MemoryStream())
using (var writer = new BinaryWriter(memoryStream))
{
writer.Write((ushort)opCode);
writer.Write(body);
writer.Flush();
outputBuffer = memoryStream.ToArray();
}
if (encrypted)
{
outputBuffer = BDOCrypt.Xor(outputBuffer);
}
var messageSize = outputBuffer.Length + OutHeaderSize;
Console.WriteLine($"Send(Size:{body.Length}, Encrypted: {encrypted}, MessageId: {opCode})");
Console.WriteLine(BufferExtensions.ToHex(body));
using (var memoryStream = new MemoryStream())
using (var writer = new BinaryWriter(memoryStream))
{
writer.Write((ushort)messageSize); // Message size;
writer.Write((byte)(encrypted ? 1 : 0)); // IsEncrypted -> 0 = false, 1 = true
writer.Write(outputBuffer);
writer.Flush();
Send(memoryStream.ToArray());
}
}
public void Send(IMessage message)
{
var messageData = MessageTable.FindMessageData(message);
if (messageData == null)
throw new Exception($"Could not find message attribute for {nameof(message)}");
byte[] bodyBuffer;
var encrypted = messageData.Encrypted;
using (var bodyStream = new MemoryStream())
using (var bodyWriter = new BinaryWriter(bodyStream))
{
bodyWriter.Write((ushort)messageData.MessageId); // MessageId
message.Serialize(bodyWriter); // Body
bodyWriter.Flush();
bodyBuffer = bodyStream.ToArray();
}
var messageId = (ushort)messageData.MessageId;
Console.WriteLine(
$"Send(Size:{bodyBuffer.Length}, Encrypted: {encrypted}, MessageId: {messageData.MessageId}, {messageId}, 0x{messageId:X2})");
Console.WriteLine(BufferExtensions.ToHex(bodyBuffer.ToArray()));
if (encrypted)
{
bodyBuffer = BDOCrypt.Xor(bodyBuffer);
}
using (var sendBuffer = new MemoryStream())
using (var messageBuilder = new BinaryWriter(sendBuffer))
{
// Build the message.
messageBuilder.Write((ushort)(bodyBuffer.Length + OutHeaderSize));
messageBuilder.Write((byte)(encrypted ? 1 : 0)); // Encrypted
messageBuilder.Write(bodyBuffer);
messageBuilder.Flush();
// Send data
var messageBuffer = sendBuffer.ToArray();
Send(messageBuffer);
}
}
private void Send(byte[] buffer)
{
try
{
_socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, SendCallback, null);
Console.WriteLine($"Sent: {buffer.Length} bytes");
}
catch (Exception ex)
{
Disconnect($"Send Exception: {ex}");
}
}
private void SendCallback(IAsyncResult ar)
{
try
{
_socket.EndSend(ar); // Returns sent bytes.
}
catch (Exception ex)
{
Disconnect($"Send Exception: {ex}");
}
}
[/SRC]
OutHeaderSize = 3;
public void SendBody(ushort opCode, byte[] body, bool encrypted = true)
{
byte[] outputBuffer;
using (var memoryStream = new MemoryStream())
using (var writer = new BinaryWriter(memoryStream))
{
writer.Write((ushort)opCode);
writer.Write(body);
writer.Flush();
outputBuffer = memoryStream.ToArray();
}
if (encrypted)
{
outputBuffer = BDOCrypt.Xor(outputBuffer);
}
var messageSize = outputBuffer.Length + OutHeaderSize;
Console.WriteLine($"Send(Size:{body.Length}, Encrypted: {encrypted}, MessageId: {opCode})");
Console.WriteLine(BufferExtensions.ToHex(body));
using (var memoryStream = new MemoryStream())
using (var writer = new BinaryWriter(memoryStream))
{
writer.Write((ushort)messageSize); // Message size;
writer.Write((byte)(encrypted ? 1 : 0)); // IsEncrypted -> 0 = false, 1 = true
writer.Write(outputBuffer);
writer.Flush();
Send(memoryStream.ToArray());
}
}
public void Send(IMessage message)
{
var messageData = MessageTable.FindMessageData(message);
if (messageData == null)
throw new Exception($"Could not find message attribute for {nameof(message)}");
byte[] bodyBuffer;
var encrypted = messageData.Encrypted;
using (var bodyStream = new MemoryStream())
using (var bodyWriter = new BinaryWriter(bodyStream))
{
bodyWriter.Write((ushort)messageData.MessageId); // MessageId
message.Serialize(bodyWriter); // Body
bodyWriter.Flush();
bodyBuffer = bodyStream.ToArray();
}
var messageId = (ushort)messageData.MessageId;
Console.WriteLine(
$"Send(Size:{bodyBuffer.Length}, Encrypted: {encrypted}, MessageId: {messageData.MessageId}, {messageId}, 0x{messageId:X2})");
Console.WriteLine(BufferExtensions.ToHex(bodyBuffer.ToArray()));
if (encrypted)
{
bodyBuffer = BDOCrypt.Xor(bodyBuffer);
}
using (var sendBuffer = new MemoryStream())
using (var messageBuilder = new BinaryWriter(sendBuffer))
{
// Build the message.
messageBuilder.Write((ushort)(bodyBuffer.Length + OutHeaderSize));
messageBuilder.Write((byte)(encrypted ? 1 : 0)); // Encrypted
messageBuilder.Write(bodyBuffer);
messageBuilder.Flush();
// Send data
var messageBuffer = sendBuffer.ToArray();
Send(messageBuffer);
}
}
private void Send(byte[] buffer)
{
try
{
_socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, SendCallback, null);
Console.WriteLine($"Sent: {buffer.Length} bytes");
}
catch (Exception ex)
{
Disconnect($"Send Exception: {ex}");
}
}
private void SendCallback(IAsyncResult ar)
{
try
{
_socket.EndSend(ar); // Returns sent bytes.
}
catch (Exception ex)
{
Disconnect($"Send Exception: {ex}");
}
}
[/SRC]