Skip to content

Commit

Permalink
Support Socket::checkLiveness and Socket::peek (#3057)
Browse files Browse the repository at this point in the history
  • Loading branch information
twose authored and matyhtf committed Jan 9, 2020
1 parent bfdfeb0 commit e4d48fc
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 21 deletions.
97 changes: 76 additions & 21 deletions swoole_socket_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ static PHP_METHOD(swoole_socket_coro, bind);
static PHP_METHOD(swoole_socket_coro, listen);
static PHP_METHOD(swoole_socket_coro, accept);
static PHP_METHOD(swoole_socket_coro, connect);
static PHP_METHOD(swoole_socket_coro, checkLiveness);
static PHP_METHOD(swoole_socket_coro, peek);
static PHP_METHOD(swoole_socket_coro, recv);
static PHP_METHOD(swoole_socket_coro, send);
static PHP_METHOD(swoole_socket_coro, sendFile);
Expand Down Expand Up @@ -84,6 +86,13 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_socket_coro_connect, 0, 0, 1)
ZEND_ARG_INFO(0, timeout)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_socket_coro_checkLiveness, 0, 0, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_socket_coro_peek, 0, 0, 0)
ZEND_ARG_INFO(0, length)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_socket_coro_recv, 0, 0, 0)
ZEND_ARG_INFO(0, length)
ZEND_ARG_INFO(0, timeout)
Expand Down Expand Up @@ -144,27 +153,29 @@ ZEND_END_ARG_INFO()

static const zend_function_entry swoole_socket_coro_methods[] =
{
PHP_ME(swoole_socket_coro, __construct, arginfo_swoole_socket_coro_construct, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, bind, arginfo_swoole_socket_coro_bind, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, listen, arginfo_swoole_socket_coro_listen, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, accept, arginfo_swoole_socket_coro_accept, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, connect, arginfo_swoole_socket_coro_connect, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, recv, arginfo_swoole_socket_coro_recv, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, recvPacket, arginfo_swoole_socket_coro_recvPacket, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, send, arginfo_swoole_socket_coro_send, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, sendFile, arginfo_swoole_socket_coro_sendFile, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, recvAll, arginfo_swoole_socket_coro_recv, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, sendAll, arginfo_swoole_socket_coro_send, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, recvfrom, arginfo_swoole_socket_coro_recvfrom, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, sendto, arginfo_swoole_socket_coro_sendto, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, getOption, arginfo_swoole_socket_coro_getOption, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, setProtocol, arginfo_swoole_socket_coro_setProtocol, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, setOption, arginfo_swoole_socket_coro_setOption, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, shutdown, arginfo_swoole_socket_coro_shutdown, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, cancel, arginfo_swoole_socket_coro_cancel, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, close, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, getpeername, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, getsockname, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, __construct, arginfo_swoole_socket_coro_construct, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, bind, arginfo_swoole_socket_coro_bind, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, listen, arginfo_swoole_socket_coro_listen, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, accept, arginfo_swoole_socket_coro_accept, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, connect, arginfo_swoole_socket_coro_connect, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, checkLiveness, arginfo_swoole_socket_coro_checkLiveness, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, peek, arginfo_swoole_socket_coro_peek, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, recv, arginfo_swoole_socket_coro_recv, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, recvPacket, arginfo_swoole_socket_coro_recvPacket, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, send, arginfo_swoole_socket_coro_send, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, sendFile, arginfo_swoole_socket_coro_sendFile, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, recvAll, arginfo_swoole_socket_coro_recv, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, sendAll, arginfo_swoole_socket_coro_send, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, recvfrom, arginfo_swoole_socket_coro_recvfrom, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, sendto, arginfo_swoole_socket_coro_sendto, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, getOption, arginfo_swoole_socket_coro_getOption, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, setProtocol, arginfo_swoole_socket_coro_setProtocol, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, setOption, arginfo_swoole_socket_coro_setOption, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, shutdown, arginfo_swoole_socket_coro_shutdown, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, cancel, arginfo_swoole_socket_coro_cancel, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, close, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, getpeername, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_socket_coro, getsockname, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};

Expand Down Expand Up @@ -1126,6 +1137,50 @@ static PHP_METHOD(swoole_socket_coro, connect)
RETURN_TRUE;
}

static PHP_METHOD(swoole_socket_coro, checkLiveness)
{
swoole_get_socket_coro(sock, ZEND_THIS);

bool liveness = sock->socket->check_liveness();
swoole_socket_coro_sync_properties(ZEND_THIS, sock);
RETURN_BOOL(liveness);
}

static PHP_METHOD(swoole_socket_coro, peek)
{
zend_long length = SW_BUFFER_SIZE_BIG;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(length)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (UNEXPECTED(length <= 0))
{
length = SW_BUFFER_SIZE_BIG;
}

swoole_get_socket_coro(sock, ZEND_THIS);

zend_string *buf = zend_string_alloc(length, 0);
ssize_t bytes = sock->socket->peek(ZSTR_VAL(buf), length);
swoole_socket_coro_sync_properties(ZEND_THIS, sock);
if (UNEXPECTED(bytes < 0))
{
zend_string_free(buf);
RETURN_FALSE;
}
else if (UNEXPECTED(bytes == 0))
{
zend_string_free(buf);
RETURN_EMPTY_STRING();
}
else
{
RETURN_STR(sw_zend_string_recycle(buf, length, bytes));
}
}

static sw_inline void swoole_socket_coro_recv(INTERNAL_FUNCTION_PARAMETERS, const bool all)
{
zend_long length = SW_BUFFER_SIZE_BIG;
Expand Down
55 changes: 55 additions & 0 deletions tests/swoole_socket_coro/peek_and_checkLiveness.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
swoole_socket_coro: peek and checkLiveness
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Coroutine;
use Swoole\Coroutine\Socket;

$pm = new ProcessManager;
$pm->initRandomDataEx(MAX_CONCURRENCY_MID, 1, 1024);
$pm->parentFunc = function () use ($pm) {
Coroutine\run(function () use ($pm) {
for ($c = MAX_CONCURRENCY_MID; $c--;) {
Coroutine::create(function () use ($pm, $c) {
$socket = new Socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
Assert::true($socket->connect('127.0.0.1', $pm->getFreePort()));
$random = get_safe_random();
Assert::same($socket->sendAll($random), strlen($random));
for ($n = 100; $n--;) {
Coroutine::sleep(0.001);
$data = $socket->peek(strlen($random));
if ($data === $random) {
break;
}
}
Assert::greaterThan($n, 0);
/* clean the socket buffer */
$socket->recv();
/* then we check the liveness */
Assert::false($socket->checkLiveness());
});
}
});
$pm->kill();
echo "DONE\n";
};
$pm->childFunc = function () use ($pm) {
$server = new Swoole\Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);
$server->on('WorkerStart', function () use ($pm) {
$pm->wakeup();
});
$server->on('Receive', function (Swoole\Server $server, int $fd, int $rid, string $data) use ($pm) {
$server->send($fd, $data);
$server->close($fd);
});
$server->start();
};
$pm->childFirst();
$pm->run();
?>
--EXPECT--
DONE

0 comments on commit e4d48fc

Please sign in to comment.