-
Notifications
You must be signed in to change notification settings - Fork 498
Added static method Socket::createListen() #844
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -1041,6 +1041,56 @@ void pthreads_socket_get_last_error(zval *object, zend_bool clear, zval *return_ | |
| } | ||
| } | ||
|
|
||
| void pthreads_socket_create_listen(zend_long port, zend_long backlog, zval *return_value) { | ||
| struct sockaddr_in la; | ||
| struct hostent *hp; | ||
| pthreads_object_t *created; | ||
| pthreads_socket_t *sock; | ||
|
|
||
| object_init_ex(return_value, pthreads_socket_entry); | ||
|
Collaborator
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 can return |
||
|
|
||
| created = PTHREADS_FETCH_FROM(Z_OBJ_P(return_value)); | ||
| sock = created->store.sock; | ||
|
|
||
| #ifndef PHP_WIN32 | ||
| if ((hp = php_network_gethostbyname("0.0.0.0")) == NULL) { | ||
| #else | ||
| if ((hp = php_network_gethostbyname("localhost")) == NULL) { | ||
| #endif | ||
| zval_ptr_dtor(return_value); | ||
|
Collaborator
Author
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. @tpunt is
Collaborator
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 looks fine to me. Technically, |
||
| RETURN_FALSE; | ||
| } | ||
|
|
||
| memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length); | ||
| la.sin_family = hp->h_addrtype; | ||
| la.sin_port = htons((unsigned short) port); | ||
|
|
||
| sock->fd = socket(AF_INET, SOCK_STREAM, 0); | ||
| sock->blocking = 1; | ||
|
|
||
| if (PTHREADS_IS_INVALID_SOCKET(sock)) { | ||
| PTHREADS_SOCKET_ERROR(sock, "Unable to create listening socket", errno); | ||
| zval_ptr_dtor(return_value); | ||
| RETURN_FALSE; | ||
| } | ||
|
|
||
| sock->domain = AF_INET; | ||
|
|
||
| if (bind(sock->fd, (struct sockaddr *)&la, sizeof(la)) != 0) { | ||
| PTHREADS_SOCKET_ERROR(sock, "Unable to bind to given address", errno); | ||
| zval_ptr_dtor(return_value); | ||
| RETURN_FALSE; | ||
| } | ||
|
|
||
| if (listen(sock->fd, backlog) != 0) { | ||
| PTHREADS_SOCKET_ERROR(sock, "Unable to listen on socket", errno); | ||
| zval_ptr_dtor(return_value); | ||
| RETURN_FALSE; | ||
| } | ||
|
|
||
| sock->error = 0; | ||
| } | ||
|
|
||
| void pthreads_socket_strerror(zend_long error, zval *return_value) { | ||
| char *errstr; | ||
| errstr = php_socket_strerror(error, NULL, 0); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --TEST-- | ||
| Basic test of Socket::createListen() | ||
| --FILE-- | ||
| <?php | ||
| $port = 31330 + rand(1,999); | ||
|
|
||
| try { | ||
| \Socket::createListen(); | ||
| } catch(Exception $exception) { var_dump($exception); } | ||
|
|
||
| $socket = \Socket::createListen($port); | ||
| $sockName = $socket->getSockName(); | ||
|
|
||
| $client = new \Socket(\Socket::AF_INET, \Socket::SOCK_STREAM, \Socket::SOL_TCP); | ||
| $client->connect($sockName['host'], $sockName['port']); | ||
|
|
||
| var_dump($client->getPeerName(), $client->getPeerName(false)); | ||
|
|
||
| $client->close(); | ||
| $socket->close(); | ||
| ?> | ||
| --EXPECTF-- | ||
|
|
||
| Warning: Socket::createListen() expects at least 1 parameter, 0 given in %s on line %i | ||
| array(2) { | ||
| ["host"]=> | ||
| string(9) "127.0.0.1" | ||
| ["port"]=> | ||
| int(%i) | ||
| } | ||
| array(1) { | ||
| ["host"]=> | ||
| string(9) "127.0.0.1" | ||
| } |
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 doesn't look right. A separate arginfo needs to be defined for this.