8000 First commit · InitPHP/Socket@0bb840e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bb840e

Browse files
author
=
committed
First commit
1 parent e4a867a commit 0bb840e

24 files changed

+1430
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.idea/
2+
/.vs/
3+
/.vscode/
4+
/vendor/
5+
/composer.lock

README.md

Lines changed: 193 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,193 @@
1-
# Socket
1+
# InitPHP Socket Manager
2+
3+
PHP Socket (TCP, TLS, UDP, SSL) Server/Client Library
4+
5+
## Requirements
6+
7+
- PHP 7.4 or higher
8+
- PHP Sockets Extension
9+
10+
## Installation
11+
12+
```
13+
composer require initphp/socket
14+
```
15+
16+
## Usage
17+
18+
**Supported Types :**
19+
20+
- TCP
21+
- UDP
22+
- TLS
23+
- SSL
24+
25+
### Factory
26+
27+
`\InitPHP\Socket\Socket::class` It allows you to easily create socket server or client.
28+
29+
#### `Socket::server()`
30+
31+
```php
32+
public static function server(int $handler = Socket::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketServerInterface
33+
```
34+
35+
- `$handler` : `Socket::SSL`, `Socket::TCP`, `Socket::TLS` or `Socket::UDP`
36+
- `$host` : Identifies the socket host. If not defined or left blank, it will throw an error.
37+
- `$port` : Identifies the socket port. If not defined or left blank, it will throw an error.
38+
- `$argument` : This value is the value that will be sent as 3 parameters to the constructor method of the handler.
39+
- SSL or TLS = (float) Defines the timeout period.
40+
- UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix"
41+
42+
#### `Socket::client()`
43+
44+
```php
45+
public static function client(int $handler = self::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketClientInterface
46+
```
47+
48+
- `$handler` : `Socket::SSL`, `Socket::TCP`, `Socket::TLS` or `Socket::UDP`
49+
- `$host` : Identifies the socket host. If not defined or left blank, it will throw an error.
50+
- `$port` : Identifies the socket port. If not defined or left blank, it will throw an error.
51+
- `$argument` : This value is the value that will be sent as 3 parameters to the constructor method of the handler.
52+
- SSL or TLS = (float) Defines the timeout period.
53+
- UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix"
54+
55+
### Methods
56+
57+
**`connection()` :** Initiates the socket connection.
58+
59+
```php
60+
public function connection(): self;
61+
```
62+
63+
**`disconnect()` :** Terminates the connection.
64+
65+
```php
66+
public function disconnect(): bool;
67+
```
68+
69+
**`read()` :** Reads data from socket.
70+
71+
```php
72+
public function read(int $length = 1024): ?string;
73+
```
74+
75+
**`write()` :** Writes data to the socket
76+
77+
```php
78+
public function write(string $string): ?int;
79+
```
80+
81+
#### Server Methods
82+
83+
**`live()` :**
84+
85+
```php
86+
public function live(callable $callback): void;
87+
```
88+
89+
**`wait()` :**
90+
91+
```php
92+
public function wait(int $second): void;
93+
```
94+
95+
#### Special methods for TLS and SSL.
96+
97+
TLS and SSL work similarly.
98+
99+
There are some additional methods you can use from TLS and SSL sockets.
100+
101+
**`timeout()` :** Defines the timeout period of the current.
102+
103+
```php
104+
public function timeout(int $second): self;
105+
```
106+
107+
**`blocking()` :** Sets the blocking mode of the current.
108+
109+
```php
110+
public function blocking(bool $mode = true): self;
111+
```
112+
113+
**`crypto()` :** Turns encryption on or off on a connected socket.
114+
115+
```php
116+
public function crypto(?string $method = null): self;
117+
```
118+
119+
Possible values for `$method` are;
120+
121+
- "sslv2"
122+
- "sslv3"
123+
- "sslv23"
124+
- "any"
125+
- "tls"
126+
- "tlsv1.0"
127+
- "tlsv1.1"
128+
- "tlsv1.2"
129+
- NULL
130+
131+
**`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)
132+
133+
```php
134+
public function option(string $key, mixed $value): self;
135+
```
136+
137+
### Socket Server
138+
139+
_**Example :**_
140+
141+
```php
142+
require_once "../vendor/autoload.php";
143+
use \InitPHP\Socket\Socket;
144+
use \InitPHP\Socket\Interfaces\SocketServerInterface;
145+
146+
$server = Socket::server(Socket::TLS, '127.0.0.1', 8080);
147+
$server->connection();
148+
149+
$server->live(function (SocketServerInterface $socket) {
150+
switch ($socket->read()) {
151+
case 'exit' :
152+
$socket->write('Goodbye!');
153+
return;
154+
case 'write' :
155+
$socket->write('Run write command.');
156+
break;
157+
case 'read' :
158+
$socket->write('Run read command.');
159+
break;
160+
default: return;
161+
}
162+
});
163+
```
164+
165+
### Socket Client
166+
167+
_**Example :**_
168+
169+
```php
170+
require_once "../vendor/autoload.php";
171+
use \InitPHP\Socket\Socket;
172+
173+
$client = Socket::client(Socket::SSL, 'smtp.gmail.com', 465);
174+
175+
$client->option('verify_peer', false)
176+
->option('verify_peer_name', false);
177+
178+
$client->connection();
179+
180+
$client->write('EHLO [127.0.0.1]');
181+
182+
echo $client->read();
183+
```
184+
185+
_In the above example, a simple smtp connection to gmail is made._
186+
187+
## Credits
188+
189+
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<info@muhammetsafak.com.tr>>
190+
191+
## License
192+
193+
Copyright © 2022 [MIT License](./LICENSE)

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "initphp/socket",
3+
"description": "Socket Server-Client Library",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"InitPHP\\Socket\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Muhammet ŞAFAK",
14+
"email": "info@muhammetsafak.com.tr",
15+
"role": "Developer",
16+
"homepage": "https://www.muhammetsafak.com.tr"
17+
}
18+
],
19+
"minimum-stability": "stable",
20+
"require": {
21+
"php": ">=7.4",
22+
"ext-sockets": "*"
23+
}
24+
}

src/Client/SSL.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* SSL.php
4+
*
5+
* This file is part of InitPHP.
6+
*
7+
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace InitPHP\Socket\Client;
17+
18+
use \InitPHP\Socket\Common\{StreamClientTrait, BaseClient};
19+
use \InitPHP\Socket\Interfaces\SocketClientInterface;
20+
21+
class SSL extends BaseClient implements SocketClientInterface
22+
{
23+
24+
use StreamClientTrait;
25+
26+
protected string $type = 'ssl';
27+
28+
}

src/Client/TCP.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* TCP.php
4+
*
5+
* This file is part of InitPHP.
6+
*
7+
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace InitPHP\Socket\Client;
17+
18+
use \InitPHP\Socket\Exception\{SocketConnectionException, SocketInvalidArgumentException};
19+
use \InitPHP\Socket\Common\BaseClient;
20+
use \InitPHP\Socket\Interfaces\SocketClientInterface;
21+
22+
use const PHP_BINARY_READ;
23+
use const SOCK_STREAM;
24+
25+
use function is_string;
26+
use function socket_connect;
27+
use function socket_close;
28+
use function socket_read;
29+
use function socket_write;
30+
use function strlen;
31+
32+
class TCP extends BaseClient implements SocketClientInterface
33+
{
34+
35+
protected ?string $domain;
36+
37+
/**
38+
* @param string $host
39+
* @param int $port
40+
* @param $argument <p>domain</p>
41+
*/
42+
public function __construct(string $host, int $port, $argument)
43+
{
44+
$this->setHost($host)->setPort($port);
45+
if($argument !== null && !is_string($argument)){
46+
throw new SocketInvalidArgumentException('The TCP client must have a value pointing to the argument domain. Only "v4", "v6" or "unix"');
47+
}
48+
$this->domain = $argument;
49+
}
50+
51+
public function connection(): self
52+
{
53+
$socket = $this->createSocketSource('tcp', SOCK_STREAM, $this->domain);
54+
if(socket_connect($socket, $this->getHost(), $this->getPort()) === FALSE){
55+
throw new SocketConnectionException('Socket Connection Error : ' . $this->getLastError());
56+
}
57+
$this->socket = $socket;
58+
return $this;
59+
}
60+
61+
public function disconnect(): bool
62+
{
63+
if(isset($this->socket)){
64+
socket_close($this->socket);
65+
}
66+
return true;
67+
}
68+
69+
public function read(int $length = 1024, int $type = PHP_BINARY_READ): ?string
70+
{
71+
$read = socket_read($this->getSocket(), $length, $type);
72+
return $read === FALSE ? null : $read;
73+
}
74+
75+
public function write(string $string): ?int
76+
{
77+
$write = socket_write($this->getSocket(), $string, strlen($string));
78+
return $write === FALSE ? null : $write;
79+
}
80+
81+
}

src/Client/TLS.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* TLS.php
4+
*
5+
* This file is part of InitPHP.
6+
*
7+
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace InitPHP\Socket\Client;
17+
18+
use \InitPHP\Socket\Common\{StreamClientTrait, BaseClient};
19+
use \InitPHP\Socket\Interfaces\SocketClientInterface;
20+
21+
class TLS extends BaseClient implements SocketClientInterface
22+
{
23+
24+
use StreamClientTrait;
25+
26+
protected string $type = 'tls';
27+
28+
}

0 commit comments

Comments
 (0)
0