Skip to content

Commit 93a2133

Browse files
Refactor WebSocket Server/Client (#5867)
* optimize websocket continue frame (#5855) * refactor websocket * fix * fix 2 * fix 3 * fix 4 * fix 5 * fix 6 * fix 7 --filter=[unit] * optimize websocket continue frame (#5861) * optimize websocket continue frame * optimize code * add test * send close frame and close connection * fix code * optimize code * optimize code * Optimize websocket namespace * Optimize websocket namespace [2] * Optimize websocket namespace [3] --------- Co-authored-by: MARiA so cute <[email protected]>
1 parent 6ed9354 commit 93a2133

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2029
-546
lines changed

core-tests/include/test_server.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
#define SERVER_THIS ((swoole::test::Server *) serv->private_data_2)
77

8-
#define ON_START_PARAMS swServer *serv
9-
#define ON_WORKER_START_PARAMS swServer *serv, swoole::Worker *worker
10-
#define ON_PACKET_PARAMS swServer *serv, swRecvData *req
11-
#define ON_RECEIVE_PARAMS swServer *serv, swRecvData *req
8+
#define ON_START_PARAMS swoole::Server *serv
9+
#define ON_WORKER_START_PARAMS swoole::Server *serv, swoole::Worker *worker
10+
#define ON_PACKET_PARAMS swoole::Server *serv, swoole::RecvData *req
11+
#define ON_RECEIVE_PARAMS swoole::Server *serv, swoole::RecvData *req
1212

1313
namespace swoole {
1414
namespace test {

core-tests/src/core/base.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ TEST(base, only_dump) {
366366
// just dump something
367367
std::string data = "hello world";
368368
swoole_dump_ascii(data.c_str(), data.length());
369-
swoole_dump_bin(data.c_str(), 'C', data.length());
370-
swoole_dump_hex(data.c_str(), data.length());
369+
swoole_dump_bin((uchar *) data.c_str(), 'C', data.length());
370+
swoole_dump_hex((uchar *) data.c_str(), data.length());
371371
ASSERT_TRUE(true);
372372
}
373373

core-tests/src/server/http_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ TEST(http_server, get_package_length) {
18471847
// websocket
18481848
sw_tg_buffer()->clear();
18491849
conn.websocket_status = websocket::STATUS_HANDSHAKE;
1850-
ASSERT_EQ(http_server::get_package_length_size(&fake_sock), SW_WEBSOCKET_MESSAGE_HEADER_SIZE);
1850+
ASSERT_EQ(http_server::get_package_length_size(&fake_sock), SW_WEBSOCKET_FRAME_HEADER_SIZE);
18511851
ASSERT_TRUE(websocket::encode(sw_tg_buffer(), SW_STRL(TEST_STR), websocket::OPCODE_TEXT, websocket::FLAG_FIN));
18521852
pl.buf = sw_tg_buffer()->str;
18531853
pl.buf_size = sw_tg_buffer()->length;

ext-src/php_swoole.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,6 @@ PHP_MSHUTDOWN_FUNCTION(swoole) {
930930
zend::known_strings_dtor();
931931

932932
php_swoole_runtime_mshutdown();
933-
php_swoole_websocket_server_mshutdown();
934933
#ifdef SW_USE_PGSQL
935934
php_swoole_pgsql_mshutdown();
936935
#endif
@@ -1129,7 +1128,7 @@ static void *_sw_zend_string_realloc(void *address, size_t size) {
11291128
}
11301129

11311130
static void _sw_zend_string_free(void *address) {
1132-
zend_string_free(zend::fetch_zend_string_by_val(address));
1131+
zend_string_release_ex(zend::fetch_zend_string_by_val(address), 0);
11331132
}
11341133

11351134
static swoole::Allocator php_allocator{
@@ -1240,7 +1239,6 @@ PHP_RINIT_FUNCTION(swoole) {
12401239
swoole_add_hook(SW_GLOBAL_HOOK_AFTER_FORK, sw_after_fork, 0);
12411240

12421241
php_swoole_http_server_rinit();
1243-
php_swoole_websocket_server_rinit();
12441242
php_swoole_coroutine_rinit();
12451243
php_swoole_runtime_rinit();
12461244
#ifdef SW_USE_ORACLE
@@ -1266,7 +1264,6 @@ PHP_RSHUTDOWN_FUNCTION(swoole) {
12661264

12671265
php_swoole_server_rshutdown();
12681266
php_swoole_http_server_rshutdown();
1269-
php_swoole_websocket_server_rshutdown();
12701267
php_swoole_async_coro_rshutdown();
12711268
php_swoole_redis_server_rshutdown();
12721269
php_swoole_coroutine_rshutdown();

ext-src/php_swoole_cxx.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,6 @@ SW_API zend_long php_swoole_parse_to_size(zval *zv);
165165
SW_API zend_string *php_swoole_serialize(zval *zdata);
166166
SW_API bool php_swoole_unserialize(zend_string *data, zval *zv);
167167

168-
#ifdef SW_HAVE_ZLIB
169-
#define php_swoole_websocket_frame_pack php_swoole_websocket_frame_pack_ex
170-
#define php_swoole_websocket_frame_object_pack php_swoole_websocket_frame_object_pack_ex
171-
#else
172-
#define php_swoole_websocket_frame_pack(buffer, zdata, opcode, flags, mask, allow_compress) \
173-
php_swoole_websocket_frame_pack_ex(buffer, zdata, opcode, flags, mask, 0)
174-
#define php_swoole_websocket_frame_object_pack(buffer, zdata, mask, allow_compress) \
175-
php_swoole_websocket_frame_object_pack_ex(buffer, zdata, mask, 0)
176-
#endif
177-
int php_swoole_websocket_frame_pack_ex(
178-
swoole::String *buffer, zval *zdata, zend_long opcode, uint8_t flags, zend_bool mask, zend_bool allow_compress);
179-
int php_swoole_websocket_frame_object_pack_ex(swoole::String *buffer,
180-
zval *zdata,
181-
zend_bool mask,
182-
zend_bool allow_compress);
183-
void php_swoole_websocket_frame_unpack(swoole::String *data, zval *zframe);
184-
void php_swoole_websocket_frame_unpack_ex(swoole::String *data, zval *zframe, uchar allow_uncompress);
185-
186168
#ifdef SW_HAVE_ZLIB
187169
int php_swoole_zlib_decompress(z_stream *stream, swoole::String *buffer, char *body, int length);
188170
#endif

ext-src/php_swoole_http.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "swoole_http.h"
2222
#include "swoole_http2.h"
2323
#include "swoole_llhttp.h"
24+
#include "swoole_websocket.h"
25+
2426
#include "thirdparty/multipart_parser.h"
2527

2628
#include <unordered_map>
@@ -74,13 +76,13 @@ enum swHttpErrorStatusCode {
7476
namespace swoole {
7577
class Server;
7678
class Coroutine;
79+
7780
namespace http2 {
7881
class Stream;
7982
class Session;
8083
} // namespace http2
8184

8285
namespace http {
83-
8486
struct Request {
8587
int version;
8688
char *path;
@@ -146,9 +148,7 @@ struct Context {
146148
uchar send_trailer_ : 1;
147149
uchar keepalive : 1;
148150
uchar websocket : 1;
149-
#ifdef SW_HAVE_ZLIB
150151
uchar websocket_compression : 1;
151-
#endif
152152
uchar upgrade : 1;
153153
uchar detached : 1;
154154
uchar parse_cookie : 1;
@@ -168,6 +168,9 @@ struct Context {
168168
std::shared_ptr<String> zlib_buffer;
169169
#endif
170170

171+
std::shared_ptr<String> frame_buffer;
172+
WebSocketSettings websocket_settings;
173+
171174
Request request;
172175
Response response;
173176

@@ -227,6 +230,7 @@ struct Context {
227230
bool compress(const char *data, size_t length);
228231
#endif
229232

233+
void recv_websocket_frame(zval *return_value, double timeout);
230234
void http2_end(zval *zdata, zval *return_value);
231235
void http2_write(zval *zdata, zval *return_value);
232236
bool http2_send_file(const char *file, uint32_t l_file, off_t offset, size_t length);

ext-src/php_swoole_http_server.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
#include "php_swoole_server.h"
2222
#include "php_swoole_http.h"
2323

24-
#include "swoole_http.h"
25-
#include "swoole_websocket.h"
2624
#include "swoole_mime_type.h"
27-
#include "swoole_http2.h"
2825

2926
bool swoole_http_server_onBeforeRequest(swoole::http::Context *ctx);
3027
void swoole_http_server_onAfterResponse(swoole::http::Context *ctx);

ext-src/php_swoole_private.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ void php_swoole_stdext_minit(int module_number);
317317
* ==============================================================
318318
*/
319319
void php_swoole_http_server_rinit();
320-
void php_swoole_websocket_server_rinit();
321320
void php_swoole_coroutine_rinit();
322321
void php_swoole_runtime_rinit();
323322
#ifdef SW_USE_ORACLE
@@ -330,7 +329,6 @@ void php_swoole_thread_rinit();
330329
* ==============================================================
331330
*/
332331
void php_swoole_http_server_rshutdown();
333-
void php_swoole_websocket_server_rshutdown();
334332
void php_swoole_async_coro_rshutdown();
335333
void php_swoole_redis_server_rshutdown();
336334
void php_swoole_coroutine_rshutdown();
@@ -362,7 +360,6 @@ void php_swoole_event_exit();
362360
* ==============================================================
363361
*/
364362
void php_swoole_runtime_mshutdown();
365-
void php_swoole_websocket_server_mshutdown();
366363
#ifdef SW_USE_PGSQL
367364
void php_swoole_pgsql_mshutdown();
368365
#endif
@@ -373,10 +370,6 @@ void php_swoole_oracle_mshutdown();
373370
void php_swoole_sqlite_mshutdown();
374371
#endif
375372

376-
static sw_inline zend_bool php_swoole_websocket_frame_is_object(zval *zdata) {
377-
return Z_TYPE_P(zdata) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zdata), swoole_websocket_frame_ce);
378-
}
379-
380373
static sw_inline size_t php_swoole_get_send_data(zval *zdata, char **str) {
381374
convert_to_string(zdata);
382375
*str = Z_STRVAL_P(zdata);
@@ -454,6 +447,10 @@ static sw_inline zend_bool ZVAL_IS_STRING(const zval *v) {
454447
return Z_TYPE_P(v) == IS_STRING;
455448
}
456449

450+
static sw_inline zend_bool ZVAL_IS_EMPTY_STRING(const zval *v) {
451+
return Z_TYPE_P(v) == IS_STRING && Z_STRLEN_P(v) == 0;
452+
}
453+
457454
static sw_inline zend_bool Z_BVAL_P(const zval *v) {
458455
return Z_TYPE_P(v) == IS_TRUE;
459456
}

ext-src/php_swoole_server.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,20 @@ void php_swoole_server_register_callbacks(swServer *serv);
142142
zend::Callable *php_swoole_server_get_callback(swServer *serv, int server_fd, int event_type);
143143
int php_swoole_create_dir(const char *path, size_t length);
144144
void php_swoole_server_before_start(swServer *serv, zval *zobject);
145-
bool php_swoole_server_isset_callback(swServer *serv, swListenPort *port, int event_type);
145+
bool php_swoole_server_isset_callback(swServer *serv, swoole::ListenPort *port, int event_type);
146146
void php_swoole_server_send_yield(swServer *serv, swoole::SessionId sesion_id, zval *zdata, zval *return_value);
147-
void php_swoole_get_recv_data(swServer *serv, zval *zdata, swRecvData *req);
148-
void php_swoole_server_onConnect(swServer *, swDataHead *);
149-
int php_swoole_server_onReceive(swServer *, swRecvData *);
150-
int php_swoole_http_server_onReceive(swServer *, swRecvData *);
151-
void php_swoole_http_server_onClose(swServer *serv, swDataHead *info);
152-
int php_swoole_redis_server_onReceive(swServer *serv, swRecvData *req);
153-
int php_swoole_server_onPacket(swServer *, swRecvData *);
154-
void php_swoole_server_onClose(swServer *, swDataHead *);
155-
void php_swoole_server_onBufferFull(swServer *, swDataHead *);
156-
void php_swoole_server_onBufferEmpty(swServer *, swDataHead *);
147+
void php_swoole_get_recv_data(swServer *serv, zval *zdata, swoole::RecvData *req);
148+
void php_swoole_server_onConnect(swServer *, swoole::DataHead *);
149+
int php_swoole_server_onReceive(swServer *, swoole::RecvData *);
150+
int php_swoole_http_server_onReceive(swServer *, swoole::RecvData *);
151+
void php_swoole_http_server_onClose(swServer *serv, swoole::DataHead *info);
152+
int php_swoole_redis_server_onReceive(swServer *serv, swoole::RecvData *req);
153+
int php_swoole_server_onPacket(swServer *, swoole::RecvData *);
154+
void php_swoole_server_onClose(swServer *, swoole::DataHead *);
155+
void php_swoole_server_onBufferFull(swServer *, swoole::DataHead *);
156+
void php_swoole_server_onBufferEmpty(swServer *, swoole::DataHead *);
157157

158158
swServer *php_swoole_server_get_and_check_server(zval *zobject);
159159
void php_swoole_server_port_deref(zend_object *object);
160+
void php_swoole_server_set_websocket_option(swoole::ListenPort *port, zend_array *vht);
160161
swoole::ServerObject *php_swoole_server_get_zend_object(swoole::Server *serv);
161-

ext-src/php_swoole_websocket.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Swoole |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 2012-2015 The Swoole Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 2.0 of the Apache license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.apache.org/licenses/LICENSE-2.0.html |
11+
| If you did not receive a copy of the Apache2.0 license and are unable|
12+
| to obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Tianfeng Han <[email protected]> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
#pragma once
20+
21+
#include "php_swoole_cxx.h"
22+
23+
#include "swoole_websocket.h"
24+
25+
#define SW_WEBSOCKET_DEFAULT_BUFFER 4096
26+
27+
namespace swoole {
28+
namespace websocket {
29+
void apply_setting(WebSocketSettings &settings, zend_array *vht, bool in_server);
30+
void recv_frame(const WebSocketSettings &settings,
31+
std::shared_ptr<String> &frame_buffer,
32+
coroutine::Socket *sock,
33+
zval *return_value,
34+
double timeout);
35+
ssize_t send_frame(const WebSocketSettings &settings,
36+
coroutine::Socket *sock,
37+
uchar opcode,
38+
uchar flags,
39+
const char *payload,
40+
size_t payload_length);
41+
void construct_frame(zval *zframe, zend_long opcode, zval *zpayload, uint8_t flags);
42+
43+
#ifdef SW_HAVE_ZLIB
44+
bool message_compress(String *buffer, const char *data, size_t length, int level);
45+
bool message_uncompress(String *buffer, const char *in, size_t in_len);
46+
#endif
47+
48+
struct FrameObject {
49+
uint8_t opcode;
50+
uint8_t flags;
51+
uint16_t code;
52+
zval *data;
53+
54+
FrameObject(zval *data, zend_long _opcode = 0, zend_long _flags = 0, zend_long _code = 0);
55+
size_t get_data_size() {
56+
return (data && ZVAL_IS_STRING(data)) ? Z_STRLEN_P(data) : 0;
57+
}
58+
bool pack(String *buffer);
59+
static bool uncompress(zval *zpayload, const char *data, size_t length);
60+
};
61+
} // namespace websocket
62+
} // namespace swoole

0 commit comments

Comments
 (0)