Skip to content

Commit b1602da

Browse files
committed
lua - actually send a comprehensible string over sockets instead of nonsense
1 parent f5d21a8 commit b1602da

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

src/BizHawk.Client.Common/Api/SocketServer.cs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,37 +67,54 @@ private void Connect()
6767
_soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
6868
_soc.Connect(_remoteEp);
6969
Connected = true;
70-
_soc.ReceiveTimeout = 5;
7170
}
7271

7372
public string GetInfo() => $"{_targetAddr.HostIP}:{_targetAddr.Port}";
7473

75-
public string ReceiveMessage(Encoding encoding = null)
74+
public string ReceiveString(Encoding encoding = null)
7675
{
7776
if (!Connected)
7877
{
7978
Connect();
8079
}
8180

82-
var receivedBytes = new byte[256];
83-
System.IO.MemoryStream ms = new System.IO.MemoryStream();
84-
for(; ;)
85-
{
86-
try
87-
{
88-
int receivedLength = _soc.Receive(receivedBytes, receivedBytes.Length, 0);
89-
if (receivedLength == 0)
90-
break;
91-
ms.Write(receivedBytes, 0, receivedLength);
92-
}
93-
catch
81+
var myencoding = encoding ?? Encoding.UTF8;
82+
83+
try
84+
{
85+
//build length of string into a string
86+
byte[] oneByte = new byte[1];
87+
StringBuilder sb = new StringBuilder();
88+
for (; ; )
89+
{
90+
int recvd = _soc.Receive(oneByte, 1, 0);
91+
if (oneByte[0] == (byte)' ')
92+
break;
93+
sb.Append((char)oneByte[0]);
94+
}
95+
96+
//receive string of indicated length
97+
int lenStringBytes = int.Parse(sb.ToString());
98+
byte[] buf = new byte[lenStringBytes];
99+
int todo = lenStringBytes;
100+
int at = 0;
101+
for (; ; )
94102
{
95-
ms.SetLength(0);
96-
break;
103+
int recvd = _soc.Receive(buf, at, todo, SocketFlags.None);
104+
if (recvd == 0)
105+
throw new InvalidOperationException("ReceiveString terminated early");
106+
todo -= recvd;
107+
at += recvd;
108+
if (todo == 0)
109+
break;
97110
}
111+
return myencoding.GetString(buf, 0, lenStringBytes);
112+
}
113+
catch
114+
{
115+
//not sure I like this, but that's how it was
116+
return "";
98117
}
99-
var myencoding = encoding ?? Encoding.UTF8;
100-
return myencoding.GetString(ms.ToArray(), 0, (int)ms.Length);
101118
}
102119

103120
public int SendBytes(byte[] sendBytes)
@@ -140,13 +157,23 @@ public string SendScreenshot(int waitingTime = 0)
140157
{
141158
return Successful ? "Screenshot was sent" : "Screenshot could not be sent";
142159
}
143-
var resp = ReceiveMessage();
160+
var resp = ReceiveString();
144161
return resp == "" ? "Failed to get a response" : resp;
145162
}
146163

147164
public int SendString(string sendString, Encoding encoding = null)
148165
{
149-
var sentBytes = SendBytes((encoding ?? Encoding.UTF8).GetBytes(sendString));
166+
var payloadBytes = (encoding ?? Encoding.UTF8).GetBytes(sendString);
167+
var strLenOfPayloadBytes = payloadBytes.Length.ToString();
168+
var strLenOfPayloadBytesAsBytes = Encoding.ASCII.GetBytes(strLenOfPayloadBytes);
169+
170+
System.IO.MemoryStream ms = new System.IO.MemoryStream();
171+
ms.Write(strLenOfPayloadBytesAsBytes, 0, strLenOfPayloadBytesAsBytes.Length);
172+
ms.WriteByte((byte)' ');
173+
ms.Write(payloadBytes,0,payloadBytes.Length);
174+
175+
int sentBytes = SendBytes(ms.ToArray());
176+
150177
Successful = sentBytes > 0;
151178
return sentBytes;
152179
}

src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public int SocketServerSendBytes(LuaTable byteArray)
6868
public string SocketServerResponse()
6969
{
7070
CheckSocketServer();
71-
return APIs.Comm.Sockets?.ReceiveMessage();
71+
return APIs.Comm.Sockets?.ReceiveString();
7272
}
7373

7474
[LuaMethod("socketServerSuccessful", "returns the status of the last Socket server action")]

0 commit comments

Comments
 (0)