Search code examples
phpcphp-internals

C PHP Extension object persistance


I've developed a PHP5 client extension for a server application i wrote, and so far it's working quite well, but it doesn't support persistent connections yet. Since this is something i want to implement before releasing the first stable version, i was searching for documentation about persistance and found the persistent allocation routines ( pemalloc, pecalloc, etc ). What i can't understand is how to retrieve a persistently allocated object upon new requests, i mean, let's say that the persistent id of a connection is:

<hostname>:<port>:<timeout>

How do i save ( or check if it was already created ) the connection object ( which is a C structure, not a zval or anything strictly PHP related ) ? How can i retrieve it later given its id ?

PS: I know about PHP persistent streams ( i've studied the pfsockopen C sources ), but i use a C client library so i can't access the socket directly or modify the C client library to use php streams instead of plain sockets.

Thanks.


Solution

  • Found the solution, it seems there's a "persistent_list" hash object, so I'm able to do:

    zend_hash_find(&EG(persistent_list), ...
    

    To find persistent data ( allocd with pemalloc obviously ), and

    zend_hash_update(&EG(persistent_list), ...
    

    To save new instances.

    ( Found this in the PostgreSQL php extension source code. )

    http://devzone.zend.com/446/extension-writing-part-iii-resources/#Heading8

    Anyone interested in my approach, it's here https://github.com/evilsocket/phpgibson