PHP Classes

Nearly what I need

Recommend this page to a friend!

      Socket Server  >  All threads  >  Nearly what I need  >  (Un) Subscribe thread alerts  
Subject:Nearly what I need
Summary:possible improvements
Author:Colin McKinnon
Date:2009-10-23 12:33:38
Update:2009-10-26 16:36:11


  1. Nearly what I need   Reply   Report abuse  
Picture of Colin McKinnon Colin McKinnon - 2009-10-23 12:33:38

I came across this today - I'd previously tried to write my own but so far without much success!

I'm planning on making some changes which I'll then be testing in a bit more detail (concerns regarding memory management) which I'm happy to share when (if!) I get round to it.

A small criticism:
Both the example and the motd use PHP_EOL - which returns different values depending on the OS where the script is running. Typically network protocols are platform independent (i.e. don't change behaviour depedning on the OS at either end) and, in the few occassions where that is not the case, there is some negotiation of the encoding. I would suggest that a fixed value is used.

But that's not a big issue.

For my purposes, and I expect for a lot of other people too, I want to implement a stateful system - i.e. each client connection would need a set of resources associated with it in the server-side script - but without knowing which connection a call to the handler corresponds to, it's not possible to implement this.

(yes, I know I could implement this in much the same way that HTTP/PHP sessions work - but I need to deal with the cleanup for a closed connection synchronously)

Currently new connections are added to the pool via:
$pool[] = $conn;

So it at least has a unique identifier in both $conn (which I could cast to an index key of my state data array) and a unique index in $pool (although I'm not sure about the memory management implications of adding index entries as ubove and unsetting individual elements of the array).

So, can you see any issues with adding the connection handle to the call to the user-defined function, e.g. (line 299)

$response = call_user_func($this->__handler, $request, (integer)$conn);

(do you know if the handler is always closed with an EOF when the socket closes?)



  2. Re: Nearly what I need   Reply   Report abuse  
Picture of Aleksey V. Zapparov Aleksey V. Zapparov - 2009-10-23 14:43:57 - In reply to message 1 from Colin McKinnon

Thanks for your reply. And happy that you found my class useful. Thank you for correction about PHP_EOL - don't know what I was thinking about when I changed "\n" to PHP_EOL :))

About identifier to be passed to handler. I'll add a passing some kind of environment variables to handler. So handler will be called with extra argument:

function some_handler($request, array $conn_env)
// Same as now

// Socket resource's id, e.g. 6
(integer) $conn['id'];

// Socket's remote side address name, e.g. ''
(string) $conn['address'];

// Socket's remote side port (if not UNIX socket), e.g. 56009
(integer) $conn['port'];

Hope this will be helpful. By the way there's a bug in connection addition:
$sock should be $this->__socket - I'll fix it

And I didn't understood your last question. Please explain in more details.

Sincerely yours,
Aleksey Zapparov AKA ixti.

  3. Re: Nearly what I need   Reply   Report abuse  
Picture of Colin McKinnon Colin McKinnon - 2009-10-25 22:43:23 - In reply to message 1 from Colin McKinnon
Wow - I'm impressed!

Exactly what I need!

I had wondered if an EOF would be sent back to the user function even if the socket was closed via a RST from the client - it was - not yet tried using a timeout (but I expect that will work too).

Running a lot of requests against the server (using a client invoked by apache's ab tool) shows that the memory usage rises over the first thousand or so requests then stabilises (using the supplied example.php server). So it should be quite possible to build complex servers around this code (at least if it breaks, then its my fault!).

Thank you Aleksey!


  4. Re: Nearly what I need   Reply   Report abuse  
Picture of Aleksey V. Zapparov Aleksey V. Zapparov - 2009-10-26 16:36:11 - In reply to message 3 from Colin McKinnon

First of all, thanks for your replies. About your question as far as I can see, you wonder what will be if client closed connection but server sent him data so RST is returned to server. In this case, as far as I can see, socket_write() should return false. Right now there's no tests for this situation. I'll add it, so there will be another event onError, which will be fired upon such situation.

Hope I clearly understand what did you mean. If not, please, explain me pictures :))

For more information send a message to info at phpclasses dot org.