-
Notifications
You must be signed in to change notification settings - Fork 504
Added method Socket::shutdown() #845
base: master
Are you sure you want to change the base?
Conversation
@dktapps please review |
0796637
to
d4c9931
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be worthwhile adding constants for the shutdown parameters to the Socket
class (something like SHUTDOWN_READ
, SHUTDOWN_WRITE
, SHUTDOWN_BOTH
).
tests/socket-shutdown.phpt
Outdated
$socket = new \Socket(\Socket::AF_INET, \Socket::SOCK_STREAM, 0); | ||
$socket->shutdown(); | ||
|
||
var_dump($socket->shutdown(4)); // invalid - false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't an exception be more appropriate here?
@@ -42,6 +42,9 @@ PHP_METHOD(Socket, getLastError); | |||
PHP_METHOD(Socket, clearError); | |||
PHP_METHOD(Socket, strerror); | |||
|
|||
#ifdef HAVE_SHUTDOWN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is this macro defined? never mind
classes/socket.h
Outdated
PHP_METHOD(Socket, shutdown) { | ||
zend_long how_shutdown = 0; | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &how_shutdown) != SUCCESS) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason in particular why we don't default this parameter to 2
? I would assume that most of the time a socket is shut down, we'd like to close it for both reads and writes, so following PHP's socket_shutdown
function seems like it would make sense.
src/socket.c
Outdated
@@ -589,6 +589,23 @@ void pthreads_socket_close(zval *object, zval *return_value) { | |||
threaded->store.sock->fd = PTHREADS_INVALID_SOCKET; | |||
} | |||
|
|||
#ifdef HAVE_SHUTDOWN | |||
void pthreads_socket_shutdown(zval *object, zend_bool how_shutdown, zval *return_value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the signature here is incorrect - how_shutdown
should be zend_long
not zend_bool
.
src/socket.h
Outdated
@@ -46,6 +46,9 @@ void pthreads_socket_read(zval *object, zend_long length, zend_long flags, zend_ | |||
void pthreads_socket_write(zval *object, zend_string *buf, zend_long length, zval *return_value); | |||
void pthreads_socket_send(zval *object, zend_string *buf, zend_long length, zend_long flags, zval *return_value); | |||
void pthreads_socket_close(zval *object, zval *return_value); | |||
#ifdef HAVE_SHUTDOWN | |||
void pthreads_socket_shutdown(zval *object, zend_bool how_shutdown, zval *return_value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above (signature is incorrect)
tests/socket-shutdown.phpt
Outdated
--TEST-- | ||
Test of Socket::shutdown() - testing params and separate shutdown of read and write channel | ||
--DESCRIPTION-- | ||
Test that creating and closing sockets works as expected on all platforms (gh issue #798) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this description might need fixing
Experiencing test failures: |
tests/socket-shutdown.phpt
Outdated
$socket = new \Socket(\Socket::AF_INET, \Socket::SOCK_STREAM, 0); | ||
$socket->shutdown(); | ||
|
||
var_dump($socket->shutdown(4)); // invalid - false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Windows, this causes an ENOTCONN
error to be emitted, which causes a RuntimeException
to be thrown. I assume that in Winsock2, socket state is checked prior to checking the argument value [citation needed].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dktapps I slightly modified the test from bind()/listen() to connect(). Can you check the test on a windows machine?
* Fixed compiler warning - wrong argtypes * Added SHUTDOWN_READ, SHUTDOWN_WRITE, SHUTDOWN_BOTH constants * Utilize new constants in tests * Socket::shutdown() now defaults to shutting down both channels with no parameters * Ensure consistent behaviour when using shutdown() with bad arguments * Fixed Socket::shutdown() arginfo and prototype * Don't return false on invalid argument - we're throwing an exception anyway
shutdown()
added