"Gabriele Farina" <da******@extending-php.net> wrote in message
news:6E*******************@news3.tin.it...
(super snip)
Ok, tnx a lot ... I read your post, but I can't understand how can I do
correctly what you said to me.
Pheraps is should work like this:
I have to save and array of reading and writing connection, and then
manage anyone of them using a callback function. I should use
socket_select to emulate select() system call, but I can't undestand
correctly the code I have to write.
Could you post here some exaple code so I can edit it to fit my needs?
I got a good knowledge of PHP, but I lack in socket management ...
tnx a lot for the answer, bye
OK, I had a long drinking night, so I will write some easy part here, spin
this off into other parts.
the array $socketID[] will hold a resource ID for each socket
and the string $funCallBackSckRecv hold the name of the call back function
for an event driven recieve
when you need to write data, use function sckWrite() to send data, it will
que it up in a buffer and return imediately
// main management loop
// everything has to be done in slices, thus psuedo thread
// this wil ensure all sockets *apear* to send/recieve data concurently
$funCallBackSckRecv = "callBack_SocketRecieve";
while (true)
{
global $funCallBackSckRecv;
global $socketID;
global $socketBufferOut;
foreach ($socketID as $sckID)
{
// some code here
// more code here
// process socket recieved data
if ($buf = socket_read ($msgsock, 2048))
{
$funCallBackSckRecv($sckID,$buf);
}
// proccess socket output buffers, send 1024 bytes each
if ($socketBufferOut[$sckID] != "")
{
$out = substr($socketBufferOut[$sckID],0,1024);
$socketBufferOut[$sckID] = substr($socketBufferOut[$sckID],1024);
socket_write($sckID,$out);
}
// end of foreach socketID
}
// end of while true
}
// buffer output
function sckWrite($sckID,$strData)
{
global $socketBufferOut;
$socketBufferOut[$sckID] .= $strData;
}
// this is a function to proccess received data on a socket
// its a psuedo event driven function
function callBack_SocketRecieve($sckID,$data)
{
// do something here with incoming data
}
OK, off the top of my head, I would say add another array called $sckState[]
to use as a way to track the current state of a socket, ie: "WAITING" "BUSY"
"DONE" etc... depends on the app you are trying to make, and use this state
to detirmine whether it should be acepting connections etc...
if you look here:
http://www.gzentools.com/snippetview...l&v=mxmail.php you can see an
example of how I use variable $mState to hold the status of a socket. (only
single socket in this case)
Ok, my head hurts, I hope this is enough to get you off and running. Also,
if I may ask, and it is no secret, what type of service are you writing?
There might be an easier way. Sometimes you can spawn a child process that
is in its own thread.
--
Mike Bradley
http://www.gzentools.com -- free online php tools