https://github.com/roadrunner-php/tcp
🔌 TCP Worker Client for RoadRunner application server
https://github.com/roadrunner-php/tcp
Last synced: 10 months ago
JSON representation
🔌 TCP Worker Client for RoadRunner application server
- Host: GitHub
- URL: https://github.com/roadrunner-php/tcp
- Owner: roadrunner-php
- License: mit
- Created: 2021-10-27T08:13:57.000Z (over 4 years ago)
- Default Branch: 4.x
- Last Pushed: 2024-04-12T08:38:28.000Z (almost 2 years ago)
- Last Synced: 2025-01-22T22:15:45.298Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 41 KB
- Stars: 8
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# RoadRunner TCP Plugin
[](https://packagist.org/packages/spiral/roadrunner-tcp)
[](https://packagist.org/packages/spiral/roadrunner-tcp)
[](https://github.com/spiral/roadrunner-tcp/actions)
[](https://github.com/spiral/roadrunner-tcp/actions)
[](https://codecov.io/gh/roadrunner-php/tcp)
[](https://packagist.org/packages/spiral/roadrunner-tcp)
[](https://shepherd.dev/github/spiral/roadrunner-php/tcp)
[](https://shepherd.dev/github/roadrunner-php/tcp)
RoadRunner is an open-source (MIT licensed) high-performance PHP application server, load balancer, and process manager.
It supports running as a service with the ability to extend its functionality on a per-project basis.
RoadRunner includes TCP server and can be used to replace classic TCP setup with much greater performance and flexibility.
Official Website |
Documentation
This repository contains the codebase TCP PHP workers. Check [spiral/roadrunner](https://github.com/spiral/roadrunner)
to get application server.
## Installation
To install application server and TCP codebase:
```bash
composer require spiral/roadrunner-tcp
```
You can use the convenient installer to download the latest available compatible version of RoadRunner assembly:
```bash
composer require spiral/roadrunner-cli --dev
```
To download latest version of application server:
```bash
vendor/bin/rr get
```
## Usage
For example, such a configuration would be quite feasible to run:
```yaml
tcp:
servers:
smtp:
addr: tcp://127.0.0.1:1025
delimiter: "\r\n" # by default
server2:
addr: tcp://127.0.0.1:8889
pool:
num_workers: 2
max_jobs: 0
allocate_timeout: 60s
destroy_timeout: 60s
```
If you have more than 1 worker in your pool TCP server will send received packets to different workers,
and if you need to collect data you have to use storage, that can be accessed by all workers, for example [RoadRunner Key Value](https://github.com/spiral/roadrunner-kv)
### Example
To init abstract RoadRunner worker:
```php
waitRequest()) {
try {
if ($request->getEvent() === TcpEvent::Connected) {
// You can close connection according your restrictions
if ($request->getRemoteAddress() !== '127.0.0.1') {
$tcpWorker->close();
continue;
}
// -----------------
// Or continue read data from server
// By default, server closes connection if a worker doesn't send CONTINUE response
$tcpWorker->read();
// -----------------
// Or send response to the TCP connection, for example, to the SMTP client
$tcpWorker->respond("220 mailamie \r\n");
} elseif ($request->getEvent() === TcpEvent::Data) {
$body = $request->getBody();
// ... handle request from TCP server [tcp_access_point_1]
if ($request->getServer() === 'tcp_access_point_1') {
// Send response and close connection
$tcpWorker->respond('Access denied', TcpResponse::RespondClose);
// ... handle request from TCP server [server2]
} elseif ($request->getServer() === 'server2') {
// Send response to the TCP connection and wait for the next request
$tcpWorker->respond(\json_encode([
'remote_addr' => $request->getRemoteAddress(),
'server' => $request->getServer(),
'uuid' => $request->getConnectionUuid(),
'body' => $request->getBody(),
'event' => $request->getEvent()
]));
}
// Handle closed connection event
} elseif ($request->getEvent() === TcpEvent::Close) {
// Do something ...
// You don't need to send response on closed connection
}
} catch (\Throwable $e) {
$tcpWorker->respond("Something went wrong\r\n", TcpResponse::RespondClose);
$worker->error((string)$e);
}
}
```
## Testing:
This codebase is automatically tested via host repository - [spiral/roadrunner](https://github.com/spiral/roadrunner).
## License:
The MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information. Maintained
by [Spiral Scout](https://spiralscout.com).