Expose networking classes in CommApi, delegate its Lua ver. to ApiHawk

HttpTest/HttpTestGet in the .NET API now return null instead of throwing (Lua
still throws)
This commit is contained in:
YoshiRulz 2020-07-26 05:03:58 +10:00
parent da5f76967f
commit 1912851186
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 49 additions and 104 deletions

View File

@ -1,29 +1,17 @@
namespace BizHawk.Client.Common
#nullable enable
namespace BizHawk.Client.Common
{
public interface ICommApi : IExternalApi
{
string SocketServerScreenShot();
string SocketServerScreenShotResponse();
string SocketServerSend(string SendString);
string SocketServerResponse();
bool SocketServerSuccessful();
void SocketServerSetTimeout(int timeout);
HttpCommunication? HTTP { get; }
void MmfSetFilename(string filename);
string MmfGetFilename();
int MmfScreenshot();
int MmfWrite(string mmf_filename, string outputString);
string MmfRead(string mmf_filename, int expectedSize);
MemoryMappedFiles? MMF { get; }
string HttpTest();
string HttpTestGet();
string HttpGet(string url);
string HttpPost(string url, string payload);
string HttpPostScreenshot();
void HttpSetTimeout(int timeout);
void HttpSetPostUrl(string url);
void HttpSetGetUrl(string url);
string HttpGetPostUrl();
string HttpGetGetUrl();
SocketServer? Sockets { get; }
string? HttpTest();
string? HttpTestGet();
}
}

View File

@ -1,59 +1,19 @@
#nullable enable
using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk
{
public sealed class CommApi : ICommApi
{
public string SocketServerScreenShot() => GlobalWin.socketServer.SendScreenshot();
public HttpCommunication? HTTP => GlobalWin.httpCommunication;
public string SocketServerScreenShotResponse() => GlobalWin.socketServer.SendScreenshot(1000);
public MemoryMappedFiles? MMF => GlobalWin.memoryMappedFiles;
public string SocketServerSend(string SendString) => $"Sent : {GlobalWin.socketServer.SendString(SendString)} bytes";
public SocketServer? Sockets => GlobalWin.socketServer;
public string SocketServerResponse() => GlobalWin.socketServer.ReceiveMessage();
public string? HttpTest() => HTTP == null ? null : string.Join("\n", HttpTestGet(), HTTP.SendScreenshot(), "done testing");
public bool SocketServerSuccessful() => GlobalWin.socketServer.Successful;
public void SocketServerSetTimeout(int timeout) => GlobalWin.socketServer.SetTimeout(timeout);
public void SocketServerSetIp(string ip) => GlobalWin.socketServer.IP = ip;
public void SetSocketServerPort(int port) => GlobalWin.socketServer.Port = port;
public string SocketServerGetIp() => GlobalWin.socketServer.IP;
public int SocketServerGetPort() => GlobalWin.socketServer.Port;
public string SocketServerGetInfo() => GlobalWin.socketServer.GetInfo();
public void MmfSetFilename(string filename) => GlobalWin.memoryMappedFiles.Filename = filename;
public string MmfGetFilename() => GlobalWin.memoryMappedFiles.Filename;
public int MmfScreenshot() => GlobalWin.memoryMappedFiles.ScreenShotToFile();
public int MmfWrite(string mmf_filename, string outputString) => GlobalWin.memoryMappedFiles.WriteToFile(mmf_filename, outputString);
public string MmfRead(string mmf_filename, int expectedSize) => GlobalWin.memoryMappedFiles.ReadFromFile(mmf_filename, expectedSize);
public string HttpTest() => string.Join("\n", HttpTestGet(), GlobalWin.httpCommunication.SendScreenshot(), "done testing");
public string HttpTestGet() => GlobalWin.httpCommunication.Get(GlobalWin.httpCommunication.GetUrl).Result;
public string HttpGet(string url) => GlobalWin.httpCommunication.ExecGet(url);
public string HttpPost(string url, string payload) => GlobalWin.httpCommunication.ExecPost(url, payload);
public string HttpPostScreenshot() => GlobalWin.httpCommunication.SendScreenshot();
public void HttpSetTimeout(int timeout) => GlobalWin.httpCommunication.SetTimeout(timeout);
public void HttpSetPostUrl(string url) => GlobalWin.httpCommunication.PostUrl = url;
public void HttpSetGetUrl(string url) => GlobalWin.httpCommunication.GetUrl = url;
public string HttpGetPostUrl() => GlobalWin.httpCommunication.PostUrl;
public string HttpGetGetUrl() => GlobalWin.httpCommunication.GetUrl;
public string? HttpTestGet() => HTTP?.Get(HTTP.GetUrl)?.Result;
}
}

View File

@ -7,7 +7,7 @@ using System.Text;
namespace BizHawk.Client.EmuHawk
{
[Description("A library for communicating with other programs")]
public sealed class CommLuaLibrary : LuaLibraryBase
public sealed class CommLuaLibrary : DelegatingLuaLibraryEmu
{
public CommLuaLibrary(Lua lua)
: base(lua) { }
@ -30,20 +30,20 @@ namespace BizHawk.Client.EmuHawk
}
[LuaMethod("socketServerIsConnected", "socketServerIsConnected")]
public bool SocketServerIsConnected() => GlobalWin.socketServer.Connected;
public bool SocketServerIsConnected() => APIs.Comm.Sockets.Connected;
[LuaMethod("socketServerScreenShot", "sends a screenshot to the Socket server")]
public string SocketServerScreenShot()
{
CheckSocketServer();
return GlobalWin.socketServer?.SendScreenshot();
return APIs.Comm.Sockets?.SendScreenshot();
}
[LuaMethod("socketServerScreenShotResponse", "sends a screenshot to the Socket server and retrieves the response")]
public string SocketServerScreenShotResponse()
{
CheckSocketServer();
return GlobalWin.socketServer?.SendScreenshot(1000).ToString();
return APIs.Comm.Sockets?.SendScreenshot(1000).ToString();
}
[LuaMethod("socketServerSend", "sends a string to the Socket server")]
@ -53,53 +53,53 @@ namespace BizHawk.Client.EmuHawk
{
return -1;
}
return GlobalWin.socketServer.SendString(SendString);
return APIs.Comm.Sockets.SendString(SendString);
}
[LuaMethod("socketServerResponse", "receives a message from the Socket server")]
public string SocketServerResponse()
{
CheckSocketServer();
return GlobalWin.socketServer?.ReceiveMessage();
return APIs.Comm.Sockets?.ReceiveMessage();
}
[LuaMethod("socketServerSuccessful", "returns the status of the last Socket server action")]
public bool SocketServerSuccessful()
{
return CheckSocketServer() && GlobalWin.socketServer.Successful;
return CheckSocketServer() && APIs.Comm.Sockets.Successful;
}
[LuaMethod("socketServerSetTimeout", "sets the timeout in milliseconds for receiving messages")]
public void SocketServerSetTimeout(int timeout)
{
CheckSocketServer();
GlobalWin.socketServer?.SetTimeout(timeout);
APIs.Comm.Sockets?.SetTimeout(timeout);
}
[LuaMethod("socketServerSetIp", "sets the IP address of the Lua socket server")]
public void SocketServerSetIp(string ip)
{
CheckSocketServer();
GlobalWin.socketServer.IP = ip;
APIs.Comm.Sockets.IP = ip;
}
[LuaMethod("socketServerSetPort", "sets the port of the Lua socket server")]
public void SocketServerSetPort(int port)
{
CheckSocketServer();
GlobalWin.socketServer.Port = port;
APIs.Comm.Sockets.Port = port;
}
[LuaMethod("socketServerGetIp", "returns the IP address of the Lua socket server")]
public string SocketServerGetIp()
{
return GlobalWin.socketServer?.IP;
return APIs.Comm.Sockets?.IP;
}
[LuaMethod("socketServerGetPort", "returns the port of the Lua socket server")]
public int? SocketServerGetPort()
{
return GlobalWin.socketServer?.Port;
return APIs.Comm.Sockets?.Port;
}
[LuaMethod("socketServerGetInfo", "returns the IP and port of the Lua socket server")]
@ -109,12 +109,12 @@ namespace BizHawk.Client.EmuHawk
{
return "";
}
return GlobalWin.socketServer.GetInfo();
return APIs.Comm.Sockets.GetInfo();
}
private bool CheckSocketServer()
{
if (GlobalWin.socketServer == null)
if (APIs.Comm.Sockets == null)
{
Log("Socket server was not initialized, please initialize it via the command line");
return false;
@ -128,39 +128,39 @@ namespace BizHawk.Client.EmuHawk
public void MmfSetFilename(string filename)
{
CheckMmf();
GlobalWin.memoryMappedFiles.Filename = filename;
APIs.Comm.MMF.Filename = filename;
}
[LuaMethod("mmfGetFilename", "Gets the filename for the screenshots")]
public string MmfGetFilename()
{
CheckMmf();
return GlobalWin.memoryMappedFiles?.Filename;
return APIs.Comm.MMF?.Filename;
}
[LuaMethod("mmfScreenshot", "Saves screenshot to memory mapped file")]
public int MmfScreenshot()
{
CheckMmf();
return GlobalWin.memoryMappedFiles.ScreenShotToFile();
return APIs.Comm.MMF.ScreenShotToFile();
}
[LuaMethod("mmfWrite", "Writes a string to a memory mapped file")]
public int MmfWrite(string mmf_filename, string outputString)
{
CheckMmf();
return GlobalWin.memoryMappedFiles.WriteToFile(mmf_filename, outputString);
return APIs.Comm.MMF.WriteToFile(mmf_filename, outputString);
}
[LuaMethod("mmfRead", "Reads a string from a memory mapped file")]
public string MmfRead(string mmf_filename, int expectedSize)
{
CheckMmf();
return GlobalWin.memoryMappedFiles?.ReadFromFile(mmf_filename, expectedSize).ToString();
return APIs.Comm.MMF?.ReadFromFile(mmf_filename, expectedSize).ToString();
}
private void CheckMmf()
{
if (GlobalWin.memoryMappedFiles == null)
if (APIs.Comm.MMF == null)
{
Log("Memory mapped file was not initialized, please initialize it via the command line");
}
@ -170,79 +170,76 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod("httpTest", "tests HTTP connections")]
public string HttpTest()
{
var list = new StringBuilder();
list.AppendLine(GlobalWin.httpCommunication.Get(GlobalWin.httpCommunication.GetUrl).Result);
list.AppendLine(GlobalWin.httpCommunication.SendScreenshot());
list.AppendLine("done testing");
return list.ToString();
if (APIs.Comm.HTTP == null) throw new NullReferenceException(); // to match previous behaviour
return APIs.Comm.HttpTest();
}
[LuaMethod("httpTestGet", "tests the HTTP GET connection")]
public string HttpTestGet()
{
CheckHttp();
return (GlobalWin.httpCommunication?.Get(GlobalWin.httpCommunication.GetUrl))?.Result;
return APIs.Comm.HttpTestGet();
}
[LuaMethod("httpGet", "makes a HTTP GET request")]
public string HttpGet(string url)
{
CheckHttp();
return GlobalWin.httpCommunication?.ExecGet(url);
return APIs.Comm.HTTP?.ExecGet(url);
}
[LuaMethod("httpPost", "makes a HTTP POST request")]
public string HttpPost(string url, string payload)
{
CheckHttp();
return GlobalWin.httpCommunication?.ExecPost(url, payload);
return APIs.Comm.HTTP?.ExecPost(url, payload);
}
[LuaMethod("httpPostScreenshot", "HTTP POST screenshot")]
public string HttpPostScreenshot()
{
CheckHttp();
return GlobalWin.httpCommunication?.SendScreenshot();
return APIs.Comm.HTTP?.SendScreenshot();
}
[LuaMethod("httpSetTimeout", "Sets HTTP timeout in milliseconds")]
public void HttpSetTimeout(int timeout)
{
CheckHttp();
GlobalWin.httpCommunication?.SetTimeout(timeout);
APIs.Comm.HTTP?.SetTimeout(timeout);
}
[LuaMethod("httpSetPostUrl", "Sets HTTP POST URL")]
public void HttpSetPostUrl(string url)
{
CheckHttp();
GlobalWin.httpCommunication.PostUrl = url;
APIs.Comm.HTTP.PostUrl = url;
}
[LuaMethod("httpSetGetUrl", "Sets HTTP GET URL")]
public void HttpSetGetUrl(string url)
{
CheckHttp();
GlobalWin.httpCommunication.GetUrl = url;
APIs.Comm.HTTP.GetUrl = url;
}
[LuaMethod("httpGetPostUrl", "Gets HTTP POST URL")]
public string HttpGetPostUrl()
{
CheckHttp();
return GlobalWin.httpCommunication?.PostUrl;
return APIs.Comm.HTTP?.PostUrl;
}
[LuaMethod("httpGetGetUrl", "Gets HTTP GET URL")]
public string HttpGetGetUrl()
{
CheckHttp();
return GlobalWin.httpCommunication?.GetUrl;
return APIs.Comm.HTTP?.GetUrl;
}
private void CheckHttp()
{
if (GlobalWin.httpCommunication == null)
if (APIs.Comm.HTTP == null)
{
Log("HTTP was not initialized, please initialize it via the command line");
}