lua - actually send a comprehensible string over sockets instead of nonsense
This commit is contained in:
parent
f5d21a8f68
commit
b1602dae06
|
@ -67,38 +67,55 @@ namespace BizHawk.Client.Common
|
||||||
_soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
_soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
_soc.Connect(_remoteEp);
|
_soc.Connect(_remoteEp);
|
||||||
Connected = true;
|
Connected = true;
|
||||||
_soc.ReceiveTimeout = 5;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetInfo() => $"{_targetAddr.HostIP}:{_targetAddr.Port}";
|
public string GetInfo() => $"{_targetAddr.HostIP}:{_targetAddr.Port}";
|
||||||
|
|
||||||
public string ReceiveMessage(Encoding encoding = null)
|
public string ReceiveString(Encoding encoding = null)
|
||||||
{
|
{
|
||||||
if (!Connected)
|
if (!Connected)
|
||||||
{
|
{
|
||||||
Connect();
|
Connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
var receivedBytes = new byte[256];
|
var myencoding = encoding ?? Encoding.UTF8;
|
||||||
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
|
||||||
for(; ;)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
int receivedLength = _soc.Receive(receivedBytes, receivedBytes.Length, 0);
|
//build length of string into a string
|
||||||
if (receivedLength == 0)
|
byte[] oneByte = new byte[1];
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (; ; )
|
||||||
|
{
|
||||||
|
int recvd = _soc.Receive(oneByte, 1, 0);
|
||||||
|
if (oneByte[0] == (byte)' ')
|
||||||
break;
|
break;
|
||||||
ms.Write(receivedBytes, 0, receivedLength);
|
sb.Append((char)oneByte[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//receive string of indicated length
|
||||||
|
int lenStringBytes = int.Parse(sb.ToString());
|
||||||
|
byte[] buf = new byte[lenStringBytes];
|
||||||
|
int todo = lenStringBytes;
|
||||||
|
int at = 0;
|
||||||
|
for (; ; )
|
||||||
|
{
|
||||||
|
int recvd = _soc.Receive(buf, at, todo, SocketFlags.None);
|
||||||
|
if (recvd == 0)
|
||||||
|
throw new InvalidOperationException("ReceiveString terminated early");
|
||||||
|
todo -= recvd;
|
||||||
|
at += recvd;
|
||||||
|
if (todo == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return myencoding.GetString(buf, 0, lenStringBytes);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
ms.SetLength(0);
|
//not sure I like this, but that's how it was
|
||||||
break;
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var myencoding = encoding ?? Encoding.UTF8;
|
|
||||||
return myencoding.GetString(ms.ToArray(), 0, (int)ms.Length);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int SendBytes(byte[] sendBytes)
|
public int SendBytes(byte[] sendBytes)
|
||||||
{
|
{
|
||||||
|
@ -140,13 +157,23 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
return Successful ? "Screenshot was sent" : "Screenshot could not be sent";
|
return Successful ? "Screenshot was sent" : "Screenshot could not be sent";
|
||||||
}
|
}
|
||||||
var resp = ReceiveMessage();
|
var resp = ReceiveString();
|
||||||
return resp == "" ? "Failed to get a response" : resp;
|
return resp == "" ? "Failed to get a response" : resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int SendString(string sendString, Encoding encoding = null)
|
public int SendString(string sendString, Encoding encoding = null)
|
||||||
{
|
{
|
||||||
var sentBytes = SendBytes((encoding ?? Encoding.UTF8).GetBytes(sendString));
|
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());
|
||||||
|
|
||||||
Successful = sentBytes > 0;
|
Successful = sentBytes > 0;
|
||||||
return sentBytes;
|
return sentBytes;
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,7 +68,7 @@ namespace BizHawk.Client.Common
|
||||||
public string SocketServerResponse()
|
public string SocketServerResponse()
|
||||||
{
|
{
|
||||||
CheckSocketServer();
|
CheckSocketServer();
|
||||||
return APIs.Comm.Sockets?.ReceiveMessage();
|
return APIs.Comm.Sockets?.ReceiveString();
|
||||||
}
|
}
|
||||||
|
|
||||||
[LuaMethod("socketServerSuccessful", "returns the status of the last Socket server action")]
|
[LuaMethod("socketServerSuccessful", "returns the status of the last Socket server action")]
|
||||||
|
|
Loading…
Reference in New Issue