Skip to content

Commit

Permalink
Fixed close timerfd bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Apr 24, 2014
1 parent 8b74307 commit b48c089
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 26 deletions.
24 changes: 18 additions & 6 deletions examples/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@
'task_worker_num' => 2,
//'dispatch_mode' => 2,
//'daemonize' => 1,
'log_file' => '/tmp/swoole.log',
//'log_file' => '/tmp/swoole.log',
//'heartbeat_idle_time' => 5,
//'heartbeat_check_interval' => 5,
));
function my_onStart($serv)

function my_onStart(swoole_server $serv)
{
global $argv;
swoole_set_process_name("php {$argv[0]}: master");
echo "MasterPid={$serv->master_pid}|Manager_pid={$serv->manager_pid}\n";
echo "Server: start.Swoole version is [".SWOOLE_VERSION."]\n";
//$serv->addtimer(1000);
$serv->addtimer(5000);
}

function my_log($msg)
{
echo "#".posix_getpid()."\t".$msg.PHP_EOL;
}

function my_onShutdown($serv)
Expand All @@ -28,18 +34,18 @@ function my_onShutdown($serv)

function my_onTimer($serv, $interval)
{
echo "Server:Timer Call.Interval=$interval\n";
my_log("Server:Timer Call.Interval=$interval");
}

function my_onClose($serv, $fd, $from_id)
{
//echo "Client: fd=$fd is closed.\n";
my_log("Client[$fd@$from_id]: fd=$fd is closed");
}

function my_onConnect($serv, $fd, $from_id)
{
//throw new Exception("hello world");
//echo "Client[$fd@$from_id]: Connect.\n";
echo "Client[$fd@$from_id]: Connect.\n";
}

function my_onWorkerStart($serv, $worker_id)
Expand Down Expand Up @@ -103,6 +109,12 @@ function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
elseif($cmd == "error")
{
hello_no_exists();
}
//关闭fd
elseif(substr($cmd, 0, 5) == "close")
{
$close_fd = substr($cmd, 6);
$serv->close($close_fd);
}
elseif($cmd == "shutdown")
{
Expand Down
5 changes: 3 additions & 2 deletions include/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,9 @@ SWINLINE int swServer_new_connection(swServer *serv, swEvent *ev);
SWINLINE void swConnection_close(swServer *serv, int fd, int notify);
SWINLINE int swConnection_error(int fd, int err);

#define SW_SERVER_MAX_FD_INDEX 0
#define SW_SERVER_MIN_FD_INDEX 1
#define SW_SERVER_MAX_FD_INDEX 0 //max connection socket
#define SW_SERVER_MIN_FD_INDEX 1 //min listen socket
#define SW_SERVER_TIMER_FD_INDEX 2 //for timerfd

//使用connection_list[0]表示最大的FD
#define swServer_set_maxfd(serv,maxfd) (serv->connection_list[SW_SERVER_MAX_FD_INDEX].fd=maxfd)
Expand Down
1 change: 1 addition & 0 deletions include/swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ typedef struct _swServerG{
int error;
int process_type;
int signal_alarm; //for timer with message queue
int signal_fd;

swServer *serv;
swFactory *factory;
Expand Down
4 changes: 2 additions & 2 deletions php_swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#include "Server.h"
#include "Client.h"

#define PHP_SWOOLE_VERSION "1.7.1-alpha"
//#define PHP_SWOOLE_CHECK_CALLBACK
#define PHP_SWOOLE_VERSION "1.7.1-beta"
#define PHP_SWOOLE_CHECK_CALLBACK

/**
* PHP5.2
Expand Down
8 changes: 6 additions & 2 deletions src/factory/FactoryProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,16 @@ int swFactoryProcess_end(swFactory *factory, swDataHead *event)
int ret;
swServer *serv = factory->ptr;
swEvent ev;

bzero(&ev, sizeof(swEvent));

ev.fd = event->fd;
ev.len = 0; //len=0表示关闭此连接
ev.type = SW_EVENT_CLOSE;
ret = swFactoryProcess_finish(factory, (swSendData *)&ev);
if (ret < 0)
{
return SW_ERR;
}
if (serv->onClose != NULL)
{
serv->onClose(serv, event->fd, event->from_id);
Expand Down Expand Up @@ -530,7 +534,7 @@ int swFactoryProcess_finish(swFactory *factory, swSendData *resp)
memcpy(sdata._send.data, resp->data, resp->info.len);

swConnection *conn = swServer_get_connection(serv, fd);
if(conn == NULL)
if (conn == NULL || conn->active == 0)
{
swWarn("connection[%d] not found.", fd);
return SW_ERR;
Expand Down
10 changes: 9 additions & 1 deletion src/network/ReactorThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ int swReactorThread_send(swSendData *_send)
swTask_sendfile *task;

swConnection *conn = swServer_get_connection(serv, fd);

if (conn == NULL || conn->active == 0)
{
swWarn("Connection[fd=%d] is not exists.", fd);
return SW_ERR;
}

swTraceLog(SW_TRACE_EVENT, "send-data. fd=%d|reactor_id=%d", fd, conn->from_id);
swReactor *reactor = &(serv->reactor_threads[conn->from_id].reactor);

Expand Down Expand Up @@ -656,6 +663,7 @@ int swReactorThread_start(swServer *serv, swReactor *main_reactor_ptr)
return SW_ERR;
}
}

//listen TCP
if (serv->have_tcp_sock == 1)
{
Expand All @@ -678,7 +686,7 @@ int swReactorThread_start(swServer *serv, swReactor *main_reactor_ptr)
param->object = serv;
param->pti = i;

if(pthread_create(&pidt, NULL, (void * (*)(void *)) swReactorThread_loop, (void *) param) < 0)
if (pthread_create(&pidt, NULL, (void * (*)(void *)) swReactorThread_loop, (void *) param) < 0)
{
swError("pthread_create[tcp_reactor] failed. Error: %s[%d]", strerror(errno), errno);
}
Expand Down
9 changes: 7 additions & 2 deletions src/network/Server.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,14 @@ int swServer_addTimer(swServer *serv, int interval)
//timer no init
if (SwooleG.timer.fd == 0)
{
if(swTimer_create(&SwooleG.timer, interval) < 0)
if (swTimer_create(&SwooleG.timer, interval) < 0)
{
return SW_ERR;
}
if (swIsMaster())
{
serv->connection_list[SW_SERVER_TIMER_FD_INDEX].fd = SwooleG.timer.fd;
}
#if SW_WORKER_IPC_MODE == 1
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_TIMER, swTimer_event_handler);
SwooleG.main_reactor->add(SwooleG.main_reactor, SwooleG.timer.fd, SW_FD_TIMER);
Expand Down Expand Up @@ -1064,6 +1068,7 @@ void swServer_signal_init(void)
swSignalfd_add(SIGALRM, swTimer_signal_handler);
//for test
swSignalfd_add(SIGVTALRM, swServer_signal_hanlder);
swServer_set_minfd(SwooleG.serv, SwooleG.signal_fd);
#else
swSignal_set(SIGHUP, SIG_IGN, 1, 0);
swSignal_set(SIGPIPE, SIG_IGN, 1, 0);
Expand Down Expand Up @@ -1153,7 +1158,7 @@ int swServer_listen(swServer *serv, swReactor *reactor)
serv->connection_list[sock].addr.sin_port = listen_host->port;
}
//将最后一个fd作为minfd和maxfd
if (sock>=0)
if (sock >= 0)
{
swServer_set_minfd(serv, sock);
swServer_set_maxfd(serv, sock);
Expand Down
3 changes: 1 addition & 2 deletions src/os/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#include "swoole.h"



/**
* clear all singal
*/
Expand Down Expand Up @@ -123,6 +121,7 @@ int swSignalfd_setup(swReactor *reactor)
swWarn("signalfd() failed. Error: %s[%d]", strerror(errno), errno);
return SW_ERR;
}
SwooleG.signal_fd = swoole_signalfd;
if (sigprocmask(SIG_BLOCK, &swoole_signalfd_mask, NULL) == -1)
{
swWarn("sigprocmask() failed. Error: %s[%d]", strerror(errno), errno);
Expand Down
4 changes: 2 additions & 2 deletions src/os/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int swTimer_create(swTimer *timer, int interval)
timer->lasttime = interval;

#if defined(HAVE_TIMERFD) && SW_WORKER_IPC_MODE == 1
if(swTimer_timerfd_set(timer, interval) < 0)
if (swTimer_timerfd_set(timer, interval) < 0)
{
return SW_ERR;
}
Expand Down Expand Up @@ -75,7 +75,7 @@ int swTimer_timerfd_set(swTimer *timer, int interval)
struct itimerspec timer_set;
bzero(&timer_set, sizeof(timer_set));

if(timer->fd == 0)
if (timer->fd == 0)
{
timer->fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC);
if (timer->fd < 0)
Expand Down
17 changes: 10 additions & 7 deletions swoole.c
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ PHP_FUNCTION(swoole_server_on)
break;
}
}
if(ret < 0)
if (ret < 0)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "swoole_server_on: unkown handler[%s].", ha_name);
}
Expand All @@ -1147,26 +1147,29 @@ PHP_FUNCTION(swoole_server_close)
zval *zobject = getThis();
swServer *serv;
swEvent ev;
long fd;
zval *fd;

if (zobject == NULL)
{
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol", &zobject, swoole_server_class_entry_ptr, &fd) == FAILURE)
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz", &zobject, swoole_server_class_entry_ptr, &fd) == FAILURE)
{
return;
}
}
else
{
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &fd) == FAILURE)
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &fd) == FAILURE)
{
return;
}
}
convert_to_long(fd);

SWOOLE_GET_SERVER(zobject, serv);
ev.fd = (int)fd;
//主进程不应当执行此操作
if(swIsMaster())
ev.fd = Z_LVAL_P(fd);

//Master can't execute it
if (swIsMaster())
{
RETURN_FALSE;
}
Expand Down

0 comments on commit b48c089

Please sign in to comment.