Skip to content

Commit

Permalink
Added disconnect and unbind
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoppanen committed Nov 28, 2012
1 parent c2800b7 commit 74b5f8c
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
?.?.?
- Added 3.2.x context options
- Added 3.2.x socket options
- Added unbind and disconnect for 3.2.x

1.0.0rc1
- Changed poll timeout to milliseconds rather than microseconds
Expand Down
43 changes: 43 additions & 0 deletions tests/033-disconnect.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Test disconnect
--SKIPIF--
<?php
require_once(dirname(__FILE__) . '/skipif.inc');
if(!method_exists(array ('zmqsocket', 'disconnect')) die('skip');
?>
--FILE--
<?php

include dirname(__FILE__) . '/zeromq_test_helper.inc';

$s = create_client();
$endpoints = $s->getendpoints();
var_dump($endpoints);

foreach ($endpoints['connect'] as $dsn) {
$s->disconnect($dsn);
}

var_dump ($s->getendpoints());
echo "OK";

--EXPECT--
array(2) {
["connect"]=>
array(1) {
[0]=>
string(20) "tcp://127.0.0.1:5566"
}
["bind"]=>
array(0) {
}
}
array(2) {
["connect"]=>
array(0) {
}
["bind"]=>
array(0) {
}
}
OK
43 changes: 43 additions & 0 deletions tests/034-unbind.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Test unbind
--SKIPIF--
<?php
require_once(dirname(__FILE__) . '/skipif.inc');
if(!method_exists(array ('zmqsocket', 'unbind')) die('skip');
?>
--FILE--
<?php

include dirname(__FILE__) . '/zeromq_test_helper.inc';

$s = create_server();
$endpoints = $s->getendpoints();
var_dump($endpoints);

foreach ($endpoints['bind'] as $dsn) {
$s->unbind($dsn);
}

var_dump ($s->getendpoints());
echo "OK";

--EXPECT--
array(2) {
["connect"]=>
array(0) {
}
["bind"]=>
array(1) {
[0]=>
string(20) "tcp://127.0.0.1:5566"
}
}
array(2) {
["connect"]=>
array(0) {
}
["bind"]=>
array(0) {
}
}
OK
62 changes: 62 additions & 0 deletions zmq.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,58 @@ PHP_METHOD(zmqsocket, connect)
}
/* }}} */

#if ZMQ_VERSION_MAJOR == 3 && ZMQ_VERSION_MINOR >= 2
/* {{{ proto ZMQSocket ZMQSocket::unbind(string $dsn)
Unbind the socket from an endpoint
*/
PHP_METHOD(zmqsocket, unbind)
{
php_zmq_socket_object *intern;
char *dsn;
int dsn_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &dsn, &dsn_len) == FAILURE) {
return;
}

intern = PHP_ZMQ_SOCKET_OBJECT;

if (zmq_unbind(intern->socket->z_socket, dsn) != 0) {
zend_throw_exception_ex(php_zmq_socket_exception_sc_entry, errno TSRMLS_CC, "Failed to unbind the ZMQ socket: %s", zmq_strerror(errno));
return;
}

zend_hash_del(&(intern->socket->bind), dsn, dsn_len + 1);
ZMQ_RETURN_THIS;
}
/* }}} */

/* {{{ proto ZMQSocket ZMQSocket::disconnect(string $dsn)
Disconnect the socket from an endpoint
*/
PHP_METHOD(zmqsocket, disconnect)
{
php_zmq_socket_object *intern;
char *dsn;
int dsn_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &dsn, &dsn_len) == FAILURE) {
return;
}

intern = PHP_ZMQ_SOCKET_OBJECT;

if (zmq_disconnect(intern->socket->z_socket, dsn) != 0) {
zend_throw_exception_ex(php_zmq_socket_exception_sc_entry, errno TSRMLS_CC, "Failed to disconnect the ZMQ socket: %s", zmq_strerror(errno));
return;
}

zend_hash_del(&(intern->socket->connect), dsn, dsn_len + 1);
ZMQ_RETURN_THIS;
}
/* }}} */
#endif

#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 3)
static int php_zmq_get_keys(zval **ppzval, int num_args, va_list args, zend_hash_key *hash_key)
{
Expand Down Expand Up @@ -1400,6 +1452,14 @@ ZEND_BEGIN_ARG_INFO_EX(zmq_socket_connect_args, 0, 0, 1)
ZEND_ARG_INFO(0, force)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(zmq_socket_unbind_args, 0, 0, 1)
ZEND_ARG_INFO(0, dsn)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(zmq_socket_disconnect_args, 0, 0, 1)
ZEND_ARG_INFO(0, dsn)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(zmq_socket_setsockopt_args, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, value)
Expand Down Expand Up @@ -1441,6 +1501,8 @@ static zend_function_entry php_zmq_socket_class_methods[] = {
PHP_ME(zmqsocket, recvmulti, zmq_socket_recv_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqsocket, bind, zmq_socket_bind_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqsocket, connect, zmq_socket_connect_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqsocket, unbind, zmq_socket_unbind_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqsocket, disconnect, zmq_socket_disconnect_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqsocket, setsockopt, zmq_socket_setsockopt_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqsocket, getendpoints, zmq_socket_getendpoints_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqsocket, getsockettype, zmq_socket_getsockettype_args, ZEND_ACC_PUBLIC)
Expand Down

0 comments on commit 74b5f8c

Please sign in to comment.