-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
=
committed
Mar 18, 2022
1 parent
e4a867a
commit 0bb840e
Showing
24 changed files
with
1,430 additions
and
1 deletion.
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,5 @@ | ||
/.idea/ | ||
/.vs/ | ||
/.vscode/ | ||
/vendor/ | ||
/composer.lock |
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 |
---|---|---|
@@ -1 +1,193 @@ | ||
# Socket | ||
# InitPHP Socket Manager | ||
|
||
PHP Socket (TCP, TLS, UDP, SSL) Server/Client Library | ||
|
||
## Requirements | ||
|
||
- PHP 7.4 or higher | ||
- PHP Sockets Extension | ||
|
||
## Installation | ||
|
||
``` | ||
composer require initphp/socket | ||
``` | ||
|
||
## Usage | ||
|
||
**Supported Types :** | ||
|
||
- TCP | ||
- UDP | ||
- TLS | ||
- SSL | ||
|
||
### Factory | ||
|
||
`\InitPHP\Socket\Socket::class` It allows you to easily create socket server or client. | ||
|
||
#### `Socket::server()` | ||
|
||
```php | ||
public static function server(int $handler = Socket::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketServerInterface | ||
``` | ||
|
||
- `$handler` : `Socket::SSL`, `Socket::TCP`, `Socket::TLS` or `Socket::UDP` | ||
- `$host` : Identifies the socket host. If not defined or left blank, it will throw an error. | ||
- `$port` : Identifies the socket port. If not defined or left blank, it will throw an error. | ||
- `$argument` : This value is the value that will be sent as 3 parameters to the constructor method of the handler. | ||
- SSL or TLS = (float) Defines the timeout period. | ||
- UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix" | ||
|
||
#### `Socket::client()` | ||
|
||
```php | ||
public static function client(int $handler = self::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketClientInterface | ||
``` | ||
|
||
- `$handler` : `Socket::SSL`, `Socket::TCP`, `Socket::TLS` or `Socket::UDP` | ||
- `$host` : Identifies the socket host. If not defined or left blank, it will throw an error. | ||
- `$port` : Identifies the socket port. If not defined or left blank, it will throw an error. | ||
- `$argument` : This value is the value that will be sent as 3 parameters to the constructor method of the handler. | ||
- SSL or TLS = (float) Defines the timeout period. | ||
- UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix" | ||
|
||
### Methods | ||
|
||
**`connection()` :** Initiates the socket connection. | ||
|
||
```php | ||
public function connection(): self; | ||
``` | ||
|
||
**`disconnect()` :** Terminates the connection. | ||
|
||
```php | ||
public function disconnect(): bool; | ||
``` | ||
|
||
**`read()` :** Reads data from socket. | ||
|
||
```php | ||
public function read(int $length = 1024): ?string; | ||
``` | ||
|
||
**`write()` :** Writes data to the socket | ||
|
||
```php | ||
public function write(string $string): ?int; | ||
``` | ||
|
||
#### Server Methods | ||
|
||
**`live()` :** | ||
|
||
```php | ||
public function live(callable $callback): void; | ||
``` | ||
|
||
**`wait()` :** | ||
|
||
```php | ||
public function wait(int $second): void; | ||
``` | ||
|
||
#### Special methods for TLS and SSL. | ||
|
||
TLS and SSL work similarly. | ||
|
||
There are some additional methods you can use from TLS and SSL sockets. | ||
|
||
**`timeout()` :** Defines the timeout period of the current. | ||
|
||
```php | ||
public function timeout(int $second): self; | ||
``` | ||
|
||
**`blocking()` :** Sets the blocking mode of the current. | ||
|
||
```php | ||
public function blocking(bool $mode = true): self; | ||
``` | ||
|
||
**`crypto()` :** Turns encryption on or off on a connected socket. | ||
|
||
```php | ||
public function crypto(?string $method = null): self; | ||
``` | ||
|
||
Possible values for `$method` are; | ||
|
||
- "sslv2" | ||
- "sslv3" | ||
- "sslv23" | ||
- "any" | ||
- "tls" | ||
- "tlsv1.0" | ||
- "tlsv1.1" | ||
- "tlsv1.2" | ||
- NULL | ||
|
||
**`option()` :** Defines connection options for SSL and TLS. see; [https://www.php.net/manual/en/context.ssl.php](https://www.php.net/manual/en/context.ssl.php) | ||
|
||
```php | ||
public function option(string $key, mixed $value): self; | ||
``` | ||
|
||
### Socket Server | ||
|
||
_**Example :**_ | ||
|
||
```php | ||
require_once "../vendor/autoload.php"; | ||
use \InitPHP\Socket\Socket; | ||
use \InitPHP\Socket\Interfaces\SocketServerInterface; | ||
|
||
$server = Socket::server(Socket::TLS, '127.0.0.1', 8080); | ||
$server->connection(); | ||
|
||
$server->live(function (SocketServerInterface $socket) { | ||
switch ($socket->read()) { | ||
case 'exit' : | ||
$socket->write('Goodbye!'); | ||
return; | ||
case 'write' : | ||
$socket->write('Run write command.'); | ||
break; | ||
case 'read' : | ||
$socket->write('Run read command.'); | ||
break; | ||
default: return; | ||
} | ||
}); | ||
``` | ||
|
||
### Socket Client | ||
|
||
_**Example :**_ | ||
|
||
```php | ||
require_once "../vendor/autoload.php"; | ||
use \InitPHP\Socket\Socket; | ||
|
||
$client = Socket::client(Socket::SSL, 'smtp.gmail.com', 465); | ||
|
||
$client->option('verify_peer', false) | ||
->option('verify_peer_name', false); | ||
|
||
$client->connection(); | ||
|
||
$client->write('EHLO [127.0.0.1]'); | ||
|
||
echo $client->read(); | ||
``` | ||
|
||
_In the above example, a simple smtp connection to gmail is made._ | ||
|
||
## Credits | ||
|
||
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<[email protected]>> | ||
|
||
## License | ||
|
||
Copyright © 2022 [MIT License](./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,24 @@ | ||
{ | ||
"name": "initphp/socket", | ||
"description": "Socket Server-Client Library", | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"InitPHP\\Socket\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Muhammet ŞAFAK", | ||
"email": "[email protected]", | ||
"role": "Developer", | ||
"homepage": "https://www.muhammetsafak.com.tr" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": ">=7.4", | ||
"ext-sockets": "*" | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
/** | ||
* SSL.php | ||
* | ||
* This file is part of InitPHP. | ||
* | ||
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 InitPHP | ||
* @license http://initphp.github.io/license.txt MIT | ||
* @version 1.0 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace InitPHP\Socket\Client; | ||
|
||
use \InitPHP\Socket\Common\{StreamClientTrait, BaseClient}; | ||
use \InitPHP\Socket\Interfaces\SocketClientInterface; | ||
|
||
class SSL extends BaseClient implements SocketClientInterface | ||
{ | ||
|
||
use StreamClientTrait; | ||
|
||
protected string $type = 'ssl'; | ||
|
||
} |
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,81 @@ | ||
<?php | ||
/** | ||
* TCP.php | ||
* | ||
* This file is part of InitPHP. | ||
* | ||
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 InitPHP | ||
* @license http://initphp.github.io/license.txt MIT | ||
* @version 1.0 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace InitPHP\Socket\Client; | ||
|
||
use \InitPHP\Socket\Exception\{SocketConnectionException, SocketInvalidArgumentException}; | ||
use \InitPHP\Socket\Common\BaseClient; | ||
use \InitPHP\Socket\Interfaces\SocketClientInterface; | ||
|
||
use const PHP_BINARY_READ; | ||
use const SOCK_STREAM; | ||
|
||
use function is_string; | ||
use function socket_connect; | ||
use function socket_close; | ||
use function socket_read; | ||
use function socket_write; | ||
use function strlen; | ||
|
||
class TCP extends BaseClient implements SocketClientInterface | ||
{ | ||
|
||
protected ?string $domain; | ||
|
||
/** | ||
* @param string $host | ||
* @param int $port | ||
* @param $argument <p>domain</p> | ||
*/ | ||
public function __construct(string $host, int $port, $argument) | ||
{ | ||
$this->setHost($host)->setPort($port); | ||
if($argument !== null && !is_string($argument)){ | ||
throw new SocketInvalidArgumentException('The TCP client must have a value pointing to the argument domain. Only "v4", "v6" or "unix"'); | ||
} | ||
$this->domain = $argument; | ||
} | ||
|
||
public function connection(): self | ||
{ | ||
$socket = $this->createSocketSource('tcp', SOCK_STREAM, $this->domain); | ||
if(socket_connect($socket, $this->getHost(), $this->getPort()) === FALSE){ | ||
throw new SocketConnectionException('Socket Connection Error : ' . $this->getLastError()); | ||
} | ||
$this->socket = $socket; | ||
return $this; | ||
} | ||
|
||
public function disconnect(): bool | ||
{ | ||
if(isset($this->socket)){ | ||
socket_close($this->socket); | ||
} | ||
return true; | ||
} | ||
|
||
public function read(int $length = 1024, int $type = PHP_BINARY_READ): ?string | ||
{ | ||
$read = socket_read($this->getSocket(), $length, $type); | ||
return $read === FALSE ? null : $read; | ||
} | ||
|
||
public function write(string $string): ?int | ||
{ | ||
$write = socket_write($this->getSocket(), $string, strlen($string)); | ||
return $write === FALSE ? null : $write; | ||
} | ||
|
||
} |
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,28 @@ | ||
<?php | ||
/** | ||
* TLS.php | ||
* | ||
* This file is part of InitPHP. | ||
* | ||
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 InitPHP | ||
* @license http://initphp.github.io/license.txt MIT | ||
* @version 1.0 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace InitPHP\Socket\Client; | ||
|
||
use \InitPHP\Socket\Common\{StreamClientTrait, BaseClient}; | ||
use \InitPHP\Socket\Interfaces\SocketClientInterface; | ||
|
||
class TLS extends BaseClient implements SocketClientInterface | ||
{ | ||
|
||
use StreamClientTrait; | ||
|
||
protected string $type = 'tls'; | ||
|
||
} |
Oops, something went wrong.