Skip to content

Commit 6057511

Browse files
authored
Merge pull request #17 from rubyqorn/1.0.0
Socket options adding feature
2 parents f0ecf4f + 96e893c commit 6057511

File tree

8 files changed

+144
-16
lines changed

8 files changed

+144
-16
lines changed

README.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,99 @@
22
# Socket
33

44
## This is a layer for client and server socket connections.
5-
#### [Detailed documentation](http://socket-docs.epizy.com)
5+
6+
### PHP extension which have to be installed before to work:
7+
* [PHP 8.0](https://www.php.net/downloads)
8+
* [Yaml](https://www.php.net/manual/en/book.yaml.php)
9+
* [Sockets](https://www.php.net/manual/en/book.sockets.php)
10+
11+
### Four actions you have to make:
12+
* Configure your configuration file.
13+
* Create client.php or server.php file
14+
* Call methods
15+
* Run from CLI or extends
16+
17+
### Examples of TCP and UNIX sockets:
18+
19+
#### 1) Create configuration file. File can be **ONLY** with yaml(yml) or json extensions
20+
21+
##### * TCP socket configuration using YAML
22+
```
23+
settings:
24+
socket_type: tcp
25+
address: 127.0.0.1
26+
port: 8000
27+
content_length: 2048
28+
```
29+
30+
##### * UNIX socket configuration using YAML
31+
```
32+
settings:
33+
socket_type: unix
34+
address: socket.sock
35+
content_length: 2048
36+
```
37+
38+
#### 2) Create client and server file handlers.
39+
40+
##### server.php
41+
```
42+
<?php
43+
44+
require_once 'vendor/autoload.php';
45+
46+
use Qonsillium\QonsilliumSocket;
47+
use Qonsillium\ServerSocket;
48+
49+
$server = new QonsilliumSocket('config.yaml');
50+
$server->runServer(function(ServerSocket $socket) {
51+
echo $socket->send('Hello from server!');
52+
});
53+
```
54+
55+
##### client.php
56+
```
57+
<?php
58+
59+
require_once 'vendor/autoload.php';
60+
61+
use Qonsillium\QonsilliumSocket;
62+
use Qonsillium\ClientSocket;
63+
64+
$server = new QonsilliumSocket('config.yaml');
65+
$server->runClient(function(ClinetSocket $socket) {
66+
echo $socket->send('Hello from client!');
67+
});
68+
```
69+
70+
#### 3) Run from CLI
71+
```
72+
john@doe:/workdir/$ php server.php
73+
john@doe:/workdir/$ php client.php
74+
```
75+
76+
#### 4) Extends from QonsilliumSocket
77+
```
78+
<?php
79+
80+
namespace App;
81+
82+
use Qonsillium\QonsilliumSocket
83+
use Qonillium\ClientSocket
84+
85+
class SocketMessagePrinter extends QonsilliumSocket
86+
{
87+
public function handleServerSocketMessage(string $myMessage)
88+
{
89+
$serverMessage = $this->runClient(function(ClientSocket $client) use ($myMessage) {
90+
return $client->send($myMessage);
91+
});
92+
93+
if ($serverMessage === 'Hello from server!') {
94+
// handle this action
95+
}
96+
}
97+
}
98+
```
99+
100+
But when you will instantiate handler class don't forget to set configuration file with socket settings in constructor method

src/ActionFactory.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public function getAddress()
3838
return $this->credentials->getCredential('host');
3939
}
4040

41+
/**
42+
* Returns socket read legnth
43+
* @return int
44+
*/
45+
private function getReadLength()
46+
{
47+
return $this->credentials->getCredential('content_length');
48+
}
49+
4150
/**
4251
* @return \Qonsillium\Actions\SocketConnector
4352
*/
@@ -59,7 +68,7 @@ public function getAcceptor(): SocketAcceptor
5968
*/
6069
public function getReader(): SocketReader
6170
{
62-
return new SocketReader();
71+
return new SocketReader($this->getReadLength());
6372
}
6473

6574
/**

src/Actions/Connections/ClientConnection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public function create()
1313
return stream_socket_client(
1414
$this->getAddress(),
1515
$errno,
16-
$errstr
16+
$errstr,
17+
$this->getTimeout() ?? $this->getTimeout()
1718
);
1819
}
1920
}

src/Actions/Connections/Connection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ abstract class Connection
1212

1313
/**
1414
* Socket connection timeout
15-
* @var int
15+
* @var int|null
1616
*/
17-
protected int $timeout;
17+
protected ?int $timeout = null;
1818

1919
/**
2020
* @param string $address

src/Actions/SocketReader.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ class SocketReader extends AbstractSocketAction
99
*/
1010
private string $message;
1111

12+
/**
13+
* @var int
14+
*/
15+
private int $length;
16+
17+
/**
18+
* Initiate SocketReader constructor method
19+
* @param int $length
20+
* @return void
21+
*/
22+
public function __construct(int $length)
23+
{
24+
$this->length = $length;
25+
}
26+
1227
/**
1328
* @param string $message
1429
* @return void
@@ -32,7 +47,7 @@ public function getMessage()
3247
*/
3348
public function make()
3449
{
35-
$this->setMessage(fread($this->getSocket(), 2048));
50+
$this->setMessage(fread($this->getSocket(), $this->length));
3651
return $this;
3752
}
3853
}

src/Bootstrapper.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,23 @@ public function setCredentials()
7474
$socketTypeFactory = $this->getSocketTypeFactory();
7575
$socketType = $socketTypeFactory->getSocket($this->settings['settings']['socket_type']);
7676

77+
// Set socket host credentials. We have only two types
78+
// It's unix and tcp, another will be ignored
79+
if ($socketType->get('socket_type') === 'unix') {
80+
$credentials->setCredential(
81+
'host',
82+
"{$socketType->get('socket_type')}://{$socketType->get('address')}"
83+
);
84+
} elseif($socketType->get('socket_type') === 'tcp') {
85+
$credentials->setCredential(
86+
'host',
87+
"{$socketType->get('socket_type')}://{$socketType->get('address')}:{$socketType->get('port')}"
88+
);
89+
}
90+
7791
$credentials->setCredential(
78-
'host',
79-
"{$socketType->get('socket_type')}://{$socketType->get('address')}:{$socketType->get('port')}"
92+
'content_length',
93+
$socketType->get('content_length')
8094
);
8195

8296
return $credentials;

src/QonsilliumSocket.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function getServerSocket(): ServerSocket
6262
*/
6363
public function runClient(Closure $handler)
6464
{
65-
$handler($this->getClientSocket());
65+
return $handler($this->getClientSocket());
6666
}
6767

6868
/**
@@ -80,6 +80,6 @@ public function runClient(Closure $handler)
8080
*/
8181
public function runServer(Closure $handler)
8282
{
83-
$handler($this->getServerSocket());
83+
return $handler($this->getServerSocket());
8484
}
8585
}

src/SocketFacade.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
namespace Qonsillium;
44

55
use Qonsillium\Exceptions\ {
6-
FailedAcceptSocket,
76
FailedConnectSocket,
8-
FailedListenSocket,
9-
FailedCreateSocket,
107
FailedWriteSocket,
11-
FailedCloseSocket,
12-
FailedBindSocket,
13-
FailedReadSocket
148
};
159

1610
class SocketFacade

0 commit comments

Comments
 (0)