I'm not sure what response if any I'll get here but I'm looking for some pretty strong expertise to help me with this project I'm working on.
Some Background...
I have written a socket server that is intended to serve as a backbone for multiplayer games written in Flash. I posted the source looking for comment but haven't had much luck.
The basic idea is that I'm trying to write a useful tool that can be used by Flash developers to create their own games. My main php script needs to be run from the command line so that it won't timeout. This file does a few things:
1) Includes classes written by the developer to define game-specific data and functions
2) Opens a socket to listen for new connections
3) Maintains an array of socket resource ids for connecting clients.
4) Loops endlessly responding to requests from all connected sockets by instantiating the service class requested and executing the method requested from each socket connection with the parameters provided.
An incoming request is a serialized array (thanks to sephiroth's actionscript serializer) which contains 3 elements:
1) The name of the class that should be instantiated to handle the request
2) The name of the class method which should be exectued
3) An array of parameters to be passed to the method.
The main loop takes each request, tries to instantiate the requested class and execute the requested method. If something's wrong, errors get sent back to the requesting socket.
My Questions
My concerns are part about performance and part about generality.
Question 1: What is the best way to offer global access to the array of connected clients?
As I mentioned before, the main loop maintains an array of connected clients so we know at any time who is connected and which socket they are on. The basic architecture of my project is like amfphp and thus requires developers to write classes to expand on the basic functionality. What is the best way to offer a developer access to my array of clients within the classes they must write? A global array? If so, how can I avoid variable name collisions with the developer's other code? Should the array be passed to the class constructor? Is an array the fastest way to maintain a list of clients who will have a socket id, a name, perhaps other stuff too or will we lose performance if we must keep traversing this array for each socket request?
Question 2: My Loop instantiates a service class object to handle every well-formed request received and then unsets it when finished. Is this inefficient?
With every iteration of the main loop, I instantiate the class requested (if it exists and is permitted) and execute the requested method with the supplied data. I then unset the instantiated object. Is this going to be slow? Keep in mind that I must do this for EVERY incoming request. The advantage of doing it this way was that I could store a reference to the requesting client in the instantiated class for quick access, eliminating the need to scan my client array for someone's information should it be needed. Should I instantiate each service class just once and re-use that object?
Question 3: Is there some way to run my script other than using the command line where a timeout can be avoided?
Like could I write a browser-accessible page which could fork off the daemon process and then finish? Is there some way to start the script (which is basically an endless loop with an escape sequence) from a browser for instance?