Socket API

Discuss topics related to development here.
Post Reply
User avatar
MarathonMan
Site Admin
Posts: 691
Joined: Fri Oct 04, 2013 4:49 pm

Socket API

Post by MarathonMan » Sun Nov 17, 2013 8:53 pm

I've started work on the socket API. CEN64 can current operate as a server and accept commands.

For now, I've only drafted up the "handshaking" process between CEN64 and frontends. I've tested the builds on Windows and Linux, and everything seems to be in working order at the moment. For those of you looking to get a headstart, an example of a client using the socket API is given here. Basically, if you pass `-port x` to CEN64, it will bind to that port and await a connection before starting the virtual console. If `-port` is not specified, CEN64 will operate in the same manner that v0.1 does.

If you pass 0 in for the port, CEN64 will ask the OS for a free port and bind to that; any non-zero number (that is a valid port) will cause CEN64 to bind to that specific port. Either way, once CEN64 binds to a port, it prints that port to stdout and waits for a client to connect. Once a connection is established, you can send messages through the socket to control CEN64 and emulation will start.

I'll work on implementing actual functionality now. :)

A prebuilt (Windows) binary can be downloaded here. Note that, as seen on the GitHub page, I have begun providing optimized versions of precompiled libraries (GLFW2, OpenAL). If you want to build your own versions of CEN64, I would encourage you to use the optimized Windows precompiled binaries, here.

User avatar
Iconoclast
Posts: 47
Joined: Fri Oct 04, 2013 8:00 pm

Re: Socket API

Post by Iconoclast » Sun Nov 17, 2013 10:05 pm

Code: Select all

  if (port < 1 || port > 65535) {
    printf("Port must in the range 1..65535.\n");
    usage(argv[0]);
    return 1;
  }
Heh. Since we are working with a minimum of the long data type, wouldn't it be cleaner and smaller to say:

Code: Select all

  if (port & ~0x0000FFFF) {
    printf("Port must be in the range 1..65535.\n");
    usage(argv[0]);
    return 1;
  }
Sure, by its self, it doesn't cover the case (port == 0), though maybe you could reserve that corner condition out as a separate means of behavior, like for the extensibility of a cool feature that might get added when triggered by a zeroed port number.

Or possibly just include (port == 0) to the same check.

Either way, slightly smaller code to generate than two inequality comparisons to nonzero integers. =D

User avatar
MarathonMan
Site Admin
Posts: 691
Joined: Fri Oct 04, 2013 4:49 pm

Re: Socket API

Post by MarathonMan » Sun Nov 17, 2013 10:21 pm

Iconoclast wrote:Either way, slightly smaller code to generate than two inequality comparisons to nonzero integers. =D
I agree that, while smaller and more efficient, I'm just leaving it be as it's much more readable. :D

If it was in a main loop or something somewhere, though, I'd probably take the performance over the readability.

User avatar
Iconoclast
Posts: 47
Joined: Fri Oct 04, 2013 8:00 pm

Re: Socket API

Post by Iconoclast » Sun Nov 17, 2013 11:20 pm

Hm...what would be at least as readable, while as efficient, would be reading it as an unsigned long.

So for your current long it looks like this:

Code: Select all

if (port < 1 || port > 65535) {
If we used something like strtoul for an unsigned long, it would just be this:

Code: Select all

if (port > 65535/* || port == 0*/) {
Which in spite of being more readable should hopefully optimize to the effective & TEST mask I wrote earlier.


But, as if you couldn't tell, I was pretty much just bored. :D

User avatar
The Extremist
Posts: 29
Joined: Sun Nov 03, 2013 6:11 pm
Location: Canadian Prairie

Re: Socket API

Post by The Extremist » Mon Nov 18, 2013 12:31 am

MarathonMan wrote:A prebuilt (Windows) binary can be downloaded here.
No way! MarathonMan's sold out to the "double-click to run" crowd! :lol:

Code: Select all

obscure linux distro command line builds 4evar /opensourceismygod

User avatar
MarathonMan
Site Admin
Posts: 691
Joined: Fri Oct 04, 2013 4:49 pm

Re: Socket API

Post by MarathonMan » Mon Nov 18, 2013 8:02 am

Iconoclast wrote:If we used something like strtoul for an unsigned long, it would just be this:

Code: Select all

if (port > 65535/* || port == 0*/) {
Ah, yes; now something like this makes much more sense to me. Maybe I'll do that. :D

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest