I've been considering having TS compile some of the Turbo scripts if a game does not identify version, or identifies needing version 1. This way, when the game runs it gets as close to having a Sphere 1.5-like environment as its going to get with TS out of the box.
I went ahead and made a second kind of socket for TS. So, to accept a connection, you now do this:
var listener = new ListeningSocket(8192); //A constructor, otherwise a synonym for ListenOnPort
while(true){
var new_socket = listener.accept(); // Will return null if no incoming connections
if(new_socket){
// Wait until we actually have something to read.
while(!new_socket.getPendingReadSize()) Delay(10);
var buffer = new_socket.read(new_socket.getPendingReadSize());
// Write out whatever we receive, just to prove it actually happened.
var logfile = new RawFile("out.log", true);
logfile.write(buffer);
logfile.close();
new_socket.close();
break;
}
Delay(10);
}
Then, if you were going to connect, it would be much like in Sphere 1.5:
var socket = new Socket("127.0.0.1", 8192);
if(!socket.isConnected()) throw "Are you sure the server is running?";
// The `.buffer' is because ByteArrays are actually UInt8Array's under the hood. This won't be necessary at some point in the future.
socket.write(CreateByteArrayFromString("Hello, server!\n").buffer);
while(socket.isConnected()) Delay(10); // The server function should drop us pretty quickly.
Abort("Success!");
This makes the implementation much simpler on the back end compared to how Sphere 1.5 does it. Given the small number of networked games for Sphere, and how simple it is to change them to use this method if desired, I don't really see too much of a need to provide Sphere 1.5 compatibility here. Maybe if I get really ambitions I will add a ListenOnPort function in script to do it.
I have not pushed the Poll/Select/KQueue function out yet because it currently doesn't properly wake if one of the sockets is a listening socket that has an incoming connection on Windows or on OS X. So basically I need to make it actually work with Select and KQueue
I have included a simple web server in the default games to demonstrate all this with TurboSphere. I need to fix an issue with rawfile sizes before that works perfectly, but it demonstrates networking is working properly. Once Poll() works properly, then TS will really be ready for networked games!