You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
28
vendor/swoole/.github/ISSUE_TEMPLATE
vendored
Executable file
28
vendor/swoole/.github/ISSUE_TEMPLATE
vendored
Executable file
@ -0,0 +1,28 @@
|
||||
Please answer these questions before submitting your issue. Thanks!
|
||||
|
||||
1. What did you do? If possible, provide a recipe for reproducing the error.
|
||||
|
||||
|
||||
|
||||
2. What did you expect to see?
|
||||
|
||||
|
||||
|
||||
3. What did you see instead?
|
||||
|
||||
|
||||
|
||||
4. What version of Swoole are you using (`php --ri swoole`)?
|
||||
|
||||
|
||||
|
||||
|
||||
5. What is your machine environment used (including version of kernel & php & gcc) ?
|
||||
|
||||
|
||||
|
||||
6. If you are using ssl, what is your openssl version?
|
||||
|
||||
|
||||
|
||||
|
81
vendor/swoole/.gitignore
vendored
Executable file
81
vendor/swoole/.gitignore
vendored
Executable file
@ -0,0 +1,81 @@
|
||||
/.cproject
|
||||
/.idea
|
||||
/.project
|
||||
/examples/yield/
|
||||
*.o
|
||||
*.lo
|
||||
*~
|
||||
*.so
|
||||
*.loT
|
||||
*.pid
|
||||
Debug/*
|
||||
modules/*
|
||||
/.deps
|
||||
/.libs/
|
||||
/core
|
||||
/examples/core
|
||||
/Debug
|
||||
/CMakeFiles
|
||||
/cmake_install.cmake
|
||||
/CMakeCache.txt
|
||||
/Makefile
|
||||
/server
|
||||
/lib
|
||||
/bin
|
||||
/install_manifest.txt
|
||||
/wiki
|
||||
/config.guess
|
||||
/config.h
|
||||
/config.h.in
|
||||
/config.log
|
||||
/config.nice
|
||||
/config.status
|
||||
/config.sub
|
||||
/configure
|
||||
/configure.in
|
||||
/Makefile.fragments
|
||||
/Makefile.global
|
||||
/Makefile.objects
|
||||
/install-sh
|
||||
/libtool
|
||||
/ltmain.sh
|
||||
/missing
|
||||
/mkinstalldirs
|
||||
/swoole.la
|
||||
/acinclude.m4
|
||||
/aclocal.m4
|
||||
/run-tests.php
|
||||
/autom4te.cache
|
||||
/build
|
||||
/examples/async/data.txt
|
||||
/examples/.idea
|
||||
/examples/recv_file.jpg
|
||||
/modules
|
||||
/examples/async/test.copy
|
||||
/examples/ssl_client
|
||||
/examples/ssl/ca.crt
|
||||
/examples/ssl/client.key
|
||||
/examples/ssl/client.pfx
|
||||
/examples/ssl/client
|
||||
/examples/ssl/corpssl.crt
|
||||
/examples/ssl/corpssl.key
|
||||
/examples/ext
|
||||
/examples/cpp_module/.cproject
|
||||
/examples/cpp_module/.project
|
||||
/benchmark/.idea/
|
||||
examples/c_module/.cproject
|
||||
examples/c_module/.project
|
||||
/tools/.idea/
|
||||
/tmp-php.ini
|
||||
.settings/
|
||||
tests/.idea
|
||||
/tests/*/*.log
|
||||
/tests/*/*.sh
|
||||
/tests/*/*.diff
|
||||
/tests/*/*.out
|
||||
/tests/*/*.exp
|
||||
/tests/*/*.php
|
||||
cmake-build-debug/
|
||||
*.cbp
|
||||
/.vscode
|
||||
/configure.ac
|
12
vendor/swoole/.gitmodules
vendored
Executable file
12
vendor/swoole/.gitmodules
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
[submodule "thirdparty/hiredis"]
|
||||
path = thirdparty/hiredis
|
||||
url = https://github.com/redis/hiredis.git
|
||||
[submodule "thirdparty/nghttp2"]
|
||||
path = thirdparty/nghttp2
|
||||
url = https://github.com/nghttp2/nghttp2.git
|
||||
[submodule "thirdparty/picohttpparser"]
|
||||
path = thirdparty/picohttpparser
|
||||
url = https://github.com/h2o/picohttpparser.git
|
||||
[submodule "thirdparty/jemalloc"]
|
||||
path = thirdparty/jemalloc
|
||||
url = https://github.com/jemalloc/jemalloc.git
|
42
vendor/swoole/.travis.yml
vendored
Executable file
42
vendor/swoole/.travis.yml
vendored
Executable file
@ -0,0 +1,42 @@
|
||||
language: php
|
||||
|
||||
compiler:
|
||||
- gcc
|
||||
- clang
|
||||
|
||||
os:
|
||||
- linux
|
||||
# - osx
|
||||
|
||||
php:
|
||||
- 7.1
|
||||
- 7.2
|
||||
|
||||
notifications:
|
||||
email: team@swoole.com
|
||||
|
||||
env:
|
||||
global:
|
||||
- secure: "Mqg9ifSV0BLv2TIBw/x64aEuB8Y5aPXwXg7xAOq08oPlHBYSBgSYxroJL5PXs0fe7PszZF20bUBinwmEXYQzqgP/40Y1kmq/kfTsDXwTOc9qbAbA1pWvH+Sk1kDP5MLJRPWBCkqctyFd0I0u0SdzT1fgSqirqEz2bMnbAUVpSvo="
|
||||
|
||||
before_install:
|
||||
- echo -n | openssl s_client -connect scan.coverity.com:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | sudo tee -a /etc/ssl/certs/ca-
|
||||
|
||||
addons:
|
||||
coverity_scan:
|
||||
project:
|
||||
name: "swoole/swoole-src"
|
||||
description: "Build submitted via Travis CI"
|
||||
notification_email: mikan.tenny@gmail.com
|
||||
build_command_prepend: "./configure; make clean"
|
||||
build_command: "make -j 4"
|
||||
branch_pattern: coverity_scan
|
||||
|
||||
#Compile
|
||||
before_script:
|
||||
- ./travis/compile.sh
|
||||
|
||||
script:
|
||||
- exit 0
|
||||
|
||||
|
47
vendor/swoole/CMakeLists.txt
vendored
Executable file
47
vendor/swoole/CMakeLists.txt
vendored
Executable file
@ -0,0 +1,47 @@
|
||||
PROJECT(libswoole)
|
||||
|
||||
ENABLE_LANGUAGE(ASM)
|
||||
SET(SWOOLE_VERSION 4.0.0)
|
||||
SET(SWOOLE_CLFLAGS pthread rt dl ssl crypt crypto nghttp2)
|
||||
CMAKE_MINIMUM_REQUIRED(VERSION 2.4)
|
||||
|
||||
SET(CMAKE_BUILD_TYPE Debug)
|
||||
|
||||
file(GLOB_RECURSE SRC_LIST FOLLOW_SYMLINKS src/*.c src/*.cc thirdparty/boost/asm/jump_x86_64_sysv_elf_gas.S thirdparty/boost/asm/make_x86_64_sysv_elf_gas.S)
|
||||
file(GLOB_RECURSE HEAD_FILES FOLLOW_SYMLINKS include/*.h)
|
||||
|
||||
SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib)
|
||||
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
|
||||
|
||||
#message(STATUS "source=${SRC_LIST}")
|
||||
#message(STATUS "header=${HEAD_FILES}")
|
||||
|
||||
add_definitions(-DHAVE_CONFIG_H)
|
||||
|
||||
INCLUDE_DIRECTORIES(BEFORE ./include ./ /usr/local/php/include /usr/local/php/include/Zend /usr/local/php/include/main)
|
||||
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
|
||||
|
||||
#libary
|
||||
add_library(shared SHARED ${SRC_LIST})
|
||||
add_library(static STATIC ${SRC_LIST})
|
||||
|
||||
set_target_properties(shared PROPERTIES OUTPUT_NAME "swoole" VERSION ${SWOOLE_VERSION})
|
||||
set_target_properties(static PROPERTIES OUTPUT_NAME "swoole" VERSION ${SWOOLE_VERSION})
|
||||
|
||||
target_link_libraries(shared ${SWOOLE_CLFLAGS})
|
||||
target_link_libraries(static ${SWOOLE_CLFLAGS})
|
||||
|
||||
LINK_DIRECTORIES(${LIBRARY_OUTPUT_PATH})
|
||||
|
||||
#test_server
|
||||
set(TEST_SRC_LIST examples/test_server.c)
|
||||
add_executable(test_server ${TEST_SRC_LIST})
|
||||
add_dependencies(test_server static)
|
||||
target_link_libraries(test_server ${SWOOLE_CLFLAGS} swoole)
|
||||
|
||||
#install
|
||||
INSTALL(CODE "MESSAGE(\"Are you run command using root user?\")")
|
||||
INSTALL(TARGETS shared static LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
|
||||
INSTALL(FILES ${HEAD_FILES} DESTINATION include/swoole)
|
||||
INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/swoole_config.h DESTINATION include/swoole)
|
||||
INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/config.h DESTINATION include/swoole)
|
3
vendor/swoole/CREDITS
vendored
Executable file
3
vendor/swoole/CREDITS
vendored
Executable file
@ -0,0 +1,3 @@
|
||||
Tianfeng Han(mikan.tenny@gmail.com)
|
||||
Tencent Inc.
|
||||
QQ: 350749960
|
202
vendor/swoole/LICENSE
vendored
Executable file
202
vendor/swoole/LICENSE
vendored
Executable file
@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright Tianfeng.Han [mikan.tenny@gmail.com]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
573
vendor/swoole/README.md
vendored
Executable file
573
vendor/swoole/README.md
vendored
Executable file
@ -0,0 +1,573 @@
|
||||
Swoole
|
||||
======
|
||||
[](https://travis-ci.org/swoole/swoole-src)
|
||||
[](LICENSE)
|
||||
[](https://gitter.im/swoole/swoole-src?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://scan.coverity.com/projects/swoole-swoole-src)
|
||||
|
||||
Swoole is an event-driven asynchronous & concurrent networking communication framework with high performance written only in C for PHP.
|
||||
|
||||
* __Document__: <https://www.swoole.co.uk/docs/>
|
||||
* __API__: <https://rawgit.com/tchiotludo/swoole-ide-helper/english/docs/index.html>
|
||||
* __IDE Helper__: <https://github.com/swoole/ide-helper>
|
||||
* __中文文档__: <http://wiki.swoole.com/>
|
||||
* __Twitter__: <https://twitter.com/php_swoole>
|
||||
* __Slack Group__: <https://swoole.slack.com/>
|
||||
|
||||
Swoft
|
||||
----
|
||||
Modern High performance AOP and Coroutine PHP Framework, base on Swoole 2 <https://github.com/swoft-cloud>
|
||||
|
||||
EasySwoole
|
||||
----
|
||||
a simple high performance PHP framework base on Swoole which make you use Swoole like echo hello world。<https://www.easyswoole.com/>
|
||||
|
||||
SwooleDistributed
|
||||
-----------------
|
||||
The high performance co operative server framework base on swoole all version, supports microservice and cluster deployment, providing developers with many advanced development and debugging components. <https://github.com/SwooleDistributed/SwooleDistributed>
|
||||
|
||||
Event-based
|
||||
------
|
||||
|
||||
The network layer in Swoole is event-based and takes full advantage of the underlaying epoll/kqueue implementation, making it really easy to serve thousands of connections.
|
||||
|
||||
Coroutine
|
||||
----------------
|
||||
[Swoole 2.0](Version2.md) supports the built-in coroutine, and you can use fully synchronized code to implement asynchronous programs. PHP code without any additional keywords, the underlying automatic coroutine-scheduling.
|
||||
|
||||
```php
|
||||
<?php
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
Swoole\Coroutine::create(function() use ($i) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
$ret = $redis->incr('coroutine');
|
||||
$redis->close();
|
||||
if ($i == 50) {
|
||||
Swoole\Coroutine::create(function() use ($i) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
$ret = $redis->set('coroutine_i', 50);
|
||||
$redis->close();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
```php
|
||||
<?php
|
||||
$server = new Swoole\Http\Server('127.0.0.1', 9501);
|
||||
|
||||
$server->on('Request', function($request, $response) {
|
||||
|
||||
$tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$ret = $tcp_cli->connect('127.0.0.1', 9906);
|
||||
$tcp_cli ->send('test for the coroutine');
|
||||
$ret = $tcp_cli->recv(5);
|
||||
$tcp_cli->close();
|
||||
|
||||
if ($ret) {
|
||||
$response->end(' swoole response is ok');
|
||||
}
|
||||
else{
|
||||
$response->end(" recv failed error : {$client->errCode}");
|
||||
}
|
||||
});
|
||||
|
||||
$server->start();
|
||||
```
|
||||
|
||||
Short API Name
|
||||
-----
|
||||
#### start a new coroutine
|
||||
```php
|
||||
go(function () {
|
||||
co::sleep(0.5);
|
||||
echo "hello";
|
||||
});
|
||||
go("test");
|
||||
go([$object, "method"]);
|
||||
```
|
||||
|
||||
#### Channel
|
||||
```php
|
||||
$chan = new chan(128);
|
||||
$chan->push(1234);
|
||||
$chan->push(1234.56);
|
||||
$chan->push("hello world");
|
||||
$chan->push(["hello world"]);
|
||||
$chan->push(new stdclass);
|
||||
$chan->push(fopen("test.txt", "r+"));
|
||||
while($chan->pop());
|
||||
```
|
||||
|
||||
#### MySQL Client
|
||||
```php
|
||||
go(function () {
|
||||
$db = new Co\MySQL();
|
||||
$server = array(
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
);
|
||||
|
||||
$db->connect($server);
|
||||
|
||||
$result = $db->query('SELECT * FROM userinfo WHERE id = 3');
|
||||
var_dump($result);
|
||||
});
|
||||
```
|
||||
|
||||
#### Redis Client
|
||||
```php
|
||||
go(function () {
|
||||
$redis = new Co\Redis;
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
$ret = $redis->set('key', 'value');
|
||||
var_dump($redis->get('key'));
|
||||
});
|
||||
```
|
||||
|
||||
#### Http Client
|
||||
```php
|
||||
go(function () {
|
||||
$http = new Co\Http\Client("www.google.com", 443, true);
|
||||
$http->setHeaders(function () {
|
||||
|
||||
});
|
||||
$ret = $http->get('/');
|
||||
var_dump($http->body);
|
||||
});
|
||||
```
|
||||
|
||||
#### Http2 Client
|
||||
```php
|
||||
go(function () {
|
||||
$http = new Co\Http2\Client("www.google.com", 443, true);
|
||||
$req = new co\Http2\Request;
|
||||
$req->path = "/index.html";
|
||||
$req->headers = [
|
||||
'host' => "www.google.com",
|
||||
"user-agent" => 'Chrome/49.0.2587.3',
|
||||
'accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'accept-encoding' => 'gzip',
|
||||
];
|
||||
$req->cookies = ['name' => 'rango', 'email' => 'rango@swoole.com'];
|
||||
$ret = $http->send($req);
|
||||
var_dump($http->recv());
|
||||
});
|
||||
```
|
||||
|
||||
#### Other
|
||||
```php
|
||||
co::sleep(100);
|
||||
co::fread($fp);
|
||||
co::gethostbyname('www.google.com');
|
||||
```
|
||||
|
||||
|
||||
Concurrent
|
||||
------
|
||||
|
||||
On the request processing part, Swoole uses a multi-process model. Every process works as a worker. All business logic is executed in workers, synchronously.
|
||||
|
||||
With the synchronous logic execution, you can easily write large and robust applications and take advantage of almost all libraries available to the PHP community.
|
||||
|
||||
In-memory
|
||||
------
|
||||
|
||||
Unlike traditional apache/php-fpm stuff, the memory allocated in Swoole will not be freed after a request, which can improve performance a lot.
|
||||
|
||||
|
||||
## Why Swoole?
|
||||
|
||||
Traditional PHP applications almost always run behind Apache/Nginx, without much control of the request. This brings several limitations:
|
||||
|
||||
1. All memory will be freed after the request. All PHP code needs be re-compiled on every request. Even with opcache enabled, all opcode still needs to be re-executed.
|
||||
2. It is almost impossible to implement long connections and connection pooling techniques.
|
||||
3. Implementing asynchronous tasks requires third-party queue servers, such as rabbitmq and beanstalkd.
|
||||
4. Implementing realtime applications such as chatting servers requires third-party languages, such as nodejs, for example.
|
||||
|
||||
This is why Swoole appeared. Swoole extends the use cases of PHP, and brings all these possibilities to the PHP world.
|
||||
By using Swoole, you can build enhanced web applications with more control, real-time chatting servers, etc, more easily.
|
||||
|
||||
## Requirements
|
||||
|
||||
* Version-1: PHP 5.3.10 or later
|
||||
* Version-2: PHP 5.5.0 or later
|
||||
* Linux, OS X and basic Windows support (thanks to cygwin)
|
||||
* GCC 4.4 or later
|
||||
|
||||
## Installation
|
||||
|
||||
1. Install via pecl
|
||||
|
||||
```
|
||||
pecl install swoole
|
||||
```
|
||||
|
||||
2. Install from source
|
||||
|
||||
```
|
||||
sudo apt-get install php5-dev
|
||||
git clone https://github.com/swoole/swoole-src.git
|
||||
cd swoole-src
|
||||
phpize
|
||||
./configure
|
||||
make && make install
|
||||
```
|
||||
|
||||
## Introduction
|
||||
|
||||
Swoole includes components for different purposes: Server, Task Worker, Timer, Event and Async IO. With these components, Swoole allows you to build many features.
|
||||
|
||||
### Server
|
||||
|
||||
This is the most important part in Swoole. It provides the necessary infrastructure to build server applications.
|
||||
With Swoole server, you can build web servers, chat messaging servers, game servers and almost anything you want.
|
||||
|
||||
The following example shows a simple echo server.
|
||||
|
||||
```php
|
||||
// Create a server instance
|
||||
$serv = new swoole_server('127.0.0.1', 9501);
|
||||
|
||||
// Attach handler for connect event. Once the client has connected to the server, the registered handler will be
|
||||
// executed.
|
||||
$serv->on('connect', function ($serv, $fd) {
|
||||
echo "Client:Connect.\n";
|
||||
});
|
||||
|
||||
// Attach handler for receive event. Every piece of data will be received by server and the registered handler will be
|
||||
// executed. All custom protocol implementation should be located there.
|
||||
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
|
||||
$serv->send($fd, $data);
|
||||
});
|
||||
|
||||
$serv->on('close', function ($serv, $fd) {
|
||||
echo "Client: Close.\n";
|
||||
});
|
||||
|
||||
// Start our server, listen on the port and be ready to accept connections.
|
||||
$serv->start();
|
||||
```
|
||||
|
||||
Try to extend your server and implement what you want!
|
||||
|
||||
### HTTP Server
|
||||
|
||||
```php
|
||||
$http = new swoole_http_server('0.0.0.0', 9501);
|
||||
|
||||
$http->on('request', function ($request, $response) {
|
||||
$response->header('Content-Type', 'text/html; charset=utf-8');
|
||||
$response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
|
||||
});
|
||||
|
||||
$http->start();
|
||||
```
|
||||
|
||||
### WebSocket Server
|
||||
|
||||
```php
|
||||
$ws = new swoole_websocket_server('0.0.0.0', 9502);
|
||||
|
||||
$ws->on('open', function ($ws, $request) {
|
||||
var_dump($request->fd, $request->get, $request->server);
|
||||
$ws->push($request->fd, "hello, welcome\n");
|
||||
});
|
||||
|
||||
$ws->on('message', function ($ws, $frame) {
|
||||
echo "Message: {$frame->data}\n";
|
||||
$ws->push($frame->fd, "server: {$frame->data}");
|
||||
});
|
||||
|
||||
$ws->on('close', function ($ws, $fd) {
|
||||
echo "client-{$fd} is closed\n";
|
||||
});
|
||||
|
||||
$ws->start();
|
||||
```
|
||||
|
||||
### Real async-mysql client
|
||||
```php
|
||||
$db = new swoole_mysql('127.0.0.1', 'root', 'root', 'test');
|
||||
|
||||
$db->on('close', function($o) {
|
||||
echo "mysql connection is closed\n";
|
||||
});
|
||||
|
||||
$db->query('select now() as now_t', function($db, $result_rows) {
|
||||
var_dump($result_rows);
|
||||
$db->close();
|
||||
});
|
||||
```
|
||||
|
||||
### Real async-redis client
|
||||
```php
|
||||
$client = new swoole_redis;
|
||||
$client->connect('127.0.0.1', 6379, function (swoole_redis $client, $result) {
|
||||
echo "connect\n";
|
||||
var_dump($result);
|
||||
$client->set('key', 'swoole', function (swoole_redis $client, $result) {
|
||||
var_dump($result);
|
||||
$client->get('key', function (swoole_redis $client, $result) {
|
||||
var_dump($result);
|
||||
$client->close();
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
### Async http Client
|
||||
|
||||
```php
|
||||
$cli = new swoole_http_client('127.0.0.1', 80);
|
||||
|
||||
$cli->setHeaders(['User-Agent' => 'swoole']);
|
||||
$cli->post('/dump.php', array('test' => '9999999'), function (swoole_http_client $cli) {
|
||||
echo "#{$cli->sock}\tPOST response Length: " . strlen($cli->body) . "\n";
|
||||
$cli->get('/index.php', function (swoole_http_client $cli) {
|
||||
echo "#{$cli->sock}\tGET response Length: " . strlen($cli->body) . "\n";
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### Async WebSocket Client
|
||||
|
||||
```php
|
||||
$cli = new swoole_http_client('127.0.0.1', 9501);
|
||||
|
||||
$cli->on('message', function ($_cli, $frame) {
|
||||
var_dump($frame);
|
||||
});
|
||||
|
||||
$cli->upgrade('/', function ($cli) {
|
||||
echo $cli->body;
|
||||
$cli->push('Hello world');
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
### Multi-port and mixed protocol
|
||||
|
||||
```php
|
||||
$serv = new swoole_http_server('127.0.0.1', 9501, SWOOLE_BASE);
|
||||
|
||||
$port2 = $serv->listen('0.0.0.0', 9502, SWOOLE_SOCK_TCP);
|
||||
$port2->on('receive', function (swoole_server $serv, $fd, $from_id, $data) {
|
||||
var_dump($data);
|
||||
$serv->send($fd, $data);
|
||||
});
|
||||
|
||||
$serv->on('request', function($req, $resp) {
|
||||
$resp->end('<h1>Hello world</h1>');
|
||||
});
|
||||
|
||||
|
||||
$serv->start();
|
||||
```
|
||||
|
||||
### Task Worker
|
||||
|
||||
Swoole brings you two types of workers: server workers and task workers. Server workers are for request
|
||||
handling, as demonstrated above. Task workers are for task execution. With task workers, we can execute our
|
||||
task asynchronously without blocking the server workers.
|
||||
|
||||
Task workers are mainly used for time-consuming tasks, such as sending password recovery emails, and ensure
|
||||
the main request returns as soon as possible.
|
||||
|
||||
The following example shows a simple server with task support.
|
||||
|
||||
```php
|
||||
$serv = new swoole_server("127.0.0.1", 9502);
|
||||
|
||||
// Sets server configuration, we set task_worker_num config greater than 0 to enable task workers support
|
||||
$serv->set(array('task_worker_num' => 4));
|
||||
|
||||
// Attach handler for receive event, which has been explained above.
|
||||
$serv->on('receive', function($serv, $fd, $from_id, $data) {
|
||||
// We dispath a task to task workers by invoke the task() method of $serv
|
||||
// This method returns a task id as the identity of ths task
|
||||
$task_id = $serv->task($data);
|
||||
echo "Dispath AsyncTask: id=$task_id\n";
|
||||
});
|
||||
|
||||
// Attach handler for task event. The handler will be executed in task workers.
|
||||
$serv->on('task', function ($serv, $task_id, $from_id, $data) {
|
||||
// Handle the task and do what you want with $data
|
||||
echo "New AsyncTask[id=$task_id]" . PHP_EOL;
|
||||
|
||||
// After the task is handled, we return the results to the caller worker.
|
||||
$serv->finish("$data -> OK");
|
||||
});
|
||||
|
||||
// Attach handler for finish event. The handler will be executed in server workers. The same worker dispatched this task before.
|
||||
$serv->on('finish', function ($serv, $task_id, $data) {
|
||||
echo "AsyncTask[$task_id] Finish: $data" . PHP_EOL;
|
||||
});
|
||||
|
||||
$serv->start();
|
||||
```
|
||||
|
||||
Swoole also supports synchronous tasks. To use synchronous tasks, just simply replace
|
||||
`$serv->task($data)` with `$serv->taskwait($data)`. Unlike `task()`, `taskwait()` will wait for a task to
|
||||
complete before it returns its response.
|
||||
|
||||
### Timer
|
||||
|
||||
Swoole has built-in millisecond timer support. By using the timer, it is easy to get a block of code
|
||||
executed periodically (really useful for managing interval tasks).
|
||||
|
||||
To demonstrate how the timer works, here is a small example:
|
||||
|
||||
```php
|
||||
//interval 2000ms
|
||||
$serv->tick(2000, function ($timer_id) {
|
||||
echo "tick-2000ms\n";
|
||||
});
|
||||
|
||||
//after 3000ms
|
||||
$serv->after(3000, function () {
|
||||
echo "after 3000ms.\n"
|
||||
});
|
||||
```
|
||||
|
||||
In the example above, we first set the `timer` event handler to `swoole_server` to enable timer support.
|
||||
Then, we add two timers by calling `bool swoole_server::addtimer($interval)` once the server started.
|
||||
To handle multiple timers, we switch the `$interval` in registered handler and do what we want to do.
|
||||
|
||||
### Event
|
||||
|
||||
Swoole's I/O layer is event-based, which is very convenient to add your own file descriptor to Swoole's main eventloop.
|
||||
With event support, you can also build fully asynchronous applications with Swoole.
|
||||
|
||||
To use events in Swoole, we can use `swoole_event_set()` to register event handler to sepecified file descriptor,
|
||||
once registered descriptors become readable or writeable, our registered handler will be invoked. Also, we can
|
||||
using `bool swoole_event_del(int $fd);` to remove registered file descriptor from eventloop.
|
||||
|
||||
The following are prototypes for the related functions:
|
||||
|
||||
```php
|
||||
bool swoole_event_add($fd, mixed $read_callback, mixed $write_callback, int $flag);
|
||||
bool swoole_event_set($fd, mixed $read_callback, mixed $write_callback, int $flag);
|
||||
bool swoole_event_del($fd);
|
||||
```
|
||||
|
||||
The `$fd` parameter can be one of the following types:
|
||||
|
||||
* unix file descriptor
|
||||
* stream resource created by `stream_socket_client()/fsockopen()`
|
||||
* sockets resources created by `socket_create()` in sockets extension (require compile swoole with --enable-sockets support)
|
||||
|
||||
The `$read_callback` and `$write_callback` are callbacks for corresponding read/write event.
|
||||
|
||||
The `$flag` is a mask to indicate what type of events we should get notified, can be `SWOOLE_EVENT_READ`,
|
||||
`SWOOLE_EVENT_WRITE` or `SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE`
|
||||
|
||||
### Async IO
|
||||
|
||||
Swoole's Async IO provides the ability to read/write files and lookup dns records asynchronously. The following
|
||||
are signatures for these functions:
|
||||
|
||||
|
||||
```php
|
||||
bool swoole_async_readfile(string $filename, mixed $callback);
|
||||
bool swoole_async_writefile('test.log', $file_content, mixed $callback);
|
||||
bool swoole_async_read(string $filename, mixed $callback, int $trunk_size = 8192);
|
||||
bool swoole_async_write(string $filename, string $content, int $offset = -1, mixed $callback = NULL);
|
||||
void swoole_async_dns_lookup(string $domain, function($host, $ip){});
|
||||
bool swoole_timer_after($after_n_ms, mixed $callback);
|
||||
bool swoole_timer_tick($n_ms, mixed $callback);
|
||||
bool swoole_timer_clear($n_ms, mixed $callback);
|
||||
```
|
||||
|
||||
Refer to [API Reference](http://wiki.swoole.com/wiki/page/183.html) for more detailed information about these functions.
|
||||
|
||||
|
||||
### Client
|
||||
|
||||
Swoole also provides a client component to build tcp/udp clients in both asynchronous and synchronous ways.
|
||||
Swoole uses the `swoole_client` class to expose all its functionalities.
|
||||
|
||||
synchronous blocking:
|
||||
```php
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP);
|
||||
if (!$client->connect('127.0.0.1', 9501, 0.5)) {
|
||||
die('connect failed.');
|
||||
}
|
||||
|
||||
if (!$client->send('Hello world')) {
|
||||
die('send failed.');
|
||||
}
|
||||
|
||||
$data = $client->recv();
|
||||
if (!$data) {
|
||||
die('recv failed.');
|
||||
}
|
||||
|
||||
$client->close();
|
||||
|
||||
```
|
||||
|
||||
Asynchronous non-blocking:
|
||||
|
||||
```php
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
|
||||
|
||||
$client->on('connect', function($cli) {
|
||||
$cli->send("Hello world\n");
|
||||
});
|
||||
$client->on('receive', function($cli, $data) {
|
||||
echo "Received: ".$data."\n";
|
||||
});
|
||||
$client->on('error', function($cli) {
|
||||
echo "Connect failed\n";
|
||||
});
|
||||
$client->on('close', function($cli) {
|
||||
echo "Connection close\n";
|
||||
});
|
||||
|
||||
$client->connect('127.0.0.1', 9501, 0.5);
|
||||
```
|
||||
|
||||
The following methods are available in swoole_client:
|
||||
|
||||
```php
|
||||
swoole_client::__construct(int $sock_type, int $is_sync = SWOOLE_SOCK_SYNC, string $key);
|
||||
int swoole_client::on(string $event, mixed $callback);
|
||||
bool swoole_client::connect(string $host, int $port, float $timeout = 0.1, int $flag = 0)
|
||||
bool swoole_client::isConnected();
|
||||
int swoole_client::send(string $data);
|
||||
bool swoole_client::sendfile(string $filename)
|
||||
string swoole_client::recv(int $size = 65535, bool $waitall = 0);
|
||||
bool swoole_client::close();
|
||||
```
|
||||
|
||||
Refer to [API Reference](http://wiki.swoole.com/wiki/page/3.html) for more detailed information about these functions.
|
||||
|
||||
## API Reference
|
||||
|
||||
* [中文](http://wiki.swoole.com/)
|
||||
* [English](https://rawgit.com/tchiotludo/swoole-ide-helper/english/docs/index.html)
|
||||
|
||||
## Related Projects
|
||||
|
||||
* [SwooleFramework](https://github.com/swoole/framework) Web framework powered by Swoole
|
||||
|
||||
## Contribution
|
||||
|
||||
Your contribution to Swoole development is very welcome!
|
||||
|
||||
You may contribute in the following ways:
|
||||
|
||||
* [Repost issues and feedback](https://github.com/swoole/swoole-src/issues)
|
||||
* Submit fixes, features via Pull Request
|
||||
* Write/polish documentation
|
||||
|
||||
## License
|
||||
|
||||
Apache License Version 2.0 see http://www.apache.org/licenses/LICENSE-2.0.html
|
171
vendor/swoole/Version2.md
vendored
Executable file
171
vendor/swoole/Version2.md
vendored
Executable file
@ -0,0 +1,171 @@
|
||||
# swoole 2.0 released
|
||||
Now swoole 2.0 is shipped with original support for coroutine. Implemented by C, swoole is an event-driven, asynchronous and concurrent network engine for PHP. The new swoole 2.0 improves development efficiency extensively -- developers can easily achieve high network throughput by using coroutines in network I/O functions, instead of using trivial `generator`s or async callbacks.
|
||||
|
||||
Furthermore, swoole 2.0 enhances the compatibility for PHP 7 without breaking the API backward-compatibility for previous swoole versions. Thus, users can painlessly upgrade to swoole 2.0.
|
||||
|
||||
### Coroutine
|
||||
With swoole 2.0, developers are able to implement async network I/O without taking care of the low-level details, such as coroutine switch. We build coroutine support of swoole 2.0 directly upon the Zend APIs, which is transparent for developers. Swoole 2.0 is able to switch among all coroutines according to the I/O result and the upper layer does not need to use async callback, which avoids callback hell. Besides, confusing keywords for coroutine creation like yield are removed.
|
||||
|
||||
Now the coroutine component in swoole 2.0 has supported most common protocols including udp, tcp, http, redis and mysql. Private protocols can also be implemented based on udp/tcp.
|
||||
|
||||
In addition, developers can also set timeout for IO operations. A corresponding error code will be returned if timeout
|
||||
|
||||
Last but not least, developers can use coroutine in both PHP7 and PHP5(>=5.5). PHP7 is recommended because of its high performance.
|
||||
|
||||
### Demo
|
||||
To set up a Http server:
|
||||
|
||||
```php
|
||||
<?php
|
||||
$server = new Swoole\Http\Server('127.0.0.1', 9501);
|
||||
|
||||
/*
|
||||
swoole will initialize a coroutine for every Request event.
|
||||
*/
|
||||
$server->on('Request', function($request, $response) {
|
||||
|
||||
$tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
/*
|
||||
In the underlining implement of method connect, swoole will
|
||||
save the php context and suspend this coroutine.
|
||||
After tcp connection is established, swoole will set the
|
||||
return value and resume this cortoutine.
|
||||
*/
|
||||
$ret = $tcp_cli->connect('127.0.0.1', 9906);
|
||||
$tcp_cli ->send('test for the coro');
|
||||
/*
|
||||
method recv will do the coroutine switching like that of connection.
|
||||
swoole will resume this coroutine if server responses nothing after 5s
|
||||
and errCode will be set 110 in the example below
|
||||
*/
|
||||
$ret = $tcp_cli->recv(5);
|
||||
$tcp_cli->close();
|
||||
|
||||
if ($ret) {
|
||||
$response->end(" swoole response is ok");
|
||||
}
|
||||
else{
|
||||
$response->end(" recv failed error : {$client->errCode}");
|
||||
}
|
||||
});
|
||||
|
||||
$server->start();
|
||||
```
|
||||
|
||||
UDP server Demo
|
||||
|
||||
```php
|
||||
<?php
|
||||
$server = new Swoole\Http\Server('127.0.0.1', 9501);
|
||||
|
||||
$server->on('Request', function($request, $response) {
|
||||
|
||||
$tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$ret = $tcp_cli ->connect('127.0.0.1', 9906);
|
||||
$tcp_cli ->send('test for the coro');
|
||||
$ret = $tcp_cli ->recv(100);
|
||||
$tcp_cli->close();
|
||||
|
||||
if ($ret) {
|
||||
$response ->end(" swoole response is ok");
|
||||
}
|
||||
else{
|
||||
$response ->end(" recv failed error : {$client->errCode}");
|
||||
}
|
||||
});
|
||||
|
||||
$server->start();
|
||||
```
|
||||
|
||||
**Demos for different kinds of clients:**
|
||||
|
||||
1. udp/tcp clieint
|
||||
|
||||
```php
|
||||
$udp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
|
||||
|
||||
$ret = $udp_cli ->connect('127.0.0.1', 9906);
|
||||
$udp_cli ->send('test for the coro');
|
||||
|
||||
$ret = $udp_cli ->recv(100);
|
||||
$udp_cli->close();
|
||||
|
||||
if ($ret) {
|
||||
$response ->end(" swoole response is ok");
|
||||
}
|
||||
else{
|
||||
$response ->end(" recv failed error : {$client->errCode}");
|
||||
}
|
||||
```
|
||||
|
||||
2. http client
|
||||
|
||||
```php
|
||||
$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', 80);
|
||||
$cli->setHeaders([
|
||||
'Host' => "localhost",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
'Accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'Accept-Encoding' => 'gzip',
|
||||
]);
|
||||
$cli->set([ 'timeout' => 1]);
|
||||
$cli->get('/index.php');
|
||||
echo $cli->body;
|
||||
$cli->close();
|
||||
```
|
||||
3. redis client
|
||||
```php
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$val = $redis->get('key');
|
||||
```
|
||||
|
||||
4. mysql client
|
||||
```php
|
||||
$swoole_mysql = new Swoole\Coroutine\MySQL();
|
||||
$swoole_mysql->connect(['host' => '127.0.0.1', 'user' => 'user', 'password' => 'pass', 'database' => 'test']);
|
||||
$res = $swoole_mysql->query('select sleep(1)');
|
||||
```
|
||||
|
||||
## Build and Install
|
||||
* Recommended with PHP7
|
||||
* Download [swoole-2.0.5](https://github.com/swoole/swoole-src/releases/tag/v2.0.5)
|
||||
|
||||
```shell
|
||||
phpize
|
||||
./configure
|
||||
make -j 4
|
||||
sudo make install
|
||||
|
||||
```
|
||||
You should add "extension=swoole.so" to php.ini, execute the demo program.
|
||||
|
||||
## Safety warning
|
||||
|
||||
#### Do not use coroutines in these functions:
|
||||
|
||||
* `__get`
|
||||
* `__set`
|
||||
* `__call`
|
||||
* `__callStatic`
|
||||
* `__toString`
|
||||
* `__invoke`
|
||||
* `__destruct`
|
||||
* `call_user_func`
|
||||
* `call_user_func_array`
|
||||
* `ReflectionFunction::invoke`
|
||||
* `ReflectionFunction::invokeArgs`
|
||||
* `ReflectionMethod::invoke`
|
||||
* `ReflectionMethod::invokeArgs`
|
||||
* `array_walk`/`array_map`
|
||||
* `ob_*`
|
||||
|
||||
#### Please turn off these extensions:
|
||||
|
||||
* xdebug
|
||||
* phptrace
|
||||
* aop
|
||||
* molten
|
||||
* xhprof
|
||||
|
||||
|
4
vendor/swoole/benchmark/ab.sh
vendored
Executable file
4
vendor/swoole/benchmark/ab.sh
vendored
Executable file
@ -0,0 +1,4 @@
|
||||
ab -c 1000 -n 1000000 -k http://127.0.0.1:9501/
|
||||
ab -c 1000 -n 1000000 -k -p post.data -T 'application/x-www-form-urlencoded' http://127.0.0.1:9501/
|
||||
#post 24K
|
||||
ab -c 100 -n 100000 -k -p post.big.data -T 'application/x-www-form-urlencoded' http://127.0.0.1:9501/
|
253
vendor/swoole/benchmark/async.php
vendored
Executable file
253
vendor/swoole/benchmark/async.php
vendored
Executable file
@ -0,0 +1,253 @@
|
||||
<?php
|
||||
//关闭错误输出
|
||||
//error_reporting(0);
|
||||
$shortopts = "c:";
|
||||
$shortopts .= "n:";
|
||||
$shortopts .= "s:";
|
||||
$shortopts .= "f:";
|
||||
$shortopts .= "p::";
|
||||
|
||||
$opt = getopt($shortopts);
|
||||
//并发数量
|
||||
if (!isset($opt['c'])) {
|
||||
exit("require -c [process_num]. ep: -c 100\n");
|
||||
}
|
||||
if (!isset($opt['n'])) {
|
||||
exit("require -n [request_num]. ep: -n 10000\n");
|
||||
}
|
||||
if (!isset($opt['s'])) {
|
||||
exit("require -s [server_url]. ep: -s tcp://127.0.0.1:9999\n");
|
||||
}
|
||||
if (!isset($opt['f'])) {
|
||||
exit("require -f [test_function]. ep: -f websocket|http|tcp|udp\n");
|
||||
}
|
||||
|
||||
class BenchMark
|
||||
{
|
||||
protected $nConcurrency;
|
||||
protected $nRequest;
|
||||
protected $host;
|
||||
protected $port;
|
||||
protected $clients = array();
|
||||
|
||||
protected $nRecvBytes = 0;
|
||||
protected $nSendBytes = 0;
|
||||
|
||||
protected $requestCount = 0;
|
||||
protected $connectErrorCount = 0;
|
||||
|
||||
protected $connectTime = 0;
|
||||
|
||||
protected $startTime;
|
||||
protected $beginSendTime;
|
||||
protected $testMethod;
|
||||
|
||||
static $sentData = "hello world\n";
|
||||
|
||||
function __construct($opt)
|
||||
{
|
||||
$this->nConcurrency = $opt['c'];
|
||||
$this->nRequest = $opt['n'];
|
||||
$serv = parse_url($opt['s']);
|
||||
$this->host = $serv['host'];
|
||||
$this->port = $serv['port'];
|
||||
$this->testMethod = $opt['f'];
|
||||
if (!method_exists($this, $this->testMethod))
|
||||
{
|
||||
throw new RuntimeException("method [{$this->testMethod}] is not exists.");
|
||||
}
|
||||
}
|
||||
|
||||
protected function finish()
|
||||
{
|
||||
foreach($this->clients as $k => $cli)
|
||||
{
|
||||
/**
|
||||
* @var $cli swoole\client
|
||||
*/
|
||||
if ($cli->isConnected())
|
||||
{
|
||||
$cli->close();
|
||||
}
|
||||
unset($this->clients[$k]);
|
||||
}
|
||||
echo "============================================================\n";
|
||||
echo " Swoole Version ".SWOOLE_VERSION."\n";
|
||||
echo "============================================================\n";
|
||||
echo "{$this->requestCount}\tbenchmark tests is finished.\n";
|
||||
echo "SendBytes:\t".number_format($this->nSendBytes)."\n";
|
||||
echo "nReceBytes:\t".number_format($this->nRecvBytes)."\n";
|
||||
echo "concurrency:\t".$this->nConcurrency,"\n";
|
||||
echo "connect failed:\t" . $this->connectErrorCount, "\n";
|
||||
echo "request num:\t" . $this->nRequest, "\n";
|
||||
$costTime = $this->format(microtime(true) - $this->startTime);
|
||||
echo "total time:\t" . ($costTime) . "\n";
|
||||
if ($this->requestCount > 0)
|
||||
{
|
||||
echo "request per second:\t" . intval($this->requestCount / $costTime), "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "request per second:\t0\n";
|
||||
}
|
||||
echo "connection time:\t" . $this->format($this->connectTime) . "\n";
|
||||
}
|
||||
|
||||
function format($time)
|
||||
{
|
||||
return round($time, 4);
|
||||
}
|
||||
|
||||
function onReceive($cli, $data)
|
||||
{
|
||||
$this->nRecvBytes += strlen($data);
|
||||
/**
|
||||
* 请求已经发完了,关闭连接,等待所有连接结束
|
||||
*/
|
||||
if ($this->requestCount >= $this->nRequest)
|
||||
{
|
||||
$cli->close();
|
||||
unset($this->clients[$cli->sock]);
|
||||
if (count($this->clients) == 0)
|
||||
{
|
||||
$this->finish();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->send($cli);
|
||||
}
|
||||
}
|
||||
|
||||
function send($cli)
|
||||
{
|
||||
$data = self::$sentData;
|
||||
$cli->send($data);
|
||||
$this->nSendBytes += strlen($data);
|
||||
$this->requestCount++;
|
||||
}
|
||||
|
||||
function push($cli)
|
||||
{
|
||||
$data = self::$sentData;
|
||||
$cli->push($data);
|
||||
$this->nSendBytes += strlen($data);
|
||||
$this->requestCount++;
|
||||
}
|
||||
|
||||
function onClose($cli)
|
||||
{
|
||||
//echo "close\n";
|
||||
}
|
||||
|
||||
function onError($cli)
|
||||
{
|
||||
$this->connectErrorCount ++;
|
||||
if ($this->connectErrorCount >= $this->nConcurrency)
|
||||
{
|
||||
$this->finish();
|
||||
}
|
||||
}
|
||||
|
||||
function onConnect($cli)
|
||||
{
|
||||
$this->send($cli);
|
||||
}
|
||||
|
||||
function websocket()
|
||||
{
|
||||
$cli = new swoole\http\client($this->host, $this->port);
|
||||
$cli->set(array('websocket_mask' => true));
|
||||
$cli->on('Message', function($cli, $frame) {
|
||||
$this->nRecvBytes += strlen($frame->data);
|
||||
/**
|
||||
* 请求已经发完了,关闭连接,等待所有连接结束
|
||||
*/
|
||||
if ($this->requestCount >= $this->nRequest)
|
||||
{
|
||||
$cli->close();
|
||||
unset($this->clients[$cli->sock]);
|
||||
if (count($this->clients) == 0)
|
||||
{
|
||||
$this->finish();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->push($cli);
|
||||
}
|
||||
});
|
||||
$cli->upgrade('/', function ($cli) {
|
||||
$this->push($cli);
|
||||
});
|
||||
return $cli;
|
||||
}
|
||||
|
||||
function long_tcp()
|
||||
{
|
||||
$cli = new swoole\client(SWOOLE_TCP | SWOOLE_ASYNC);
|
||||
$cli->on('receive', [$this, 'onReceive']);
|
||||
$cli->on('close', [$this, 'onClose']);
|
||||
$cli->on('connect', [$this, 'onConnect']);
|
||||
$cli->on('error', [$this, 'onError']);
|
||||
$cli->connect($this->host, $this->port);
|
||||
return $cli;
|
||||
}
|
||||
|
||||
function eof()
|
||||
{
|
||||
$eof = "\r\n\r\n";
|
||||
$cli = new swoole\client(SWOOLE_TCP | SWOOLE_ASYNC);
|
||||
$cli->set(array('open_eof_check' => true, "package_eof" => $eof));
|
||||
$cli->on('receive', [$this, 'onReceive']);
|
||||
$cli->on('close', [$this, 'onClose']);
|
||||
$cli->on('connect', [$this, 'onConnect']);
|
||||
$cli->on('error', [$this, 'onError']);
|
||||
$cli->connect($this->host, $this->port);
|
||||
self::$sentData .= $eof;
|
||||
return $cli;
|
||||
}
|
||||
|
||||
function length()
|
||||
{
|
||||
$cli = new swoole\client(SWOOLE_TCP | SWOOLE_ASYNC);
|
||||
$cli->set(array(
|
||||
'open_length_check' => true,
|
||||
"package_length_type" => 'N',
|
||||
'package_body_offset' => 4,
|
||||
));
|
||||
$cli->on('receive', [$this, 'onReceive']);
|
||||
$cli->on('close', [$this, 'onClose']);
|
||||
$cli->on('connect', [$this, 'onConnect']);
|
||||
$cli->on('error', [$this, 'onError']);
|
||||
$cli->connect($this->host, $this->port);
|
||||
self::$sentData = pack('N', strlen(self::$sentData)) . self::$sentData;
|
||||
return $cli;
|
||||
}
|
||||
|
||||
function udp()
|
||||
{
|
||||
$cli = new swoole\client(SWOOLE_UDP | SWOOLE_ASYNC);
|
||||
$cli->on('receive', [$this, 'onReceive']);
|
||||
$cli->on('close', [$this, 'onClose']);
|
||||
$cli->on('connect', [$this, 'onConnect']);
|
||||
$cli->on('error', [$this, 'onError']);
|
||||
$cli->connect($this->host, $this->port);
|
||||
return $cli;
|
||||
}
|
||||
|
||||
function run()
|
||||
{
|
||||
$this->startTime = microtime(true);
|
||||
for ($i = 0; $i < $this->nConcurrency; $i++)
|
||||
{
|
||||
$cli = call_user_func([$this, $this->testMethod]);
|
||||
$this->clients[$cli->sock] = $cli;
|
||||
}
|
||||
$this->beginSendTime = microtime(true);
|
||||
$this->connectTime = $this->beginSendTime - $this->startTime;
|
||||
}
|
||||
}
|
||||
|
||||
$bench = new BenchMark($opt);
|
||||
$bench->run();
|
84
vendor/swoole/benchmark/coroutine.php
vendored
Executable file
84
vendor/swoole/benchmark/coroutine.php
vendored
Executable file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
public $redisPool = [];
|
||||
public $mysqlPool = [];
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new Swoole\Http\Server("0.0.0.0", 9501, SWOOLE_BASE);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
// $this->server->on('Connect', [$this, 'onConnect']);
|
||||
$this->server->on('Request', [$this, 'onRequest']);
|
||||
// $this->server->on('Close', [$this, 'onClose']);
|
||||
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
function log($msg)
|
||||
{
|
||||
echo $msg."\n";
|
||||
}
|
||||
|
||||
public function onConnect($serv, $fd, $reactorId)
|
||||
{
|
||||
// $this->log("onConnect: fd=$fd");
|
||||
// $redis = new Swoole\Coroutine\Redis();
|
||||
// $redis->connect('127.0.0.1', 6379);
|
||||
// $this->redisPool[$fd] = $redis;
|
||||
}
|
||||
|
||||
public function onClose($serv, $fd, $reactorId)
|
||||
{
|
||||
// $this->log("onClose: fd=$fd");
|
||||
// $redis = $this->redisPool[$fd];
|
||||
// $redis->close();
|
||||
// unset($this->redisPool[$fd]);
|
||||
}
|
||||
|
||||
public function onRequest($request, $response)
|
||||
{
|
||||
// $this->log("onRequest: fd=$request->fd");
|
||||
|
||||
if (empty($this->redisPool[$request->fd]))
|
||||
{
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$this->redisPool[$request->fd] = $redis;
|
||||
}
|
||||
else
|
||||
{
|
||||
$redis = $this->redisPool[$request->fd];
|
||||
}
|
||||
$ret = $redis->get('key');
|
||||
|
||||
if (empty($this->mysqlPool[$request->fd]))
|
||||
{
|
||||
$mysql = new Swoole\Coroutine\MySQL();
|
||||
$mysql->connect([
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 3306,
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
]);
|
||||
}
|
||||
else
|
||||
{
|
||||
$mysql = $this->mysqlPool[$request->fd];
|
||||
}
|
||||
|
||||
$ret2 = $mysql->query('show tables');
|
||||
$response->end('redis value=' . $ret.', mysql talbes='.var_export($ret2, true));
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
$server->run();
|
||||
|
||||
|
||||
|
33
vendor/swoole/benchmark/eof_server.php
vendored
Executable file
33
vendor/swoole/benchmark/eof_server.php
vendored
Executable file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
Swoole\Async::set(array('enable_reuse_port' => true));
|
||||
$serv = new swoole_server("0.0.0.0", 9502, SWOOLE_BASE);
|
||||
//$serv = new swoole_server("0.0.0.0", 9502);
|
||||
$serv->set(array(
|
||||
'worker_num' => 8,
|
||||
'open_eof_check' => true,
|
||||
'package_eof' => "\r\n\r\n",
|
||||
));
|
||||
$serv->on('workerstart', function ($server, $id)
|
||||
{
|
||||
global $argv;
|
||||
swoole_set_process_name("php {$argv[0]}: worker");
|
||||
});
|
||||
|
||||
$serv->on('connect', function (swoole_server $serv, $fd, $from_id)
|
||||
{
|
||||
//echo "connect\n";;
|
||||
});
|
||||
|
||||
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data)
|
||||
{
|
||||
$serv->send($fd, "Swoole: " . $data);
|
||||
//$serv->close($fd);
|
||||
});
|
||||
|
||||
$serv->on('close', function (swoole_server $serv, $fd, $from_id)
|
||||
{
|
||||
//var_dump($serv->connection_info($fd));
|
||||
//echo "onClose\n";
|
||||
});
|
||||
|
||||
$serv->start();
|
23
vendor/swoole/benchmark/http.go
vendored
Executable file
23
vendor/swoole/benchmark/http.go
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Last-Modified", "Thu, 18 Jun 2015 10:24:27 GMT")
|
||||
w.Header().Add("Accept-Ranges", "bytes")
|
||||
w.Header().Add("E-Tag", "55829c5b-17")
|
||||
w.Header().Add("Server", "golang-http-server")
|
||||
fmt.Fprint(w, "<h1>\nHello world!\n</h1>\n")
|
||||
})
|
||||
|
||||
log.Printf("Go http Server listen on :8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
8
vendor/swoole/benchmark/http.js
vendored
Executable file
8
vendor/swoole/benchmark/http.js
vendored
Executable file
@ -0,0 +1,8 @@
|
||||
var http = require('http');
|
||||
http.createServer(function (req, res) {
|
||||
res.writeHead(200, {
|
||||
'Server': "node.js"}
|
||||
);
|
||||
res.end("<h1>Hello World</h2>");
|
||||
}).listen(8080, '127.0.0.1');
|
||||
console.log('Server running at http://127.0.0.1:8080/');
|
15
vendor/swoole/benchmark/http.php
vendored
Executable file
15
vendor/swoole/benchmark/http.php
vendored
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
$http = new swoole_http_server("127.0.0.1", 9501, SWOOLE_BASE);
|
||||
|
||||
$http->set([
|
||||
'worker_num' => 4,
|
||||
]);
|
||||
|
||||
$http->on('request', function ($request, swoole_http_response $response) {
|
||||
$response->header('Last-Modified', 'Thu, 18 Jun 2015 10:24:27 GMT');
|
||||
$response->header('E-Tag', '55829c5b-17');
|
||||
$response->header('Accept-Ranges', 'bytes');
|
||||
$response->end("<h1>\nHello Swoole.\n</h1>");
|
||||
});
|
||||
|
||||
$http->start();
|
27
vendor/swoole/benchmark/http2.go
vendored
Executable file
27
vendor/swoole/benchmark/http2.go
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"github.com/valyala/fasthttp"
|
||||
"runtime"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
|
||||
|
||||
m := func(ctx *fasthttp.RequestCtx) {
|
||||
switch string(ctx.Path()) {
|
||||
case "/":
|
||||
ctx.Response.Header.Set("Last-Modified", "Thu, 18 Jun 2015 10:24:27 GMT")
|
||||
ctx.Response.Header.Set("Accept-Ranges", "bytes")
|
||||
ctx.Response.Header.Set("E-Tag", "55829c5b-17")
|
||||
ctx.Response.Header.Set("Server", "golang-http-server")
|
||||
fmt.Fprint(ctx, "<h1>\nHello world!\n</h1>\n")
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Go fatshttp Server listen on :8888")
|
||||
log.Fatal(fasthttp.ListenAndServe(":8888", m))
|
||||
}
|
34
vendor/swoole/benchmark/length_server.php
vendored
Executable file
34
vendor/swoole/benchmark/length_server.php
vendored
Executable file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
Swoole\Async::set(array('enable_reuse_port' => true));
|
||||
$serv = new swoole_server("0.0.0.0", 9502, SWOOLE_BASE);
|
||||
//$serv = new swoole_server("0.0.0.0", 9502);
|
||||
$serv->set(array(
|
||||
'worker_num' => 8,
|
||||
'open_length_check' => true,
|
||||
"package_length_type" => 'N',
|
||||
'package_body_offset' => 4,
|
||||
));
|
||||
$serv->on('workerstart', function ($server, $id)
|
||||
{
|
||||
global $argv;
|
||||
swoole_set_process_name("php {$argv[0]}: worker");
|
||||
});
|
||||
|
||||
$serv->on('connect', function (swoole_server $serv, $fd, $from_id)
|
||||
{
|
||||
//echo "connect\n";;
|
||||
});
|
||||
|
||||
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data)
|
||||
{
|
||||
$serv->send($fd, $data);
|
||||
//$serv->close($fd);
|
||||
});
|
||||
|
||||
$serv->on('close', function (swoole_server $serv, $fd, $from_id)
|
||||
{
|
||||
//var_dump($serv->connection_info($fd));
|
||||
//echo "onClose\n";
|
||||
});
|
||||
|
||||
$serv->start();
|
1
vendor/swoole/benchmark/post.big.data
vendored
Executable file
1
vendor/swoole/benchmark/post.big.data
vendored
Executable file
File diff suppressed because one or more lines are too long
1
vendor/swoole/benchmark/post.data
vendored
Executable file
1
vendor/swoole/benchmark/post.data
vendored
Executable file
@ -0,0 +1 @@
|
||||
test1=hello&test2=world&myname=rango&go=start&language=php
|
37
vendor/swoole/benchmark/redis.go
vendored
Executable file
37
vendor/swoole/benchmark/redis.go
vendored
Executable file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"github.com/hoisie/redis"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var (
|
||||
client *redis.Client
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
client = &redis.Client{
|
||||
Addr: "127.0.0.1:6379",
|
||||
Db: 0,
|
||||
MaxPoolSize: 10000,
|
||||
}
|
||||
|
||||
// 限制为CPU的数量减一
|
||||
runtime.GOMAXPROCS( runtime.NumCPU() - 1 )
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
result, err := client.Get("hello")
|
||||
if err != nil {
|
||||
fmt.Fprint(w, err.Error())
|
||||
println(err.Error())
|
||||
} else {
|
||||
fmt.Fprint(w, "<h1>Hello world!. result="+ string(result)+"</h1>")
|
||||
}
|
||||
})
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
574
vendor/swoole/benchmark/run.php
vendored
Executable file
574
vendor/swoole/benchmark/run.php
vendored
Executable file
@ -0,0 +1,574 @@
|
||||
<?php
|
||||
require __DIR__.'/../examples/websocket/WebSocketClient.php';
|
||||
|
||||
//关闭错误输出
|
||||
//error_reporting(0);
|
||||
$shortopts = "c:";
|
||||
$shortopts .= "n:";
|
||||
$shortopts .= "s:";
|
||||
$shortopts .= "f:";
|
||||
$shortopts .= "p::";
|
||||
|
||||
$opt = getopt($shortopts);
|
||||
//并发数量
|
||||
if(!isset($opt['c'])) exit("require -c [process_num]. ep: -c 100\n");
|
||||
if(!isset($opt['n'])) exit("require -n [request_num]. ep: -n 10000\n");
|
||||
if(!isset($opt['s'])) exit("require -s [server_url]. ep: -s tcp://127.0.0.1:9999\n");
|
||||
if(!isset($opt['f'])) exit("require -f [test_function]. ep: -f short_tcp\n");
|
||||
|
||||
$bc = new Swoole_Benchmark(trim($opt['f']));
|
||||
$bc->process_num = (int)$opt['c'];
|
||||
$bc->request_num = (int)$opt['n'];
|
||||
$bc->server_url = trim($opt['s']);
|
||||
$bc->server_config = parse_url($bc->server_url);
|
||||
$bc->send_data = "GET / HTTP/1.1\r\n";
|
||||
$bc->send_data .= "Host: www.baidu.com\r\n";
|
||||
$bc->send_data .= "Connection: keep-alive\r\n";
|
||||
$bc->send_data .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n";
|
||||
$bc->send_data .= "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36\r\n\r\n";
|
||||
|
||||
$bc->read_len = 65536;
|
||||
if(!empty($opt['p'])) $bc->show_detail = true;
|
||||
|
||||
function eof(Swoole_Benchmark $bc)
|
||||
{
|
||||
static $client = null;
|
||||
static $i;
|
||||
$start = microtime(true);
|
||||
if (empty($client)) {
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
|
||||
$client->set(array('open_eof_check' => true, "package_eof" => "\r\n\r\n"));
|
||||
$end = microtime(true);
|
||||
$conn_use = $end - $start;
|
||||
$bc->max_conn_time = $conn_use;
|
||||
$i = 0;
|
||||
//echo "connect {$bc->server_url} \n";
|
||||
if (!$client->connect($bc->server_config['host'], $bc->server_config['port'], 2)) {
|
||||
error:
|
||||
echo "Error: " . swoole_strerror($client->errCode) . "[{$client->errCode}]\n";
|
||||
$client = null;
|
||||
return false;
|
||||
}
|
||||
$start = $end;
|
||||
}
|
||||
/*--------写入Sokcet-------*/
|
||||
$data = str_repeat('A', rand(100, 200))."\r\n\r\n";
|
||||
if (!$client->send($data))
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
$end = microtime(true);
|
||||
$write_use = $end - $start;
|
||||
if ($write_use > $bc->max_write_time) $bc->max_write_time = $write_use;
|
||||
$start = $end;
|
||||
/*--------读取Sokcet-------*/
|
||||
$i ++;
|
||||
$ret = $client->recv();
|
||||
if (empty($ret))
|
||||
{
|
||||
echo $bc->pid, "#$i", " is lost\n";
|
||||
return false;
|
||||
}
|
||||
elseif(strlen($ret) != strlen($data))
|
||||
{
|
||||
echo "#$i\tlength error\n";
|
||||
var_dump($ret);
|
||||
echo "-----------------------------------\n";
|
||||
var_dump($data);
|
||||
}
|
||||
$end = microtime(true);
|
||||
$read_use = $end - $start;
|
||||
if ($read_use > $bc->max_read_time) $bc->max_read_time = $read_use;
|
||||
return true;
|
||||
}
|
||||
|
||||
function long_tcp(Swoole_Benchmark $bc)
|
||||
{
|
||||
static $fp = null;
|
||||
static $i;
|
||||
$start = microtime(true);
|
||||
if(empty($fp))
|
||||
{
|
||||
$fp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
|
||||
$end = microtime(true);
|
||||
$conn_use = $end-$start;
|
||||
$bc->max_conn_time = $conn_use;
|
||||
$i = 0;
|
||||
//echo "connect {$bc->server_url} \n";
|
||||
if (!$fp->connect($bc->server_config['host'], $bc->server_config['port'], 2))
|
||||
{
|
||||
error:
|
||||
echo "Error: ".swoole_strerror($fp->errCode)."[{$fp->errCode}]\n";
|
||||
$fp = null;
|
||||
return false;
|
||||
}
|
||||
$start = $end;
|
||||
}
|
||||
/*--------写入Sokcet-------*/
|
||||
if (!$fp->send($bc->send_data))
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
$end = microtime(true);
|
||||
$write_use = $end - $start;
|
||||
if ($write_use > $bc->max_write_time)
|
||||
{
|
||||
$bc->max_write_time = $write_use;
|
||||
}
|
||||
$start = $end;
|
||||
/*--------读取Sokcet-------*/
|
||||
while(true)
|
||||
{
|
||||
$ret = $fp->recv(65530);
|
||||
if (empty($ret) or substr($ret, -1, 1) == "\n")
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
//var_dump($ret);
|
||||
$i++;
|
||||
if (empty($ret))
|
||||
{
|
||||
echo $bc->pid, "#$i@", " is lost\n";
|
||||
return false;
|
||||
}
|
||||
$end = microtime(true);
|
||||
$read_use = $end - $start;
|
||||
if ($read_use > $bc->max_read_time)
|
||||
{
|
||||
$bc->max_read_time = $read_use;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function websocket(Swoole_Benchmark $bc)
|
||||
{
|
||||
static $client = null;
|
||||
static $i;
|
||||
$start = microtime(true);
|
||||
|
||||
if (empty($client))
|
||||
{
|
||||
$client = new WebSocketClient($bc->server_config['host'], $bc->server_config['port']);
|
||||
if (!$client->connect())
|
||||
{
|
||||
echo "connect failed\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
$end = microtime(true);
|
||||
$conn_use = $end - $start;
|
||||
$bc->max_conn_time = $conn_use;
|
||||
$i = 0;
|
||||
$start = $end;
|
||||
}
|
||||
/*--------写入Sokcet-------*/
|
||||
if (!$client->send($bc->send_data))
|
||||
{
|
||||
echo "send failed\n";
|
||||
return false;
|
||||
}
|
||||
$end = microtime(true);
|
||||
$write_use = $end - $start;
|
||||
if ($write_use > $bc->max_write_time)
|
||||
{
|
||||
$bc->max_write_time = $write_use;
|
||||
}
|
||||
$start = $end;
|
||||
/*--------读取Sokcet-------*/
|
||||
$ret = $client->recv();
|
||||
//var_dump($ret);
|
||||
$i++;
|
||||
if (empty($ret))
|
||||
{
|
||||
echo $bc->pid, "#$i@", " is lost\n";
|
||||
return false;
|
||||
}
|
||||
$end = microtime(true);
|
||||
$read_use = $end - $start;
|
||||
if ($read_use > $bc->max_read_time)
|
||||
{
|
||||
$bc->max_read_time = $read_use;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉计时信息的UDP
|
||||
* @param $bc
|
||||
* @return bool
|
||||
*/
|
||||
function udp(Swoole_Benchmark $bc)
|
||||
{
|
||||
static $fp;
|
||||
if (empty($fp))
|
||||
{
|
||||
$fp = stream_socket_client($bc->server_url, $errno, $errstr, 1);
|
||||
if (!$fp)
|
||||
{
|
||||
echo "{$errstr}[{$errno}]\n";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/*--------写入Sokcet-------*/
|
||||
fwrite($fp, $bc->send_data);
|
||||
/*--------读取Sokcet-------*/
|
||||
$ret = fread($fp, $bc->read_len);
|
||||
if (empty($ret))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function udp2(Swoole_Benchmark $bc)
|
||||
{
|
||||
static $fp;
|
||||
$start = microtime(true);
|
||||
if (empty($fp))
|
||||
{
|
||||
$u = parse_url($bc->server_url);
|
||||
$fp = new swoole_client(SWOOLE_SOCK_UDP);
|
||||
$fp->connect($u['host'], $u['port'], 0.5, 0);
|
||||
$end = microtime(true);
|
||||
$conn_use = $end - $start;
|
||||
$bc->max_conn_time = $conn_use;
|
||||
$start = $end;
|
||||
}
|
||||
/*--------写入Sokcet-------*/
|
||||
$fp->send($bc->send_data);
|
||||
$end = microtime(true);
|
||||
$write_use = $end - $start;
|
||||
if ($write_use > $bc->max_write_time)
|
||||
{
|
||||
$bc->max_write_time = $write_use;
|
||||
}
|
||||
$start = $end;
|
||||
/*--------读取Sokcet-------*/
|
||||
$ret = $fp->recv();
|
||||
if (empty($ret))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$end = microtime(true);
|
||||
$read_use = $end - $start;
|
||||
if ($read_use > $bc->max_read_time)
|
||||
{
|
||||
$bc->max_read_time = $read_use;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function short_tcp($bc)
|
||||
{
|
||||
$fp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
|
||||
if(!$fp->connect($bc->server_config['host'], $bc->server_config['port'], 1))
|
||||
{
|
||||
error:
|
||||
echo "Error: ".socket_strerror($fp->errCode)."[{$fp->errCode}]\n";
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!$fp->send($bc->send_data))
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
$ret = $fp->recv();
|
||||
$fp->close();
|
||||
if(!empty($ret)) return true;
|
||||
else return false;
|
||||
}
|
||||
}
|
||||
|
||||
function long_socks5($bc)
|
||||
{
|
||||
static $fp = null;
|
||||
static $i;
|
||||
$start = microtime(true);
|
||||
if(empty($fp))
|
||||
{
|
||||
$fp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC,5);
|
||||
$end = microtime(true);
|
||||
$conn_use = $end-$start;
|
||||
$bc->max_conn_time = $conn_use;
|
||||
$i = 0;
|
||||
//echo "connect {$bc->server_url} \n";
|
||||
if (!$fp->connect($bc->server_config['host'], $bc->server_config['port'], 2))
|
||||
{
|
||||
error:
|
||||
echo "Error: ".swoole_strerror($fp->errCode)."[{$fp->errCode}]\n";
|
||||
$fp = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
$fp->send(pack("C3", 0x05, 0x01, 0x00));//greet
|
||||
$data = $fp->recv();
|
||||
$response = unpack("Cversion/Cmethod", $data);
|
||||
if ($response['version'] != 0x05)
|
||||
{
|
||||
exit('SOCKS version is not supported.');
|
||||
}
|
||||
$headers = getHeader($bc->send_data);
|
||||
if (empty($headers['port'])) {
|
||||
$headers['port'] = 80;
|
||||
}
|
||||
$g = pack("C5", 0x05, 0x01, 0x00, 0x03, strlen($headers['host'])) . $headers['host'] . pack("n", $headers['port']);
|
||||
$fp->send($g);
|
||||
$data = $fp->recv();
|
||||
$response = unpack("Cversion/Cresult/Creg/Ctype/Lip/Sport", $data);
|
||||
if ($response['result'] != 0x00)
|
||||
{
|
||||
echo 'SOCKS connection request failed: ' . getSocksRefusalMsg($response['result']), $response['result'];exit;
|
||||
}
|
||||
|
||||
$start = $end;
|
||||
}
|
||||
/*--------写入Sokcet-------*/
|
||||
if (!$fp->send($bc->send_data))
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
$end = microtime(true);
|
||||
$write_use = $end - $start;
|
||||
if ($write_use > $bc->max_write_time)
|
||||
{
|
||||
$bc->max_write_time = $write_use;
|
||||
}
|
||||
$start = $end;
|
||||
/*--------读取Sokcet-------*/
|
||||
while(true)
|
||||
{
|
||||
$ret = $fp->recv(65530);
|
||||
if (empty($ret) or substr($ret, -1, 1) == "\n")
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
//var_dump($ret);
|
||||
$i++;
|
||||
if (empty($ret))
|
||||
{
|
||||
echo $bc->pid, "#$i@", " is lost\n";
|
||||
return false;
|
||||
}
|
||||
$end = microtime(true);
|
||||
$read_use = $end - $start;
|
||||
if ($read_use > $bc->max_read_time)
|
||||
{
|
||||
$bc->max_read_time = $read_use;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function getHeader($message)
|
||||
{
|
||||
// 标准每行应该以"\r\n"行终止,这里兼容以"\n"作为行终止的情况,所以按"\n"分割行
|
||||
$lines = explode("\n", $message);
|
||||
foreach ($lines as &$line)
|
||||
{
|
||||
// 按"\n"分割行以后,某些行末可能存在"\r"字符,这里将其过滤掉
|
||||
$line = rtrim($line, "\r");
|
||||
}
|
||||
unset($line);
|
||||
|
||||
if (count($lines) <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
$headers = [];
|
||||
|
||||
foreach ($lines as $line)
|
||||
{
|
||||
$pos = strpos($line, ':');
|
||||
// 非标准首部,抛弃
|
||||
if ($pos === false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
$field = trim(substr($line, 0, $pos));
|
||||
$value = trim(substr($line, $pos + 1));
|
||||
|
||||
// 如果有host头部,重新设置host和port
|
||||
if (strtolower($field) === 'host')
|
||||
{
|
||||
$segments = explode(':', $value);
|
||||
$host = $segments[0];
|
||||
$headers['host'] = $host;
|
||||
if (isset($segments[1]))
|
||||
{
|
||||
$port = intval($segments[1]);
|
||||
$headers['port'] = $port;
|
||||
}
|
||||
}
|
||||
$headers[$field] = $value;
|
||||
}
|
||||
return $headers;
|
||||
}
|
||||
//请求数量最好是进程数的倍数
|
||||
$bc->process_req_num = intval($bc->request_num/$bc->process_num);
|
||||
$bc->run();
|
||||
$bc->report();
|
||||
$bc->end();
|
||||
|
||||
class Swoole_Benchmark
|
||||
{
|
||||
public $test_func;
|
||||
public $process_num;
|
||||
public $request_num;
|
||||
public $server_url;
|
||||
public $server_config;
|
||||
public $send_data;
|
||||
public $read_len;
|
||||
|
||||
public $time_end;
|
||||
private $shm_key;
|
||||
public $main_pid;
|
||||
public $child_pid = array();
|
||||
|
||||
public $show_detail = false;
|
||||
public $max_write_time = 0;
|
||||
public $max_read_time = 0;
|
||||
public $max_conn_time = 0;
|
||||
|
||||
public $pid;
|
||||
|
||||
protected $tmp_dir = '/tmp/swoole_bench/';
|
||||
|
||||
function __construct($func)
|
||||
{
|
||||
if (!function_exists($func))
|
||||
{
|
||||
exit(__CLASS__ . ": function[$func] not exists\n");
|
||||
}
|
||||
if (!is_dir($this->tmp_dir))
|
||||
{
|
||||
mkdir($this->tmp_dir);
|
||||
}
|
||||
$this->test_func = $func;
|
||||
}
|
||||
function end()
|
||||
{
|
||||
unlink($this->shm_key);
|
||||
foreach($this->child_pid as $pid)
|
||||
{
|
||||
$f = $this->tmp_dir . 'lost_' . $pid . '.log';
|
||||
if (is_file($f)) unlink($f);
|
||||
}
|
||||
}
|
||||
|
||||
function run()
|
||||
{
|
||||
$this->main_pid = posix_getpid();
|
||||
$this->shm_key = $this->tmp_dir.'t.log';
|
||||
for ($i = 0; $i < $this->process_num; $i++)
|
||||
{
|
||||
$this->child_pid[] = $this->start(array($this, 'worker'));
|
||||
}
|
||||
for ($i = 0; $i < $this->process_num; $i++)
|
||||
{
|
||||
$status = 0;
|
||||
$pid = pcntl_wait($status);
|
||||
}
|
||||
$this->time_end = microtime(true);
|
||||
}
|
||||
|
||||
function init_signal()
|
||||
{
|
||||
pcntl_signal(SIGUSR1,array($this, "sig_handle"));
|
||||
}
|
||||
|
||||
function sig_handle($sig)
|
||||
{
|
||||
switch ($sig)
|
||||
{
|
||||
case SIGUSR1:
|
||||
return;
|
||||
}
|
||||
$this->init_signal();
|
||||
}
|
||||
|
||||
function start($func)
|
||||
{
|
||||
$pid = pcntl_fork();
|
||||
if($pid>0)
|
||||
{
|
||||
return $pid;
|
||||
}
|
||||
elseif($pid==0)
|
||||
{
|
||||
$this->worker();
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "Error:fork fail\n";
|
||||
}
|
||||
}
|
||||
function worker()
|
||||
{
|
||||
$lost = 0;
|
||||
if(!file_exists($this->shm_key))
|
||||
{
|
||||
file_put_contents($this->shm_key,microtime(true));
|
||||
}
|
||||
if($this->show_detail) $start = microtime(true);
|
||||
$this->pid = posix_getpid();
|
||||
|
||||
for ($i = 0; $i < $this->process_req_num; $i++)
|
||||
{
|
||||
$func = $this->test_func;
|
||||
if(!$func($this)) $lost++;
|
||||
}
|
||||
if ($this->show_detail)
|
||||
{
|
||||
$log = $this->pid . "#\ttotal_use(s):" . substr(microtime(true) - $start, 0, 5);
|
||||
$log .= "\tconnect(ms):" . substr($this->max_conn_time * 1000, 0, 5);
|
||||
$log .= "\twrite(ms):" . substr($this->max_write_time * 1000, 0, 5);
|
||||
$log .= "\tread(ms):" . substr($this->max_read_time * 1000, 0, 5);
|
||||
file_put_contents($this->tmp_dir.'lost_' . $this->pid . '.log', $lost . "\n" . $log);
|
||||
}
|
||||
else
|
||||
{
|
||||
file_put_contents($this->tmp_dir.'lost_'.$this->pid.'.log', $lost);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
function report()
|
||||
{
|
||||
$time_start = file_get_contents($this->shm_key);
|
||||
$usetime = $this->time_end - $time_start;
|
||||
$lost = 0;
|
||||
|
||||
foreach ($this->child_pid as $f)
|
||||
{
|
||||
$file = $this->tmp_dir.'lost_'.$f.'.log';
|
||||
if (is_file($file))
|
||||
{
|
||||
$_lost = file_get_contents($file);
|
||||
$log = explode("\n",$_lost,2);
|
||||
}
|
||||
if (!empty($log))
|
||||
{
|
||||
$lost += intval($log[0]);
|
||||
if ($this->show_detail) echo $log[1], "\n";
|
||||
}
|
||||
}
|
||||
//并发量
|
||||
echo "concurrency:\t".$this->process_num,"\n";
|
||||
//请求量
|
||||
echo "request num:\t".$this->request_num,"\n";
|
||||
//请求量
|
||||
echo "lost num:\t".$lost,"\n";
|
||||
//请求量
|
||||
echo "success num:\t".($this->request_num-$lost),"\n";
|
||||
//总时间
|
||||
echo "total time:\t".substr($usetime,0,5),"\n";
|
||||
//每秒处理能力
|
||||
echo "req per second:\t".intval($this->request_num/$usetime),"\n";
|
||||
//每次请求平均时间ms
|
||||
echo "one req use(ms):\t".substr($usetime/$this->request_num*1000,0,5),"\n";
|
||||
}
|
||||
}
|
||||
|
483
vendor/swoole/benchmark/seria_bench.php
vendored
Executable file
483
vendor/swoole/benchmark/seria_bench.php
vendored
Executable file
@ -0,0 +1,483 @@
|
||||
<?php
|
||||
$target = array (
|
||||
'battle_id'=> 257
|
||||
,'user_id'=> 41248
|
||||
,'user_id2'=> 23989
|
||||
,'player'=> 41248
|
||||
,'formation'=> Array ('41248'=> 1 ,'23989'=> 2,'ssss'=>3)
|
||||
,'result'=> 1
|
||||
,'battle_type'=> 1
|
||||
,'speed'=> Array( '41248'=> 0,'23989'=> 0 )
|
||||
,'attacker'=> Array(
|
||||
'1'=> Array (
|
||||
'user_id'=> 41248
|
||||
,'soldier_id'=> 28
|
||||
,'prototype_id'=> 4
|
||||
,'bid'=> 1
|
||||
,'level'=> 1
|
||||
,'rare'=> 1
|
||||
,'skill_id'=> 1
|
||||
,'totalhp'=> 3997
|
||||
,'hp'=> 3997
|
||||
,'attack_general'=> 346
|
||||
,'attack_skill'=> 596
|
||||
,'attack_explode'=> 458
|
||||
,'attack_type'=> 1
|
||||
,'defense'=> 0
|
||||
,'anger'=> 50
|
||||
,'dodge'=> 2
|
||||
,'crit'=> 2
|
||||
,'block'=> 2
|
||||
,'block_effect'=> 0.5
|
||||
,'crit_effect'=> 2
|
||||
,'foramtion_effect'=> 0)
|
||||
,'4'=> Array (
|
||||
'user_id'=> 41248
|
||||
,'soldier_id'=> 29
|
||||
,'prototype_id'=> 2
|
||||
,'bid'=> 1
|
||||
,'level'=> 1
|
||||
,'rare'=> 1
|
||||
,'skill_id'=> 1
|
||||
,'totalhp'=> 3555
|
||||
,'hp'=> 3555
|
||||
,'attack_general'=> 396
|
||||
,'attack_skill'=> 581
|
||||
,'attack_explode'=> 418
|
||||
,'attack_type'=> 1
|
||||
,'defense'=> 0
|
||||
,'anger'=> 50
|
||||
,'dodge'=> 2
|
||||
,'crit'=> 2
|
||||
,'block'=> 0
|
||||
,'block_effect'=> 0.5
|
||||
,'crit_effect'=> 2
|
||||
,'foramtion_effect'=> 0
|
||||
)
|
||||
,'5'=> Array (
|
||||
'user_id'=> 41248
|
||||
,'soldier_id'=> 30
|
||||
,'prototype_id'=> 6
|
||||
,'bid'=> 1
|
||||
,'level'=> 1
|
||||
,'rare'=> 1
|
||||
,'skill_id'=> 1
|
||||
,'totalhp'=> 3043
|
||||
,'hp'=> 3043
|
||||
,'attack_general'=> 351
|
||||
,'attack_skill'=> 540
|
||||
,'attack_explode'=> 474
|
||||
,'attack_type'=> 1
|
||||
,'defense'=> 0
|
||||
,'anger'=> 50
|
||||
,'dodge'=> 2
|
||||
,'crit'=> 2
|
||||
,'block'=> 0
|
||||
,'block_effect'=> 0.5
|
||||
,'crit_effect'=> 2
|
||||
,'foramtion_effect'=> 0)
|
||||
,'7'=> Array (
|
||||
'user_id'=> 41248
|
||||
,'soldier_id'=> 37
|
||||
,'prototype_id'=> 2
|
||||
,'bid'=> 1
|
||||
,'level'=> 1
|
||||
,'rare'=> 1
|
||||
,'skill_id'=> 1
|
||||
,'totalhp'=> 3491
|
||||
,'hp'=> 3491
|
||||
,'attack_general'=> 393
|
||||
,'attack_skill'=> 532
|
||||
,'attack_explode'=> 456
|
||||
,'attack_type'=> 1
|
||||
,'defense'=> 0
|
||||
,'anger'=> 50
|
||||
,'dodge'=> 2
|
||||
,'crit'=> 2
|
||||
,'block'=> 0
|
||||
,'block_effect'=> 0.5
|
||||
,'crit_effect'=> 2
|
||||
,'foramtion_effect'=> 0 ))
|
||||
,'defender'=> Array(
|
||||
'2'=> Array(
|
||||
'user_id'=> 23989
|
||||
,'soldier_id'=> 24
|
||||
,'prototype_id'=> 1
|
||||
,'bid'=> 1
|
||||
,'level'=> 1
|
||||
,'rare'=> 1
|
||||
,'skill_id'=> 1
|
||||
,'totalhp'=> 3230
|
||||
,'hp'=> 3230
|
||||
,'attack_general'=> 390
|
||||
,'attack_skill'=> 567
|
||||
,'attack_explode'=> 442
|
||||
,'attack_type'=> 1
|
||||
,'defense'=> 0
|
||||
,'anger'=> 50
|
||||
,'dodge'=> 2
|
||||
,'crit'=> 2
|
||||
,'block'=> 0
|
||||
,'block_effect'=> 0.5
|
||||
,'crit_effect'=> 2
|
||||
,'foramtion_effect'=> 0)
|
||||
,'5'=> Array(
|
||||
'user_id'=> 23989
|
||||
,'soldier_id'=> 25
|
||||
,'prototype_id'=> 2
|
||||
,'bid'=> 1
|
||||
,'level'=> 1
|
||||
,'rare'=> 1
|
||||
,'skill_id'=> 1
|
||||
,'totalhp'=> 3400
|
||||
,'hp'=> 3400
|
||||
,'attack_general'=> 379
|
||||
,'attack_skill'=> 536
|
||||
,'attack_explode'=> 405
|
||||
,'attack_type'=> 1
|
||||
,'defense'=> 0
|
||||
,'anger'=> 50
|
||||
,'dodge'=> 2
|
||||
,'crit'=> 2
|
||||
,'block'=> 0
|
||||
,'block_effect'=> 0.5
|
||||
,'crit_effect'=> 2
|
||||
,'foramtion_effect'=> 0 )
|
||||
,'7'=> Array(
|
||||
'user_id'=> 23989
|
||||
,'soldier_id'=> 26
|
||||
,'prototype_id'=> 6
|
||||
,'bid'=> 1
|
||||
,'level'=> 1
|
||||
,'rare'=> 1
|
||||
,'skill_id'=> 1
|
||||
,'totalhp'=> 3669
|
||||
,'hp'=> 3669
|
||||
,'attack_general'=> 362
|
||||
,'attack_skill'=> 549
|
||||
,'attack_explode'=> 426
|
||||
,'attack_type'=> 1
|
||||
,'defense'=> 0
|
||||
,'anger'=> 50
|
||||
,'dodge'=> 2
|
||||
,'crit'=> 2
|
||||
,'block'=> 0
|
||||
,'block_effect'=> 0.5
|
||||
,'crit_effect'=> 2
|
||||
,'foramtion_effect'=> 0 )
|
||||
,'9'=> Array(
|
||||
'user_id'=> 23989
|
||||
,'soldier_id'=> 27
|
||||
,'prototype_id'=> 1
|
||||
,'bid'=> 1
|
||||
,'level'=> 1
|
||||
,'rare'=> 1
|
||||
,'skill_id'=> 1
|
||||
,'totalhp'=> 3618
|
||||
,'hp'=> 3618
|
||||
,'attack_general'=> 326
|
||||
,'attack_skill'=> 510
|
||||
,'attack_explode'=> 419
|
||||
,'attack_type'=> 1
|
||||
,'defense'=> 0
|
||||
,'anger'=> 50
|
||||
,'dodge'=> 2
|
||||
,'crit'=> 2
|
||||
,'block'=> 0
|
||||
,'block_effect'=> 0.5
|
||||
,'crit_effect'=> 2
|
||||
,'foramtion_effect'=> 0) )
|
||||
,'battle_process'=> Array(
|
||||
'0'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
|
||||
,'1'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
|
||||
,'2'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
,'3'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
|
||||
,'4'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
,'5'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
|
||||
,'6'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
|
||||
,'7'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
,'8'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
|
||||
,'9'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
,'10'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
,'11'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
|
||||
,'12'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
,'13'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
,'14'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
|
||||
,'15'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
,'16'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
,'17'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
|
||||
,'18'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
,'19'=> Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
|
||||
,'20'=>Array(
|
||||
'user_id'=> 41248
|
||||
,'asid'=> 28
|
||||
,'bsid'=> 'danger'
|
||||
,'harm'=> 'danger2'
|
||||
,'dhp'=> Array('0'=> 2019 )
|
||||
,'attacker_anger'=> 66
|
||||
,'defender_anger'=> Array('0'=> 94 )
|
||||
,'skill'=> 0
|
||||
,'state'=> 0
|
||||
)
|
||||
)
|
||||
|
||||
);
|
||||
$json = json_encode($target);
|
||||
$seri = serialize($target);
|
||||
$sw = swoole_serialize::pack($target);
|
||||
var_dump("json :". strlen($json));
|
||||
var_dump("serialize :". strlen($seri));
|
||||
var_dump("swoole :". strlen($sw));
|
||||
$stime = microtime(true);
|
||||
for ($i = 0; $i < 50000; $i ++) {
|
||||
json_encode($target);
|
||||
}
|
||||
$etime = microtime(true);
|
||||
var_dump("json_encode :". ($etime - $stime));
|
||||
//----------------------------------
|
||||
$stime = microtime(true);
|
||||
for ($i = 0; $i < 50000; $i ++) {
|
||||
json_decode($json. true);
|
||||
}
|
||||
$etime = microtime(true);
|
||||
var_dump("json_decode :". ($etime - $stime));
|
||||
//----------------------------------
|
||||
$stime = microtime(true);
|
||||
for ($i = 0; $i < 50000; $i ++) {
|
||||
serialize($target);
|
||||
}
|
||||
$etime = microtime(true);
|
||||
var_dump("serialize :". ($etime - $stime));
|
||||
//----------------------------------
|
||||
$stime = microtime(true);
|
||||
for ($i = 0; $i < 50000; $i ++) {
|
||||
unserialize($seri);
|
||||
}
|
||||
$etime = microtime(true);
|
||||
var_dump("unserialize :". ($etime - $stime));
|
||||
//----------------------------------
|
||||
//----------------------------------
|
||||
//----------------------------------
|
||||
$stime = microtime(true);
|
||||
for ($i = 0; $i < 50000; $i ++) {
|
||||
swoole_serialize::pack($target);
|
||||
}
|
||||
$etime = microtime(true);
|
||||
var_dump("swoole_serialize :". ($etime - $stime));
|
||||
$stime = microtime(true);
|
||||
for ($i = 0; $i < 50000; $i ++) {
|
||||
swoole_serialize::unpack($sw);
|
||||
}
|
||||
$etime = microtime(true);
|
||||
var_dump("swoole_unserialize :". ($etime - $stime));
|
||||
?>
|
46
vendor/swoole/benchmark/table.php
vendored
Executable file
46
vendor/swoole/benchmark/table.php
vendored
Executable file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
$table = new swoole_table(1 << 18);
|
||||
$table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8
|
||||
$table->column('name', swoole_table::TYPE_STRING, 17623);
|
||||
$table->column('num', swoole_table::TYPE_FLOAT);
|
||||
$table->create();
|
||||
|
||||
define('N', 100000);
|
||||
|
||||
//$worker = new swoole_process('child1', false, false);
|
||||
//$worker->start();
|
||||
//
|
||||
//child
|
||||
function child1($worker)
|
||||
{
|
||||
global $table;
|
||||
$s = microtime(true);
|
||||
for($i =0; $i < N; $i++)
|
||||
{
|
||||
$table->set('tianfenghan@qq.com', array('id' => 145, 'name' => 'rango', 'num' => 3.1415));
|
||||
$table->set('350749960@qq.com', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415));
|
||||
$table->set('hello@qq.com', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415));
|
||||
$table->set('tianfenghan@chelun.com', array('id' => 145, 'name' => 'rango', 'num' => 3.1415));
|
||||
$table->set('12811247@qq.com', array('id' => 1358, 'name' => "Swoole", 'num' => 3.1415));
|
||||
}
|
||||
echo "set - ".(5*N)." use: ".round((microtime(true) - $s) * 1000, 4)."ms\n";
|
||||
}
|
||||
|
||||
//master
|
||||
sleep(1);
|
||||
|
||||
child1(1245);
|
||||
$s = microtime(true);
|
||||
for($i =0; $i < N; $i++)
|
||||
{
|
||||
$arr1 = $table->get('tianfenghan@qq.com');
|
||||
$arr2 = $table->get('350749960@qq.com');
|
||||
$arr3 = $table->get('hello@qq.com');
|
||||
$arr4 = $table->get('tianfenghan@chelun.com');
|
||||
$arr5 = $table->get('12811247@qq.com');
|
||||
}
|
||||
|
||||
echo "get - ".(5*N)." use: ".round((microtime(true) - $s) * 1000, 4)."ms\n";
|
||||
$s = microtime(true);
|
||||
|
||||
var_dump($arr1, $arr2, $arr3, $arr4, $arr5);
|
41
vendor/swoole/benchmark/tcp.go
vendored
Executable file
41
vendor/swoole/benchmark/tcp.go
vendored
Executable file
@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func Echo(c net.Conn) {
|
||||
defer c.Close()
|
||||
for {
|
||||
line, err := bufio.NewReader(c).ReadString('\n')
|
||||
if err != nil {
|
||||
//fmt.Printf("Failure to read:%s\n", err.Error())
|
||||
return
|
||||
}
|
||||
_, err = c.Write([]byte(line))
|
||||
if err != nil {
|
||||
//fmt.Printf("Failure to write: %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
|
||||
|
||||
fmt.Printf("Server is ready...\n")
|
||||
l, err := net.Listen("tcp", ":8053")
|
||||
if err != nil {
|
||||
fmt.Printf("Failure to listen: %s\n", err.Error())
|
||||
}
|
||||
|
||||
for {
|
||||
if c, err := l.Accept(); err == nil {
|
||||
go Echo(c) //new thread
|
||||
}
|
||||
}
|
||||
}
|
8
vendor/swoole/benchmark/tcp.js
vendored
Executable file
8
vendor/swoole/benchmark/tcp.js
vendored
Executable file
@ -0,0 +1,8 @@
|
||||
var net = require('net');
|
||||
|
||||
var server = net.createServer(function (socket) {
|
||||
socket.write("Echo server\r\n");
|
||||
socket.pipe(socket);
|
||||
});
|
||||
|
||||
server.listen(7001, "127.0.0.1");
|
31
vendor/swoole/benchmark/tcp.php
vendored
Executable file
31
vendor/swoole/benchmark/tcp.php
vendored
Executable file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
Swoole\Async::set(array('enable_reuse_port' => true));
|
||||
$serv = new swoole_server("0.0.0.0", 9502, SWOOLE_BASE);
|
||||
//$serv = new swoole_server("0.0.0.0", 9502);
|
||||
$serv->set(array(
|
||||
'worker_num' => 8,
|
||||
));
|
||||
$serv->on('workerstart', function ($server, $id)
|
||||
{
|
||||
global $argv;
|
||||
swoole_set_process_name("php {$argv[0]}: worker");
|
||||
});
|
||||
|
||||
$serv->on('connect', function (swoole_server $serv, $fd, $from_id)
|
||||
{
|
||||
//echo "connect\n";;
|
||||
});
|
||||
|
||||
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data)
|
||||
{
|
||||
$serv->send($fd, "Swoole: " . $data);
|
||||
//$serv->close($fd);
|
||||
});
|
||||
|
||||
$serv->on('close', function (swoole_server $serv, $fd, $from_id)
|
||||
{
|
||||
//var_dump($serv->connection_info($fd));
|
||||
//echo "onClose\n";
|
||||
});
|
||||
|
||||
$serv->start();
|
37
vendor/swoole/benchmark/timer.php
vendored
Executable file
37
vendor/swoole/benchmark/timer.php
vendored
Executable file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
const N = 100000;
|
||||
|
||||
function test()
|
||||
{
|
||||
global $timers;
|
||||
shuffle($timers);
|
||||
$stime = microtime(true);
|
||||
foreach($timers as $id)
|
||||
{
|
||||
swoole_timer_clear($id);
|
||||
}
|
||||
$etime = microtime(true);
|
||||
echo "del ".N." timer :". ($etime - $stime)."s\n";
|
||||
}
|
||||
|
||||
class TestClass
|
||||
{
|
||||
static function timer()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
$timers = [];
|
||||
$stime = microtime(true);
|
||||
for($i = 0; $i < N; $i++)
|
||||
{
|
||||
$timers[] = swoole_timer_after(rand(1, 9999999), 'test');
|
||||
//swoole_timer_after(rand(1, 9999999), function () {
|
||||
// echo "hello world\n";
|
||||
//});
|
||||
//swoole_timer_after(rand(1, 9999999), array('TestClass', 'timer'));
|
||||
}
|
||||
$etime = microtime(true);
|
||||
echo "add ".N." timer :". ($etime - $stime)."s\n";
|
||||
swoole_event_wait();
|
36
vendor/swoole/benchmark/udp.php
vendored
Executable file
36
vendor/swoole/benchmark/udp.php
vendored
Executable file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
//Swoole\Async::set(array('enable_reuse_port' => true));
|
||||
//$serv = new swoole_server("0.0.0.0", 9502, SWOOLE_BASE, SWOOLE_SOCK_UDP);
|
||||
$serv = new swoole_server("0.0.0.0", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
|
||||
$serv->set(array(
|
||||
'dispatch_mode' => 1,
|
||||
'worker_num' => 8, //worker process num
|
||||
// //'log_file' => '/tmp/swoole.log',
|
||||
// //'daemonize' => true,
|
||||
));
|
||||
|
||||
function my_onStart($serv)
|
||||
{
|
||||
echo "MasterPid={$serv->master_pid}|Manager_pid={$serv->manager_pid}\n";
|
||||
echo "Server: start.Swoole version is [" . SWOOLE_VERSION . "]\n";
|
||||
}
|
||||
|
||||
function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
|
||||
{
|
||||
//var_dump($serv->connection_info($fd, $from_id));
|
||||
//echo "worker_pid=".posix_getpid().PHP_EOL;
|
||||
//var_dump($fd, $from_id);
|
||||
$serv->send($fd, 'Swoole: ' . $data, $from_id);
|
||||
}
|
||||
|
||||
function my_onPacket(swoole_server $serv, $data, $addr)
|
||||
{
|
||||
// var_dump($addr);
|
||||
$serv->sendto($addr['address'], $addr['port'], 'Swoole: ' . $data);
|
||||
}
|
||||
|
||||
$serv->on('Start', 'my_onStart');
|
||||
$serv->on('Receive', 'my_onReceive');
|
||||
//$serv->on('Packet', 'my_onPacket');
|
||||
$serv->start();
|
||||
|
25
vendor/swoole/benchmark/websocket.php
vendored
Executable file
25
vendor/swoole/benchmark/websocket.php
vendored
Executable file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
//$server = new swoole_websocket_server("0.0.0.0", 9502);
|
||||
$server = new swoole_websocket_server("0.0.0.0", 9502, SWOOLE_BASE);
|
||||
$server->set(['worker_num' => 4]);
|
||||
//
|
||||
//$server->on('open', function (swoole_websocket_server $_server, swoole_http_request $request) {
|
||||
// //echo "server#{$_server->worker_pid}: handshake success with fd#{$request->fd}\n";
|
||||
//
|
||||
//// var_dump($request);
|
||||
//});
|
||||
|
||||
$server->on('message', function (swoole_websocket_server $_server, $frame) {
|
||||
//var_dump($frame);
|
||||
//echo "received ".strlen($frame->data)." bytes\n";
|
||||
//echo "receive from {$fd}:{$data},opcode:{$opcode},fin:{$fin}\n";
|
||||
$_server->push($frame->fd, "server:" . $frame->data);
|
||||
// $_server->close($frame->fd);
|
||||
});
|
||||
|
||||
//$server->on('close', function ($_server, $fd) {
|
||||
// echo "client {$fd} closed\n";
|
||||
//});
|
||||
|
||||
|
||||
$server->start();
|
17
vendor/swoole/build.sh
vendored
Executable file
17
vendor/swoole/build.sh
vendored
Executable file
@ -0,0 +1,17 @@
|
||||
git submodule update --init --recursive
|
||||
cd thirdparty/hiredis
|
||||
make
|
||||
sudo make install
|
||||
cd ../nghttp2
|
||||
cmake .
|
||||
make
|
||||
sudo make install
|
||||
sudo ldconfig
|
||||
cd ../..
|
||||
phpize --clean
|
||||
phpize
|
||||
make clean
|
||||
./configure --enable-openssl --enable-sockets --enable-async-redis --enable-mysqlnd --enable-http2
|
||||
make -j
|
||||
make install
|
||||
|
7
vendor/swoole/clear.sh
vendored
Executable file
7
vendor/swoole/clear.sh
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
|
||||
find . -name \*.lo -o -name \*.o | xargs rm -f
|
||||
find . -name \*.la -o -name \*.a | xargs rm -f
|
||||
find . -name \*.so | xargs rm -f
|
||||
find . -name .libs -a -type d|xargs rm -rf
|
||||
rm -f libphp.la modules/* libs/*
|
||||
|
654
vendor/swoole/config.m4
vendored
Executable file
654
vendor/swoole/config.m4
vendored
Executable file
@ -0,0 +1,654 @@
|
||||
dnl config.m4 for extension swoole
|
||||
|
||||
dnl +----------------------------------------------------------------------+
|
||||
dnl | Swoole |
|
||||
dnl +----------------------------------------------------------------------+
|
||||
dnl | This source file is subject to version 2.0 of the Apache license, |
|
||||
dnl | that is bundled with this package in the file LICENSE, and is |
|
||||
dnl | available through the world-wide-web at the following url: |
|
||||
dnl | http://www.apache.org/licenses/LICENSE-2.0.html |
|
||||
dnl | If you did not receive a copy of the Apache2.0 license and are unable|
|
||||
|
||||
dnl | to obtain it through the world-wide-web, please send a note to |
|
||||
dnl | license@swoole.com so we can mail you a copy immediately. |
|
||||
dnl +----------------------------------------------------------------------+
|
||||
dnl | Author: Tianfeng Han <mikan.tenny@gmail.com> |
|
||||
dnl +----------------------------------------------------------------------+
|
||||
|
||||
PHP_ARG_ENABLE(swoole-debug, whether to enable swoole debug,
|
||||
[ --enable-swoole-debug Enable swoole debug], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(trace-log, Whether to enable trace log,
|
||||
[ --enable-trace-log Enable swoole trace log], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(sockets, enable sockets support,
|
||||
[ --enable-sockets Do you have sockets extension?], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(async_redis, enable async_redis support,
|
||||
[ --enable-async-redis Do you have hiredis?], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(coroutine-postgresql, enable coroutine postgresql support,
|
||||
[ --enable-coroutine-postgresql Do you install postgresql?], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(openssl, enable openssl support,
|
||||
[ --enable-openssl Use openssl?], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(http2, enable http2.0 support,
|
||||
[ --enable-http2 Use http2.0?], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(thread, enable thread support,
|
||||
[ --enable-thread Experimental: Use thread?], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(hugepage, enable hugepage support,
|
||||
[ --enable-hugepage Experimental: Use hugepage?], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(swoole, swoole support,
|
||||
[ --enable-swoole Enable swoole support], [enable_swoole="yes"])
|
||||
|
||||
PHP_ARG_ENABLE(swoole_static, swoole static compile support,
|
||||
[ --enable-swoole-static Enable swoole static compile support], no, no)
|
||||
|
||||
PHP_ARG_WITH(swoole, swoole support,
|
||||
[ --with-swoole With swoole support])
|
||||
|
||||
PHP_ARG_WITH(libpq_dir, for libpq support,
|
||||
[ --with-libpq-dir[=DIR] Include libpq support (requires libpq >= 9.5)], no, no)
|
||||
|
||||
PHP_ARG_WITH(openssl_dir, for OpenSSL support,
|
||||
[ --with-openssl-dir[=DIR] Include OpenSSL support (requires OpenSSL >= 0.9.6)], no, no)
|
||||
|
||||
PHP_ARG_WITH(phpx_dir, for PHP-X support,
|
||||
[ --with-phpx-dir[=DIR] Include PHP-X support], no, no)
|
||||
|
||||
PHP_ARG_WITH(jemalloc_dir, for jemalloc support,
|
||||
[ --with-jemalloc-dir[=DIR] Include jemalloc support], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(mysqlnd, enable mysqlnd support,
|
||||
[ --enable-mysqlnd Do you have mysqlnd?], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(asan, whether to enable asan,
|
||||
[ --enable-asan Enable asan], no, no)
|
||||
|
||||
PHP_ARG_ENABLE(picohttpparser, enable picohttpparser support,
|
||||
[ --enable-picohttpparser Experimental: Do you have picohttpparser?], no, no)
|
||||
|
||||
PHP_ARG_WITH(swoole, swoole support,
|
||||
[ --with-swoole With swoole support])
|
||||
|
||||
PHP_ARG_ENABLE(timewheel, enable timewheel support,
|
||||
[ --enable-timewheel Experimental: Enable timewheel heartbeat?], no, no)
|
||||
|
||||
AC_DEFUN([SWOOLE_HAVE_PHP_EXT], [
|
||||
extname=$1
|
||||
haveext=$[PHP_]translit($1,a-z_-,A-Z__)
|
||||
|
||||
AC_MSG_CHECKING([for ext/$extname support])
|
||||
if test -x "$PHP_EXECUTABLE"; then
|
||||
grepext=`$PHP_EXECUTABLE -m | $EGREP ^$extname\$`
|
||||
if test "$grepext" = "$extname"; then
|
||||
[PHP_HTTP_HAVE_EXT_]translit($1,a-z_-,A-Z__)=1
|
||||
AC_MSG_RESULT([yes])
|
||||
$2
|
||||
else
|
||||
[PHP_HTTP_HAVE_EXT_]translit($1,a-z_-,A-Z__)=
|
||||
AC_MSG_RESULT([no])
|
||||
$3
|
||||
fi
|
||||
elif test "$haveext" != "no" && test "x$haveext" != "x"; then
|
||||
[PHP_HTTP_HAVE_EXT_]translit($1,a-z_-,A-Z__)=1
|
||||
AC_MSG_RESULT([yes])
|
||||
$2
|
||||
else
|
||||
[PHP_HTTP_HAVE_EXT_]translit($1,a-z_-,A-Z__)=
|
||||
AC_MSG_RESULT([no])
|
||||
$3
|
||||
fi
|
||||
])
|
||||
|
||||
AC_DEFUN([AC_SWOOLE_CPU_AFFINITY],
|
||||
[
|
||||
AC_MSG_CHECKING([for cpu affinity])
|
||||
AC_TRY_COMPILE(
|
||||
[
|
||||
#ifdef __FreeBSD__
|
||||
#include <sys/types.h>
|
||||
#include <sys/cpuset.h>
|
||||
typedef cpuset_t cpu_set_t;
|
||||
#else
|
||||
#include <sched.h>
|
||||
#endif
|
||||
], [
|
||||
cpu_set_t cpu_set;
|
||||
CPU_ZERO(&cpu_set);
|
||||
], [
|
||||
AC_DEFINE([HAVE_CPU_AFFINITY], 1, [cpu affinity?])
|
||||
AC_MSG_RESULT([yes])
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
])
|
||||
])
|
||||
|
||||
AC_DEFUN([AC_SWOOLE_HAVE_REUSEPORT],
|
||||
[
|
||||
AC_MSG_CHECKING([for socket REUSEPORT])
|
||||
AC_TRY_COMPILE(
|
||||
[
|
||||
#include <sys/socket.h>
|
||||
], [
|
||||
int val = 1;
|
||||
setsockopt(0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
|
||||
], [
|
||||
AC_DEFINE([HAVE_REUSEPORT], 1, [have SO_REUSEPORT?])
|
||||
AC_MSG_RESULT([yes])
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
])
|
||||
])
|
||||
|
||||
AC_DEFUN([AC_SWOOLE_HAVE_FUTEX],
|
||||
[
|
||||
AC_MSG_CHECKING([for futex])
|
||||
AC_TRY_COMPILE(
|
||||
[
|
||||
#include <linux/futex.h>
|
||||
#include <syscall.h>
|
||||
#include <unistd.h>
|
||||
], [
|
||||
int futex_addr;
|
||||
int val1;
|
||||
syscall(SYS_futex, &futex_addr, val1, NULL, NULL, 0);
|
||||
], [
|
||||
AC_DEFINE([HAVE_FUTEX], 1, [have FUTEX?])
|
||||
AC_MSG_RESULT([yes])
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
])
|
||||
])
|
||||
|
||||
AC_DEFUN([AC_SWOOLE_HAVE_LINUX_AIO],
|
||||
[
|
||||
AC_MSG_CHECKING([for linux aio])
|
||||
AC_TRY_COMPILE(
|
||||
[
|
||||
#include <sys/syscall.h>
|
||||
#include <linux/aio_abi.h>
|
||||
#include <unistd.h>
|
||||
], [
|
||||
struct iocb *iocbps[1];
|
||||
struct iocb iocbp;
|
||||
aio_context_t context;
|
||||
iocbps[0] = &iocbp;
|
||||
io_submit(context, 1, iocbps);
|
||||
], [
|
||||
AC_DEFINE([HAVE_LINUX_AIO], 1, [have LINUX_AIO?])
|
||||
AC_MSG_RESULT([yes])
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
])
|
||||
])
|
||||
|
||||
AC_DEFUN([AC_SWOOLE_HAVE_UCONTEXT],
|
||||
[
|
||||
AC_MSG_CHECKING([for ucontext])
|
||||
AC_TRY_COMPILE(
|
||||
[
|
||||
#include <stdio.h>
|
||||
#include <ucontext.h>
|
||||
#include <unistd.h>
|
||||
], [
|
||||
ucontext_t context;
|
||||
getcontext(&context);
|
||||
], [
|
||||
AC_DEFINE([HAVE_UCONTEXT], 1, [have ucontext?])
|
||||
AC_MSG_RESULT([yes])
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
])
|
||||
])
|
||||
|
||||
AC_DEFUN([AC_SWOOLE_HAVE_BOOST_CONTEXT],
|
||||
[
|
||||
AC_MSG_CHECKING([for boost.context])
|
||||
AC_LANG([C++])
|
||||
AC_TRY_COMPILE(
|
||||
[
|
||||
#include <boost/context/all.hpp>
|
||||
], [
|
||||
|
||||
], [
|
||||
AC_DEFINE([HAVE_BOOST_CONTEXT], 1, [have boost.context?])
|
||||
SW_HAVE_BOOST_CONTEXT=yes
|
||||
AC_MSG_RESULT([yes])
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
])
|
||||
])
|
||||
|
||||
AC_DEFUN([AC_SWOOLE_HAVE_VALGRIND],
|
||||
[
|
||||
AC_MSG_CHECKING([for valgrind])
|
||||
AC_LANG([C++])
|
||||
AC_TRY_COMPILE(
|
||||
[
|
||||
#include <valgrind/valgrind.h>
|
||||
], [
|
||||
|
||||
], [
|
||||
AC_DEFINE([HAVE_VALGRIND], 1, [have valgrind?])
|
||||
AC_MSG_RESULT([yes])
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
])
|
||||
])
|
||||
|
||||
AC_MSG_CHECKING([if compiling with clang])
|
||||
AC_COMPILE_IFELSE([
|
||||
AC_LANG_PROGRAM([], [[
|
||||
#ifndef __clang__
|
||||
not clang
|
||||
#endif
|
||||
]])],
|
||||
[CLANG=yes], [CLANG=no]
|
||||
)
|
||||
AC_MSG_RESULT([$CLANG])
|
||||
|
||||
if test "$CLANG" = "yes"; then
|
||||
CFLAGS="$CFLAGS -std=gnu89"
|
||||
fi
|
||||
|
||||
AC_CANONICAL_HOST
|
||||
|
||||
if test "$PHP_SWOOLE" != "no"; then
|
||||
|
||||
PHP_ADD_LIBRARY(pthread)
|
||||
PHP_SUBST(SWOOLE_SHARED_LIBADD)
|
||||
|
||||
AC_ARG_ENABLE(debug,
|
||||
[--enable-debug, compile with debug symbols],
|
||||
[PHP_DEBUG=$enableval],
|
||||
[PHP_DEBUG=0]
|
||||
)
|
||||
|
||||
if test "$PHP_SWOOLE_DEBUG" != "no"; then
|
||||
AC_DEFINE(SW_DEBUG, 1, [do we enable swoole debug])
|
||||
PHP_DEBUG=1
|
||||
fi
|
||||
|
||||
if test "$PHP_ASAN" != "no"; then
|
||||
PHP_DEBUG=1
|
||||
CFLAGS="$CFLAGS -fsanitize=address -fno-omit-frame-pointer"
|
||||
fi
|
||||
|
||||
if test "$PHP_TRACE_LOG" != "no"; then
|
||||
AC_DEFINE(SW_LOG_TRACE_OPEN, 1, [enable trace log])
|
||||
fi
|
||||
|
||||
if test "$PHP_SOCKETS" = "yes"; then
|
||||
AC_DEFINE(SW_SOCKETS, 1, [enable sockets support])
|
||||
fi
|
||||
|
||||
if test "$PHP_HTTP2" = "yes"; then
|
||||
AC_DEFINE(SW_USE_HTTP2, 1, [enable http2.0 support])
|
||||
fi
|
||||
|
||||
if test "$PHP_HUGEPAGE" = "yes"; then
|
||||
AC_DEFINE(SW_USE_HUGEPAGE, 1, [enable hugepage support])
|
||||
fi
|
||||
|
||||
if test "$PHP_THREAD" = "yes"; then
|
||||
AC_DEFINE(SW_USE_THREAD, 1, [enable thread support])
|
||||
fi
|
||||
|
||||
if test "$PHP_TIMEWHEEL" = "yes"; then
|
||||
AC_DEFINE(SW_USE_TIMEWHEEL, 1, [enable timewheel support])
|
||||
fi
|
||||
|
||||
AC_SWOOLE_CPU_AFFINITY
|
||||
AC_SWOOLE_HAVE_REUSEPORT
|
||||
AC_SWOOLE_HAVE_FUTEX
|
||||
AC_SWOOLE_HAVE_LINUX_AIO
|
||||
AC_SWOOLE_HAVE_UCONTEXT
|
||||
AC_SWOOLE_HAVE_BOOST_CONTEXT
|
||||
AC_SWOOLE_HAVE_VALGRIND
|
||||
|
||||
CFLAGS="-Wall -pthread $CFLAGS"
|
||||
LDFLAGS="$LDFLAGS -lpthread"
|
||||
|
||||
if test `uname` = "Darwin"; then
|
||||
AC_CHECK_LIB(c, clock_gettime, AC_DEFINE(HAVE_CLOCK_GETTIME, 1, [have clock_gettime]))
|
||||
else
|
||||
AC_CHECK_LIB(rt, clock_gettime, AC_DEFINE(HAVE_CLOCK_GETTIME, 1, [have clock_gettime]))
|
||||
PHP_ADD_LIBRARY(rt, 1, SWOOLE_SHARED_LIBADD)
|
||||
LDFLAGS="$LDFLAGS -z now"
|
||||
fi
|
||||
|
||||
if test "$PHP_OPENSSL" != "no" || test "$PHP_OPENSSL_DIR" != "no"; then
|
||||
if test "$PHP_OPENSSL_DIR" != "no"; then
|
||||
AC_DEFINE(HAVE_OPENSSL, 1, [have openssl])
|
||||
PHP_ADD_INCLUDE("${PHP_OPENSSL_DIR}/include")
|
||||
PHP_ADD_LIBRARY_WITH_PATH(ssl, "${PHP_OPENSSL_DIR}/${PHP_LIBDIR}")
|
||||
else
|
||||
AC_CHECK_LIB(ssl, SSL_connect, AC_DEFINE(HAVE_OPENSSL, 1, [have openssl]))
|
||||
fi
|
||||
|
||||
AC_DEFINE(SW_USE_OPENSSL, 1, [enable openssl support])
|
||||
PHP_ADD_LIBRARY(ssl, 1, SWOOLE_SHARED_LIBADD)
|
||||
PHP_ADD_LIBRARY(crypto, 1, SWOOLE_SHARED_LIBADD)
|
||||
fi
|
||||
|
||||
if test "$PHP_PHPX_DIR" != "no"; then
|
||||
PHP_ADD_INCLUDE("${PHP_PHPX_DIR}/include")
|
||||
PHP_ADD_LIBRARY_WITH_PATH(phpx, "${PHP_PHPX_DIR}/${PHP_LIBDIR}")
|
||||
AC_DEFINE(SW_USE_PHPX, 1, [enable PHP-X support])
|
||||
PHP_ADD_LIBRARY(phpx, 1, SWOOLE_SHARED_LIBADD)
|
||||
CXXFLAGS="$CXXFLAGS -std=c++11"
|
||||
fi
|
||||
|
||||
if test "$PHP_JEMALLOC_DIR" != "no"; then
|
||||
AC_DEFINE(SW_USE_JEMALLOC, 1, [use jemalloc])
|
||||
PHP_ADD_INCLUDE("${PHP_JEMALLOC_DIR}/include")
|
||||
PHP_ADD_LIBRARY_WITH_PATH(jemalloc, "${PHP_JEMALLOC_DIR}/${PHP_LIBDIR}")
|
||||
PHP_ADD_LIBRARY(jemalloc, 1, SWOOLE_SHARED_LIBADD)
|
||||
fi
|
||||
|
||||
PHP_ADD_LIBRARY(pthread, 1, SWOOLE_SHARED_LIBADD)
|
||||
|
||||
if test "$PHP_ASYNC_REDIS" = "yes"; then
|
||||
AC_DEFINE(SW_USE_REDIS, 1, [enable async-redis support])
|
||||
PHP_ADD_LIBRARY(hiredis, 1, SWOOLE_SHARED_LIBADD)
|
||||
fi
|
||||
|
||||
if test "$PHP_COROUTINE_POSTGRESQL" = "yes"; then
|
||||
if test "$PHP_LIBPQ" != "no" || test "$PHP_LIBPQ_DIR" != "no"; then
|
||||
if test "$PHP_LIBPQ_DIR" != "no"; then
|
||||
AC_DEFINE(HAVE_LIBPQ, 1, [have libpq])
|
||||
AC_MSG_RESULT(libpq include success)
|
||||
PHP_ADD_INCLUDE("${PHP_LIBPQ_DIR}/include")
|
||||
else
|
||||
PGSQL_SEARCH_PATHS="/usr /usr/local /usr/local/pgsql"
|
||||
for i in $PGSQL_SEARCH_PATHS; do
|
||||
for j in include include/pgsql include/postgres include/postgresql ""; do
|
||||
if test -r "$i/$j/libpq-fe.h"; then
|
||||
PGSQL_INC_BASE=$i
|
||||
PGSQL_INCLUDE=$i/$j
|
||||
AC_MSG_RESULT(libpq-fe.h found in PGSQL_INCLUDE)
|
||||
PHP_ADD_INCLUDE("${PGSQL_INCLUDE}")
|
||||
fi
|
||||
done
|
||||
done
|
||||
fi
|
||||
AC_DEFINE(SW_USE_POSTGRESQL, 1, [enable coroutine-postgresql support])
|
||||
PHP_ADD_LIBRARY(pq, 1, SWOOLE_SHARED_LIBADD)
|
||||
fi
|
||||
if test -z "$PGSQL_INCLUDE"; then
|
||||
AC_MSG_ERROR(Cannot find libpq-fe.h. Please confirm the libpq or specify correct PostgreSQL(libpq) installation path)
|
||||
fi
|
||||
fi
|
||||
|
||||
if test "$PHP_HTTP2" = "yes"; then
|
||||
PHP_ADD_LIBRARY(nghttp2, 1, SWOOLE_SHARED_LIBADD)
|
||||
fi
|
||||
|
||||
if test "$PHP_MYSQLND" = "yes"; then
|
||||
PHP_ADD_EXTENSION_DEP(mysqli, mysqlnd)
|
||||
AC_DEFINE(SW_USE_MYSQLND, 1, [use mysqlnd])
|
||||
fi
|
||||
|
||||
AC_CHECK_LIB(c, accept4, AC_DEFINE(HAVE_ACCEPT4, 1, [have accept4]))
|
||||
AC_CHECK_LIB(c, signalfd, AC_DEFINE(HAVE_SIGNALFD, 1, [have signalfd]))
|
||||
AC_CHECK_LIB(c, timerfd_create, AC_DEFINE(HAVE_TIMERFD, 1, [have timerfd]))
|
||||
AC_CHECK_LIB(c, eventfd, AC_DEFINE(HAVE_EVENTFD, 1, [have eventfd]))
|
||||
AC_CHECK_LIB(c, epoll_create, AC_DEFINE(HAVE_EPOLL, 1, [have epoll]))
|
||||
AC_CHECK_LIB(c, poll, AC_DEFINE(HAVE_POLL, 1, [have poll]))
|
||||
AC_CHECK_LIB(c, sendfile, AC_DEFINE(HAVE_SENDFILE, 1, [have sendfile]))
|
||||
AC_CHECK_LIB(c, kqueue, AC_DEFINE(HAVE_KQUEUE, 1, [have kqueue]))
|
||||
AC_CHECK_LIB(c, backtrace, AC_DEFINE(HAVE_EXECINFO, 1, [have execinfo]))
|
||||
AC_CHECK_LIB(c, daemon, AC_DEFINE(HAVE_DAEMON, 1, [have daemon]))
|
||||
AC_CHECK_LIB(c, mkostemp, AC_DEFINE(HAVE_MKOSTEMP, 1, [have mkostemp]))
|
||||
AC_CHECK_LIB(c, inotify_init, AC_DEFINE(HAVE_INOTIFY, 1, [have inotify]))
|
||||
AC_CHECK_LIB(c, malloc_trim, AC_DEFINE(HAVE_MALLOC_TRIM, 1, [have malloc_trim]))
|
||||
AC_CHECK_LIB(c, inotify_init1, AC_DEFINE(HAVE_INOTIFY_INIT1, 1, [have inotify_init1]))
|
||||
AC_CHECK_LIB(c, gethostbyname2_r, AC_DEFINE(HAVE_GETHOSTBYNAME2_R, 1, [have gethostbyname2_r]))
|
||||
AC_CHECK_LIB(c, ptrace, AC_DEFINE(HAVE_PTRACE, 1, [have ptrace]))
|
||||
AC_CHECK_LIB(pthread, pthread_rwlock_init, AC_DEFINE(HAVE_RWLOCK, 1, [have pthread_rwlock_init]))
|
||||
AC_CHECK_LIB(pthread, pthread_spin_lock, AC_DEFINE(HAVE_SPINLOCK, 1, [have pthread_spin_lock]))
|
||||
AC_CHECK_LIB(pthread, pthread_mutex_timedlock, AC_DEFINE(HAVE_MUTEX_TIMEDLOCK, 1, [have pthread_mutex_timedlock]))
|
||||
AC_CHECK_LIB(pthread, pthread_barrier_init, AC_DEFINE(HAVE_PTHREAD_BARRIER, 1, [have pthread_barrier_init]))
|
||||
AC_CHECK_LIB(pcre, pcre_compile, AC_DEFINE(HAVE_PCRE, 1, [have pcre]))
|
||||
AC_CHECK_LIB(hiredis, redisConnect, AC_DEFINE(HAVE_HIREDIS, 1, [have hiredis]))
|
||||
AC_CHECK_LIB(pq, PQconnectdb, AC_DEFINE(HAVE_POSTGRESQL, 1, [have postgresql]))
|
||||
AC_CHECK_LIB(nghttp2, nghttp2_hd_inflate_new, AC_DEFINE(HAVE_NGHTTP2, 1, [have nghttp2]))
|
||||
|
||||
AC_CHECK_LIB(z, gzgets, [
|
||||
AC_DEFINE(SW_HAVE_ZLIB, 1, [have zlib])
|
||||
PHP_ADD_LIBRARY(z, 1, SWOOLE_SHARED_LIBADD)
|
||||
])
|
||||
|
||||
swoole_source_file="swoole.c \
|
||||
swoole_server.c \
|
||||
swoole_server_port.c \
|
||||
swoole_atomic.c \
|
||||
swoole_lock.c \
|
||||
swoole_client.c \
|
||||
swoole_client_coro.c \
|
||||
swoole_coroutine.cc \
|
||||
swoole_coroutine_util.c \
|
||||
swoole_event.c \
|
||||
swoole_socket_coro.c \
|
||||
swoole_timer.c \
|
||||
swoole_async.c \
|
||||
swoole_process.c \
|
||||
swoole_process_pool.c \
|
||||
swoole_serialize.c \
|
||||
swoole_buffer.c \
|
||||
swoole_table.c \
|
||||
swoole_http_server.c \
|
||||
swoole_http_v2_server.c \
|
||||
swoole_http_v2_client.c \
|
||||
swoole_http_v2_client_coro.c \
|
||||
swoole_websocket_server.c \
|
||||
swoole_http_client.c \
|
||||
swoole_http_client_coro.c \
|
||||
swoole_mysql.c \
|
||||
swoole_mysql_coro.c \
|
||||
swoole_postgresql_coro.c \
|
||||
swoole_redis.c \
|
||||
swoole_redis_coro.c \
|
||||
swoole_redis_server.c \
|
||||
swoole_mmap.c \
|
||||
swoole_channel.c \
|
||||
swoole_channel_coro.cc \
|
||||
swoole_ringqueue.c \
|
||||
swoole_msgqueue.c \
|
||||
swoole_trace.c \
|
||||
swoole_runtime.cc \
|
||||
swoole_memory_pool.c \
|
||||
src/core/base.c \
|
||||
src/core/log.c \
|
||||
src/core/hashmap.c \
|
||||
src/core/RingQueue.c \
|
||||
src/core/Channel.c \
|
||||
src/core/string.c \
|
||||
src/core/array.c \
|
||||
src/core/socket.c \
|
||||
src/core/list.c \
|
||||
src/core/heap.c \
|
||||
src/core/error.cc \
|
||||
src/coroutine/base.cc \
|
||||
src/coroutine/boost.cc \
|
||||
src/coroutine/context.cc \
|
||||
src/coroutine/ucontext.cc \
|
||||
src/memory/ShareMemory.c \
|
||||
src/memory/MemoryGlobal.c \
|
||||
src/memory/RingBuffer.c \
|
||||
src/memory/FixedPool.c \
|
||||
src/memory/Malloc.c \
|
||||
src/memory/Table.c \
|
||||
src/memory/Buffer.c \
|
||||
src/factory/Factory.c \
|
||||
src/factory/FactoryThread.c \
|
||||
src/factory/FactoryProcess.c \
|
||||
src/reactor/ReactorBase.c \
|
||||
src/reactor/ReactorSelect.c \
|
||||
src/reactor/ReactorPoll.c \
|
||||
src/reactor/ReactorEpoll.c \
|
||||
src/reactor/ReactorKqueue.c \
|
||||
src/pipe/PipeBase.c \
|
||||
src/pipe/PipeEventfd.c \
|
||||
src/pipe/PipeUnsock.c \
|
||||
src/lock/Semaphore.c \
|
||||
src/lock/Mutex.c \
|
||||
src/lock/RWLock.c \
|
||||
src/lock/SpinLock.c \
|
||||
src/lock/FileLock.c \
|
||||
src/lock/Cond.c \
|
||||
src/network/Server.c \
|
||||
src/network/TaskWorker.c \
|
||||
src/network/Client.c \
|
||||
src/network/Connection.c \
|
||||
src/network/ProcessPool.c \
|
||||
src/network/ThreadPool.c \
|
||||
src/network/ReactorThread.c \
|
||||
src/network/ReactorProcess.c \
|
||||
src/network/Manager.c \
|
||||
src/network/Worker.c \
|
||||
src/network/Timer.c \
|
||||
src/network/Port.c \
|
||||
src/network/DNS.c \
|
||||
src/network/TimeWheel.c \
|
||||
src/network/Stream.c \
|
||||
src/os/base.c \
|
||||
src/os/msg_queue.c \
|
||||
src/os/sendfile.c \
|
||||
src/os/signal.c \
|
||||
src/os/timer.c \
|
||||
src/protocol/Base.c \
|
||||
src/protocol/SSL.c \
|
||||
src/protocol/Http.c \
|
||||
src/protocol/Http2.c \
|
||||
src/protocol/WebSocket.c \
|
||||
src/protocol/Mqtt.c \
|
||||
src/protocol/Socks5.c \
|
||||
src/protocol/MimeTypes.c \
|
||||
src/protocol/Redis.c \
|
||||
src/protocol/Base64.c"
|
||||
|
||||
if test "$PHP_SWOOLE_STATIC" = "no"; then
|
||||
swoole_source_file="$swoole_source_file thirdparty/php_http_parser.c"
|
||||
else
|
||||
CFLAGS="$CFLAGS -DSW_STATIC_COMPILATION"
|
||||
fi
|
||||
|
||||
swoole_source_file="$swoole_source_file thirdparty/multipart_parser.c"
|
||||
|
||||
if test "$PHP_PICOHTTPPARSER" = "yes"; then
|
||||
AC_DEFINE(SW_USE_PICOHTTPPARSER, 1, [enable picohttpparser support])
|
||||
swoole_source_file="$swoole_source_file thirdparty/picohttpparser/picohttpparser.c"
|
||||
fi
|
||||
|
||||
SW_NO_USE_ASM_CONTEXT="no"
|
||||
SW_ASM_DIR="thirdparty/boost/asm/"
|
||||
|
||||
AS_CASE([$host_cpu],
|
||||
[x86_64*], [SW_CPU="x86_64"],
|
||||
[x86*], [SW_CPU="x86"],
|
||||
[arm*], [SW_CPU="arm"],
|
||||
[arm64*], [SW_CPU="arm64"],
|
||||
[
|
||||
SW_NO_USE_ASM_CONTEXT="yes"
|
||||
AC_DEFINE([SW_NO_USE_ASM_CONTEXT], 1, [use boost asm context?])
|
||||
]
|
||||
)
|
||||
|
||||
AS_CASE([$host_os],
|
||||
[linux*], [SW_OS="LINUX"],
|
||||
[darwin*], [SW_OS="MAC"],
|
||||
[cygwin*], [SW_OS="WIN"],
|
||||
[mingw*], [SW_OS="WIN"],
|
||||
[
|
||||
SW_NO_USE_ASM_CONTEXT="yes"
|
||||
AC_DEFINE([SW_NO_USE_ASM_CONTEXT], 1, [use boost asm context?])
|
||||
]
|
||||
)
|
||||
|
||||
if test "$SW_CPU" = 'x86_64'; then
|
||||
if test "$SW_OS" = 'LINUX'; then
|
||||
SW_CONTEXT_ASM_FILE="x86_64_sysv_elf_gas.S"
|
||||
elif test "$SW_OS" = 'MAC'; then
|
||||
SW_CONTEXT_ASM_FILE="x86_64_sysv_macho_gas.S"
|
||||
else
|
||||
SW_NO_USE_ASM_CONTEXT="yes"
|
||||
AC_DEFINE([SW_NO_USE_ASM_CONTEXT], 1, [use boost asm context?])
|
||||
fi
|
||||
elif test "$SW_CPU" = 'x86'; then
|
||||
if test "$SW_OS" = 'LINUX'; then
|
||||
SW_CONTEXT_ASM_FILE="i386_sysv_elf_gas.S"
|
||||
elif test "$SW_OS" = 'MAC'; then
|
||||
SW_CONTEXT_ASM_FILE="i386_sysv_macho_gas.S"
|
||||
else
|
||||
SW_NO_USE_ASM_CONTEXT="yes"
|
||||
AC_DEFINE([SW_NO_USE_ASM_CONTEXT], 1, [use boost asm context?])
|
||||
fi
|
||||
elif test "$SW_CPU" = 'arm'; then
|
||||
if test "$SW_OS" = 'LINUX'; then
|
||||
SW_CONTEXT_ASM_FILE="arm_aapcs_elf_gas.S"
|
||||
elif test "$SW_OS" = 'MAC'; then
|
||||
SW_CONTEXT_ASM_FILE="arm_aapcs_macho_gas.S"
|
||||
else
|
||||
SW_NO_USE_ASM_CONTEXT="yes"
|
||||
AC_DEFINE([SW_NO_USE_ASM_CONTEXT], 1, [use boost asm context?])
|
||||
fi
|
||||
elif test "$SW_CPU" = 'arm64'; then
|
||||
if test "$SW_OS" = 'LINUX'; then
|
||||
SW_CONTEXT_ASM_FILE="arm64_aapcs_elf_gas.S"
|
||||
elif test "$SW_OS" = 'MAC'; then
|
||||
SW_CONTEXT_ASM_FILE="arm64_aapcs_macho_gas.S"
|
||||
else
|
||||
SW_NO_USE_ASM_CONTEXT="yes"
|
||||
AC_DEFINE([SW_NO_USE_ASM_CONTEXT], 1, [use boost asm context?])
|
||||
fi
|
||||
elif test "$SW_CPU" = 'mips32'; then
|
||||
if test "$SW_OS" = 'LINUX'; then
|
||||
SW_CONTEXT_ASM_FILE="mips32_o32_elf_gas.S"
|
||||
else
|
||||
SW_NO_USE_ASM_CONTEXT="yes"
|
||||
AC_DEFINE([SW_NO_USE_ASM_CONTEXT], 1, [use boost asm context?])
|
||||
fi
|
||||
fi
|
||||
|
||||
if test "$SW_NO_USE_ASM_CONTEXT" = 'no'; then
|
||||
swoole_source_file="$swoole_source_file ${SW_ASM_DIR}make_${SW_CONTEXT_ASM_FILE} \
|
||||
${SW_ASM_DIR}jump_${SW_CONTEXT_ASM_FILE} "
|
||||
elif test "$SW_HAVE_BOOST_CONTEXT" = 'yes'; then
|
||||
LDFLAGS="$LDFLAGS -lboost_context"
|
||||
fi
|
||||
|
||||
PHP_NEW_EXTENSION(swoole, $swoole_source_file, $ext_shared,,, cxx)
|
||||
|
||||
PHP_ADD_INCLUDE([$ext_srcdir])
|
||||
PHP_ADD_INCLUDE([$ext_srcdir/include])
|
||||
|
||||
PHP_INSTALL_HEADERS([ext/swoole], [*.h config.h include/*.h])
|
||||
|
||||
PHP_REQUIRE_CXX()
|
||||
PHP_ADD_LIBRARY(stdc++, 1, SWOOLE_SHARED_LIBADD)
|
||||
|
||||
if test "$PHP_PICOHTTPPARSER" = "yes"; then
|
||||
PHP_ADD_INCLUDE([$ext_srcdir/thirdparty/picohttpparser])
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/picohttpparser)
|
||||
fi
|
||||
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/src/core)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/src/memory)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/src/factory)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/src/reactor)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/src/pipe)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/src/lock)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/src/os)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/src/network)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/src/protocol)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/src/coroutine)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/boost)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/boost/asm)
|
||||
fi
|
17
vendor/swoole/examples/async/dns_lookup.php
vendored
Executable file
17
vendor/swoole/examples/async/dns_lookup.php
vendored
Executable file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
swoole_async_set(array(
|
||||
//使用纯异步IO
|
||||
'use_async_resolver' => true,
|
||||
'disable_dns_cache' => true,
|
||||
'dns_lookup_random' => true,
|
||||
'dns_server' => '114.114.114.114',
|
||||
));
|
||||
swoole_async_dns_lookup("www.sina.com.cn", function ($host, $ip)
|
||||
{
|
||||
echo "{$host} reslove to {$ip}\n";
|
||||
});
|
||||
|
||||
swoole_async_dns_lookup("www.baidu.com", function ($host, $ip)
|
||||
{
|
||||
echo "{$host} reslove to {$ip}\n";
|
||||
});
|
6
vendor/swoole/examples/async/exec.php
vendored
Executable file
6
vendor/swoole/examples/async/exec.php
vendored
Executable file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
$pid = Swoole\Async::exec("ps aux", function ($result, $status) {
|
||||
var_dump(strlen($result), $status);
|
||||
});
|
||||
|
||||
var_dump($pid);
|
19
vendor/swoole/examples/async/read.php
vendored
Executable file
19
vendor/swoole/examples/async/read.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
//ini_set("swoole.aio_mode", 1);
|
||||
swoole_async_read(
|
||||
__DIR__ . '/data.txt',
|
||||
function ($filename, $content)
|
||||
{
|
||||
echo "file: $filename\ncontent-length: " . strlen($content) . "\nContent: $content\n";
|
||||
if (empty($content))
|
||||
{
|
||||
echo "file is end.\n";
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
},
|
||||
8192
|
||||
);
|
10
vendor/swoole/examples/async/readfile.php
vendored
Executable file
10
vendor/swoole/examples/async/readfile.php
vendored
Executable file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
swoole_async::set(['aio_mode' => SWOOLE_AIO_LINUX]);
|
||||
|
||||
swoole_async_readfile(__DIR__.'/../test.jpg', function($filename, $content){
|
||||
echo "file: $filename\ncontent-length: ".strlen($content)."\nContent:\n";
|
||||
swoole_async_writefile(__DIR__.'/test.copy', $content, function($write_file) {
|
||||
echo "file: $write_file\n";
|
||||
swoole_event_exit();
|
||||
});
|
||||
});
|
12
vendor/swoole/examples/async/write.php
vendored
Executable file
12
vendor/swoole/examples/async/write.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
function write_callback($file, $writen)
|
||||
{
|
||||
echo "write $file [$writen]\n";
|
||||
//return true: write contine. return false: close the file.
|
||||
return true;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < 10; $i++)
|
||||
{
|
||||
swoole_async_write("data.txt", str_repeat('A', 10) . "\n", -1, "write_callback");
|
||||
}
|
5
vendor/swoole/examples/async/writefile.php
vendored
Executable file
5
vendor/swoole/examples/async/writefile.php
vendored
Executable file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
use Swoole\Async;
|
||||
|
||||
//Async::set(array('aio_mode' => SWOOLE_AIO_LINUX));
|
||||
Async::writeFile(__DIR__.'/data2.txt', str_repeat('C', 1023)."\n", null, FILE_APPEND);
|
8
vendor/swoole/examples/atomic/long.php
vendored
Executable file
8
vendor/swoole/examples/atomic/long.php
vendored
Executable file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
$l = new Swoole\Atomic\Long( -2 ** 36);
|
||||
echo $l->get()."\n";
|
||||
echo $l->add(20)."\n";
|
||||
echo $l->sub(20)."\n";
|
||||
echo $l->sub(-20)."\n";
|
||||
echo $l->cmpset(-2 ** 36, 0)."\n";
|
||||
echo $l->cmpset(-2 ** 36 + 20, 0)."\n";
|
7
vendor/swoole/examples/atomic/test.php
vendored
Executable file
7
vendor/swoole/examples/atomic/test.php
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
$atomic = new swoole_atomic(123);
|
||||
echo $atomic->add(12)."\n";
|
||||
echo $atomic->sub(11)."\n";
|
||||
echo $atomic->cmpset(122, 999)."\n";
|
||||
echo $atomic->cmpset(124, 999)."\n";
|
||||
echo $atomic->get()."\n";
|
16
vendor/swoole/examples/atomic/wait.php
vendored
Executable file
16
vendor/swoole/examples/atomic/wait.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
$n = new swoole_atomic(0);
|
||||
|
||||
if (pcntl_fork() > 0)
|
||||
{
|
||||
echo "master start\n";
|
||||
$n->wait(1.5);
|
||||
echo "master end\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "child start\n";
|
||||
sleep(1);
|
||||
$n->wakeup();
|
||||
echo "child end\n";
|
||||
}
|
15
vendor/swoole/examples/buffer.php
vendored
Executable file
15
vendor/swoole/examples/buffer.php
vendored
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
$buffer = new swoole_buffer;
|
||||
$buffer->append(str_repeat("A", 10));
|
||||
$buffer->append(str_repeat("B", 20));
|
||||
$buffer->append(str_repeat("C", 30));
|
||||
|
||||
var_dump($buffer);
|
||||
echo $buffer->substr(0, 10, true)."\n";
|
||||
echo $buffer->substr(0, 20, true)."\n";
|
||||
echo $buffer->substr(0, 30)."\n";
|
||||
$buffer->clear();
|
||||
|
||||
echo $buffer->substr(0, 10, true)."\n";
|
||||
var_dump($buffer);
|
||||
sleep(1);
|
29
vendor/swoole/examples/c10k.php
vendored
Executable file
29
vendor/swoole/examples/c10k.php
vendored
Executable file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
$clients = array();
|
||||
for($j = 0; $j < 2; $j++)
|
||||
{
|
||||
$pid = pcntl_fork();
|
||||
if($pid > 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
for($i = 0; $i < 9999; $i++){
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
$ret = $client->connect('127.0.0.1', 9501, 0.5);
|
||||
if(!$ret)
|
||||
{
|
||||
echo "#$i\tConnect fail. errno=".$client->errCode;
|
||||
die("\n");
|
||||
}
|
||||
$clients[] = $client;
|
||||
usleep(10);
|
||||
}
|
||||
echo "Worker #".posix_getpid()." connect $i finish\n";
|
||||
sleep(1000);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
sleep(1000);
|
||||
|
45
vendor/swoole/examples/channel.php
vendored
Executable file
45
vendor/swoole/examples/channel.php
vendored
Executable file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
$chan = new Swoole\Channel(1024 * 256);
|
||||
$n = 100000;
|
||||
$bytes = 0;
|
||||
|
||||
if (pcntl_fork() > 0)
|
||||
{
|
||||
echo "Father\n";
|
||||
for ($i = 0; $i < $n; $i++)
|
||||
{
|
||||
$data = str_repeat('A', rand(100, 200));
|
||||
if ($chan->push($data) === false)
|
||||
{
|
||||
echo "channel full\n";
|
||||
usleep(1000);
|
||||
$i--;
|
||||
continue;
|
||||
}
|
||||
$bytes += strlen($data);
|
||||
// echo "#$i\tpush ".strlen($data)." bytes\n";
|
||||
}
|
||||
|
||||
echo "total push bytes: $bytes\n";
|
||||
var_dump($chan->stats());
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "Child\n";
|
||||
for ($i = 0; $i < $n; $i++)
|
||||
{
|
||||
$data = $chan->pop();
|
||||
if ($data === false)
|
||||
{
|
||||
echo "channel empty\n";
|
||||
usleep(1000);
|
||||
$i--;
|
||||
continue;
|
||||
}
|
||||
$bytes += strlen($data);
|
||||
// echo "#$i\tpop " . strlen($data) . " bytes\n";
|
||||
}
|
||||
echo "total pop bytes: $bytes\n";
|
||||
var_dump($chan->stats());
|
||||
}
|
||||
|
35
vendor/swoole/examples/channel/leader_follower.php
vendored
Executable file
35
vendor/swoole/examples/channel/leader_follower.php
vendored
Executable file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
use Swoole\Timer;
|
||||
use Swoole\Process;
|
||||
use Swoole\Channel;
|
||||
|
||||
$chan = new Channel(1024 * 256);
|
||||
|
||||
$worker_num = 4;
|
||||
$workers = array();
|
||||
|
||||
for ($i = 0; $i < $worker_num; $i++)
|
||||
{
|
||||
$process = new Process(function ($worker) use ($chan, $i)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
$data = $chan->pop();
|
||||
if (empty($data))
|
||||
{
|
||||
usleep(200000);
|
||||
continue;
|
||||
}
|
||||
echo "worker#$i\t$data\n";
|
||||
}
|
||||
}, false);
|
||||
$process->id = $i;
|
||||
$pid = $process->start();
|
||||
$workers[$pid] = $process;
|
||||
}
|
||||
|
||||
Timer::tick(2000, function () use ($chan)
|
||||
{
|
||||
static $index = 0;
|
||||
$chan->push("hello-" . $index++);
|
||||
});
|
53
vendor/swoole/examples/client/async.php
vendored
Executable file
53
vendor/swoole/examples/client/async.php
vendored
Executable file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞
|
||||
//$client->set(array(
|
||||
// 'socket_buffer_size' => 1024 * 1024 * 2,
|
||||
// 'open_eof_check' => true,
|
||||
// 'package_eof' => "\r\n\r\n",
|
||||
//));
|
||||
|
||||
$client->_count = 0;
|
||||
$client->on("connect", function(swoole_client $cli) {
|
||||
//swoole_timer_clear($cli->timer);
|
||||
$cli->send("GET / HTTP/1.1\r\n\r\n");
|
||||
//$cli->sendfile(__DIR__.'/test.txt');
|
||||
//$cli->_count = 0;
|
||||
});
|
||||
|
||||
$client->on("receive", function(swoole_client $cli, $data){
|
||||
echo "Receive: $data";
|
||||
$cli->_count++;
|
||||
if ($cli->_count > 5)
|
||||
{
|
||||
//睡眠模式,不再接收新的数据
|
||||
echo "count=10, sleep(5000ms)\n";
|
||||
$cli->sleep();
|
||||
$cli->_count = 0;
|
||||
swoole_timer_after(5000, function() use ($cli) {
|
||||
//唤醒
|
||||
$cli->wakeup();
|
||||
});
|
||||
//$cli->close();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
$cli->send(str_repeat('A', 100)."\n");
|
||||
}
|
||||
});
|
||||
|
||||
$client->on("error", function(swoole_client $cli){
|
||||
echo "error\n";
|
||||
});
|
||||
|
||||
$client->on("close", function(swoole_client $cli){
|
||||
echo "Connection close\n";
|
||||
});
|
||||
|
||||
$client->connect('127.0.0.1', 9501);
|
||||
//$client->timer = swoole_timer_after(1000, function () use ($client) {
|
||||
// echo "socket timeout\n";
|
||||
// $client->close();
|
||||
//});
|
||||
|
||||
//echo "connect to 127.0.0.1:9501\n";
|
34
vendor/swoole/examples/client/get_socket.php
vendored
Executable file
34
vendor/swoole/examples/client/get_socket.php
vendored
Executable file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
function getClient()
|
||||
{
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP);
|
||||
if (!$client->connect('127.0.0.1', 9501, -1))
|
||||
{
|
||||
exit("connect failed. Error: {$client->errCode}\n");
|
||||
}
|
||||
|
||||
$res = $client->getSocket();
|
||||
return $client;
|
||||
}
|
||||
|
||||
$client = getClient();
|
||||
|
||||
$count = 0;
|
||||
//$client->set(array('open_eof_check' => true, 'package_eof' => "\r\n\r\n"));
|
||||
|
||||
//$client = new swoole_client(SWOOLE_SOCK_UNIX_DGRAM, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
//if (!$client->connect(dirname(__DIR__).'/server/svr.sock', 0, -1, 1))
|
||||
|
||||
|
||||
var_dump($client->getsockname());
|
||||
$client->send("hello world\r\n\r\n");
|
||||
|
||||
//for($i=0; $i < 3; $i ++)
|
||||
{
|
||||
echo $client->recv();
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
$client->close();
|
||||
|
18
vendor/swoole/examples/client/long_tcp.php
vendored
Executable file
18
vendor/swoole/examples/client/long_tcp.php
vendored
Executable file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
for($i=0; $i < 100; $i++)
|
||||
{
|
||||
$client = new swoole_client(SWOOLE_TCP | SWOOLE_KEEP);
|
||||
if(!$client->connect('127.0.0.1', 9501))
|
||||
{
|
||||
exit("connect failed\n");
|
||||
}
|
||||
$client->send(str_repeat("A", 600));
|
||||
$data = $client->recv(7000, 0);
|
||||
if($data === false)
|
||||
{
|
||||
echo "recv fail\n";
|
||||
break;
|
||||
}
|
||||
var_dump($data);
|
||||
$client->close();
|
||||
}
|
32
vendor/swoole/examples/client/select.php
vendored
Executable file
32
vendor/swoole/examples/client/select.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
$clients = array();
|
||||
|
||||
for($i=0; $i< 20; $i++)
|
||||
{
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
$ret = $client->connect('127.0.0.1', 9501, 0.5, 0);
|
||||
if(!$ret)
|
||||
{
|
||||
echo "Connect Server fail.errCode=".$client->errCode;
|
||||
}
|
||||
else
|
||||
{
|
||||
$client->send("HELLO WORLD\n");
|
||||
$clients[$client->sock] = $client;
|
||||
}
|
||||
}
|
||||
|
||||
while (!empty($clients))
|
||||
{
|
||||
$write = $error = array();
|
||||
$read = array_values($clients);
|
||||
$n = swoole_client_select($read, $write, $error, 0.6);
|
||||
if ($n > 0)
|
||||
{
|
||||
foreach ($read as $index => $c)
|
||||
{
|
||||
echo "Recv #{$c->sock}: " . $c->recv() . "\n";
|
||||
unset($clients[$c->sock]);
|
||||
}
|
||||
}
|
||||
}
|
30
vendor/swoole/examples/client/sync.php
vendored
Executable file
30
vendor/swoole/examples/client/sync.php
vendored
Executable file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP);
|
||||
$count = 0;
|
||||
//$client->set(array('open_eof_check' => true, 'package_eof' => "\r\n\r\n"));
|
||||
|
||||
//$client = new swoole_client(SWOOLE_SOCK_UNIX_DGRAM, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
//if (!$client->connect(dirname(__DIR__).'/server/svr.sock', 0, -1, 1))
|
||||
|
||||
do_connect:
|
||||
if (!$client->connect('127.0.0.1', 9501, -1))
|
||||
{
|
||||
exit("connect failed. Error: {$client->errCode}\n");
|
||||
}
|
||||
|
||||
var_dump($client->getsockname());
|
||||
$client->send("hello world\r\n\r\n");
|
||||
|
||||
//for($i=0; $i < 3; $i ++)
|
||||
{
|
||||
echo $client->recv();
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
$client->close();
|
||||
$count++;
|
||||
if ($count < 20)
|
||||
{
|
||||
goto do_connect;
|
||||
}
|
||||
|
10
vendor/swoole/examples/client/test.txt
vendored
Executable file
10
vendor/swoole/examples/client/test.txt
vendored
Executable file
@ -0,0 +1,10 @@
|
||||
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
|
||||
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
|
||||
XXXXXXXXXXXXXXXXXXXXXXx
|
||||
XXXXXXXXXXXXXXXXXXXXX
|
||||
XXXXXXXXXXXXXXXXXXXXXXX
|
||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
|
||||
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
|
||||
DDDDDDDDDDDDDDDDDDDDDDDDDDD
|
||||
|
23
vendor/swoole/examples/client/udp_async.php
vendored
Executable file
23
vendor/swoole/examples/client/udp_async.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$client = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC); //异步非阻塞
|
||||
|
||||
$client->on("connect", function(swoole_client $cli) {
|
||||
echo "connected\n";
|
||||
$cli->send("hello world\n");
|
||||
});
|
||||
|
||||
$client->on('close', function($cli){
|
||||
echo "closed\n";
|
||||
});
|
||||
|
||||
$client->on('error', function($cli){
|
||||
echo "error\n";
|
||||
});
|
||||
|
||||
$client->on("receive", function(swoole_client $cli, $data){
|
||||
echo "received: $data\n";
|
||||
sleep(1);
|
||||
$cli->send("hello_".rand(1000,9999));
|
||||
});
|
||||
|
||||
$client->connect('127.0.0.1', 9502, 0.5);
|
12
vendor/swoole/examples/client/udp_sync.php
vendored
Executable file
12
vendor/swoole/examples/client/udp_sync.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
$client = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_SYNC);
|
||||
$client->connect('127.0.0.1', 9502);
|
||||
|
||||
for ($i = 0; $i < 100; $i++)
|
||||
{
|
||||
$client->send("admin");
|
||||
echo $client->recv()."\n";
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
|
21
vendor/swoole/examples/client2.php
vendored
Executable file
21
vendor/swoole/examples/client2.php
vendored
Executable file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
$clients = array();
|
||||
for($i = 0; $i < 1; $i++){
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
$ret = $client->connect('127.0.0.1', 9501, 0.5, 0);
|
||||
if(!$ret)
|
||||
{
|
||||
echo "Over flow. errno=".$client->errCode;
|
||||
die("\n");
|
||||
}
|
||||
$clients[] = $client;
|
||||
}
|
||||
sleep(1);
|
||||
while (1) {
|
||||
foreach ($clients as $client) {
|
||||
$client->send("sss");
|
||||
$data = $client->recv();
|
||||
var_dump($data);
|
||||
}
|
||||
sleep(1);
|
||||
}
|
117
vendor/swoole/examples/coroutine/TestHttpServ.php
vendored
Executable file
117
vendor/swoole/examples/coroutine/TestHttpServ.php
vendored
Executable file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: winterswang
|
||||
* @Date: 2015-06-18 16:45:09
|
||||
* @Last Modified by: winterswang
|
||||
* @Last Modified time: 2016-09-18 17:33:51
|
||||
*/
|
||||
|
||||
class TestHttpServer {
|
||||
|
||||
public $http;
|
||||
public $queue;
|
||||
public $setting = array();
|
||||
|
||||
/**
|
||||
* [__construct description]
|
||||
* @param array $setting [description]
|
||||
*/
|
||||
public function __construct(){
|
||||
|
||||
}
|
||||
|
||||
public function set($setting){
|
||||
|
||||
$this ->setting = $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* [init description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function init(){
|
||||
|
||||
if (!isset($this ->setting['host'])) {
|
||||
$this ->setting['host'] = '0.0.0.0';
|
||||
}
|
||||
if (!isset($this ->setting['port'])) {
|
||||
$this ->setting['port'] = '9999';
|
||||
}
|
||||
|
||||
$this ->http = new swoole_http_server($this ->setting['host'], $this ->setting['port']);
|
||||
$this ->http ->set($this ->setting);
|
||||
|
||||
$this ->http ->on('request', array($this, 'onRequest'));
|
||||
$this ->http ->on('close', array($this, 'onClose'));
|
||||
}
|
||||
|
||||
/**
|
||||
* [onRequest description]
|
||||
* @param [type] $request [description]
|
||||
* @param [type] $response [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function onRequest($request, $response){
|
||||
|
||||
// $udp = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC);
|
||||
// $udp->on("connect", function(swoole_client $cli) {
|
||||
// $cli->send("udp test");
|
||||
// });
|
||||
// $udp->on("receive", function(swoole_client $cli, $data)use($response){
|
||||
|
||||
$tcp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
|
||||
$tcp->on("connect", function(swoole_client $cli) {
|
||||
$cli->send("tcp test");
|
||||
});
|
||||
$tcp->on("receive", function(swoole_client $cli, $data)use($response){
|
||||
$response ->end("<h1> swoole response</h1>");
|
||||
});
|
||||
$tcp->on("close", function(swoole_client $cli){
|
||||
});
|
||||
$tcp->on("error", function(swoole_client $cli){
|
||||
});
|
||||
$tcp->connect('10.100.64.151', 9805);
|
||||
|
||||
// });
|
||||
// $udp->on("close", function(swoole_client $cli){
|
||||
// });
|
||||
// $udp->connect('10.100.65.222', 9906);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* [onClose description]
|
||||
* @param [type] $server [description]
|
||||
* @param [type] $fd [description]
|
||||
* @param [type] $from_id [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function onClose($server, $fd, $from_id){
|
||||
|
||||
//echo " on close fd = $fd from_id = $from_id \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* [start description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function start(){
|
||||
|
||||
$this ->init();
|
||||
$this ->http ->start();
|
||||
}
|
||||
}
|
||||
|
||||
$setting = array(
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 10005,
|
||||
'worker_num' => 4,
|
||||
'dispatch_mode' => 2, //固定分配请求到worker
|
||||
'reactor_num' => 4, //亲核
|
||||
'daemonize' => 1, //守护进程
|
||||
'backlog' => 128,
|
||||
'log_file' => '/data/log/test_http_server.log',
|
||||
);
|
||||
$th = new TestHttpServer();
|
||||
$th ->set($setting);
|
||||
$th ->start();
|
27
vendor/swoole/examples/coroutine/client_send_yield.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/client_send_yield.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$client->set(array(
|
||||
'socket_buffer_size' => 1024 * 512,
|
||||
));
|
||||
if (!$client->connect('127.0.0.1', 9501, -1))
|
||||
{
|
||||
exit("connect failed. Error: {$client->errCode}\n");
|
||||
}
|
||||
$length = 0;
|
||||
$size = 1024 * 64;
|
||||
while (true)
|
||||
{
|
||||
$ret = $client->send(str_repeat('A', $size));
|
||||
if ($ret == false)
|
||||
{
|
||||
var_dump($ret);
|
||||
break;
|
||||
}
|
||||
$length += $size;
|
||||
echo "send $length success\n";
|
||||
}
|
||||
var_dump($client->errCode);
|
||||
});
|
||||
|
||||
swoole_event_wait();
|
27
vendor/swoole/examples/coroutine/client_send_yield_server.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/client_send_yield_server.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
$socket = stream_socket_server("tcp://0.0.0.0:9501", $errno, $errstr);
|
||||
if (!$socket) {
|
||||
echo "$errstr ($errno)<br />\n";
|
||||
} else {
|
||||
while (true) {
|
||||
$conn = stream_socket_accept($socket);
|
||||
if (!$conn) {
|
||||
continue;
|
||||
}
|
||||
$i = 0;
|
||||
$length = 0;
|
||||
while(true) {
|
||||
$data = fread($conn, 8192);
|
||||
if ($data == false)
|
||||
{
|
||||
break;
|
||||
}
|
||||
$length += strlen($data);
|
||||
echo "recv " . $length . " bytes\n";
|
||||
usleep(100000);
|
||||
}
|
||||
fclose($conn);
|
||||
echo "closed\n";
|
||||
}
|
||||
fclose($socket);
|
||||
}
|
19
vendor/swoole/examples/coroutine/coro_array_map.php
vendored
Executable file
19
vendor/swoole/examples/coroutine/coro_array_map.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function() {
|
||||
array_map("test",array("func param\n"));
|
||||
echo "co flow end\n";
|
||||
});
|
||||
|
||||
function test($p) {
|
||||
go(function() use ($p){
|
||||
echo $p;
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
echo "co resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "map func end \n";
|
||||
});
|
||||
}
|
||||
echo "main end\n";
|
20
vendor/swoole/examples/coroutine/coro_call_user.php
vendored
Executable file
20
vendor/swoole/examples/coroutine/coro_call_user.php
vendored
Executable file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function() {
|
||||
echo "co func start\n";
|
||||
$name = "call_user_func";
|
||||
$ret = $name("test","test\n");
|
||||
echo "co func end ret:{$ret}\n";
|
||||
});
|
||||
|
||||
function test($params)
|
||||
{
|
||||
echo "func params:$params";
|
||||
co::sleep(1);
|
||||
echo "func end\n";
|
||||
return "test return\n";
|
||||
}
|
||||
echo "main script last\n";
|
||||
|
25
vendor/swoole/examples/coroutine/coro_channel.php
vendored
Executable file
25
vendor/swoole/examples/coroutine/coro_channel.php
vendored
Executable file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
$http = new swoole_http_server("127.0.0.1", 9501, SWOOLE_BASE);
|
||||
$http->set(array(
|
||||
'log_file' => '/dev/null'
|
||||
));
|
||||
use Swoole\Coroutine as co;
|
||||
// $http->on("WorkerStart", function (\swoole_server $serv)
|
||||
// {
|
||||
//
|
||||
// });
|
||||
$http->on('request', function (swoole_http_request $request, swoole_http_response $response)
|
||||
{
|
||||
$ch = new co\Channel(1);
|
||||
$out = new co\Channel(1);
|
||||
Swoole\Coroutine::create(function() use ($ch, $out) {
|
||||
$out->push("OK");
|
||||
$out->push("OK");
|
||||
});
|
||||
$ret = $out->pop();
|
||||
var_dump($ret);
|
||||
$ret = $out->pop();
|
||||
var_dump($ret);
|
||||
$response->end("$ret\n");
|
||||
});
|
||||
$http->start();
|
28
vendor/swoole/examples/coroutine/coro_destruct.php
vendored
Executable file
28
vendor/swoole/examples/coroutine/coro_destruct.php
vendored
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
class T
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
function test()
|
||||
{
|
||||
echo "call function \n";
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
{
|
||||
go(function () {
|
||||
echo "coro start\n";
|
||||
co::sleep(1.0);
|
||||
echo "coro exit\n";
|
||||
});
|
||||
echo "111\n";
|
||||
}
|
||||
}
|
||||
|
||||
$t = new T();
|
||||
$t->test();
|
||||
echo "end \n";
|
32
vendor/swoole/examples/coroutine/coro_destuct.php
vendored
Executable file
32
vendor/swoole/examples/coroutine/coro_destuct.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
|
||||
class T
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
echo "call __construct \n";
|
||||
}
|
||||
|
||||
function test()
|
||||
{
|
||||
echo "call function \n";
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
{
|
||||
echo "call __destruct \n";
|
||||
go(function () {
|
||||
echo "co[1] start\n";
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
co::sleep(1.0);
|
||||
echo "co[1] resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "co[1] exit\n";
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$t = new T();
|
||||
$t->test();
|
||||
unset($t);
|
9
vendor/swoole/examples/coroutine/coro_empty.php
vendored
Executable file
9
vendor/swoole/examples/coroutine/coro_empty.php
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
// co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function () {
|
||||
echo "no coro exit\n";
|
||||
});
|
||||
echo "exec file end\n";
|
||||
|
11
vendor/swoole/examples/coroutine/coro_gethost.php
vendored
Executable file
11
vendor/swoole/examples/coroutine/coro_gethost.php
vendored
Executable file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function () {
|
||||
$ip = co::gethostbyname('www.baidu.com');
|
||||
var_dump($ip);
|
||||
});
|
||||
echo "111\n";
|
||||
|
||||
echo "222\n";
|
9
vendor/swoole/examples/coroutine/coro_include.php
vendored
Executable file
9
vendor/swoole/examples/coroutine/coro_include.php
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
if (substr(PHP_OS, 0, 3) == 'WIN')
|
||||
{
|
||||
exit("skip for Windows");
|
||||
}
|
||||
if (!extension_loaded("swoole"))
|
||||
{
|
||||
exit("swoole extension is required");
|
||||
}
|
22
vendor/swoole/examples/coroutine/coro_invoke.php
vendored
Executable file
22
vendor/swoole/examples/coroutine/coro_invoke.php
vendored
Executable file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function() {
|
||||
|
||||
|
||||
$function = new ReflectionFunction('title');
|
||||
|
||||
$function->invoke();
|
||||
echo "invoke444\n";
|
||||
|
||||
});
|
||||
|
||||
function title() {
|
||||
echo "333invoke_________________________________\n";
|
||||
$tcpclient = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
var_dump($tcpclient->connect('127.0.0.1', 9501, 1));
|
||||
|
||||
}
|
||||
|
||||
echo "111\n";
|
18
vendor/swoole/examples/coroutine/coro_nested.php
vendored
Executable file
18
vendor/swoole/examples/coroutine/coro_nested.php
vendored
Executable file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
echo "before coro\n";
|
||||
go(function () {
|
||||
echo "co[1] start\n";
|
||||
go(function () {
|
||||
echo "co[2] start\n";
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
echo "co[2] resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "co[2] exit\n";
|
||||
});
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
echo "co[1] resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "co[1] exit\n";
|
||||
});
|
||||
echo "out coro \n";
|
16
vendor/swoole/examples/coroutine/coro_nested_empty.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/coro_nested_empty.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
function test()
|
||||
{
|
||||
echo "before coro\n";
|
||||
go(function () {
|
||||
echo "co[1] start\n";
|
||||
go(function () {
|
||||
echo "co[2] start\n";
|
||||
echo "co[2] exit\n";
|
||||
});
|
||||
echo "co[1] exit\n";
|
||||
});
|
||||
echo "func end \n";
|
||||
}
|
||||
test();
|
39
vendor/swoole/examples/coroutine/coro_serialize.php
vendored
Executable file
39
vendor/swoole/examples/coroutine/coro_serialize.php
vendored
Executable file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
class Obj {
|
||||
public $a;
|
||||
protected $b;
|
||||
private $c;
|
||||
var $d;
|
||||
|
||||
function __construct($a, $b, $c, $d) {
|
||||
$this->a = $a;
|
||||
$this->b = $b;
|
||||
$this->c = $c;
|
||||
$this->d = $d;
|
||||
}
|
||||
|
||||
function __sleep() {
|
||||
// co::sleep(0.5);
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 10);
|
||||
var_dump($res);
|
||||
if ($res)
|
||||
{
|
||||
echo("connect success. Error: {$client->errCode}\n");
|
||||
}
|
||||
echo "sleep\n";
|
||||
return array('a', 'b', 'c');
|
||||
}
|
||||
|
||||
// function __wakeup() {
|
||||
// $this->d = $this->a + $this->b + $this->c;
|
||||
// }
|
||||
}
|
||||
$o = new Obj(1, 2, 3, 4);
|
||||
co::create(function() use($o) {
|
||||
$serialized = serialize($o);
|
||||
$unserialized = unserialize($serialized);
|
||||
echo "res:".var_export($unserialized,1)."\n";
|
||||
echo "call user\n";
|
||||
});
|
9
vendor/swoole/examples/coroutine/coro_sleep.php
vendored
Executable file
9
vendor/swoole/examples/coroutine/coro_sleep.php
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
// require __DIR__ . "/coro_include.php";
|
||||
use Swoole\Coroutine as co;
|
||||
co::create(function () {
|
||||
echo "start\n";
|
||||
co::sleep(0.5);
|
||||
echo "OK\n";
|
||||
});
|
||||
echo "11\n";
|
19
vendor/swoole/examples/coroutine/coro_stackless.php
vendored
Executable file
19
vendor/swoole/examples/coroutine/coro_stackless.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
echo "start\n";
|
||||
co::create(function () {
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
var_dump($res);
|
||||
if ($res) {
|
||||
echo ("connect success. Error: {$client->errCode}\n");
|
||||
}
|
||||
|
||||
$res = $client->send("hello");
|
||||
echo "send res:" . var_export($res, 1) . "\n";
|
||||
$data = $client->recv();
|
||||
echo "recv data" . var_export($data, 1) . "\n";
|
||||
});
|
||||
echo "end\n";
|
16
vendor/swoole/examples/coroutine/coro_util.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/coro_util.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$id = go(function(){
|
||||
$id = co::getUid();
|
||||
echo "start coro $id\n";
|
||||
co::suspend($id);
|
||||
echo "resume coro $id @1\n";
|
||||
co::suspend($id);
|
||||
echo "resume coro $id @2\n";
|
||||
});
|
||||
echo "start to resume $id @1\n";
|
||||
co::resume($id);
|
||||
echo "start to resume $id @2\n";
|
||||
co::resume($id);
|
||||
echo "main\n";
|
79
vendor/swoole/examples/coroutine/defer_client.php
vendored
Executable file
79
vendor/swoole/examples/coroutine/defer_client.php
vendored
Executable file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/* new multi implement test */
|
||||
$server = new Swoole\Http\Server("127.0.0.1", 9502, SWOOLE_BASE);
|
||||
|
||||
$server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
$server->on('Request', function ($request, $response) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$redis->setDefer(true);
|
||||
$redis->get('key');
|
||||
$res = $redis->get('key');//get false
|
||||
var_dump($res);
|
||||
|
||||
var_dump($redis->setDefer());//get true
|
||||
var_dump($redis->setDefer(false));//get false
|
||||
|
||||
//穿插其他client也能正常工作
|
||||
$redis_tmp = new Swoole\Coroutine\Redis();
|
||||
$res = $redis_tmp->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis_tmp->set('key_tmp', 'HaHa');//get true
|
||||
var_dump($res);
|
||||
|
||||
|
||||
$http_client= new Swoole\Coroutine\Http\Client('km.oa.com', 80);
|
||||
$http_client->setDefer();
|
||||
$http_client->get('/');
|
||||
|
||||
$mysql = new Swoole\Coroutine\MySQL();
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$mysql->setDefer(true);
|
||||
$mysql->query('select sleep(1)', 2);
|
||||
|
||||
$udp = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
|
||||
$res = $udp->connect("127.0.0.1", 9906, 2);
|
||||
$udp->send('Hello World!');
|
||||
|
||||
//穿插其他client也能正常工作
|
||||
$udp_tmp = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
|
||||
$res = $udp_tmp->connect("127.0.0.1", 9909, 2);//nonexistent server
|
||||
$res = $udp_tmp->recv();//get false with timeout
|
||||
var_dump($res);
|
||||
|
||||
$udp_res = $udp->recv();
|
||||
$res = $mysql->query('select sleep(1)', 2);//get false
|
||||
var_dump($res);
|
||||
$res = $mysql->setDefer(false);
|
||||
var_dump($res);//get false
|
||||
$res = $mysql->setDefer();
|
||||
var_dump($res);//get true
|
||||
$mysql_res = $mysql->recv();
|
||||
$res = $redis->get('key');//get false
|
||||
var_dump($res);
|
||||
$redis_res = $redis->recv();
|
||||
$res = $http_client->get('/');
|
||||
var_dump($res);//get false
|
||||
$res = $http_client->recv();
|
||||
var_dump($res);//get true
|
||||
|
||||
var_dump($udp_res, $mysql_res, $redis_res, $http_client);
|
||||
var_dump($http_client->setDefer(false));
|
||||
var_dump($mysql->getDefer(), $redis->getDefer(), $http_client->getDefer());
|
||||
$response->end('Test End');
|
||||
});
|
||||
$server->start();
|
27
vendor/swoole/examples/coroutine/enable_coroutine.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/enable_coroutine.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
|
||||
$http = new swoole_http_server('127.0.0.1', 9501);
|
||||
|
||||
$http->set([
|
||||
'enable_coroutine' => false, // close build-in coroutine
|
||||
]);
|
||||
|
||||
$http->on('workerStart', function () {
|
||||
echo "Coroutine is " . (Co::getuid() > 0 ? 'enable' : 'disable')."\n";
|
||||
});
|
||||
|
||||
$http->on("request", function (Request $request, Response $response) {
|
||||
$response->header("Content-Type", "text/plain");
|
||||
if ($request->server['request_uri'] == '/co') {
|
||||
go(function () use ($response) {
|
||||
$response->end("Hello Coroutine #" . Co::getuid());
|
||||
});
|
||||
} else {
|
||||
$response->end("Hello Swoole #" . Co::getuid());
|
||||
}
|
||||
});
|
||||
|
||||
$http->start();
|
16
vendor/swoole/examples/coroutine/exception/empty.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/exception/empty.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
go(function () {
|
||||
try {
|
||||
echo "before\n";
|
||||
co::sleep(0.5);
|
||||
echo "after\n";
|
||||
throw new Exception('coro Exception.');
|
||||
} catch (Exception $e) {
|
||||
echo 'Caught exception: ', $e->getMessage(), "\n";
|
||||
} finally {
|
||||
echo "First finally.\n";
|
||||
}
|
||||
});
|
||||
echo "exec file end\n";
|
||||
|
||||
|
20
vendor/swoole/examples/coroutine/fgets.php
vendored
Executable file
20
vendor/swoole/examples/coroutine/fgets.php
vendored
Executable file
@ -0,0 +1,20 @@
|
||||
<?Php
|
||||
$fp = fopen(__DIR__ . "/defer_client.php", "r");
|
||||
stream_set_chunk_size($fp, 1024);
|
||||
|
||||
go(function () use ($fp)
|
||||
{
|
||||
for($i = 0; $i<100;$i++) {
|
||||
$r = co::fgets($fp);
|
||||
if (empty($r) and feof($fp))
|
||||
{
|
||||
//echo "EOF\n";
|
||||
break;
|
||||
}
|
||||
//echo "len=".strlen($r)."\n";
|
||||
echo $r;
|
||||
//echo "---------------------------------------\n";
|
||||
//var_dump($r);
|
||||
//co::sleep(1);
|
||||
}
|
||||
});
|
11
vendor/swoole/examples/coroutine/fread.php
vendored
Executable file
11
vendor/swoole/examples/coroutine/fread.php
vendored
Executable file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$fp = fopen(__DIR__ . "/defer_client.php", "r");
|
||||
|
||||
co::create(function () use ($fp)
|
||||
{
|
||||
fseek($fp, 256);
|
||||
$r = co::fread($fp);
|
||||
var_dump($r);
|
||||
});
|
10
vendor/swoole/examples/coroutine/fwrite.php
vendored
Executable file
10
vendor/swoole/examples/coroutine/fwrite.php
vendored
Executable file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$fp = fopen(__DIR__ . "/test.data", "a+");
|
||||
|
||||
co::create(function () use ($fp)
|
||||
{
|
||||
$r = co::fwrite($fp, "hello world\n", 5);
|
||||
var_dump($r);
|
||||
});
|
8
vendor/swoole/examples/coroutine/gethostbyname.php
vendored
Executable file
8
vendor/swoole/examples/coroutine/gethostbyname.php
vendored
Executable file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function() {
|
||||
$ip = co::gethostbyname("www.baidu.com");
|
||||
echo "IP: $ip\n";
|
||||
});
|
||||
|
71
vendor/swoole/examples/coroutine/http2_client.php
vendored
Executable file
71
vendor/swoole/examples/coroutine/http2_client.php
vendored
Executable file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
//const TEST = array('get', 'post', 'pipeline');
|
||||
const TEST = array('pipeline');
|
||||
|
||||
co::create(function () use ($fp)
|
||||
{
|
||||
$cli = new co\Http2\Client('127.0.0.1', 9518);
|
||||
|
||||
$cli->set([ 'timeout' => 1]);
|
||||
var_dump($cli->connect());
|
||||
|
||||
if (in_array('get', TEST))
|
||||
{
|
||||
$req = new co\Http2\Request;
|
||||
$req->path = "/index.html";
|
||||
$req->headers = [
|
||||
'host' => "localhost",
|
||||
"user-agent" => 'Chrome/49.0.2587.3',
|
||||
'accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'accept-encoding' => 'gzip',
|
||||
];
|
||||
$req->cookies = ['name' => 'rango', 'email' => '1234@qq.com'];
|
||||
var_dump($cli->send($req));
|
||||
|
||||
$resp = $cli->recv();
|
||||
var_dump($resp);
|
||||
}
|
||||
|
||||
if (in_array('post', TEST))
|
||||
{
|
||||
$req2 = new co\Http2\Request;
|
||||
$req2->path = "/index.php";
|
||||
$req2->headers = [
|
||||
'host' => "localhost",
|
||||
"user-agent" => 'Chrome/49.0.2587.3',
|
||||
'accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'accept-encoding' => 'gzip',
|
||||
];
|
||||
$req2->data = "hello world\n";
|
||||
var_dump($cli->send($req2));
|
||||
|
||||
$resp = $cli->recv();
|
||||
var_dump($resp);
|
||||
}
|
||||
|
||||
if (in_array('pipeline', TEST))
|
||||
{
|
||||
$req3 = new co\Http2\Request;
|
||||
$req3->path = "/index.php";
|
||||
$req3->headers = [
|
||||
'host' => "localhost",
|
||||
"user-agent" => 'Chrome/49.0.2587.3',
|
||||
'accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'accept-encoding' => 'gzip',
|
||||
];
|
||||
$req3->pipeline = true;
|
||||
$req3->method = "POST";
|
||||
$streamId = $cli->send($req3);
|
||||
|
||||
$cli->write($streamId, ['int' => rand(1000, 9999)]);
|
||||
$cli->write($streamId, ['int' => rand(1000, 9999)]);
|
||||
//end stream
|
||||
$cli->write($streamId, ['int' => rand(1000, 9999), 'end' => true], true);
|
||||
|
||||
var_dump($cli->recv());
|
||||
}
|
||||
|
||||
// $cli->close();
|
||||
});
|
43
vendor/swoole/examples/coroutine/http_backend_serv.php
vendored
Executable file
43
vendor/swoole/examples/coroutine/http_backend_serv.php
vendored
Executable file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: syyuanyizhi@163.com
|
||||
connect refuse: errorCode 111
|
||||
I/O timeout:errorCode 110
|
||||
http 9510
|
||||
tcp 9511
|
||||
|
||||
*/
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new Swoole\Http\Server("0.0.0.0", 9510);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
'daemonize' => true,
|
||||
'log_file' => '/data/markyuan/swoole.log',
|
||||
]);
|
||||
$this->server->on('Request', ['Server', 'onRequest']);
|
||||
$this->server->start();
|
||||
}
|
||||
public static function onRequest($request, $response)
|
||||
{
|
||||
|
||||
$response->end('xxxx');
|
||||
}
|
||||
|
||||
|
||||
public static function staticFunc()
|
||||
{
|
||||
echo "in static function";
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
|
||||
$server->run();
|
||||
|
||||
|
||||
|
12
vendor/swoole/examples/coroutine/http_client.php
vendored
Executable file
12
vendor/swoole/examples/coroutine/http_client.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::create(function () {
|
||||
$cli = new co\http\client('127.0.0.1', 9501);
|
||||
$cli->setHeaders(['Host' => 'localhost']);
|
||||
$cli->set(['http_proxy_host' => HTTP_PROXY_HOST, 'http_proxy_port' => HTTP_PROXY_PORT]);
|
||||
$result = $cli->get('/get?json=true');
|
||||
var_dump($cli->body);
|
||||
// assert($result);
|
||||
// $ret = json_decode($cli->body, true);
|
||||
// assert(is_array($ret) and $ret['json'] == 'true');
|
||||
});
|
13
vendor/swoole/examples/coroutine/http_download.php
vendored
Executable file
13
vendor/swoole/examples/coroutine/http_download.php
vendored
Executable file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$host = 'www.swoole.com';
|
||||
$cli = new \Swoole\Coroutine\Http\Client($host, 443, true);
|
||||
$cli->set(['timeout' => -1]);
|
||||
$cli->setHeaders([
|
||||
'Host' => $host,
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
'Accept' => '*',
|
||||
'Accept-Encoding' => 'gzip'
|
||||
]);
|
||||
$cli->download('/static/files/swoole-logo.svg', __DIR__ . '/logo.svg');
|
||||
});
|
37
vendor/swoole/examples/coroutine/http_server.php
vendored
Executable file
37
vendor/swoole/examples/coroutine/http_server.php
vendored
Executable file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
ini_set("memory_limit","512M");
|
||||
use Swoole\Coroutine as co;
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
public $redisPool = [];
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new Swoole\Http\Server("0.0.0.0", 9502, SWOOLE_BASE);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
// $this->server->on('Connect', [$this, 'onConnect']);
|
||||
$this->server->on('Request', [$this, 'onRequest']);
|
||||
// $this->server->on('Close', [$this, 'onClose']);
|
||||
$this->server->set(['trace_flags' => 1 << 15, 'log_level' => 0]);
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
public function onRequest($request, $response)
|
||||
{
|
||||
$fd = $request->fd;
|
||||
co::create(function () {
|
||||
co::sleep(0.1);
|
||||
});
|
||||
$response->end(111);
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
Swoole\Coroutine::set(array(
|
||||
'max_coroutine' => 1000,
|
||||
));
|
||||
$server->run();
|
165
vendor/swoole/examples/coroutine/httpmulti.php
vendored
Executable file
165
vendor/swoole/examples/coroutine/httpmulti.php
vendored
Executable file
@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: syyuanyizhi@163.com
|
||||
connect refuse: errorCode 111
|
||||
I/O timeout:errorCode 110
|
||||
http 9510
|
||||
tcp 9511
|
||||
|
||||
*/
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new Swoole\Http\Server("0.0.0.0", 9508);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
'daemonize' => true,
|
||||
'log_file' => '/data/markyuan/swoole.log',
|
||||
]);
|
||||
$this->server->on('Request', ['Server', 'onRequest']);
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
private static function https(){
|
||||
//--enable-openssl
|
||||
for($i=0;$i<2;$i++){
|
||||
$cli = new Swoole\Coroutine\Http\Client('0.0.0.0',443,TRUE );
|
||||
$cli->set([ 'timeout' => 1]);
|
||||
$cli->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
'Accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'Accept-Encoding' => 'gzip',
|
||||
]);
|
||||
$ret = ($cli->get('/cgi-bin/token?appid=3333&secret=222'.$i.$i.$i.$i.$i));
|
||||
error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$cli->close();
|
||||
}
|
||||
}
|
||||
|
||||
private static function http(){
|
||||
error_log(__LINE__.'---------- begin --- http --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
for($i=0;$i<2;$i++){
|
||||
$cli = new Swoole\Coroutine\Http\Client('0.0.0.0', 9510);
|
||||
$cli->set([ 'timeout' => 1]);
|
||||
$cli->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
'Accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'Accept-Encoding' => 'gzip',
|
||||
]);
|
||||
error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$ret = ($cli->get('/cn/token?appid=1FxxxxS9V'.$i.$i.$i.$i.$i));
|
||||
error_log(__LINE__.var_export($ret,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$cli->close();
|
||||
}
|
||||
error_log(__LINE__.'---------- end --- http --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
|
||||
}
|
||||
|
||||
private static function multihttp(){
|
||||
|
||||
error_log(__LINE__.'---------- begin --- multi --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
|
||||
$cliAA= new Swoole\Coroutine\Http\Client('0.0.0.0', 9510);
|
||||
$cliAA->set(['timeout' => 1]);
|
||||
$cliAA->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
]);
|
||||
$cliBB= new Swoole\Coroutine\Http\Client('0.0.0.0', 9510);
|
||||
$cliBB->set([ 'timeout' => 1]);//
|
||||
$cliBB->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
]);
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA=$cliAA->setDefer(1);
|
||||
$retBB=$cliBB->setDefer(1);
|
||||
error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA = ($cliAA->get('/cn/token?appid=AAA'));
|
||||
$retBB = ($cliBB->get('/cn/token?appid=BBB'));
|
||||
error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA=$cliAA->recv();
|
||||
$retBB=$cliBB->recv();
|
||||
error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA=$cliAA->close();
|
||||
$retBB=$cliBB->close();
|
||||
error_log(__LINE__.'---------- end --- multi --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static function tcp(){
|
||||
for($i=0;$i<2;$i++){
|
||||
$tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$ret = $tcp_cli ->connect("0.0.0.0", 9511);
|
||||
$ret = $tcp_cli ->send('test for the coro');
|
||||
$ret = $tcp_cli ->recv();
|
||||
$ret=$tcp_cli->close();
|
||||
}
|
||||
}
|
||||
|
||||
private static function coro_dns(){
|
||||
swoole_async_set(array('use_async_resolver'=>1));
|
||||
swoole_async_set(array('dns_cache_refresh_time'=>0));
|
||||
$ret=swoole_async_dns_lookup_coro("www.baidu.com",0.5);
|
||||
error_log(' ip and host '.$host.print_r($ret,true),'3','/home/yuanyizhi/markyuan/markyuan.log');
|
||||
return $ret;
|
||||
// swoole_async_dns_lookup("www.baidu.com", function($host, $ip){
|
||||
// error_log(' ip and host '.$host.' and ip '.$ip,'3','/home/yuanyizhi/markyuan/markyuan.log');
|
||||
// });
|
||||
}
|
||||
|
||||
|
||||
private static function tcpmulti(){
|
||||
$cliAA = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$cliBB = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$retAA = $cliAA ->connect("0.0.0.0", 9511);
|
||||
$retBB = $cliBB ->connect("0.0.0.0", 9511);
|
||||
$retAA = $cliAA ->send('test for the coro');
|
||||
$retBB = $cliBB ->send('test for the coro');
|
||||
$retAA = $cliAA->recv();
|
||||
$retBB = $cliBB->recv();
|
||||
$cliAA->close();
|
||||
$cliBB->close();
|
||||
}
|
||||
|
||||
public static function onRequest($request, $response)
|
||||
{
|
||||
// self::multihttp();
|
||||
// self::http();
|
||||
//self::https();
|
||||
// self::tcp();
|
||||
// self::tcpmulti();
|
||||
$ret=self::coro_dns();
|
||||
$response->end(print_r($ret,true));
|
||||
}
|
||||
|
||||
|
||||
public static function staticFunc()
|
||||
{
|
||||
echo "in static function";
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
|
||||
$server->run();
|
||||
|
||||
|
||||
|
32
vendor/swoole/examples/coroutine/mysql_chan.php
vendored
Executable file
32
vendor/swoole/examples/coroutine/mysql_chan.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$chan = new chan(4);
|
||||
|
||||
go(function () use ($chan) {
|
||||
|
||||
$db = new co\MySQL();
|
||||
$server = array(
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
);
|
||||
|
||||
echo "connect\n";
|
||||
$ret1 = $db->connect($server);
|
||||
var_dump($ret1);
|
||||
|
||||
echo "prepare\n";
|
||||
$ret2 = $db->query('SELECT * FROM userinfo WHERE id=3');
|
||||
var_dump($ret2);
|
||||
|
||||
$chan->push($db);
|
||||
});
|
||||
|
||||
go(function () use ($chan) {
|
||||
$db = $chan->pop();
|
||||
$ret2 = $db->query('SELECT * FROM userinfo WHERE id=3');
|
||||
var_dump($ret2);
|
||||
});
|
14
vendor/swoole/examples/coroutine/mysql_execute_empty.php
vendored
Executable file
14
vendor/swoole/examples/coroutine/mysql_execute_empty.php
vendored
Executable file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$db = new Swoole\Coroutine\Mysql;
|
||||
$server = [
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test'
|
||||
];
|
||||
$db->connect($server);
|
||||
$stmt = $db->prepare('SELECT * FROM `userinfo`');
|
||||
$ret = $stmt->execute();
|
||||
var_dump($ret);
|
||||
});
|
42
vendor/swoole/examples/coroutine/mysql_prepare.php
vendored
Executable file
42
vendor/swoole/examples/coroutine/mysql_prepare.php
vendored
Executable file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function() {
|
||||
|
||||
$db = new co\MySQL();
|
||||
$server = array(
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
);
|
||||
|
||||
echo "connect\n";
|
||||
$ret1 = $db->connect($server);
|
||||
var_dump($ret1);
|
||||
|
||||
echo "prepare [1]\n";
|
||||
$stmt1 = $db->prepare('SELECT * FROM userinfo WHERE id=?');
|
||||
var_dump($stmt1);
|
||||
if ($stmt1 == false)
|
||||
{
|
||||
var_dump($db->errno, $db->error);
|
||||
}
|
||||
|
||||
echo "execute\n";
|
||||
$ret3 = $stmt1->execute(array(10));
|
||||
var_dump(count($ret3));
|
||||
|
||||
echo "prepare [2]\n";
|
||||
$stmt2 = $db->prepare('SELECT * FROM userinfo WHERE id > ? and level > ?');
|
||||
var_dump($stmt2);
|
||||
if ($stmt2 == false)
|
||||
{
|
||||
var_dump($db->errno, $db->error);
|
||||
}
|
||||
|
||||
echo "execute\n";
|
||||
$ret4 = $stmt2->execute(array(10, 99));
|
||||
var_dump($ret4);
|
||||
});
|
||||
|
25
vendor/swoole/examples/coroutine/mysql_prepare_2.php
vendored
Executable file
25
vendor/swoole/examples/coroutine/mysql_prepare_2.php
vendored
Executable file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
|
||||
go(function() {
|
||||
|
||||
$db = new Co\MySQL();
|
||||
$server = array(
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
);
|
||||
|
||||
echo "connect\n";
|
||||
$ret1 = $db->connect($server);
|
||||
var_dump($ret1);
|
||||
|
||||
echo "prepare [1]\n";
|
||||
$stmt1 = $db->prepare('show tables');
|
||||
echo "execute\n";
|
||||
$ret1 = $stmt1->execute([]);
|
||||
var_dump($ret1);
|
||||
|
||||
});
|
||||
|
28
vendor/swoole/examples/coroutine/mysql_procedure_exec.php
vendored
Executable file
28
vendor/swoole/examples/coroutine/mysql_procedure_exec.php
vendored
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
go(function () {
|
||||
$db = new Swoole\Coroutine\Mysql;
|
||||
$server = [
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test'
|
||||
];
|
||||
|
||||
$clear = <<<SQL
|
||||
DROP PROCEDURE IF EXISTS `say`
|
||||
SQL;
|
||||
$procedure = <<<SQL
|
||||
CREATE DEFINER=`root`@`localhost` PROCEDURE `say`(content varchar(255))
|
||||
BEGIN
|
||||
SELECT concat('you said: \"', content, '\"');
|
||||
END
|
||||
SQL;
|
||||
|
||||
$db->connect($server);
|
||||
if ($db->query($clear) && $db->query($procedure)) {
|
||||
$stmt = $db->prepare('CALL say(?)');
|
||||
$ret = $stmt->execute(['hello mysql!']);
|
||||
var_dump(current($ret[0])); // you said: "hello mysql!"
|
||||
}
|
||||
});
|
26
vendor/swoole/examples/coroutine/mysql_query.php
vendored
Executable file
26
vendor/swoole/examples/coroutine/mysql_query.php
vendored
Executable file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function() {
|
||||
|
||||
|
||||
$function = new ReflectionFunction('title');
|
||||
|
||||
$function->invoke();
|
||||
echo "invoke444\n";
|
||||
|
||||
});
|
||||
|
||||
function title() {
|
||||
echo "333invoke_________________________________\n";
|
||||
$tcpclient = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
var_dump($tcpclient->connect('127.0.0.1', 9501, 1));
|
||||
|
||||
}
|
||||
|
||||
echo "111\n";
|
||||
|
||||
|
||||
echo "222\n";
|
||||
co::go();
|
15
vendor/swoole/examples/coroutine/mysql_unixsocket.php
vendored
Executable file
15
vendor/swoole/examples/coroutine/mysql_unixsocket.php
vendored
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
go(function(){
|
||||
$db = new Swoole\Coroutine\Mysql;
|
||||
$server = [
|
||||
'host' => 'unix:/tmp/mysql.sock',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test'
|
||||
];
|
||||
$db->connect($server);
|
||||
$stmt = $db->prepare('SELECT * FROM `user` WHERE id=?');
|
||||
$ret = $stmt->execute([1]);
|
||||
var_dump($ret);
|
||||
});
|
71
vendor/swoole/examples/coroutine/reconnect_test.php
vendored
Executable file
71
vendor/swoole/examples/coroutine/reconnect_test.php
vendored
Executable file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/* new multi implement test */
|
||||
$server = new Swoole\Http\Server("127.0.0.1", 9502, SWOOLE_BASE);
|
||||
|
||||
$server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
$server->on('Request', function ($request, $response) {
|
||||
|
||||
/*
|
||||
$mysql = new Swoole\Coroutine\MySQL();
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$mysql->close();
|
||||
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $mysql->query('select sleep(1)', 2);
|
||||
var_dump($res);
|
||||
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $mysql->query('select sleep(1)', 2);
|
||||
var_dump($res);
|
||||
*/
|
||||
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$redis->close();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis->get('key');
|
||||
var_dump($res);
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis->get('key');
|
||||
var_dump($res);
|
||||
|
||||
$response->end('Test End');
|
||||
});
|
||||
$server->start();
|
8
vendor/swoole/examples/coroutine/redis/auth.php
vendored
Executable file
8
vendor/swoole/examples/coroutine/redis/auth.php
vendored
Executable file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$redis = new Swoole\Coroutine\Redis;
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$redis->auth('root');
|
||||
$redis->set('key', 'swoole redis work');
|
||||
var_dump($redis->get('key'));
|
||||
});
|
7
vendor/swoole/examples/coroutine/redis/eval.php
vendored
Executable file
7
vendor/swoole/examples/coroutine/redis/eval.php
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
go(function (){
|
||||
$redis = new Co\Redis;
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$res = $redis->eval("return redis.call('get', 'key')");
|
||||
var_dump($res);
|
||||
});
|
7
vendor/swoole/examples/coroutine/redis/request.php
vendored
Executable file
7
vendor/swoole/examples/coroutine/redis/request.php
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$redis = new Co\Redis;
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$res = $redis->request(['object', 'encoding', 'key1']);
|
||||
var_dump($res);
|
||||
});
|
23
vendor/swoole/examples/coroutine/redis_pool.php
vendored
Executable file
23
vendor/swoole/examples/coroutine/redis_pool.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$count = 0;
|
||||
$pool = new SplQueue();
|
||||
$server = new Swoole\Http\Server('127.0.0.1', 9501, SWOOLE_BASE);
|
||||
|
||||
$server->on('Request', function($request, $response) use(&$count, $pool) {
|
||||
if (count($pool) == 0) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$pool->push($redis);
|
||||
}
|
||||
$redis = $pool->pop();
|
||||
$count ++;
|
||||
$ret = $redis->set('key', 'value');
|
||||
$response->end("swoole response is ok, count = $count, result=" . var_export($ret, true));
|
||||
$pool->push($redis);
|
||||
});
|
||||
|
||||
$server->start();
|
15
vendor/swoole/examples/coroutine/redis_subscribe.php
vendored
Executable file
15
vendor/swoole/examples/coroutine/redis_subscribe.php
vendored
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function () {
|
||||
$redis = new co\Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
while (true)
|
||||
{
|
||||
$val = $redis->subscribe(['test']);
|
||||
//订阅的channel,以第一次调用subscribe时的channel为准,后续的subscribe调用是为了收取Redis Server的回包
|
||||
//如果需要改变订阅的channel,请close掉连接,再调用subscribe
|
||||
var_dump($val);
|
||||
}
|
||||
});
|
33
vendor/swoole/examples/coroutine/select/1.php
vendored
Executable file
33
vendor/swoole/examples/coroutine/select/1.php
vendored
Executable file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
$c1 = new chan();
|
||||
//consumer first with select mode
|
||||
$num = 10;
|
||||
go(function () use ($c1,$num) {
|
||||
$read_list = [$c1];
|
||||
$write_list = null;
|
||||
echo "select yield\n";
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
if ($read_list)
|
||||
{
|
||||
foreach($read_list as $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
echo "main end\n";
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user