Skip to content

Commit 0e855e7

Browse files
authored
optimize callback name, allow to set open_http_protocol with Server (swoole#4063)
* optimize callback name, allow to set open_http_protocol with Server * optimize code, add test * fix tests * optimize code * optimize code [2] * fix * fix 2 * fix 3 * fix 4, optimize code * fix tests * fix tests [2]
1 parent 6672e54 commit 0e855e7

File tree

9 files changed

+405
-294
lines changed

9 files changed

+405
-294
lines changed

ext-src/php_swoole.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ extern zend_class_entry *swoole_socket_coro_ce;
171171
extern zend_class_entry *swoole_client_ce;
172172
extern zend_class_entry *swoole_server_ce;
173173
extern zend_object_handlers swoole_server_handlers;
174+
extern zend_class_entry *swoole_redis_server_ce;
175+
extern zend_object_handlers swoole_redis_server_handlers;
174176
extern zend_class_entry *swoole_connection_iterator_ce;
175177
extern zend_class_entry *swoole_process_ce;
176178
extern zend_class_entry *swoole_http_server_ce;

ext-src/php_swoole_server.h

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include "php_swoole_cxx.h"
2222
#include "swoole_server.h"
2323

24+
#include <unordered_map>
25+
#include <list>
26+
#include <vector>
27+
2428
//--------------------------------------------------------
2529
enum php_swoole_server_callback_type {
2630
SW_SERVER_CB_onStart, // master
@@ -54,30 +58,90 @@ enum php_swoole_server_port_callback_type {
5458
#define PHP_SWOOLE_SERVER_CALLBACK_NUM (SW_SERVER_CB_onPipeMessage + 1)
5559
#define PHP_SWOOLE_SERVER_PORT_CALLBACK_NUM (SW_SERVER_CB_onBufferEmpty + 1)
5660

57-
struct php_swoole_server_port_property {
61+
namespace swoole {
62+
struct TaskCo;
63+
64+
struct ServerPortProperty {
5865
zval *callbacks[PHP_SWOOLE_SERVER_PORT_CALLBACK_NUM];
5966
zend_fcall_info_cache *caches[PHP_SWOOLE_SERVER_PORT_CALLBACK_NUM];
6067
zval _callbacks[PHP_SWOOLE_SERVER_PORT_CALLBACK_NUM];
61-
swServer *serv;
62-
swListenPort *port;
68+
Server *serv;
69+
ListenPort *port;
6370
zval *zsetting;
6471
};
6572

73+
struct ServerProperty {
74+
std::vector<zval *> ports;
75+
std::vector<zval *> user_processes;
76+
ServerPortProperty *primary_port;
77+
zend_fcall_info_cache *callbacks[PHP_SWOOLE_SERVER_CALLBACK_NUM];
78+
std::unordered_map<TaskId, zend_fcall_info_cache> task_callbacks;
79+
std::unordered_map<TaskId, TaskCo *> task_coroutine_map;
80+
std::unordered_map<SessionId, std::list<FutureTask *> *> send_coroutine_map;
81+
};
82+
83+
struct ServerObject {
84+
Server *serv;
85+
ServerProperty *property;
86+
zend_object std;
87+
88+
zend_class_entry *get_ce() {
89+
return Z_OBJCE_P(get_object());
90+
}
91+
92+
zval *get_object() {
93+
return (zval *) serv->private_data_2;
94+
}
95+
96+
bool isset_callback(ListenPort *port, int event_type) {
97+
ServerPortProperty *port_property = (ServerPortProperty *) port->ptr;
98+
return (port_property->callbacks[event_type] || property->primary_port->callbacks[event_type]);
99+
}
100+
101+
zend_bool is_websocket_server() {
102+
return instanceof_function(get_ce(), swoole_websocket_server_ce);
103+
}
104+
105+
zend_bool is_http_server() {
106+
return instanceof_function(get_ce(), swoole_http_server_ce);
107+
}
108+
109+
zend_bool is_redis_server() {
110+
return instanceof_function(get_ce(), swoole_redis_server_ce);
111+
}
112+
113+
void register_callback();
114+
void on_before_start();
115+
};
116+
117+
struct TaskCo {
118+
FutureTask context;
119+
int *list;
120+
uint32_t count;
121+
zval *result;
122+
TimerNode *timer;
123+
ServerObject *server_object;
124+
};
125+
126+
} // namespace swoole
127+
66128
void php_swoole_server_register_callbacks(swServer *serv);
67129
zend_fcall_info_cache *php_swoole_server_get_fci_cache(swServer *serv, int server_fd, int event_type);
68130
int php_swoole_create_dir(const char *path, size_t length);
69131
void php_swoole_server_before_start(swServer *serv, zval *zobject);
132+
bool php_swoole_server_isset_callback(swServer *serv, swListenPort *port, int event_type);
70133
void php_swoole_http_server_init_global_variant();
71134
void php_swoole_server_send_yield(swServer *serv, swoole::SessionId sesion_id, zval *zdata, zval *return_value);
72135
void php_swoole_get_recv_data(swServer *serv, zval *zdata, swRecvData *req);
73-
void php_swoole_onConnect(swServer *, swDataHead *);
74-
int php_swoole_onReceive(swServer *, swRecvData *);
75-
int php_swoole_http_onReceive(swServer *, swRecvData *);
76-
void php_swoole_http_onClose(swServer *, swDataHead *);
77-
int php_swoole_onPacket(swServer *, swRecvData *);
78-
void php_swoole_onClose(swServer *, swDataHead *);
79-
void php_swoole_onBufferFull(swServer *, swDataHead *);
80-
void php_swoole_onBufferEmpty(swServer *, swDataHead *);
136+
void php_swoole_server_onConnect(swServer *, swDataHead *);
137+
int php_swoole_server_onReceive(swServer *, swRecvData *);
138+
int php_swoole_http_server_onReceive(swServer *, swRecvData *);
139+
int php_swoole_redis_server_onReceive(swServer *serv, swRecvData *req);
140+
void php_swoole_http_server_onClose(swServer *, swDataHead *);
141+
int php_swoole_server_onPacket(swServer *, swRecvData *);
142+
void php_swoole_server_onClose(swServer *, swDataHead *);
143+
void php_swoole_server_onBufferFull(swServer *, swDataHead *);
144+
void php_swoole_server_onBufferEmpty(swServer *, swDataHead *);
81145

82146
swServer *php_swoole_server_get_and_check_server(zval *zobject);
83147
void php_swoole_server_port_deref(zend_object *object);

ext-src/swoole_http_server.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static bool http_context_send_data(http_context *ctx, const char *data, size_t l
3838
static bool http_context_sendfile(http_context *ctx, const char *file, uint32_t l_file, off_t offset, size_t length);
3939
static bool http_context_disconnect(http_context *ctx);
4040

41-
int php_swoole_http_onReceive(Server *serv, RecvData *req) {
41+
int php_swoole_http_server_onReceive(Server *serv, RecvData *req) {
4242
SessionId session_id = req->info.fd;
4343
int server_fd = req->info.server_fd;
4444

@@ -50,8 +50,14 @@ int php_swoole_http_onReceive(Server *serv, RecvData *req) {
5050

5151
ListenPort *port = serv->get_port_by_server_fd(server_fd);
5252
// other server port
53-
if (!port->open_http_protocol) {
54-
return php_swoole_onReceive(serv, req);
53+
if (!port->open_http_protocol || !php_swoole_server_isset_callback(serv, port, SW_SERVER_CB_onRequest)) {
54+
SW_LOOP {
55+
if (port->open_websocket_protocol && php_swoole_server_isset_callback(serv, port, SW_SERVER_CB_onMessage)) {
56+
break;
57+
} else {
58+
return php_swoole_server_onReceive(serv, req);
59+
}
60+
}
5561
}
5662
// websocket client
5763
if (conn->websocket_status == WEBSOCKET_STATUS_ACTIVE) {
@@ -142,12 +148,12 @@ int php_swoole_http_onReceive(Server *serv, RecvData *req) {
142148
return SW_OK;
143149
}
144150

145-
void php_swoole_http_onClose(Server *serv, DataHead *ev) {
151+
void php_swoole_http_server_onClose(Server *serv, DataHead *ev) {
146152
Connection *conn = serv->get_connection_by_session_id(ev->fd);
147153
if (!conn) {
148154
return;
149155
}
150-
php_swoole_onClose(serv, ev);
156+
php_swoole_server_onClose(serv, ev);
151157
#ifdef SW_USE_HTTP2
152158
if (conn->http2_stream) {
153159
swoole_http2_server_session_free(conn);

ext-src/swoole_redis_server.cc

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ BEGIN_EXTERN_C()
2424
#include "ext/standard/php_string.h"
2525
END_EXTERN_C()
2626

27+
using swoole::Server;
28+
using swoole::RecvData;
29+
using swoole::ListenPort;
30+
using swoole::Connection;
2731

28-
static zend_class_entry *swoole_redis_server_ce;
29-
static zend_object_handlers swoole_redis_server_handlers;
32+
zend_class_entry *swoole_redis_server_ce;
33+
zend_object_handlers swoole_redis_server_handlers;
3034

3135
static std::unordered_map<std::string, zend_fcall_info_cache> redis_handlers;
3236

@@ -38,9 +42,6 @@ static PHP_METHOD(swoole_redis_server, format);
3842
SW_EXTERN_C_END
3943

4044
// clang-format off
41-
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_redis_server_start, 0, 0, 0)
42-
ZEND_END_ARG_INFO()
43-
4445
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_redis_server_setHandler, 0, 0, 2)
4546
ZEND_ARG_INFO(0, command)
4647
ZEND_ARG_CALLABLE_INFO(0, callback, 0)
@@ -57,7 +58,6 @@ ZEND_END_ARG_INFO()
5758

5859
const zend_function_entry swoole_redis_server_methods[] =
5960
{
60-
PHP_ME(swoole_redis_server, start, arginfo_swoole_redis_server_start, ZEND_ACC_PUBLIC)
6161
PHP_ME(swoole_redis_server, setHandler, arginfo_swoole_redis_server_setHandler, ZEND_ACC_PUBLIC)
6262
PHP_ME(swoole_redis_server, getHandler, arginfo_swoole_redis_server_getHandler, ZEND_ACC_PUBLIC)
6363
PHP_ME(swoole_redis_server, format, arginfo_swoole_redis_server_format, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
@@ -92,18 +92,18 @@ void php_swoole_redis_server_rshutdown() {
9292
redis_handlers.clear();
9393
}
9494

95-
static int redis_onReceive(swServer *serv, swRecvData *req) {
95+
int php_swoole_redis_server_onReceive(Server *serv, RecvData *req) {
9696
int fd = req->info.fd;
97-
swConnection *conn = serv->get_connection_by_session_id(fd);
97+
Connection *conn = serv->get_connection_by_session_id(fd);
9898
if (!conn) {
9999
swWarn("connection[%d] is closed", fd);
100100
return SW_ERR;
101101
}
102102

103-
swListenPort *port = serv->get_port_by_fd(conn->fd);
103+
ListenPort *port = serv->get_port_by_fd(conn->fd);
104104
// other server port
105105
if (!port->open_redis_protocol) {
106-
return php_swoole_onReceive(serv, req);
106+
return php_swoole_server_onReceive(serv, req);
107107
}
108108

109109
zval zdata;
@@ -206,44 +206,6 @@ static int redis_onReceive(swServer *serv, swRecvData *req) {
206206
return SW_OK;
207207
}
208208

209-
static PHP_METHOD(swoole_redis_server, start) {
210-
swServer *serv = php_swoole_server_get_and_check_server(ZEND_THIS);
211-
zval *zserv = ZEND_THIS;
212-
213-
if (serv->is_started()) {
214-
php_swoole_error(E_WARNING, "server is running, unable to execute %s->start", SW_Z_OBJCE_NAME_VAL_P(zserv));
215-
RETURN_FALSE;
216-
}
217-
218-
php_swoole_server_register_callbacks(serv);
219-
220-
serv->onReceive = redis_onReceive;
221-
222-
zval *zsetting = sw_zend_read_and_convert_property_array(swoole_server_ce, zserv, ZEND_STRL("setting"), 0);
223-
224-
add_assoc_bool(zsetting, "open_http_protocol", 0);
225-
add_assoc_bool(zsetting, "open_mqtt_protocol", 0);
226-
add_assoc_bool(zsetting, "open_eof_check", 0);
227-
add_assoc_bool(zsetting, "open_length_check", 0);
228-
add_assoc_bool(zsetting, "open_redis_protocol", 0);
229-
230-
auto primary_port = serv->get_primary_port();
231-
232-
primary_port->open_http_protocol = 0;
233-
primary_port->open_mqtt_protocol = 0;
234-
primary_port->open_eof_check = 0;
235-
primary_port->open_length_check = 0;
236-
primary_port->open_redis_protocol = 1;
237-
238-
php_swoole_server_before_start(serv, zserv);
239-
240-
if (serv->start() < 0) {
241-
php_swoole_fatal_error(E_ERROR, "server failed to start. Error: %s", sw_error);
242-
}
243-
244-
RETURN_TRUE;
245-
}
246-
247209
static PHP_METHOD(swoole_redis_server, setHandler) {
248210
char *command;
249211
size_t command_len;

0 commit comments

Comments
 (0)