To All, 

While examining a problem pointed out by a sockets extension user I
noticed a consistency problem with some of the functions. While working
on a simple patch to correct this issue I decided to review the entire
module, and I noticed a lot of problems. 

They can be outlined as follows: 

Problems
--------

1. Consistency problems 
    a. Some functions take host, some take ip, some take both 
    b. Parts of the code use socklen_t, parts use int 
    c. Windows code doesn't support PHP_NORMAL_READ 
    d. Some protos are off with the function 
2. Logic Problems 
    a. Max fd code does not work correctly (wrong approach) 
    b. socket_recv does not properly handle error conditions 
3. API problems 
    a. Read functions make it difficult to error-check and 
       track a safe connection close 
    b. socket_create_listen only allows a listener to be 
       installed on the local interface 
    c. socket_last_error clears the last error after reading (WTF) 
    d. socket_fd code makes little sense in this style, and 
       introduces problems that the max_fd code cannot solve 

Instead of just committing a gigantic patch to solve these problems, and
redefine the extensions behavior, I decided to open a thread for
discussion on how I intend to solve all the problems. 

My solution does involve modifying pieces of the API, which causes BC
concerns. I do think about these things, and I really think we should
stabilize and finalize this module, as we have already radically changed
the API once before. 

If you could set these concerns aside momentarily, and here me out on
the changes I would like to make. 

Suggested Changes
=================

1. Change socket_recv to return the retval instead of string 
-------------------------------------------------------------

The error detection and eof (safe connection close) is incredibly
difficult to detect using the read functions. You end up writing code
like: 

while (false!==($retval=socket_read($sock, $len))) { 
if (strlen($retval)==0) { 
$eof=1; print "Eof has occured!!!"; 
                break; 
} 
} 
if ($retval===FALSE) print strerror(socket_last_error($sock)); 

yuck!

vs.

while(($retval==socket_read($sock, $buf, $len)) > 0) {
        // do something 
}
if ($retval==-1) {
        print strerror(socket_last_error($sock));
} elseif ($retval==0) {
        $eof=1;
        print "Eof has occured!!!";
}


I understand that these functions were written to match other areas of
php; however, sockets require more error checking than most other api's

I propose leaving socket_read as is, and changing socket_recv to be the
same as it was in the previous API. This will be consistent with the
socket_recvfrom object.

2. Get rid of socket_fd_*. 
-----------------------------

It makes little sense to make fd masks available to the user, because
this can be much easier to handle by using arrays. 

This will fix the max_fd code that attempts to calculate the highest
file descriptor in the fd_set.

Select would then look like
socket_select (array read_socks(), array write_socks(), array
except_socks(), int tv_sec, int uv_sec)

3. Add socket_clear_error(), and change socket_last_error() to not clear
-------------------------------------------------------------------------

I think that socket_last_error() clearing the last error is a huge WTF


4. Fix All other issues listed above
------------------------------------

This is mainly the consistency problems.

Timeline & Stabilization
=========================

I would like to fix all of this by 4.2.X. I propose that we then mark
the extension as stable, and freeze the API. 


Please send your feedback on your thoughts and concerns on these
changes.


Thanks,
-Jason



-- 
Jason T. Greene
Internet Software Engineer

<[EMAIL PROTECTED]>
<[EMAIL PROTECTED]> 
<[EMAIL PROTECTED]>

Use PHP: http://www.php.net


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to