Skip to content

Commit

Permalink
Added swoole_server::heartbeat() no close connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jul 14, 2014
1 parent 5345a6f commit 233a73b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 34 deletions.
5 changes: 3 additions & 2 deletions examples/server.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
$serv = new swoole_server("127.0.0.1", 9501);

$serv = new swoole_server("0.0.0.0", 9501);
$serv->addlistener('0.0.0.0', 9502, SWOOLE_SOCK_UDP);
$serv->set(array(
'worker_num' => 2,
//'open_eof_check' => true,
Expand Down Expand Up @@ -172,6 +172,7 @@ function broadcast($serv, $fd = 0, $data = "hello")
foreach($conn_list as $conn)
{
if($conn === $fd) continue;
sleep(5);
$ret1 = $serv->send($conn, $data);
var_dump($ret1);
$ret2 = $serv->close($conn);
Expand Down
69 changes: 44 additions & 25 deletions swoole.c
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,7 @@ PHP_FUNCTION(swoole_server_close)
//Master can't execute it
if (swIsMaster())
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can not close connection in master process.");
RETURN_FALSE;
}

Expand Down Expand Up @@ -1364,41 +1365,59 @@ PHP_FUNCTION(swoole_server_heartbeat)
zval *zobject = getThis();
swServer *serv;
swEvent ev;
zend_bool close_connection = 0;

if (zobject == NULL)
{
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zobject, swoole_server_class_entry_ptr) == FAILURE)
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &zobject, swoole_server_class_entry_ptr) == FAILURE)
{
return;
}
}
SWOOLE_GET_SERVER(zobject, serv);

if(serv->heartbeat_idle_time < 1)
else
{
RETURN_FALSE;
return;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &zobject, swoole_server_class_entry_ptr) == FAILURE)
{
return;
}
}
SWOOLE_GET_SERVER(zobject, serv);

int serv_max_fd = swServer_get_maxfd(serv);
int serv_min_fd = swServer_get_minfd(serv);

array_init(return_value);

int fd;
int checktime = (int) SwooleGS->now - serv->heartbeat_idle_time;

//遍历到最大fd
for(fd = serv_min_fd; fd<= serv_max_fd; fd++)
{
swTrace("check fd=%d", fd);
if(1 == serv->connection_list[fd].active && (serv->connection_list[fd].last_time < checktime))
{
ev.fd = fd;
serv->factory.end(&serv->factory, &ev);
add_next_index_long(return_value, fd);
}
}
if (serv->heartbeat_idle_time < 1)
{
RETURN_FALSE;
}

int serv_max_fd = swServer_get_maxfd(serv);
int serv_min_fd = swServer_get_minfd(serv);

array_init(return_value);

int fd;
int checktime = (int) SwooleGS->now - serv->heartbeat_idle_time;

ev.type = SW_CLOSE_INITIATIVE;

for (fd = serv_min_fd; fd <= serv_max_fd; fd++)
{
swTrace("heartbeat check fd=%d", fd);
if (1 == serv->connection_list[fd].active && (serv->connection_list[fd].last_time < checktime))
{
/**
* Close the connection
*/
if (close_connection)
{
ev.fd = fd;
serv->factory.end(&serv->factory, &ev);
if (serv->onClose != NULL)
{
serv->onClose(serv, ev.fd, ev.from_id);
}
}
add_next_index_long(return_value, fd);
}
}
}

PHP_FUNCTION(swoole_server_shutdown)
Expand Down
48 changes: 41 additions & 7 deletions tests/unittest.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,68 @@
<?php
$ifs = swoole_get_local_ip();
if (isset($ifs['eth0']))
{
$server_ip = $ifs['eth0'];
}
else
{
die("Step-0 failed. Error: swoole_get_local_ip() failed.");
}
echo "-------------------------------------------------------------\n";
echo "Swoole Unit Tests. ServerIP: {$server_ip}\n";
echo "-------------------------------------------------------------\n";
/**
* UnitTests for swoole server.
* This is just a client. Server see examples/server.php
*/

$i = 0;
$client = new swoole_client(SWOOLE_TCP);

if (!$client->connect('127.0.0.1', 9501))
if (!$client->connect($server_ip, 9501))
{
echo "Step-1: failed. Error: connect to server failed.\n";
echo "Step-".$i++.": failed. Error: connect to server failed.\n";
}
else
{
if (!$client->send("hello"))
{
echo "Step-2: failed. Error: send to server failed.\n";
echo "Step-".$i++.": failed. Error: send to server failed.\n";
}
$data = $client->recv();
if (!$data)
{
echo "Step-3: failed. Error: send to server failed.\n";
echo "Step-".$i++.": failed. Error: send to server failed.\n";
}
else if ($data != "Swoole: hello")
{
echo "Step-4: failed. Error: recv error data.\n";
echo "Step-".$i++.": failed. Error: recv error data.\n";
}
else
{
echo "TCP-Test-OK\n";
}
}

if (!$client->close())
{
echo "Step-8: failed. Error: close failed.\n";
echo "Step-".$i++.": failed. Error: close failed.\n";
}
echo "-------------------------------------------------------------\n";
$client = new swoole_client(SWOOLE_UDP);
$client->connect($server_ip, 9502);
$client->send("hello");
$data = $client->recv();
if (!$data)
{
echo "Step-".$i++.": failed. Error: send to server failed.\n";
}
else if ($data != "Swoole: hello")
{
echo "Step-".$i++.": failed. Error: recv error data.\n";
}
else
{
echo "UDP-Test-OK\n";
}
echo "-------------------------------------------------------------\n";
echo "UnitTest Finish.\n";

0 comments on commit 233a73b

Please sign in to comment.