Skip to content

Commit

Permalink
Prefix socketServerScreenShot blob with length too (resolves #3461)
Browse files Browse the repository at this point in the history
fixes b1602da
  • Loading branch information
YoshiRulz committed Nov 26, 2022
1 parent b8182f9 commit c49a8d3
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/BizHawk.Client.Common/Api/SocketServer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

using BizHawk.Common.CollectionExtensions;

namespace BizHawk.Client.Common
{
public sealed class SocketServer
{
public static readonly byte[] LENGTH_PREFIX_SEPARATOR = { (byte) ' ' };

public static byte[] PrefixWithLength(byte[] payload)
=> Encoding.ASCII.GetBytes(payload.Length.ToString()).Concat(LENGTH_PREFIX_SEPARATOR).ToArray()
.ConcatArray(payload);

private IPEndPoint _remoteEp;

private Socket _soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Expand Down Expand Up @@ -134,7 +143,7 @@ public int SendBytes(byte[] sendBytes)

public string SendScreenshot(int waitingTime = 0)
{
var bmpBytes = _takeScreenshotCallback();
var bmpBytes = PrefixWithLength(_takeScreenshotCallback());
var sentBytes = 0;
var tries = 0;
while (sentBytes <= 0 && tries < Retries)
Expand Down Expand Up @@ -166,17 +175,7 @@ public string SendScreenshot(int waitingTime = 0)

public int SendString(string sendString, Encoding encoding = null)
{
var payloadBytes = (encoding ?? Encoding.UTF8).GetBytes(sendString);
var strLenOfPayloadBytes = payloadBytes.Length.ToString();
var strLenOfPayloadBytesAsBytes = Encoding.ASCII.GetBytes(strLenOfPayloadBytes);

System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Write(strLenOfPayloadBytesAsBytes, 0, strLenOfPayloadBytesAsBytes.Length);
ms.WriteByte((byte)' ');
ms.Write(payloadBytes,0,payloadBytes.Length);

int sentBytes = SendBytes(ms.ToArray());

var sentBytes = SendBytes(PrefixWithLength((encoding ?? Encoding.UTF8).GetBytes(sendString)));
Successful = sentBytes > 0;
return sentBytes;
}
Expand Down

0 comments on commit c49a8d3

Please sign in to comment.