forked from zhanglei/async-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_channel.c
160 lines (132 loc) · 4.81 KB
/
swoole_channel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| Copyright (c) 2012-2015 The Swoole Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
static PHP_METHOD(swoole_channel, __construct);
static PHP_METHOD(swoole_channel, __destruct);
static PHP_METHOD(swoole_channel, push);
static PHP_METHOD(swoole_channel, pop);
static PHP_METHOD(swoole_channel, peek);
static PHP_METHOD(swoole_channel, stats);
zend_class_entry *swoole_channel_ce;
static zend_object_handlers swoole_channel_handlers;
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_channel_construct, 0, 0, 1)
ZEND_ARG_INFO(0, size)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_channel_push, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
ZEND_END_ARG_INFO()
static const zend_function_entry swoole_channel_methods[] =
{
PHP_ME(swoole_channel, __construct, arginfo_swoole_channel_construct, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel, __destruct, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel, push, arginfo_swoole_channel_push, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel, pop, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel, peek, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel, stats, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void swoole_channel_init(int module_number)
{
SW_INIT_CLASS_ENTRY(swoole_channel, "Swoole\\Channel", "swoole_channel", NULL, swoole_channel_methods);
SW_SET_CLASS_SERIALIZABLE(swoole_channel, zend_class_serialize_deny, zend_class_unserialize_deny);
SW_SET_CLASS_CLONEABLE(swoole_channel, sw_zend_class_clone_deny);
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_channel, sw_zend_class_unset_property_deny);
}
static PHP_METHOD(swoole_channel, __construct)
{
long size;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE)
{
RETURN_FALSE;
}
if (size < SW_BUFFER_SIZE_STD)
{
size = SW_BUFFER_SIZE_STD;
}
swChannel *chan = swChannel_new(size, SW_BUFFER_SIZE_STD, SW_CHAN_LOCK | SW_CHAN_SHM);
if (chan == NULL)
{
zend_throw_exception(swoole_exception_ce, "failed to create channel.", SW_ERROR_MALLOC_FAIL);
RETURN_FALSE;
}
swoole_set_object(getThis(), chan);
}
static PHP_METHOD(swoole_channel, __destruct)
{
SW_PREVENT_USER_DESTRUCT();
swoole_set_object(getThis(), NULL);
}
static PHP_METHOD(swoole_channel, push)
{
swChannel *chan = swoole_get_object(getThis());
zval *zdata;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zdata) == FAILURE)
{
RETURN_FALSE;
}
swEventData buf;
if (php_swoole_task_pack(&buf, zdata) < 0)
{
RETURN_FALSE;
}
SW_CHECK_RETURN(swChannel_push(chan, &buf, sizeof(buf.info) + buf.info.len));
}
static PHP_METHOD(swoole_channel, pop)
{
swChannel *chan = swoole_get_object(getThis());
swEventData buf;
int n = swChannel_pop(chan, &buf, sizeof(buf));
if (n < 0)
{
RETURN_FALSE;
}
zval *ret_data = php_swoole_task_unpack(&buf);
if (ret_data == NULL)
{
RETURN_FALSE;
}
RETVAL_ZVAL(ret_data, 0, NULL);
efree(ret_data);
}
static PHP_METHOD(swoole_channel, peek)
{
swChannel *chan = swoole_get_object(getThis());
swEventData buf;
int n = swChannel_peek(chan, &buf, sizeof(buf));
if (n < 0)
{
RETURN_FALSE;
}
swTask_type(&buf) |= SW_TASK_PEEK;
zval *ret_data = php_swoole_task_unpack(&buf);
if (ret_data == NULL)
{
RETURN_FALSE;
}
RETVAL_ZVAL(ret_data, 0, NULL);
efree(ret_data);
}
static PHP_METHOD(swoole_channel, stats)
{
swChannel *chan = swoole_get_object(getThis());
array_init(return_value);
add_assoc_long_ex(return_value, ZEND_STRL("queue_num"), chan->num);
add_assoc_long_ex(return_value, ZEND_STRL("queue_bytes"), chan->bytes);
}