-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy_server_coro.cc
More file actions
158 lines (124 loc) · 5 KB
/
study_server_coro.cc
File metadata and controls
158 lines (124 loc) · 5 KB
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
#include "study_server_coro.h"
using study::coroutine::Socket;
zend_class_entry study_coroutine_server_coro_ce;
zend_class_entry *study_coroutine_server_coro_ce_ptr;
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_server_coro_void, 0, 0, 0)
ZEND_END_ARG_INFO()
PHP_METHOD(study_coroutine_server_coro, __construct)
{
zval *zhost;
zend_long zport;
zval zsock;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_ZVAL(zhost)
Z_PARAM_LONG(zport)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
Socket *sock = new Socket(AF_INET, SOCK_STREAM, 0);
sock->bind(ST_SOCK_TCP, Z_STRVAL_P(zhost), zport);
sock->listen();
ZVAL_PTR(&zsock, sock);
zend_update_property(study_coroutine_server_coro_ce_ptr, getThis(), ZEND_STRL("zsock"), &zsock);
zend_update_property_string(study_coroutine_server_coro_ce_ptr, getThis(), ZEND_STRL("host"), Z_STRVAL_P(zhost));
zend_update_property_long(study_coroutine_server_coro_ce_ptr, getThis(), ZEND_STRL("port"), zport);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_server_coro_construct, 0, 0, 2)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, port)
ZEND_END_ARG_INFO()
PHP_METHOD(study_coroutine_server_coro, accept)
{
zval *zsock;
Socket *sock;
int connfd;
zsock = st_zend_read_property(study_coroutine_server_coro_ce_ptr, getThis(), ZEND_STRL("zsock"), 0);
sock = (Socket *) Z_PTR_P(zsock);
connfd = sock->accept();
RETURN_LONG(connfd);
}
PHP_METHOD(study_coroutine_server_coro, recv)
{
ssize_t ret;
zend_long fd;
zend_long length = 65536;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_LONG(fd)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(length)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
Socket::init_read_buffer();
Socket conn(fd);
ret = conn.recv(Socket::read_buffer, Socket::read_buffer_len);
if (ret == 0) {
zend_update_property_long(study_coroutine_server_coro_ce_ptr, getThis(), ZEND_STRL("errCode"), ST_ERROR_SESSION_CLOSED_BY_CLIENT);
zend_update_property_string(study_coroutine_server_coro_ce_ptr, getThis(), ZEND_STRL("errMsg"), st_strerror(ST_ERROR_SESSION_CLOSED_BY_CLIENT));
RETURN_FALSE;
}
if (ret < 0) {
php_error_docref(NULL, E_WARNING, "recv error");
RETURN_FALSE;
}
Socket::read_buffer[ret] = '\0';
RETURN_STRING(Socket::read_buffer);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_server_coro_recv, 0, 0, 2)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_INFO(0, length)
ZEND_END_ARG_INFO()
PHP_METHOD(study_coroutine_server_coro, send)
{
ssize_t retval;
zend_long fd;
char *data;
size_t length;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(fd)
Z_PARAM_STRING(data, length)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
Socket conn(fd);
retval = conn.send(data, length);
if (retval < 0) {
php_error_docref(NULL, E_WARNING, "send error");
RETURN_FALSE;
}
RETURN_LONG(retval);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_server_coro_send, 0, 0, 2)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
PHP_METHOD(study_coroutine_server_coro, close)
{
int ret;
zend_long fd;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(fd)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
Socket sock(fd);
ret = sock.close();
if (ret < 0) {
php_error_docref(NULL, E_WARNING, "close error");
RETURN_FALSE;
}
RETURN_LONG(ret);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_server_coro_close, 0, 0, 2)
ZEND_ARG_INFO(0, fd)
ZEND_END_ARG_INFO()
static const zend_function_entry study_coroutine_server_coro_methods[] = {
PHP_ME(study_coroutine_server_coro, __construct, arginfo_study_coroutine_server_coro_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(study_coroutine_server_coro, accept, arginfo_study_coroutine_server_coro_void, ZEND_ACC_PUBLIC)
PHP_ME(study_coroutine_server_coro, recv, arginfo_study_coroutine_server_coro_recv, ZEND_ACC_PUBLIC)
PHP_ME(study_coroutine_server_coro, send, arginfo_study_coroutine_server_coro_send, ZEND_ACC_PUBLIC)
PHP_ME(study_coroutine_server_coro, close, arginfo_study_coroutine_server_coro_close, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void study_coroutine_server_coro_init()
{
INIT_NS_CLASS_ENTRY(study_coroutine_server_coro_ce, "Study", "Coroutine\\Server", study_coroutine_server_coro_methods);
study_coroutine_server_coro_ce_ptr = zend_register_internal_class(&study_coroutine_server_coro_ce TSRMLS_CC);
zend_declare_property_long(study_coroutine_server_coro_ce_ptr, ZEND_STRL("zsock"), -1, ZEND_ACC_PUBLIC);
zend_declare_property_string(study_coroutine_server_coro_ce_ptr, ZEND_STRL("host"), "", ZEND_ACC_PUBLIC);
zend_declare_property_long(study_coroutine_server_coro_ce_ptr, ZEND_STRL("port"), -1, ZEND_ACC_PUBLIC);
zend_declare_property_long(study_coroutine_server_coro_ce_ptr, ZEND_STRL("errCode"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_string(study_coroutine_server_coro_ce_ptr, ZEND_STRL("errMsg"), "", ZEND_ACC_PUBLIC);
}