Re: [PHP-DEV] Socket Rework Complete

2002-03-08 Thread J Smith


Never try to do something as complicated as attach some source to a mailing 
list post after two beer and a hefty lunch. 

Attachment is really here this time.

J



J Smith wrote:

> 
> Speaking of the FD_* macros and such, I'm trying to visualize how to
> re-write some code that I have that relies on the socket_fd_* functions.
> If you get the chance, could you look over this simplified version of what
> I'm doing and point me towards the proper direction to get it working with
> the new API. I want to have this stuff ready when PHP 4.2 or 4.3 come out,
> and seeing as you're the one closest to the sockets extension, I thought
> you could give me some pointers as to how it would work with the abscence
> of the sockets_fd_* functions. (I'm a little confused what to do without
> the FD_* stuff. This could make for a good example in the documentation,
> anyways, if it works as hoped.)
> 
> Attached is the stripped-down source.
> 
> J
> 
> 


#!/usr/local/bin/php -q
 $maxi)
$maxi = $i;

if (--$nready <= 0)
continue;
}


// check the clients for incoming data. 

for ($i = 0; $i <= $maxi; $i++)
{
if ($client[$i] < 0)
continue;

if (socket_fd_isset($allset, $client[$i]))
{
$n = trim(socket_read($client[$i], MAXLINE));

if (!$n)
closeClient($i);
else
{
// if a client has sent some data, do one of these:

if ($n == "/killme")
killDaemon();
else if ($n == "/quit")
closeClient($i);
else
{
// print something on the server, then echo the incoming
// data to all of the clients in the $client array.

print "From {$remote_host[$i]}:{$remote_port[$i]}, client[$i]: $n\n";
for ($j = 0; $j <= $maxi; $j++)
{
if ($client[$j] != -1)
socket_write($client[$j], "From client[$i]: $n\r\n");
}
}
}

if  (--$nready <= 0)
break;
}
}
}

?>



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


Re: [PHP-DEV] Socket Rework Complete

2002-03-08 Thread Jason Greene

I would be happy to help in any way possible.
Your source wasn't attached though. Would you like an example?
(That or you can resend your source)

-Jason


On Fri, 2002-03-08 at 13:10, J Smith wrote:
> 
> Speaking of the FD_* macros and such, I'm trying to visualize how to 
> re-write some code that I have that relies on the socket_fd_* functions. If 
> you get the chance, could you look over this simplified version of what I'm 
> doing and point me towards the proper direction to get it working with the 
> new API. I want to have this stuff ready when PHP 4.2 or 4.3 come out, and 
> seeing as you're the one closest to the sockets extension, I thought you 
> could give me some pointers as to how it would work with the abscence of 
> the sockets_fd_* functions. (I'm a little confused what to do without the 
> FD_* stuff. This could make for a good example in the documentation, 
> anyways, if it works as hoped.)
> 
> Attached is the stripped-down source.
> 
> J
> 
> 
> Jason Greene wrote:
> 
> > 
> > The correct behavior that is consistent with the API follows(that I am
> > planning):
> > Set retval of socket_select() to be the number of file descriptors
> > available. If error occurs set to false.
> > 
> > Using the c-api one commonly writes code like this
> > --
> > Error on an actual error or no sockets available:
> > 
> > ret=select(rfds, NULL, NULL, &tv);
> > if (ret < 0)
> >error_func();
> > 
> > Error differently:
> > 
> > ret=select(rfds, NULL, NULL, &tv);
> > if (ret == -1)
> > error_sys();
> > if (ret == 0)
> > error_noavail();
> > 
> > 
> > The equiv in php using false:
> > --
> > 
> > Error on an actual error or no sockets available:
> > 
> > $ret=socket_select($rfds, NULL, NULL, 1);
> > if (ret == 0)
> >error_func();
> > 
> > Error differently:
> > 
> > $ret=socket_select($rfds, NULL, NULL, 1);
> > if (ret === FALSE)
> >error_sys();
> > if (ret === 0)
> >error_noavail();
> > 
> > 
> >> According to the man file:
> >>
> > 
> >> (Although without the functions equivalent
> >> to the FD_* macros', the extension as a whole is getting pretty far
> >> removed from the standard sockets library as it is.)
> >> 
> >> J
> > 
> > Yes, the goal is to follow a more php like behavior. Those who follow
> > the C-API will have some slight adjusting to, but the beauty of the
> > high-level language is that we can automate and simplify the
> > functionality.
> > 
> > This also makes it easier for people who don't know C to pickup the
> > functionality.
> > 
> > -Jason
> >> --
> >> PHP Development Mailing List 
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >> 
> 
> 
> -- 
> PHP Development Mailing List 
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
-- 
Jason T. Greene
Internet Software Engineer

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

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



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




Re: [PHP-DEV] Socket Rework Complete

2002-03-08 Thread J Smith


Speaking of the FD_* macros and such, I'm trying to visualize how to 
re-write some code that I have that relies on the socket_fd_* functions. If 
you get the chance, could you look over this simplified version of what I'm 
doing and point me towards the proper direction to get it working with the 
new API. I want to have this stuff ready when PHP 4.2 or 4.3 come out, and 
seeing as you're the one closest to the sockets extension, I thought you 
could give me some pointers as to how it would work with the abscence of 
the sockets_fd_* functions. (I'm a little confused what to do without the 
FD_* stuff. This could make for a good example in the documentation, 
anyways, if it works as hoped.)

Attached is the stripped-down source.

J


Jason Greene wrote:

> 
> The correct behavior that is consistent with the API follows(that I am
> planning):
> Set retval of socket_select() to be the number of file descriptors
> available. If error occurs set to false.
> 
> Using the c-api one commonly writes code like this
> --
> Error on an actual error or no sockets available:
> 
> ret=select(rfds, NULL, NULL, &tv);
> if (ret < 0)
>error_func();
> 
> Error differently:
> 
> ret=select(rfds, NULL, NULL, &tv);
> if (ret == -1)
> error_sys();
> if (ret == 0)
> error_noavail();
> 
> 
> The equiv in php using false:
> --
> 
> Error on an actual error or no sockets available:
> 
> $ret=socket_select($rfds, NULL, NULL, 1);
> if (ret == 0)
>error_func();
> 
> Error differently:
> 
> $ret=socket_select($rfds, NULL, NULL, 1);
> if (ret === FALSE)
>error_sys();
> if (ret === 0)
>error_noavail();
> 
> 
>> According to the man file:
>>
> 
>> (Although without the functions equivalent
>> to the FD_* macros', the extension as a whole is getting pretty far
>> removed from the standard sockets library as it is.)
>> 
>> J
> 
> Yes, the goal is to follow a more php like behavior. Those who follow
> the C-API will have some slight adjusting to, but the beauty of the
> high-level language is that we can automate and simplify the
> functionality.
> 
> This also makes it easier for people who don't know C to pickup the
> functionality.
> 
> -Jason
>> --
>> PHP Development Mailing List 
>> To unsubscribe, visit: http://www.php.net/unsub.php
>> 


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




Re: [PHP-DEV] Socket Rework Complete

2002-03-08 Thread Jason Greene

On Fri, 2002-03-08 at 11:02, Markus Fischer wrote:
> On Fri, Mar 08, 2002 at 11:22:08AM -0500, J Smith wrote : 
> > I'm thinking that this functionality is a bit too far removed from the 
> > standard select() call [...]
> 
> I agree on that. Let there be an int return from
> socket_select(). You still can return 'false' on error
> condition.

Yes, that is the behavior I was planning.
The only problem is that the current function returns -1 instead of
FALSE.

-Jason

> 
> -- 
> PHP Development Mailing List 
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
-- 
Jason T. Greene
Internet Software Engineer

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

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



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




Re: [PHP-DEV] Socket Rework Complete

2002-03-08 Thread Jason Greene

On Fri, 2002-03-08 at 10:22, J Smith wrote:
> 
> I don't think that's a hot idea. The return value of select() is an int for 
> reason and not merely true or false. It's supposed to return the number of 
> file descriptors that are ready for reading, writing or exceptions. 

The correct behavior that is consistent with the API follows(that I am
planning):
Set retval of socket_select() to be the number of file descriptors
available. If error occurs set to false.

Using the c-api one commonly writes code like this
--
Error on an actual error or no sockets available:

ret=select(rfds, NULL, NULL, &tv);
if (ret < 0)
   error_func();

Error differently:

ret=select(rfds, NULL, NULL, &tv);
if (ret == -1)
error_sys();
if (ret == 0)
error_noavail();


The equiv in php using false:
--

Error on an actual error or no sockets available:

$ret=socket_select($rfds, NULL, NULL, 1);
if (ret == 0)
   error_func();

Error differently:

$ret=socket_select($rfds, NULL, NULL, 1);
if (ret === FALSE)
   error_sys();
if (ret === 0)
   error_noavail();


> According to the man file:
>

> (Although without the functions equivalent 
> to the FD_* macros', the extension as a whole is getting pretty far removed 
> from the standard sockets library as it is.)
> 
> J

Yes, the goal is to follow a more php like behavior. Those who follow
the C-API will have some slight adjusting to, but the beauty of the
high-level language is that we can automate and simplify the
functionality.

This also makes it easier for people who don't know C to pickup the
functionality. 

-Jason
> -- 
> PHP Development Mailing List 
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
-- 
Jason T. Greene
Internet Software Engineer

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

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



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




Re: [PHP-DEV] Socket Rework Complete

2002-03-08 Thread Markus Fischer

On Fri, Mar 08, 2002 at 11:22:08AM -0500, J Smith wrote : 
> I'm thinking that this functionality is a bit too far removed from the 
> standard select() call [...]

I agree on that. Let there be an int return from
socket_select(). You still can return 'false' on error
condition.


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




Re: [PHP-DEV] Socket Rework Complete

2002-03-08 Thread J Smith


I don't think that's a hot idea. The return value of select() is an int for 
reason and not merely true or false. It's supposed to return the number of 
file descriptors that are ready for reading, writing or exceptions. 
According to the man file:

"On success, select and pselect return the number of descriptors contained 
in the descriptor sets, which may be zero if  the  timeout expires before 
anything interesting happens.  On error, -1 is returned, and errno is set 
appropriately; the sets and timeout become undefined, so do not rely on 
their contents after an error."

Returning true or false probably isn't a good idea -- how are you supposed 
to know how many file descriptors are available if the answer is either 1 
or 0?

Another function may be needed that can take a peek at the number of ready 
file descriptors if you really want socket_select() to return true/false. 
I'm thinking that this functionality is a bit too far removed from the 
standard select() call, though. (Although without the functions equivalent 
to the FD_* macros', the extension as a whole is getting pretty far removed 
from the standard sockets library as it is.)

J


Jason Greene wrote:

>> 
>> The way I would go about fixing it would be to change socket_select() to
>> return true or false, and if there was an error (as indicated by retval),
>> store errno somewhere and let socket_last_error()/socket_clear_error()
>> retrieve that value if there isn't a socket specified.
> 
> I agree with this approach, however, IMO this is a functionality
> enhancement (soring a global last error), and probably should wait till
> 4.3.0.
> 
> Thoughts anyone?
> 


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




Re: [PHP-DEV] Socket Rework Complete

2002-03-08 Thread Jason Greene

On Fri, 2002-03-08 at 03:48, Chris Vandomelen wrote:
> > For all those who don't follow CVS. The sockets extension modifications
> > I listed out a few weeks ago are complete, and will be included in the
> > 4.2.0 release.
> 
> I haven't been following CVS, nor have I really paid a lot of attention to
> the module. I have received the occasional email about the extension
> though, the most recent one being in relation to socket_select().

I understand. Due to the amount of work I have been doing on this
module, and the amount of usefulness I get out of this module, I am
planning on continuing the maintenance & enhancement work.
 
> In the current CVS, there is a bug in the socket_select() function that
> causes it to only return success or failure (RETURN_LONG(retval)), which,
> if I'm not mistaken about the rest of the API changes, should be changed
> to reflect a true/false value. 

Yes this is a bug, socket_select() should return false and print an
error message. It appears that socket_send() and socket_sendto() suffer
from the same bug. Thanks for catching this. A patch to resolve this
should be merged. (I should have time later today to correct that)


> It also doesn't give you access to the
> error which occured (since retval only represents the return of socket(),
> but nothing about the actual reason for failure), which is more the bug
> than anything.
> 
> The way I would go about fixing it would be to change socket_select() to
> return true or false, and if there was an error (as indicated by retval),
> store errno somewhere and let socket_last_error()/socket_clear_error()
> retrieve that value if there isn't a socket specified.

I agree with this approach, however, IMO this is a functionality
enhancement (soring a global last error), and probably should wait till
4.3.0.

Thoughts anyone?

> I've attached a unified diff of the changes to make. The changes are
> untested (I need to actually check out a complete version before making
> the changes...), but they should compile and work cleanly. (No
> guarantees.)


Just from an initial view:

A problem I notice in this patch is the constant use of
SOCKETSLS_FETCH(), a more efficient approach would be to modify the
macros in php_sockets.h to retrieve the global structure from TSRMLS().

Also there is a bug in the patch pertaining to the modifications to the
socket_last_error() function: arg1 should be initialized to NULL

> Chris
> 
> 
> 

Thanks for your feedback and patch,

-Jason



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




Re: [PHP-DEV] Socket Rework Complete

2002-03-08 Thread Chris Vandomelen

> For all those who don't follow CVS. The sockets extension modifications
> I listed out a few weeks ago are complete, and will be included in the
> 4.2.0 release.

I haven't been following CVS, nor have I really paid a lot of attention to
the module. I have received the occasional email about the extension
though, the most recent one being in relation to socket_select().

In the current CVS, there is a bug in the socket_select() function that
causes it to only return success or failure (RETURN_LONG(retval)), which,
if I'm not mistaken about the rest of the API changes, should be changed
to reflect a true/false value. It also doesn't give you access to the
error which occured (since retval only represents the return of socket(),
but nothing about the actual reason for failure), which is more the bug
than anything.

The way I would go about fixing it would be to change socket_select() to
return true or false, and if there was an error (as indicated by retval),
store errno somewhere and let socket_last_error()/socket_clear_error()
retrieve that value if there isn't a socket specified.

I've attached a unified diff of the changes to make. The changes are
untested (I need to actually check out a complete version before making
the changes...), but they should compile and work cleanly. (No
guarantees.)

Chris



Index: php_sockets.h
===
RCS file: /root/src/cvsroot/php4/ext/sockets/php_sockets.h,v
retrieving revision 1.22
diff -u -r1.22 php_sockets.h
--- php_sockets.h   2002/03/06 20:19:09 1.22
+++ php_sockets.h   2002/03/08 09:38:13
@@ -95,6 +95,7 @@
 
 typedef struct {
zend_bool   use_system_read;
+   int last_error;
 } php_sockets_globals;
 
 /* Prototypes */
Index: sockets.c
===
RCS file: /root/src/cvsroot/php4/ext/sockets/sockets.c,v
retrieving revision 1.95
diff -u -r1.95 sockets.c
--- sockets.c   2002/03/06 20:19:09 1.95
+++ sockets.c   2002/03/08 09:46:53
@@ -89,8 +89,13 @@
 #define PHP_NORMAL_READ 0x0001
 #define PHP_BINARY_READ 0x0002
 
-#define PHP_SOCKET_ERROR(socket,msg,errn)  socket->error = errn;   \
-  
 php_error(E_WARNING, "%s() %s [%d]: %s", get_active_function_name(TSRMLS_C), msg, 
errn, php_strerror(errn))
+#define PHP_SOCKET_ERROR(socket,msg,errn)  \
+   if (socket) \
+   socket->error = errn;   \
+   SOCKETSG(last_error) = errn;\
+   php_error(E_WARNING, "%s() %s [%d]: %s",\
+   get_active_function_name(TSRMLS_C), msg,\
+   errn, php_strerror(errn))
 
 static int le_iov;
 #define le_iov_name "Socket I/O vector"
@@ -199,6 +204,7 @@
struct sockaddr_in  la;
struct hostent  *hp;
php_socket  *sock = 
(php_socket*)emalloc(sizeof(php_socket));
+   SOCKETSLS_FETCH();
 
*php_sock = sock;
 
@@ -246,7 +252,8 @@
 {
socklen_t   salen;
php_socket  *out_sock = (php_socket*)emalloc(sizeof(php_socket));
-   
+   SOCKETSLS_FETCH();
+
*new_sock = out_sock;
salen = sizeof(*la);
 
@@ -352,6 +359,7 @@
 int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock  
TSRMLS_DC) {
struct in_addr tmp;
struct hostent *host_entry;
+   SOCKETSLS_FETCH();
 
if (inet_aton(string, &tmp)) {
sin->sin_addr.s_addr = tmp.s_addr;
@@ -494,6 +502,7 @@
fd_set  rfds, wfds, efds;
SOCKET  max_fd = 0;
int retval, sets = 0, usec = 0, sec=0;
+   SOCKETSLS_FETCH();
 
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!a!l|l", &r_array, 
&w_array, &e_array, &sec, &usec) == FAILURE)
return;
@@ -515,11 +524,17 @@
tv.tv_usec = usec;

retval = select(max_fd+1, &rfds, &wfds, &efds, &tv);
+
+   if (retval == -1)
+   {
+   PHP_SOCKET_ERROR(NULL, "can't select", errno);
+   RETURN_FALSE;
+   }

if (r_array != NULL) php_sock_array_from_fd_set(r_array, &rfds TSRMLS_CC);
if (w_array != NULL) php_sock_array_from_fd_set(w_array, &wfds TSRMLS_CC);
if (e_array != NULL) php_sock_array_from_fd_set(e_array, &efds TSRMLS_CC);   
-   
+
RETURN_LONG(retval); 
 
 }
@@ -629,6 +644,7 @@
zval*arg1;
php_socket  *php_sock;
int backlog = 0;
+   SOCKETSLS_FETCH();
 
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &arg1, &backlog) 
== FAILURE)
return;
@@ -704,6 +720,7 @@
read_func   read_

[PHP-DEV] Socket Rework Complete

2002-03-07 Thread Jason Greene

For all those who don't follow CVS. The sockets extension modifications
I listed out a few weeks ago are complete, and will be included in the
4.2.0 release. 

The extension will still be marked as experimental; however, if 4.2.0
goes well, and there are few bugs, perhaps it can be marked as stable by
4.3.0.

I ask all those that use the sockets extension try and get any bug
reports to me before RC1 closes (That way the fixes will make 4.2.0).
Also, between 4.2.0 and 4.3.0 would be a good time frame to suggest
behavior changes because once the extension is marked stable, these will
be a lot tougher to put through.


Thanks,

-Jason



-- 
Jason T. Greene
Internet Software Engineer

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

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



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