Skip to content

Commit 4c76a55

Browse files
committed
Added swoole_event_set
1 parent 88310eb commit 4c76a55

File tree

8 files changed

+265
-67
lines changed

8 files changed

+265
-67
lines changed

examples/event/sockets.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* require ./configure --enable-sockets
4+
*/
5+
6+
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Unable to create socket\n");
7+
8+
socket_set_nonblock($socket) or die("Unable to set nonblock on socket\n");
9+
10+
function socket_onRead($socket)
11+
{
12+
static $i = 0;
13+
14+
echo socket_read($socket, 8192)."\n";
15+
$i ++;
16+
if ($i > 10)
17+
{
18+
echo "finish\n";
19+
swoole_event_del($socket);
20+
socket_close($socket);
21+
}
22+
else
23+
{
24+
sleep(1);
25+
swoole_event_set($socket, null, 'socket_onWrite', SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
26+
}
27+
}
28+
29+
function socket_onWrite($socket)
30+
{
31+
socket_write($socket, "hi swoole");
32+
swoole_event_set($socket, null, null, SWOOLE_EVENT_READ);
33+
}
34+
35+
function socket_onConnect($socket)
36+
{
37+
$err = socket_get_option($socket, SOL_SOCKET, SO_ERROR);
38+
if ($err == 0)
39+
{
40+
echo "connect server success\n";
41+
swoole_event_set($socket, null, null, SWOOLE_EVENT_READ);
42+
swoole_event_set($socket, null, 'socket_onWrite', SWOOLE_EVENT_READ);
43+
socket_write($socket, "first package\n");
44+
}
45+
else
46+
{
47+
echo "connect server failed\n";
48+
swoole_event_del($socket);
49+
socket_close($socket);
50+
}
51+
}
52+
53+
swoole_event_add($socket, 'socket_onRead', 'socket_onConnect', SWOOLE_EVENT_WRITE);
54+
@socket_connect($socket, '127.0.0.1', 9501);

examples/event/stream.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
$fp = stream_socket_client("tcp://127.0.0.1:9501", $errno, $errstr, 30);
3+
if (!$fp) {
4+
exit("$errstr ($errno)<br />\n");
5+
}
6+
fwrite($fp, "HELLO world");
7+
8+
function stream_onRead($fp)
9+
{
10+
echo fread($fp, 1024)."\n";
11+
sleep(1);
12+
swoole_event_set($fp, null, null, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
13+
//swoole_event_del($fp);
14+
//fclose($fp);
15+
}
16+
17+
function stream_onWrite($fp)
18+
{
19+
fwrite($fp, "hi swoole\n");
20+
swoole_event_set($fp, null, null, SWOOLE_EVENT_READ);
21+
}
22+
23+
swoole_event_add($fp, 'stream_onRead', 'stream_onWrite');
24+
25+
echo "start\n";

examples/stream.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

include/swoole.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,13 +703,9 @@ int swFactoryThread_finish(swFactory *factory, swSendData *data);
703703
enum SW_EVENTS
704704
{
705705
SW_EVENT_DEAULT = 256,
706-
#define SW_EVENT_DEAULT SW_EVENT_DEAULT
707706
SW_EVENT_READ = 1u << 9,
708-
#define SW_EVENT_READ SW_EVENT_READ
709707
SW_EVENT_WRITE = 1u << 10,
710-
#define SW_EVENT_WRITE SW_EVENT_WRITE
711708
SW_EVENT_ERROR = 1u << 11,
712-
#define SW_EVENT_ERROR SW_EVENT_ERROR
713709
};
714710

715711
SWINLINE int swReactor_error(swReactor *reactor);

php_swoole.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ extern zend_class_entry *swoole_lock_class_entry_ptr;
131131
extern zend_class_entry *swoole_client_class_entry_ptr;
132132
extern zend_class_entry *swoole_server_class_entry_ptr;
133133

134-
extern HashTable php_sw_reactor_callback;
134+
extern HashTable php_sw_event_callback;
135135
extern HashTable php_sw_client_callback;
136136
extern HashTable php_sw_timer_callback;
137137
extern HashTable php_sw_long_connections;
@@ -168,6 +168,7 @@ PHP_FUNCTION(swoole_connection_list);
168168
PHP_FUNCTION(swoole_connection_info);
169169

170170
PHP_FUNCTION(swoole_event_add);
171+
PHP_FUNCTION(swoole_event_set);
171172
PHP_FUNCTION(swoole_event_del);
172173
PHP_FUNCTION(swoole_event_wait);
173174
PHP_FUNCTION(swoole_event_exit);

src/factory/FactoryProcess.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ int swFactoryProcess_send2client(swReactor *reactor, swDataHead *ev)
10041004
if (n > 0)
10051005
{
10061006
memcpy(&_send.info, &resp.info, sizeof(resp.info));
1007+
_send.data = resp.data;
10071008
return swReactorThread_send(&_send);
10081009
}
10091010
else

swoole.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
zval *php_sw_callback[PHP_SERVER_CALLBACK_NUM];
2828

29-
HashTable php_sw_reactor_callback;
29+
HashTable php_sw_event_callback;
3030
HashTable php_sw_timer_callback;
3131
HashTable php_sw_client_callback;
3232
HashTable php_sw_aio_callback;
@@ -323,6 +323,7 @@ const zend_function_entry swoole_functions[] =
323323
PHP_FE(swoole_connection_list, arginfo_swoole_connection_list)
324324
/*------swoole_event-----*/
325325
PHP_FE(swoole_event_add, arginfo_swoole_event_add)
326+
PHP_FE(swoole_event_set, NULL)
326327
PHP_FE(swoole_event_del, arginfo_swoole_event_del)
327328
PHP_FE(swoole_event_exit, arginfo_swoole_event_exit)
328329
PHP_FE(swoole_event_wait, arginfo_swoole_event_wait)
@@ -498,6 +499,9 @@ PHP_MINIT_FUNCTION(swoole)
498499
REGISTER_LONG_CONSTANT("SWOOLE_ASYNC", SW_FLAG_ASYNC, CONST_CS | CONST_PERSISTENT);
499500
REGISTER_LONG_CONSTANT("SWOOLE_KEEP", SW_FLAG_KEEP, CONST_CS | CONST_PERSISTENT);
500501

502+
REGISTER_LONG_CONSTANT("SWOOLE_EVENT_READ", SW_EVENT_READ, CONST_CS | CONST_PERSISTENT);
503+
REGISTER_LONG_CONSTANT("SWOOLE_EVENT_WRITE", SW_EVENT_WRITE, CONST_CS | CONST_PERSISTENT);
504+
501505
REGISTER_LONG_CONSTANT("SWOOLE_SIGN", SW_NUM_SIGN, CONST_CS | CONST_PERSISTENT);
502506
REGISTER_LONG_CONSTANT("SWOOLE_UNSIGN", SW_NUM_UNSIGN, CONST_CS | CONST_PERSISTENT);
503507
REGISTER_LONG_CONSTANT("SWOOLE_NET", SW_NUM_NET, CONST_CS | CONST_PERSISTENT);
@@ -582,7 +586,7 @@ PHP_MINFO_FUNCTION(swoole)
582586
PHP_RINIT_FUNCTION(swoole)
583587
{
584588
//swoole_event_add
585-
zend_hash_init(&php_sw_reactor_callback, 16, NULL, ZVAL_PTR_DTOR, 0);
589+
zend_hash_init(&php_sw_event_callback, 16, NULL, ZVAL_PTR_DTOR, 0);
586590
//swoole_client::on
587591
zend_hash_init(&php_sw_client_callback, 16, NULL, ZVAL_PTR_DTOR, 0);
588592
//swoole_timer_add
@@ -596,7 +600,7 @@ PHP_RINIT_FUNCTION(swoole)
596600

597601
PHP_RSHUTDOWN_FUNCTION(swoole)
598602
{
599-
zend_hash_destroy(&php_sw_reactor_callback);
603+
zend_hash_destroy(&php_sw_event_callback);
600604
zend_hash_destroy(&php_sw_client_callback);
601605
zend_hash_destroy(&php_sw_timer_callback);
602606
zend_hash_destroy(&php_sw_aio_callback);
@@ -974,7 +978,7 @@ PHP_FUNCTION(swoole_server_set)
974978
static int php_swoole_set_callback(int key, zval *cb TSRMLS_DC)
975979
{
976980
char *func_name = NULL;
977-
if(!zend_is_callable(cb, 0, &func_name TSRMLS_CC))
981+
if (!zend_is_callable(cb, 0, &func_name TSRMLS_CC))
978982
{
979983
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Function '%s' is not callable", func_name);
980984
efree(func_name);

0 commit comments

Comments
 (0)