Squash merge #1505 - set socketServer IP/port via Lua (resolves #1495)

This commit is contained in:
Maximilian Peters 2019-05-29 07:06:38 +02:00 committed by James Groom
parent 27a4062ea2
commit 80c0fe571b
8 changed files with 298 additions and 143 deletions

View File

@ -13,7 +13,7 @@
#region MemoryMappedFiles #region MemoryMappedFiles
void MmfSetFilename(string filename); void MmfSetFilename(string filename);
string MmfSetFilename(); string MmfGetFilename();
int MmfScreenshot(); int MmfScreenshot();
int MmfWrite(string mmf_filename, string outputString); int MmfWrite(string mmf_filename, string outputString);
string MmfRead(string mmf_filename, int expectedSize); string MmfRead(string mmf_filename, int expectedSize);

View File

@ -48,14 +48,41 @@ namespace BizHawk.Client.EmuHawk
{ {
GlobalWin.socketServer.SetTimeout(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()
{
return GlobalWin.socketServer.Ip;
}
public int SocketServerGetPort()
{
return GlobalWin.socketServer.Port;
}
public string SocketServerGetInfo()
{
return GlobalWin.socketServer.GetInfo();
}
// All MemoryMappedFile related methods // All MemoryMappedFile related methods
public void MmfSetFilename(string filename) public void MmfSetFilename(string filename)
{ {
GlobalWin.memoryMappedFiles.SetFilename(filename); GlobalWin.memoryMappedFiles.Filename = filename;
} }
public string MmfSetFilename()
public string MmfGetFilename()
{ {
return GlobalWin.memoryMappedFiles.GetFilename(); return GlobalWin.memoryMappedFiles.Filename;
} }
public int MmfScreenshot() public int MmfScreenshot()
@ -67,10 +94,13 @@ namespace BizHawk.Client.EmuHawk
{ {
return GlobalWin.memoryMappedFiles.WriteToFile(mmf_filename, Encoding.ASCII.GetBytes(outputString)); return GlobalWin.memoryMappedFiles.WriteToFile(mmf_filename, Encoding.ASCII.GetBytes(outputString));
} }
public string MmfRead(string mmf_filename, int expectedSize) public string MmfRead(string mmf_filename, int expectedSize)
{ {
return GlobalWin.memoryMappedFiles.ReadFromFile(mmf_filename, expectedSize).ToString(); return GlobalWin.memoryMappedFiles.ReadFromFile(mmf_filename, expectedSize).ToString();
} }
// All HTTP related methods // All HTTP related methods
public string HttpTest() public string HttpTest()
{ {
@ -103,19 +133,19 @@ namespace BizHawk.Client.EmuHawk
} }
public void HttpSetPostUrl(string url) public void HttpSetPostUrl(string url)
{ {
GlobalWin.httpCommunication.SetPostUrl(url); GlobalWin.httpCommunication.PostUrl = url;
} }
public void HttpSetGetUrl(string url) public void HttpSetGetUrl(string url)
{ {
GlobalWin.httpCommunication.SetGetUrl(url); GlobalWin.httpCommunication.GetUrl = url;
} }
public string HttpGetPostUrl() public string HttpGetPostUrl()
{ {
return GlobalWin.httpCommunication.GetPostUrl(); return GlobalWin.httpCommunication.PostUrl;
} }
public string HttpGetGetUrl() public string HttpGetGetUrl()
{ {
return GlobalWin.httpCommunication.GetGetUrl(); return GlobalWin.httpCommunication.GetUrl;
} }
} }
} }

View File

@ -2,14 +2,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.IO; using System.IO;
using System.Windows.Forms;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
class ArgParser class ArgParser
//parses command line arguments and adds the values to a class attribute //parses command line arguments and adds the values to a class attribute
//default values are null for strings and false for boolean //default values are null for strings and false for boolean
//the last value will overwrite previously set values //the last value will overwrite previously set values
//unrecognized parameters are simply ignored or in the worst case assumed to be a ROM name [cmdRom] //unrecognized parameters are simply ignored or in the worst case assumed to be a ROM name [cmdRom]
{ {
public string cmdRom = null; public string cmdRom = null;
public string cmdLoadSlot = null; public string cmdLoadSlot = null;
@ -21,13 +22,13 @@ namespace BizHawk.Client.EmuHawk
public string cmdDumpName = null; public string cmdDumpName = null;
public HashSet<int> _currAviWriterFrameList; public HashSet<int> _currAviWriterFrameList;
public int _autoDumpLength; public int _autoDumpLength;
public bool _autoCloseOnDump = false; public bool _autoCloseOnDump = false;
// chrome is never shown, even in windowed mode // chrome is never shown, even in windowed mode
public bool _chromeless = false; public bool _chromeless = false;
public bool startFullscreen = false; public bool startFullscreen = false;
public string luaScript = null; public string luaScript = null;
public bool luaConsole = false; public bool luaConsole = false;
public int socket_port = 9999; public int socket_port = 0;
public string socket_ip = null; public string socket_ip = null;
public string mmf_filename = null; public string mmf_filename = null;
public string URL_get = null; public string URL_get = null;
@ -35,15 +36,14 @@ namespace BizHawk.Client.EmuHawk
public bool? audiosync = null; public bool? audiosync = null;
public void ParseArguments(string[] args) public void ParseArguments(string[] args)
{ {
for (int i = 0; i < args.Length; i++) for (int i = 0; i < args.Length; i++)
{ {
// For some reason sometimes visual studio will pass this to us on the commandline. it makes no sense. // For some reason sometimes visual studio will pass this to us on the commandline. it makes no sense.
if (args[i] == ">") if (args[i] == ">")
{ {
i++; i++;
var stdout = args[i]; var stdout = args[i];
Console.SetOut(new StreamWriter(stdout)); Console.SetOut(new StreamWriter(stdout));
continue; continue;
} }
@ -78,9 +78,9 @@ namespace BizHawk.Client.EmuHawk
foreach (string item in items) foreach (string item in items)
{ {
_currAviWriterFrameList.Add(int.Parse(item)); _currAviWriterFrameList.Add(int.Parse(item));
} }
// automatically set dump length to maximum frame // automatically set dump length to maximum frame
_autoDumpLength = _currAviWriterFrameList.OrderBy(x => x).Last(); _autoDumpLength = _currAviWriterFrameList.OrderBy(x => x).Last();
} }
else if (arg.StartsWith("--dump-name=")) else if (arg.StartsWith("--dump-name="))
@ -140,39 +140,55 @@ namespace BizHawk.Client.EmuHawk
{ {
cmdRom = args[i]; cmdRom = args[i];
} }
} }
////initialize HTTP communication
//initialize HTTP communication
if (URL_get != null || URL_post != null) if (URL_get != null || URL_post != null)
{ {
GlobalWin.httpCommunication = new Communication.HttpCommunication();
if (URL_get != null) if (URL_get != null)
{ {
GlobalWin.httpCommunication.initialized = true; GlobalWin.httpCommunication.GetUrl = URL_get;
GlobalWin.httpCommunication.SetGetUrl(URL_get);
} }
if (URL_post != null) if (URL_post != null)
{ {
GlobalWin.httpCommunication.initialized = true; GlobalWin.httpCommunication.PostUrl = URL_post;
GlobalWin.httpCommunication.SetPostUrl(URL_post);
} }
} }
//inititalize socket server
if (socket_ip != null && socket_port > -1) //inititalize socket server
{ if (socket_ip != null && socket_port > 0)
GlobalWin.socketServer.initialized = true; {
GlobalWin.socketServer = new Communication.SocketServer();
GlobalWin.socketServer.SetIp(socket_ip, socket_port); GlobalWin.socketServer.SetIp(socket_ip, socket_port);
} }
else if (socket_ip != null) else if (socket_ip == null ^ socket_port == 0)
{ {
GlobalWin.socketServer.initialized = true; throw new ArgParserException("Socket server needs both --socket_ip and --socket_port. Socket server was not started");
GlobalWin.socketServer.SetIp(socket_ip); }
}
//initialize mapped memory files
//initialize mapped memory files
if (mmf_filename != null) if (mmf_filename != null)
{ {
GlobalWin.memoryMappedFiles.initialized = true; GlobalWin.memoryMappedFiles = new Communication.MemoryMappedFiles();
GlobalWin.memoryMappedFiles.SetFilename(mmf_filename); GlobalWin.memoryMappedFiles.Filename = mmf_filename;
} }
} }
public static string GetCmdConfigFile(string[] args)
{
return args.FirstOrDefault(arg => arg.StartsWith("--config=", StringComparison.InvariantCultureIgnoreCase))?.Substring(9);
}
}
class ArgParserException : Exception
{
public ArgParserException()
{
}
public ArgParserException(string message) : base(message)
{
}
} }
} }

View File

@ -12,7 +12,6 @@ using BizHawk.Emulation.Common;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Emulation.Common.IEmulatorExtensions;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
@ -23,16 +22,14 @@ namespace BizHawk.Client.EmuHawk
public class HttpCommunication public class HttpCommunication
{ {
private static HttpClient client = new HttpClient(); private static HttpClient client = new HttpClient();
private string PostUrl = "http://localhost:9876/post/"; public string PostUrl { get; set; } = null;
private string GetUrl = "http://localhost:9876/index"; public string GetUrl { get; set; } = null;
public bool initialized = false;
private ScreenShot screenShot = new ScreenShot(); private ScreenShot screenShot = new ScreenShot();
public int timeout = 0; public int timeout = 0;
public int default_timeout = 500; public int default_timeout = 500;
public void SetTimeout(int _timeout) public void SetTimeout(int _timeout)
{ {
//timeout = _timeout.TotalMilliseconds;
if (timeout == 0 && _timeout == 0) if (timeout == 0 && _timeout == 0)
{ {
timeout = default_timeout; timeout = default_timeout;
@ -41,32 +38,15 @@ namespace BizHawk.Client.EmuHawk
{ {
client.Timeout = new TimeSpan(0, 0, 0, _timeout / 1000, _timeout % 1000); client.Timeout = new TimeSpan(0, 0, 0, _timeout / 1000, _timeout % 1000);
timeout = _timeout; timeout = _timeout;
} }
}
public void SetPostUrl(string url)
{
PostUrl = url;
}
public void SetGetUrl(string url)
{
GetUrl = url;
}
public string GetGetUrl()
{
return GetUrl;
}
public string GetPostUrl()
{
return PostUrl;
} }
public async Task<string> Get(string url) public async Task<string> Get(string url)
{ {
client.DefaultRequestHeaders.ConnectionClose = false; client.DefaultRequestHeaders.ConnectionClose = false;
HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false); HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);
if (response.IsSuccessStatusCode) { if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync(); return await response.Content.ReadAsStringAsync();
} }
else else
@ -82,11 +62,9 @@ namespace BizHawk.Client.EmuHawk
try try
{ {
response = await client.PostAsync(url, content).ConfigureAwait(false); response = await client.PostAsync(url, content).ConfigureAwait(false);
} }
catch (Exception e) catch (Exception e)
{ {
MessageBox.Show(e.ToString());
return e.ToString(); return e.ToString();
} }
@ -161,19 +139,40 @@ namespace BizHawk.Client.EmuHawk
return Post(PostUrl, content).Result; return Post(PostUrl, content).Result;
} }
} }
public class SocketServer public class SocketServer
{ {
string ip = null;
public string Ip
{
get { return ip; }
set
{
ip = value;
ipAdd = System.Net.IPAddress.Parse(ip);
Connect();
}
}
public string ip = "192.168.178.21"; int port = 0;
public int port = 9999; public int Port
public Decoder decoder = Encoding.UTF8.GetDecoder(); {
public Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); get { return port; }
public IPAddress ipAdd; set
public IPEndPoint remoteEP; {
public IVideoProvider currentVideoProvider = null; port = value;
Connect();
}
}
Decoder decoder = Encoding.UTF8.GetDecoder();
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAdd;
IPEndPoint remoteEP;
IVideoProvider currentVideoProvider = null;
public bool connected = false; public bool connected = false;
public bool initialized = false; public bool initialized = false;
public int retries = 10; public int Retries { get; set; } = 10;
public bool success = false; //indicates whether the last command was executed succesfully public bool success = false; //indicates whether the last command was executed succesfully
public void Initialize(IVideoProvider _currentVideoProvider) public void Initialize(IVideoProvider _currentVideoProvider)
@ -181,8 +180,8 @@ namespace BizHawk.Client.EmuHawk
currentVideoProvider = _currentVideoProvider; currentVideoProvider = _currentVideoProvider;
SetIp(ip, port); SetIp(ip, port);
initialized = true; initialized = true;
} }
public void Connect() public void Connect()
{ {
if (!initialized) if (!initialized)
@ -194,42 +193,40 @@ namespace BizHawk.Client.EmuHawk
soc.Connect(remoteEP); soc.Connect(remoteEP);
connected = true; connected = true;
soc.ReceiveTimeout = 5; soc.ReceiveTimeout = 5;
}
public void SetIp(string ip_)
{
ip = ip_;
ipAdd = System.Net.IPAddress.Parse(ip);
remoteEP = new IPEndPoint(ipAdd, port);
} }
public void SetIp(string ip_, int port_) public void SetIp(string ip_, int port_)
{ {
ip = ip_; ip = ip_;
port = port_; port = port_;
ipAdd = System.Net.IPAddress.Parse(ip); ipAdd = System.Net.IPAddress.Parse(ip);
remoteEP = new IPEndPoint(ipAdd, port); remoteEP = new IPEndPoint(ipAdd, port);
} }
public string GetInfo()
{
return $"{ip}:{port}";
}
public void SetTimeout(int timeout) public void SetTimeout(int timeout)
{ {
soc.ReceiveTimeout = timeout; soc.ReceiveTimeout = timeout;
} }
public void SocketConnected() public void SocketConnected()
{ {
bool part1 = soc.Poll(1000, SelectMode.SelectRead); bool part1 = soc.Poll(1000, SelectMode.SelectRead);
bool part2 = (soc.Available == 0); bool part2 = (soc.Available == 0);
if (part1 && part2) connected = !(part1 && part2);
connected = false;
else
connected = true;
} }
public int SendString(string SendString) public int SendString(string SendString)
{ {
int sentBytes = SendBytes(Encoding.ASCII.GetBytes(SendString)); int sentBytes = SendBytes(Encoding.ASCII.GetBytes(SendString));
success = sentBytes > 0; success = sentBytes > 0;
return sentBytes; return sentBytes;
} }
public int SendBytes(byte[] SendBytes) public int SendBytes(byte[] SendBytes)
{ {
int sentBytes = 0; int sentBytes = 0;
@ -248,6 +245,7 @@ namespace BizHawk.Client.EmuHawk
{ {
return SendScreenshot(0); return SendScreenshot(0);
} }
public string SendScreenshot(int waitingTime) public string SendScreenshot(int waitingTime)
{ {
if (!connected) if (!connected)
@ -262,7 +260,7 @@ namespace BizHawk.Client.EmuHawk
byte[] bmpBytes = screenShot.ImageToByte(img); byte[] bmpBytes = screenShot.ImageToByte(img);
int sentBytes = 0; int sentBytes = 0;
int tries = 0; int tries = 0;
while (sentBytes <= 0 && tries < retries) while (sentBytes <= 0 && tries < Retries)
{ {
try try
{ {
@ -279,7 +277,7 @@ namespace BizHawk.Client.EmuHawk
Connect(); Connect();
} }
} }
success = (tries < retries); success = (tries < Retries);
} }
} }
String resp = ""; String resp = "";
@ -294,7 +292,7 @@ namespace BizHawk.Client.EmuHawk
{ {
return resp; return resp;
} }
resp = "";
resp = ReceiveMessage(); resp = ReceiveMessage();
if (resp == "") if (resp == "")
{ {
@ -302,6 +300,7 @@ namespace BizHawk.Client.EmuHawk
} }
return resp; return resp;
} }
public string ReceiveMessage() public string ReceiveMessage()
{ {
if (!connected) if (!connected)
@ -318,14 +317,15 @@ namespace BizHawk.Client.EmuHawk
{ {
receivedLength = soc.Receive(receivedBytes, receivedBytes.Length, 0); receivedLength = soc.Receive(receivedBytes, receivedBytes.Length, 0);
resp += Encoding.ASCII.GetString(receivedBytes); resp += Encoding.ASCII.GetString(receivedBytes);
} catch }
catch
{ {
receivedLength = 0; receivedLength = 0;
} }
} }
return resp; return resp;
} }
public bool Successful() public bool Successful()
{ {
return success; return success;
@ -334,29 +334,19 @@ namespace BizHawk.Client.EmuHawk
public class MemoryMappedFiles public class MemoryMappedFiles
{ {
public string filename_main = "BizhawkTemp_main"; public string Filename { get; set; } = "BizhawkTemp_main";
public Dictionary<string, MemoryMappedFile> mmf_files = new Dictionary<string, MemoryMappedFile>(); public Dictionary<string, MemoryMappedFile> mmf_files = new Dictionary<string, MemoryMappedFile>();
public int index = 0; public int index = 0;
public bool initialized = false;
public int main_size = 10 ^ 5; public int main_size = 10 ^ 5;
ScreenShot screenShot = new ScreenShot(); ScreenShot screenShot = new ScreenShot();
public void SetFilename(string filename)
{
filename_main = filename;
}
public string GetFilename()
{
return filename_main;
}
public int ScreenShotToFile() public int ScreenShotToFile()
{ {
ScreenShot screenShot = new ScreenShot(); ScreenShot screenShot = new ScreenShot();
var bb = screenShot.MakeScreenShotImage(); var bb = screenShot.MakeScreenShotImage();
var img = bb.ToSysdrawingBitmap(); var img = bb.ToSysdrawingBitmap();
byte[] bmpBytes = screenShot.ImageToByte(img); byte[] bmpBytes = screenShot.ImageToByte(img);
return WriteToFile(@filename_main, bmpBytes); return WriteToFile(@Filename, bmpBytes);
} }
public int WriteToFile(string filename, byte[] outputBytes) public int WriteToFile(string filename, byte[] outputBytes)
@ -384,7 +374,6 @@ namespace BizHawk.Client.EmuHawk
} }
catch (Exception) catch (Exception)
{ {
} }
mmf_file = MemoryMappedFile.CreateOrOpen(filename, outputBytes.Length); mmf_file = MemoryMappedFile.CreateOrOpen(filename, outputBytes.Length);
@ -417,6 +406,7 @@ namespace BizHawk.Client.EmuHawk
{ {
private IVideoProvider currentVideoProvider = null; private IVideoProvider currentVideoProvider = null;
private ImageConverter converter = new ImageConverter(); private ImageConverter converter = new ImageConverter();
public BitmapBuffer MakeScreenShotImage() public BitmapBuffer MakeScreenShotImage()
{ {
if (currentVideoProvider == null) if (currentVideoProvider == null)
@ -425,14 +415,17 @@ namespace BizHawk.Client.EmuHawk
} }
return GlobalWin.DisplayManager.RenderVideoProvider(currentVideoProvider); return GlobalWin.DisplayManager.RenderVideoProvider(currentVideoProvider);
} }
public byte[] ImageToByte(Image img) public byte[] ImageToByte(Image img)
{ {
return (byte[])converter.ConvertTo(img, typeof(byte[])); return (byte[])converter.ConvertTo(img, typeof(byte[]));
} }
public string ImageToString(Image img) public string ImageToString(Image img)
{ {
return Convert.ToBase64String(ImageToByte(img)); return Convert.ToBase64String(ImageToByte(img));
} }
public string GetScreenShotAsString() public string GetScreenShotAsString()
{ {
BitmapBuffer bb = MakeScreenShotImage(); BitmapBuffer bb = MakeScreenShotImage();
@ -440,6 +433,18 @@ namespace BizHawk.Client.EmuHawk
return Convert.ToBase64String(imgBytes); return Convert.ToBase64String(imgBytes);
} }
} }
class CommunicationSocketServerException : Exception
{
public CommunicationSocketServerException()
{
}
public CommunicationSocketServerException(string message) : base(message)
{
}
}
} }
} }

View File

@ -26,8 +26,8 @@ namespace BizHawk.Client.EmuHawk
public static GLManager GLManager; public static GLManager GLManager;
public static int ExitCode; public static int ExitCode;
public static Communication.HttpCommunication httpCommunication = new Communication.HttpCommunication(); public static Communication.HttpCommunication httpCommunication = null;
public static Communication.SocketServer socketServer = new Communication.SocketServer(); public static Communication.SocketServer socketServer = null;
public static Communication.MemoryMappedFiles memoryMappedFiles = new Communication.MemoryMappedFiles(); public static Communication.MemoryMappedFiles memoryMappedFiles = null;
} }
} }

View File

@ -155,8 +155,15 @@ namespace BizHawk.Client.EmuHawk
.ReadAllBytes(); .ReadAllBytes();
} }
}; };
try
argParser.ParseArguments(args); {
argParser.ParseArguments(args);
}
catch (ArgParserException e)
{
MessageBox.Show(e.Message);
}
Database.LoadDatabase(Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "gamedb.txt")); Database.LoadDatabase(Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "gamedb.txt"));
@ -1398,6 +1405,7 @@ namespace BizHawk.Client.EmuHawk
private int _lastOpenRomFilter; private int _lastOpenRomFilter;
private ArgParser argParser = new ArgParser(); private ArgParser argParser = new ArgParser();
// Resources // Resources
private Bitmap _statusBarDiskLightOnImage; private Bitmap _statusBarDiskLightOnImage;
private Bitmap _statusBarDiskLightOffImage; private Bitmap _statusBarDiskLightOffImage;

View File

@ -113,9 +113,8 @@ namespace BizHawk.Client.EmuHawk
HawkFile.ArchiveHandlerFactory = new SevenZipSharpArchiveHandler(); HawkFile.ArchiveHandlerFactory = new SevenZipSharpArchiveHandler();
var argParser = new ArgParser(); string cmdConfigFile = ArgParser.GetCmdConfigFile(args);
argParser.ParseArguments(args); if (cmdConfigFile != null) PathManager.SetDefaultIniPath(cmdConfigFile);
if (argParser.cmdConfigFile != null) PathManager.SetDefaultIniPath(argParser.cmdConfigFile);
try try
{ {

View File

@ -4,10 +4,6 @@ using NLua;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using System.Text; using System.Text;
using System.Collections.Generic;
using System.Net.Http;
using System.Windows.Forms;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
@ -37,70 +33,146 @@ namespace BizHawk.Client.EmuHawk
{ {
list.AppendLine(function.ToString()); list.AppendLine(function.ToString());
} }
return list.ToString(); return list.ToString();
} }
[LuaMethod("socketServerScreenShot", "sends a screenshot to the Socket server")] [LuaMethod("socketServerScreenShot", "sends a screenshot to the Socket server")]
public string SocketServerScreenShot() public string SocketServerScreenShot()
{ {
return GlobalWin.socketServer.SendScreenshot(); CheckSocketServer();
return GlobalWin.socketServer?.SendScreenshot();
} }
[LuaMethod("socketServerScreenShotResponse", "sends a screenshot to the Socket server and retrieves the response")] [LuaMethod("socketServerScreenShotResponse", "sends a screenshot to the Socket server and retrieves the response")]
public string SocketServerScreenShotResponse() public string SocketServerScreenShotResponse()
{ {
return GlobalWin.socketServer.SendScreenshot(1000).ToString(); CheckSocketServer();
return GlobalWin.socketServer?.SendScreenshot(1000).ToString();
} }
[LuaMethod("socketServerSend", "sends a string to the Socket server")] [LuaMethod("socketServerSend", "sends a string to the Socket server")]
public string SocketServerSend(string SendString) public int SocketServerSend(string SendString)
{ {
return $"Sent : {GlobalWin.socketServer.SendString(SendString)} bytes"; if (!CheckSocketServer())
{
return -1;
}
return GlobalWin.socketServer.SendString(SendString);
} }
[LuaMethod("socketServerResponse", "receives a message from the Socket server")] [LuaMethod("socketServerResponse", "receives a message from the Socket server")]
public string SocketServerResponse() public string SocketServerResponse()
{ {
return GlobalWin.socketServer.ReceiveMessage(); CheckSocketServer();
return GlobalWin.socketServer?.ReceiveMessage();
} }
[LuaMethod("socketServerSuccessful", "returns the status of the last Socket server action")] [LuaMethod("socketServerSuccessful", "returns the status of the last Socket server action")]
public bool SocketServerSuccessful() public bool SocketServerSuccessful()
{ {
if (!CheckSocketServer())
{
return false;
}
return GlobalWin.socketServer.Successful(); return GlobalWin.socketServer.Successful();
} }
[LuaMethod("socketServerSetTimeout", "sets the timeout in milliseconds for receiving messages")] [LuaMethod("socketServerSetTimeout", "sets the timeout in milliseconds for receiving messages")]
public void SocketServerSetTimeout(int timeout) public void SocketServerSetTimeout(int timeout)
{ {
GlobalWin.socketServer.SetTimeout(timeout); CheckSocketServer();
GlobalWin.socketServer?.SetTimeout(timeout);
} }
[LuaMethod("socketServerSetIp", "sets the IP address of the Lua socket server")]
public void SocketServerSetIp(string ip)
{
CheckSocketServer();
GlobalWin.socketServer.Ip = ip;
}
[LuaMethod("socketServerSetPort", "sets the port of the Lua socket server")]
public void SocketServerSetPort(int port)
{
CheckSocketServer();
GlobalWin.socketServer.Port = port;
}
[LuaMethod("socketServerGetIp", "returns the IP address of the Lua socket server")]
public string SocketServerGetIp()
{
return GlobalWin.socketServer?.Ip;
}
[LuaMethod("socketServerGetPort", "returns the port of the Lua socket server")]
public int? SocketServerGetPort()
{
return GlobalWin.socketServer?.Port;
}
[LuaMethod("socketServerGetInfo", "returns the IP and port of the Lua socket server")]
public string SocketServerGetInfo()
{
if (!CheckSocketServer())
{
return "";
}
return GlobalWin.socketServer.GetInfo();
}
private bool CheckSocketServer()
{
if (GlobalWin.socketServer == null)
{
Log("Socket server was not initialized, please initialize it via the command line");
return false;
}
return true;
}
// All MemoryMappedFile related methods // All MemoryMappedFile related methods
[LuaMethod("mmfSetFilename", "Sets the filename for the screenshots")] [LuaMethod("mmfSetFilename", "Sets the filename for the screenshots")]
public void MmfSetFilename(string filename) public void MmfSetFilename(string filename)
{ {
GlobalWin.memoryMappedFiles.SetFilename(filename); CheckMmf();
GlobalWin.memoryMappedFiles.Filename = filename;
} }
[LuaMethod("mmfGetFilename", "Gets the filename for the screenshots")] [LuaMethod("mmfGetFilename", "Gets the filename for the screenshots")]
public string MmfSetFilename() public string MmfGetFilename()
{ {
return GlobalWin.memoryMappedFiles.GetFilename(); CheckMmf();
return GlobalWin.memoryMappedFiles?.Filename;
} }
[LuaMethod("mmfScreenshot", "Saves screenshot to memory mapped file")] [LuaMethod("mmfScreenshot", "Saves screenshot to memory mapped file")]
public int MmfScreenshot() public int MmfScreenshot()
{ {
CheckMmf();
return GlobalWin.memoryMappedFiles.ScreenShotToFile(); return GlobalWin.memoryMappedFiles.ScreenShotToFile();
} }
[LuaMethod("mmfWrite", "Writes a string to a memory mapped file")] [LuaMethod("mmfWrite", "Writes a string to a memory mapped file")]
public int MmfWrite(string mmf_filename, string outputString) public int MmfWrite(string mmf_filename, string outputString)
{ {
CheckMmf();
return GlobalWin.memoryMappedFiles.WriteToFile(mmf_filename, Encoding.ASCII.GetBytes(outputString)); return GlobalWin.memoryMappedFiles.WriteToFile(mmf_filename, Encoding.ASCII.GetBytes(outputString));
} }
[LuaMethod("mmfRead", "Reads a string from a memory mapped file")] [LuaMethod("mmfRead", "Reads a string from a memory mapped file")]
public string MmfRead(string mmf_filename, int expectedSize) public string MmfRead(string mmf_filename, int expectedSize)
{ {
return GlobalWin.memoryMappedFiles.ReadFromFile(mmf_filename, expectedSize).ToString(); CheckMmf();
return GlobalWin.memoryMappedFiles?.ReadFromFile(mmf_filename, expectedSize).ToString();
} }
private void CheckMmf()
{
if (GlobalWin.memoryMappedFiles == null)
{
Log("Memory mapped file was not initialized, please initialize it via the command line");
}
}
// All HTTP related methods // All HTTP related methods
[LuaMethod("httpTest", "tests HTTP connections")] [LuaMethod("httpTest", "tests HTTP connections")]
public string HttpTest() public string HttpTest()
@ -111,51 +183,76 @@ namespace BizHawk.Client.EmuHawk
list.AppendLine("done testing"); list.AppendLine("done testing");
return list.ToString(); return list.ToString();
} }
[LuaMethod("httpTestGet", "tests the HTTP GET connection")] [LuaMethod("httpTestGet", "tests the HTTP GET connection")]
public string HttpTestGet() public string HttpTestGet()
{ {
return GlobalWin.httpCommunication.TestGet(); CheckHttp();
return GlobalWin.httpCommunication?.TestGet();
} }
[LuaMethod("httpGet", "makes a HTTP GET request")] [LuaMethod("httpGet", "makes a HTTP GET request")]
public string HttpGet(string url) public string HttpGet(string url)
{ {
return GlobalWin.httpCommunication.ExecGet(url); CheckHttp();
return GlobalWin.httpCommunication?.ExecGet(url);
} }
[LuaMethod("httpPost", "makes a HTTP POST request")] [LuaMethod("httpPost", "makes a HTTP POST request")]
public string HttpPost(string url, string payload) public string HttpPost(string url, string payload)
{ {
return GlobalWin.httpCommunication.ExecPost(url, payload); CheckHttp();
return GlobalWin.httpCommunication?.ExecPost(url, payload);
} }
[LuaMethod("httpPostScreenshot", "HTTP POST screenshot")] [LuaMethod("httpPostScreenshot", "HTTP POST screenshot")]
public string HttpPostScreenshot() public string HttpPostScreenshot()
{ {
return GlobalWin.httpCommunication.SendScreenshot(); CheckHttp();
return GlobalWin.httpCommunication?.SendScreenshot();
} }
[LuaMethod("httpSetTimeout", "Sets HTTP timeout in milliseconds")] [LuaMethod("httpSetTimeout", "Sets HTTP timeout in milliseconds")]
public void HttpSetTimeout(int timeout) public void HttpSetTimeout(int timeout)
{ {
GlobalWin.httpCommunication.SetTimeout(timeout); CheckHttp();
GlobalWin.httpCommunication?.SetTimeout(timeout);
} }
[LuaMethod("httpSetPostUrl", "Sets HTTP POST URL")] [LuaMethod("httpSetPostUrl", "Sets HTTP POST URL")]
public void HttpSetPostUrl(string url) public void HttpSetPostUrl(string url)
{ {
GlobalWin.httpCommunication.SetPostUrl(url); CheckHttp();
GlobalWin.httpCommunication.PostUrl = url;
} }
[LuaMethod("httpSetGetUrl", "Sets HTTP GET URL")] [LuaMethod("httpSetGetUrl", "Sets HTTP GET URL")]
public void HttpSetGetUrl(string url) public void HttpSetGetUrl(string url)
{ {
GlobalWin.httpCommunication.SetGetUrl(url); CheckHttp();
GlobalWin.httpCommunication.GetUrl = url;
} }
[LuaMethod("httpGetPostUrl", "Gets HTTP POST URL")] [LuaMethod("httpGetPostUrl", "Gets HTTP POST URL")]
public string HttpGetPostUrl() public string HttpGetPostUrl()
{ {
return GlobalWin.httpCommunication.GetPostUrl(); CheckHttp();
return GlobalWin.httpCommunication?.PostUrl;
} }
[LuaMethod("httpGetGetUrl", "Gets HTTP GET URL")] [LuaMethod("httpGetGetUrl", "Gets HTTP GET URL")]
public string HttpGetGetUrl() public string HttpGetGetUrl()
{ {
return GlobalWin.httpCommunication.GetGetUrl(); CheckHttp();
return GlobalWin.httpCommunication?.GetUrl;
}
private void CheckHttp()
{
if (GlobalWin.httpCommunication == null)
{
Log("HTTP was not initialized, please initialize it via the command line");
}
} }
} }
} }