Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Added method Socket::shutdown() #845

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Added method Socket::shutdown() #845

wants to merge 5 commits into from

Conversation

sirsnyder
Copy link
Collaborator

@sirsnyder sirsnyder commented Feb 28, 2018

  • Method shutdown() added
  • added one test

@sirsnyder
Copy link
Collaborator Author

@dktapps please review

Copy link
Contributor

@dktapps dktapps left a 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).

$socket = new \Socket(\Socket::AF_INET, \Socket::SOCK_STREAM, 0);
$socket->shutdown();

var_dump($socket->shutdown(4)); // invalid - false
Copy link
Contributor

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
Copy link
Contributor

@dktapps dktapps Mar 1, 2018

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) {
Copy link
Collaborator

@tpunt tpunt Mar 1, 2018

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) {
Copy link
Contributor

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);
Copy link
Contributor

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)

--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)
Copy link
Contributor

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

@dktapps
Copy link
Contributor

dktapps commented Mar 2, 2018

Experiencing test failures:
socket-shutdown.log

$socket = new \Socket(\Socket::AF_INET, \Socket::SOCK_STREAM, 0);
$socket->shutdown();

var_dump($socket->shutdown(4)); // invalid - false
Copy link
Contributor

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].

Copy link
Collaborator Author

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?

dktapps and others added 4 commits January 13, 2019 15:14
* 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants