Skip to content

Commit 2b5ef15

Browse files
committed
Add the async. prefix stream protocol, unlike runtime hooks, the async. protocol is always available
1 parent 379ff4c commit 2b5ef15

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

ext-src/swoole_runtime.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ static int socket_close(php_stream *stream, int close_handle);
101101
static int socket_stat(php_stream *stream, php_stream_statbuf *ssb);
102102
static int socket_cast(php_stream *stream, int castas, void **ret);
103103
static bool socket_ssl_set_options(Socket *sock, php_stream_context *context);
104+
105+
static php_stream *socket_create(const char *proto,
106+
size_t protolen,
107+
const char *resourcename,
108+
size_t resourcenamelen,
109+
const char *persistent_id,
110+
int options,
111+
int flags,
112+
struct timeval *timeout,
113+
php_stream_context *context STREAMS_DC);
114+
104115
// clang-format off
105116
static zend_class_entry *swoole_runtime_ce;
106117

@@ -284,6 +295,13 @@ void php_swoole_runtime_minit(int module_number) {
284295
#endif
285296
swoole_proc_open_init(module_number);
286297

298+
php_stream_xport_register("async.tcp", socket_create);
299+
php_stream_xport_register("async.udp", socket_create);
300+
php_stream_xport_register("async.unix", socket_create);
301+
php_stream_xport_register("async.udg", socket_create);
302+
php_stream_xport_register("async.ssl", socket_create);
303+
php_stream_xport_register("async.tls", socket_create);
304+
287305
php_register_url_stream_wrapper(SW_ASYNC_FILE_PROTOCOL, &sw_php_plain_files_wrapper);
288306
}
289307

@@ -1214,6 +1232,11 @@ static php_stream *socket_create(const char *proto,
12141232
proto, protolen, resourcename, resourcenamelen, persistent_id, options, flags, timeout, context STREAMS_CC);
12151233
}
12161234

1235+
if (SW_STR_STARTS_WITH(proto, protolen, "async.")) {
1236+
proto += sizeof("async.") - 1;
1237+
protolen -= sizeof("async.") - 1;
1238+
}
1239+
12171240
if (SW_STREQ(proto, protolen, "tcp")) {
12181241
sock = new Socket(resourcename[0] == '[' ? SW_SOCK_TCP6 : SW_SOCK_TCP);
12191242
} else if (SW_STREQ(proto, protolen, "ssl") || SW_STREQ(proto, protolen, "tls")) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
swoole_runtime: async protocol
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/../include/skipif.inc';
6+
?>
7+
--FILE--
8+
<?php
9+
require __DIR__ . '/../include/bootstrap.php';
10+
11+
Swoole\Runtime::enableCoroutine(0);
12+
const N = 5;
13+
14+
go(function () {
15+
$socket = new Swoole\Coroutine\Socket(AF_UNIX, SOCK_DGRAM, 0);
16+
$socket->bind(__DIR__ . '/test.sock');
17+
18+
for ($i = 0; $i < N; $i++) {
19+
$peer = null;
20+
$data = $socket->recvfrom($peer);
21+
echo "[Server] recv : $data\n";
22+
}
23+
});
24+
25+
go(function () {
26+
$fp = stream_socket_client("async.udg://".__DIR__."/test.sock", $errno, $errstr, 30);
27+
if (!$fp) {
28+
echo "$errstr ($errno)<br />\n";
29+
} else {
30+
for ($i = 0; $i < N; $i++) {
31+
fwrite($fp, "hello-{$i}");
32+
}
33+
fclose($fp);
34+
}
35+
});
36+
Swoole\Event::wait();
37+
?>
38+
--EXPECT--
39+
[Server] recv : hello-0
40+
[Server] recv : hello-1
41+
[Server] recv : hello-2
42+
[Server] recv : hello-3
43+
[Server] recv : hello-4

0 commit comments

Comments
 (0)