-
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?
Changes from 1 commit
d4c9931
bc9f34d
16e0e6a
f1641c3
82444c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,9 @@ PHP_METHOD(Socket, getLastError); | |
PHP_METHOD(Socket, clearError); | ||
PHP_METHOD(Socket, strerror); | ||
|
||
#ifdef HAVE_SHUTDOWN | ||
PHP_METHOD(Socket, shutdown); | ||
#endif | ||
PHP_METHOD(Socket, close); | ||
|
||
ZEND_BEGIN_ARG_INFO_EX(Socket___construct, 0, 0, 3) | ||
|
@@ -137,6 +140,12 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(Socket_strerror, 0, 1, IS_STRING, 1) | |
ZEND_ARG_TYPE_INFO(0, error, IS_LONG, 0) | ||
ZEND_END_ARG_INFO() | ||
|
||
#ifdef HAVE_SHUTDOWN | ||
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(Socket_shutdown, 0, 1, _IS_BOOL, 0) | ||
ZEND_ARG_TYPE_INFO(0, how, IS_LONG, 0) | ||
ZEND_END_ARG_INFO() | ||
#endif | ||
|
||
ZEND_BEGIN_ARG_INFO_EX(Socket_void, 0, 0, 0) | ||
ZEND_END_ARG_INFO() | ||
|
||
|
@@ -162,6 +171,9 @@ zend_function_entry pthreads_socket_methods[] = { | |
PHP_ME(Socket, getPeerName, Socket_getHost, ZEND_ACC_PUBLIC) | ||
PHP_ME(Socket, getSockName, Socket_getHost, ZEND_ACC_PUBLIC) | ||
PHP_ME(Socket, close, Socket_void, ZEND_ACC_PUBLIC) | ||
#ifdef HAVE_SHUTDOWN | ||
PHP_ME(Socket, shutdown, Socket_shutdown, ZEND_ACC_PUBLIC) | ||
#endif | ||
PHP_ME(Socket, getLastError, Socket_getLastError, ZEND_ACC_PUBLIC) | ||
PHP_ME(Socket, clearError, Socket_void, ZEND_ACC_PUBLIC) | ||
PHP_ME(Socket, strerror, Socket_strerror, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) | ||
|
@@ -399,6 +411,19 @@ PHP_METHOD(Socket, strerror) { | |
pthreads_socket_strerror(error, return_value); | ||
} /* }}} */ | ||
|
||
#ifdef HAVE_SHUTDOWN | ||
/* {{{ proto bool Socket::shutdown(int how) */ | ||
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 commentThe 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 |
||
RETURN_FALSE; | ||
} | ||
|
||
pthreads_socket_shutdown(getThis(), how_shutdown, return_value); | ||
} /* }}} */ | ||
#endif | ||
|
||
/* {{{ proto bool Socket::close(void) */ | ||
PHP_METHOD(Socket, close) { | ||
if (zend_parse_parameters_none() != SUCCESS) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. the signature here is incorrect - |
||
pthreads_object_t *threaded = | ||
PTHREADS_FETCH_FROM(Z_OBJ_P(object)); | ||
|
||
PTHREADS_SOCKET_CHECK(threaded->store.sock); | ||
|
||
if (shutdown(threaded->store.sock->fd, how_shutdown) != SUCCESS) { | ||
PTHREADS_SOCKET_ERROR(threaded->store.sock, "Unable to shutdown socket", errno); | ||
|
||
RETURN_FALSE; | ||
} | ||
|
||
RETURN_TRUE; | ||
} | ||
#endif | ||
|
||
void pthreads_socket_set_blocking(zval *object, zend_bool blocking, zval *return_value) { | ||
pthreads_object_t *threaded = | ||
PTHREADS_FETCH_FROM(Z_OBJ_P(object)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. same as above (signature is incorrect) |
||
#endif | ||
void pthreads_socket_set_blocking(zval *object, zend_bool blocking, zval *return_value); | ||
void pthreads_socket_get_peer_name(zval *object, zend_bool port, zval *return_value); | ||
void pthreads_socket_get_sock_name(zval *object, zend_bool port, zval *return_value); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--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 commentThe reason will be displayed to describe this comment to others. Learn more. this description might need fixing |
||
--SKIPIF-- | ||
<?php | ||
if(!method_exists(\Socket::class, 'shutdown')) { | ||
die('skip.. Socket::shutdown() doesn\'t exist'); | ||
} | ||
--FILE-- | ||
<?php | ||
$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 commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't an exception be more appropriate here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Windows, this causes an There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
|
||
$socket->bind('0.0.0.0'); | ||
$socket->listen(1); | ||
|
||
var_dump($socket->shutdown(0)); // close reading | ||
|
||
try { | ||
var_dump($socket->shutdown(1)); // close writing | ||
} catch(Exception $exception) { var_dump('not connected'); } | ||
|
||
$socket->close(); | ||
?> | ||
--EXPECTF-- | ||
Warning: Socket::shutdown() expects exactly 1 parameter, 0 given in %s on line %d | ||
bool(false) | ||
bool(true) | ||
string(13) "not connected" |
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