Skip to content

Add big-endian support in MySqlX protocol handling #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 9.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions MySQL.Data/src/X/Communication/XCompressionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using System.IO;
using System.IO.Compression;
using ZstdSharp;
using System.Buffers.Binary;

namespace MySqlX.Communication
{
Expand Down Expand Up @@ -329,7 +330,7 @@ internal byte[] Decompress(byte[] input, int length)

var messageSizeBytes = new byte[4];
Buffer.BlockCopy(decompressedData, 0, messageSizeBytes, 0, messageSizeBytes.Length);
var firstMessageSize = BitConverter.ToInt32(messageSizeBytes, 0) + 4;
var firstMessageSize = BinaryPrimitives.ReadInt32LittleEndian(messageSizeBytes.AsSpan().Slice(0)) + 4;
LastMessageContainsMultipleMessages = firstMessageSize < decompressedData.Length;
if (!LastMessageContainsMultipleMessages
|| (_multipleMessagesStream != null
Expand Down Expand Up @@ -440,7 +441,7 @@ internal byte[] ReadNextBufferedMessage()
var messageSizeBytes = new byte[4];
_multipleMessagesStream.Read(messageSizeBytes, 0, messageSizeBytes.Length);
byte messageType = (byte)_multipleMessagesStream.ReadByte();
var messageSize = BitConverter.ToInt32(messageSizeBytes, 0);
var messageSize = BinaryPrimitives.ReadInt32LittleEndian(messageSizeBytes.AsSpan().Slice(0));
var data = new byte[messageSize - 1];
_multipleMessagesStream.Read(data, 0, data.Length);
_lastCommunicationPacket = new CommunicationPacket(messageType, messageSize - 1, data);
Expand Down
5 changes: 3 additions & 2 deletions MySQL.Data/src/X/Communication/XPacketProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using System.Linq;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Buffers.Binary;

namespace MySql.Data.X.Communication
{
Expand Down Expand Up @@ -153,7 +154,7 @@ public CommunicationPacket GetPacketFromNetworkStream(bool readFromQueue=false)

byte[] header = new byte[HEADER_SIZE];
ReadFully(header, 0, HEADER_SIZE);
int length = BitConverter.ToInt32(header, 0);
int length = BinaryPrimitives.ReadInt32LittleEndian(header.AsSpan().Slice(0));
byte[] data = new byte[length - 1];
ReadFully(data, 0, length - 1);
// If compression is enabled and message is of type compression.
Expand All @@ -168,7 +169,7 @@ public CommunicationPacket GetPacketFromNetworkStream(bool readFromQueue=false)
{
data[i] = decompressedPayload[i + HEADER_SIZE];
}
returnPacket = new CommunicationPacket(decompressedPayload[MESSAGE_TYPE], BitConverter.ToInt32(decompressedPayload, 0) - 1, data);
returnPacket = new CommunicationPacket(decompressedPayload[MESSAGE_TYPE], BinaryPrimitives.ReadInt32LittleEndian(decompressedPayload.AsSpan().Slice(0)) - 1, data);
IdentifyPacket(returnPacket);
return returnPacket;
}
Expand Down
12 changes: 9 additions & 3 deletions MySQL.Data/src/X/Communication/XPacketReaderWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
using System;
using System.IO;
using System.Net.Sockets;
using System.Buffers.Binary;

namespace MySqlX.Communication
{
Expand Down Expand Up @@ -96,7 +97,8 @@ public void Write(int id, IMessage message)
var messageHeader = new byte[5];
var messageBytes = message.ToByteArray();
byte[] payload = new byte[messageHeader.Length + messageBytes.Length];
var sizeArray = BitConverter.GetBytes(messageSize + 1);
byte[] sizeArray = new byte[4];
BinaryPrimitives.WriteInt32LittleEndian(sizeArray,(messageSize + 1));
Buffer.BlockCopy(sizeArray, 0, messageHeader, 0, sizeArray.Length);
messageHeader[4] = (byte)id;
Buffer.BlockCopy(messageHeader, 0, payload, 0, messageHeader.Length);
Expand All @@ -108,7 +110,9 @@ public void Write(int id, IMessage message)
compression.Payload = ByteString.CopyFrom(CompressionWriteController.Compress(payload));

// Build the X Protocol frame.
_stream.Write(BitConverter.GetBytes(compression.CalculateSize() + 1), 0, 4);
byte[] bytesToWrite = new byte[4];
BinaryPrimitives.WriteInt32LittleEndian(bytesToWrite,(compression.CalculateSize() + 1));
_stream.Write(bytesToWrite, 0, 4);
_stream.WriteByte((byte)(ClientMessageId.COMPRESSION));
if (messageSize > 0)
{
Expand All @@ -117,7 +121,9 @@ public void Write(int id, IMessage message)
}
else
{
_stream.Write(BitConverter.GetBytes(messageSize + 1), 0, 4);
byte[] bytesToWrite = new byte[4];
BinaryPrimitives.WriteInt32LittleEndian(bytesToWrite,messageSize + 1);
_stream.Write(bytesToWrite, 0, 4);
_stream.WriteByte((byte)id);
if (messageSize > 0)
{
Expand Down