I'd like to experiment with connecting to openMSX and sending commands from within my own scripts/tools, more precisely in C# and from Windows, since I think that would be very useful for my MSX developments. But so far my attempts have been unsuccessful, so I'm here in search for some help.
First, I've read the "Controlling openMSX from External Applications" page. I see here that it's possible to open a connection to openMSX using sockets, that's great since sockets programming in C# is a piece of cake! So here goes my first attempt:
var c = new TcpClient(AddressFamily.InterNetwork); c.Client.Connect("localhost", 9962); //I've manually read the port from the socket.pid file var responseBuffer = new byte[10000]; var size = c.Client.Receive(responseBuffer, responseBuffer.Length, SocketFlags.None); var response = Encoding.ASCII.GetString(responseBuffer, 0, size); response.Dump(); //I expect to have received "<openmsx-output>" here
But bummer, this doesn't work. The connection succeeds, but it gets closed after a few seconds without having received anything from openMSX.
By doing some googling I come up with this forum thread in which I see that apparently I need to manage some "SSPI authentication". And here goes my first set of questions: why is that? Why isn't this mentioned at all in the documentation, or in any of the documents in the openMSX GitHub repo (I have downloaded the entire repo to verify this) so I had to discover this elsewhere? Why isn't there a config/command line option in openMSX to disable the need for this authentication? That's quite frustrating.
Next I tried using named pipes, but that didn't make things easier. Yes, I can just run openmsx -control pipe
and I see the <openmsx-output>
prompt, and I guess that from that point I could send commands by typing them (although in practice, typing <openmsx-control>
+ enter throws "The syntax of the command is incorrect" and closes the program). But how do I control this from code? Second attempt:
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = @"c:\Program Files\openMSX\openmsx.exe";
process.StartInfo.Arguments = "-control pipe";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.StandardOutput.ReadToEnd().Dump();
Result: the code freezes at the last line.
Now, by looking at Catapult, I see that it executes openMSX with a command line argument like -control pipe:Catapult-8664-14
. So third attempt: running openmsx -control pipe:foobar
and then this:
var client = new NamedPipeClientStream("foobar");
client.Connect();
StreamReader reader = new StreamReader(client);
reader.ReadToEnd().Dump();
Result: the code freezes at the second line.
At this point I decided to give up for now and ask for some help, and that's what I'm doing by writing this.
I'm sure that I'm doing something wrong and that controlling openMSX programmatically is actually easier than I think, so any pointers will be appreciated; but for what's worth, and if I'm allowed to rant a little bit, I think that it shouldn't be so painful for an experienced developer to get this up and running, and it should be enough with some "official" documentation reading, without having to dive through forums or source code repos. And that's it, end of the old man yelling at a cloud for now. :)