Skip to content

Commit

Permalink
Added tcp-server component (#612)
Browse files Browse the repository at this point in the history
* 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
3 people authored Apr 21, 2024
0 parents commit 92d220d
Show file tree
Hide file tree
Showing 14 changed files with 607 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
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
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: huangdijia
custom: https://hdj.me/sponsors/
13 changes: 13 additions & 0 deletions .github/workflows/close-pull-request.yml
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!"
25 changes: 25 additions & 0 deletions .github/workflows/release.yaml
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
21 changes: 21 additions & 0 deletions LICENSE
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.
165 changes: 165 additions & 0 deletions README.md
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)
44 changes: 44 additions & 0 deletions composer.json
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
}
}
25 changes: 25 additions & 0 deletions src/ConfigProvider.php
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,
],
];
}
}
16 changes: 16 additions & 0 deletions src/Exception/InvalidMethodException.php
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
{
}
18 changes: 18 additions & 0 deletions src/Exception/ServerException.php
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
{
}
39 changes: 39 additions & 0 deletions src/Listener/InitSenderListener.php
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);
}
}
}
Loading

0 comments on commit 92d220d

Please sign in to comment.