Skip to content

Commit

Permalink
Added swoole_event_set
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Apr 21, 2014
1 parent 88310eb commit 4c76a55
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 67 deletions.
54 changes: 54 additions & 0 deletions examples/event/sockets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* require ./configure --enable-sockets
*/

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Unable to create socket\n");

socket_set_nonblock($socket) or die("Unable to set nonblock on socket\n");

function socket_onRead($socket)
{
static $i = 0;

echo socket_read($socket, 8192)."\n";
$i ++;
if ($i > 10)
{
echo "finish\n";
swoole_event_del($socket);
socket_close($socket);
}
else
{
sleep(1);
swoole_event_set($socket, null, 'socket_onWrite', SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
}
}

function socket_onWrite($socket)
{
socket_write($socket, "hi swoole");
swoole_event_set($socket, null, null, SWOOLE_EVENT_READ);
}

function socket_onConnect($socket)
{
$err = socket_get_option($socket, SOL_SOCKET, SO_ERROR);
if ($err == 0)
{
echo "connect server success\n";
swoole_event_set($socket, null, null, SWOOLE_EVENT_READ);
swoole_event_set($socket, null, 'socket_onWrite', SWOOLE_EVENT_READ);
socket_write($socket, "first package\n");
}
else
{
echo "connect server failed\n";
swoole_event_del($socket);
socket_close($socket);
}
}

swoole_event_add($socket, 'socket_onRead', 'socket_onConnect', SWOOLE_EVENT_WRITE);
@socket_connect($socket, '127.0.0.1', 9501);
25 changes: 25 additions & 0 deletions examples/event/stream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
$fp = stream_socket_client("tcp://127.0.0.1:9501", $errno, $errstr, 30);
if (!$fp) {
exit("$errstr ($errno)<br />\n");
}
fwrite($fp, "HELLO world");

function stream_onRead($fp)
{
echo fread($fp, 1024)."\n";
sleep(1);
swoole_event_set($fp, null, null, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
//swoole_event_del($fp);
//fclose($fp);
}

function stream_onWrite($fp)
{
fwrite($fp, "hi swoole\n");
swoole_event_set($fp, null, null, SWOOLE_EVENT_READ);
}

swoole_event_add($fp, 'stream_onRead', 'stream_onWrite');

echo "start\n";
14 changes: 0 additions & 14 deletions examples/stream.php

This file was deleted.

4 changes: 0 additions & 4 deletions include/swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,13 +703,9 @@ int swFactoryThread_finish(swFactory *factory, swSendData *data);
enum SW_EVENTS
{
SW_EVENT_DEAULT = 256,
#define SW_EVENT_DEAULT SW_EVENT_DEAULT
SW_EVENT_READ = 1u << 9,
#define SW_EVENT_READ SW_EVENT_READ
SW_EVENT_WRITE = 1u << 10,
#define SW_EVENT_WRITE SW_EVENT_WRITE
SW_EVENT_ERROR = 1u << 11,
#define SW_EVENT_ERROR SW_EVENT_ERROR
};

SWINLINE int swReactor_error(swReactor *reactor);
Expand Down
3 changes: 2 additions & 1 deletion php_swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ extern zend_class_entry *swoole_lock_class_entry_ptr;
extern zend_class_entry *swoole_client_class_entry_ptr;
extern zend_class_entry *swoole_server_class_entry_ptr;

extern HashTable php_sw_reactor_callback;
extern HashTable php_sw_event_callback;
extern HashTable php_sw_client_callback;
extern HashTable php_sw_timer_callback;
extern HashTable php_sw_long_connections;
Expand Down Expand Up @@ -168,6 +168,7 @@ PHP_FUNCTION(swoole_connection_list);
PHP_FUNCTION(swoole_connection_info);

PHP_FUNCTION(swoole_event_add);
PHP_FUNCTION(swoole_event_set);
PHP_FUNCTION(swoole_event_del);
PHP_FUNCTION(swoole_event_wait);
PHP_FUNCTION(swoole_event_exit);
Expand Down
1 change: 1 addition & 0 deletions src/factory/FactoryProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ int swFactoryProcess_send2client(swReactor *reactor, swDataHead *ev)
if (n > 0)
{
memcpy(&_send.info, &resp.info, sizeof(resp.info));
_send.data = resp.data;
return swReactorThread_send(&_send);
}
else
Expand Down
12 changes: 8 additions & 4 deletions swoole.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

zval *php_sw_callback[PHP_SERVER_CALLBACK_NUM];

HashTable php_sw_reactor_callback;
HashTable php_sw_event_callback;
HashTable php_sw_timer_callback;
HashTable php_sw_client_callback;
HashTable php_sw_aio_callback;
Expand Down Expand Up @@ -323,6 +323,7 @@ const zend_function_entry swoole_functions[] =
PHP_FE(swoole_connection_list, arginfo_swoole_connection_list)
/*------swoole_event-----*/
PHP_FE(swoole_event_add, arginfo_swoole_event_add)
PHP_FE(swoole_event_set, NULL)
PHP_FE(swoole_event_del, arginfo_swoole_event_del)
PHP_FE(swoole_event_exit, arginfo_swoole_event_exit)
PHP_FE(swoole_event_wait, arginfo_swoole_event_wait)
Expand Down Expand Up @@ -498,6 +499,9 @@ PHP_MINIT_FUNCTION(swoole)
REGISTER_LONG_CONSTANT("SWOOLE_ASYNC", SW_FLAG_ASYNC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_KEEP", SW_FLAG_KEEP, CONST_CS | CONST_PERSISTENT);

REGISTER_LONG_CONSTANT("SWOOLE_EVENT_READ", SW_EVENT_READ, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_EVENT_WRITE", SW_EVENT_WRITE, CONST_CS | CONST_PERSISTENT);

REGISTER_LONG_CONSTANT("SWOOLE_SIGN", SW_NUM_SIGN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_UNSIGN", SW_NUM_UNSIGN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_NET", SW_NUM_NET, CONST_CS | CONST_PERSISTENT);
Expand Down Expand Up @@ -582,7 +586,7 @@ PHP_MINFO_FUNCTION(swoole)
PHP_RINIT_FUNCTION(swoole)
{
//swoole_event_add
zend_hash_init(&php_sw_reactor_callback, 16, NULL, ZVAL_PTR_DTOR, 0);
zend_hash_init(&php_sw_event_callback, 16, NULL, ZVAL_PTR_DTOR, 0);
//swoole_client::on
zend_hash_init(&php_sw_client_callback, 16, NULL, ZVAL_PTR_DTOR, 0);
//swoole_timer_add
Expand All @@ -596,7 +600,7 @@ PHP_RINIT_FUNCTION(swoole)

PHP_RSHUTDOWN_FUNCTION(swoole)
{
zend_hash_destroy(&php_sw_reactor_callback);
zend_hash_destroy(&php_sw_event_callback);
zend_hash_destroy(&php_sw_client_callback);
zend_hash_destroy(&php_sw_timer_callback);
zend_hash_destroy(&php_sw_aio_callback);
Expand Down Expand Up @@ -974,7 +978,7 @@ PHP_FUNCTION(swoole_server_set)
static int php_swoole_set_callback(int key, zval *cb TSRMLS_DC)
{
char *func_name = NULL;
if(!zend_is_callable(cb, 0, &func_name TSRMLS_CC))
if (!zend_is_callable(cb, 0, &func_name TSRMLS_CC))
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Function '%s' is not callable", func_name);
efree(func_name);
Expand Down
Loading

0 comments on commit 4c76a55

Please sign in to comment.