-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added tcp-server component * Up README.md * Optimal The Sender * 更新 LICENSE * Update tcp-sender ConfigProvider * Update tcp-sender * Update README.md * Add tcp-sender component * Update tcp-sender ConfigProvider to use fully qualified class names * Update OnPipeMessageListener and Sender classes * Refactor Sender constructor parameter order in Sender.php * Refactor TcpServer constructor parameter order in TcpServer.php * Refactor Sender constructor parameter order and update TcpSender tests --------- Co-authored-by: Deeka Wong <[email protected]> Co-authored-by: Deeka Wong <[email protected]>
- Loading branch information
Showing
14 changed files
with
607 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.github export-ignore | ||
/.vscode export-ignore | ||
/tests export-ignore | ||
.gitattributes export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github: huangdijia | ||
custom: https://hdj.me/sponsors/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: Close Pull Request | ||
|
||
on: | ||
pull_request_target: | ||
types: [opened] | ||
|
||
jobs: | ||
run: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: superbrothers/close-pull-request@v3 | ||
with: | ||
comment: "Thank you for your pull request. However, you have submitted this PR on the friendsofhyperf organization which is a read-only sub split of `friendsofhyperf/components`. Please submit your PR on the https://github.com/friendsofhyperf/components repository.<br><br>Thanks!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
on: | ||
push: | ||
# Sequence of patterns matched against refs/tags | ||
tags: | ||
- 'v*' # Push events to matching v*, i.e. v1.0, v1.0.0 | ||
|
||
name: Release | ||
|
||
jobs: | ||
release: | ||
name: Release | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: Create Release | ||
id: create_release | ||
uses: actions/create-release@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
tag_name: ${{ github.ref }} | ||
release_name: Release ${{ github.ref }} | ||
draft: false | ||
prerelease: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 D.J.Hwang | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
# TcpSender | ||
|
||
[![Latest Stable Version](https://poser.pugx.org/friendsofhyperf/tcp-sender/version.png)](https://packagist.org/packages/friendsofhyperf/tcp-sender) | ||
[![Total Downloads](https://poser.pugx.org/friendsofhyperf/tcp-sender/d/total.png)](https://packagist.org/packages/friendsofhyperf/tcp-sender) | ||
[![GitHub license](https://img.shields.io/github/license/friendsofhyperf/tcp-sender)](https://github.com/friendsofhyperf/tcp-sender) | ||
|
||
Another TcpSender component for Hyperf. | ||
|
||
## Installation | ||
|
||
- Installation | ||
|
||
```shell | ||
composer require friendsofhyperf/tcp-sender | ||
``` | ||
|
||
## Usage | ||
|
||
### config/autoload/servers.php | ||
|
||
```php | ||
'servers' => [ | ||
[ | ||
'name' => 'tcp', | ||
'type' => Server::SERVER_BASE, | ||
'host' => '0.0.0.0', | ||
'port' => 9401, | ||
'sock_type' => SWOOLE_SOCK_TCP, | ||
'callbacks' => [ | ||
Event::ON_CONNECT => [TcpServer::class,'onConnect'], | ||
Event::ON_CLOSE => [TcpServer::class,'onClose'], | ||
Event::ON_RECEIVE => [TcpServer::class,'onReceive'], | ||
], | ||
'options' => [ | ||
// Whether to enable request lifecycle event | ||
'enable_request_lifecycle' => false, | ||
], | ||
] | ||
], | ||
``` | ||
|
||
### 多进程模型 | ||
|
||
#### TcpServer | ||
|
||
```php | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Hyperf\Contract\OnCloseInterface; | ||
use Hyperf\Contract\OnReceiveInterface; | ||
use FriendsOfHyperf\TcpSender\Sender; | ||
use Swoole\Server; | ||
|
||
class TcpServer implements OnCloseInterface,OnReceiveInterface | ||
{ | ||
public function __construct(private Sender $sender) | ||
{ | ||
} | ||
|
||
/** | ||
* @param Server $server | ||
*/ | ||
public function onConnect($server, $fd, $reactorId): void | ||
{ | ||
$server->send($fd, sprintf('Client %s connected.'.PHP_EOL, $fd)); | ||
} | ||
|
||
public function onClose($server, int $fd, int $reactorId): void | ||
{ | ||
$server->send($fd, sprintf('Client %s closed.'.PHP_EOL, $fd)); | ||
} | ||
|
||
public function onReceive($server, int $fd, int $reactorId, string $data): void | ||
{ | ||
$server->send($fd, sprintf('Client %s send: %s'.PHP_EOL, $fd, $data)); | ||
var_dump($data); | ||
} | ||
|
||
|
||
} | ||
``` | ||
|
||
### 协程风格服务 单进程模型 | ||
|
||
#### TcpServer | ||
|
||
```php | ||
namespace App; | ||
|
||
use Hyperf\Contract\OnReceiveInterface; | ||
use FriendsOfHyperf\TcpSender\Sender; | ||
use Swoole\Coroutine\Server\Connection; | ||
use Swoole\Server; | ||
|
||
class TcpServer implements OnReceiveInterface | ||
{ | ||
public function __construct(private Sender $sender) | ||
{ | ||
} | ||
|
||
public function onConnect(Connection $connection, $fd): void | ||
{ | ||
// 设置 fd 和 connection 的映射关系 | ||
$this->sender->setResponse($fd,$connection); | ||
$connection->send(sprintf('Client %s connected.'.PHP_EOL, $fd)); | ||
} | ||
|
||
public function onClose($connection, int $fd): void | ||
{ | ||
// 删除 fd 和 connection 的映射关系 | ||
$this->sender->setResponse($fd,null); | ||
} | ||
|
||
public function onReceive($server, int $fd, int $reactorId, string $data): void | ||
{ | ||
$server->send($fd, sprintf('Client %s send: %s'.PHP_EOL, $fd, $data)); | ||
} | ||
|
||
|
||
} | ||
``` | ||
|
||
## YourService or YourController | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller; | ||
|
||
use FriendsOfHyperf\TcpSender\Sender; | ||
|
||
class IndexController extends AbstractController | ||
{ | ||
public function __construct(private Sender $sender) | ||
{ | ||
} | ||
|
||
public function index() | ||
{ | ||
// 向指定的fd发送消息 | ||
$this->sender->send(1, 'Hello Hyperf.'); | ||
$user = $this->request->input('user', 'Hyperf'); | ||
$method = $this->request->getMethod(); | ||
|
||
return [ | ||
'method' => $method, | ||
'message' => "Hello {$user}.", | ||
]; | ||
} | ||
} | ||
|
||
``` | ||
|
||
## Contact | ||
|
||
- [Twitter](https://twitter.com/huangdijia) | ||
- [Gmail](mailto:[email protected]) | ||
|
||
## License | ||
|
||
[MIT](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"name": "friendsofhyperf/tcp-sender", | ||
"description": "Another tcp server component for Hyperf.", | ||
"keywords": [ | ||
"hyperf", | ||
"v3.1" | ||
], | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "huangdijia", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "zds" | ||
} | ||
], | ||
"support": { | ||
"issues": "https://github.com/friendsofhyperf/components/issues", | ||
"source": "https://github.com/friendsofhyperf/components" | ||
}, | ||
"require": { | ||
"hyperf/event": "~3.1.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"FriendsOfHyperf\\TcpSender\\": "src" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-main": "3.1-dev" | ||
}, | ||
"hyperf": { | ||
"config": "FriendsOfHyperf\\TcpSender\\ConfigProvider" | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
"config": { | ||
"sort-packages": true, | ||
"optimize-autoloader": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
|
||
namespace FriendsOfHyperf\TcpSender; | ||
|
||
class ConfigProvider | ||
{ | ||
public function __invoke() | ||
{ | ||
return [ | ||
'listeners' => [ | ||
Listener\InitSenderListener::class, | ||
Listener\OnPipeMessageListener::class, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
|
||
namespace FriendsOfHyperf\TcpSender\Exception; | ||
|
||
class InvalidMethodException extends ServerException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
|
||
namespace FriendsOfHyperf\TcpSender\Exception; | ||
|
||
use RuntimeException; | ||
|
||
class ServerException extends RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
|
||
namespace FriendsOfHyperf\TcpSender\Listener; | ||
|
||
use FriendsOfHyperf\TcpSender\Sender; | ||
use Hyperf\Event\Contract\ListenerInterface; | ||
use Hyperf\Framework\Event\AfterWorkerStart; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class InitSenderListener implements ListenerInterface | ||
{ | ||
public function __construct(private readonly ContainerInterface $container) | ||
{ | ||
} | ||
|
||
public function listen(): array | ||
{ | ||
return [ | ||
AfterWorkerStart::class, | ||
]; | ||
} | ||
|
||
public function process(object $event): void | ||
{ | ||
if ($this->container->has(Sender::class)) { | ||
$sender = $this->container->get(Sender::class); | ||
$sender->setWorkerId($event->workerId); | ||
} | ||
} | ||
} |
Oops, something went wrong.