Prefix `socketServerScreenShot` blob with length too (resolves #3461)

fixes b1602dae0
This commit is contained in:
YoshiRulz 2022-11-26 18:41:33 +10:00
parent b8182f9149
commit c49a8d338c
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
1 changed files with 11 additions and 12 deletions

View File

@ -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);
@ -134,7 +143,7 @@ namespace BizHawk.Client.Common
public string SendScreenshot(int waitingTime = 0)
{
var bmpBytes = _takeScreenshotCallback();
var bmpBytes = PrefixWithLength(_takeScreenshotCallback());
var sentBytes = 0;
var tries = 0;
while (sentBytes <= 0 && tries < Retries)
@ -166,17 +175,7 @@ namespace BizHawk.Client.Common
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;
}