Skip to content

Commit f0ecf4f

Browse files
authored
Merge pull request #16 from rubyqorn/1.0.0
Migrated to stream socket
2 parents ceb19e5 + 10e3b6c commit f0ecf4f

20 files changed

+221
-687
lines changed

src/AbstractSocket.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,4 @@ public function __construct(SocketFacade $facade)
2626
* @return string|bool
2727
*/
2828
abstract public function send(string $message);
29-
30-
/**
31-
* Close created socket connection
32-
* @throws \Qonsillium\Exceptions\FailedCloseSocket
33-
* @return void
34-
*/
35-
abstract public function close();
36-
37-
/**
38-
* Destruct AbstractSocket class and
39-
* close socket connection
40-
* @return void
41-
*/
42-
public function __destruct()
43-
{
44-
$this->close();
45-
}
4629
}

src/ActionFactory.php

Lines changed: 6 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
use Qonsillium\Credential\SocketCredentials;
66
use Qonsillium\Actions\{
77
SocketAcceptor,
8-
SocketBinder,
98
SocketCloser,
109
SocketConnector,
11-
SocketCreator,
12-
SocketListener,
1310
SocketReader,
1411
SocketWriter
1512
};
@@ -33,93 +30,20 @@ public function __construct(SocketCredentials $credentials)
3330
}
3431

3532
/**
36-
* Returns socket domain setting from config file
33+
* Returns socket connection address
3734
* @return string
3835
*/
39-
private function getDomain()
36+
public function getAddress()
4037
{
41-
return $this->credentials->getCredential('domain');
42-
}
43-
44-
/**
45-
* Returns socket type setting from config file
46-
* @return string
47-
*/
48-
private function getType()
49-
{
50-
return $this->credentials->getCredential('type');
51-
}
52-
53-
/**
54-
* Returns socket protocol setting from config file
55-
* @return string
56-
*/
57-
private function getProtocol()
58-
{
59-
return $this->credentials->getCredential('protocol');
60-
}
61-
62-
/**
63-
* Return setted host or socket file name from
64-
* credentials handler
65-
* @return string
66-
*/
67-
private function getAddress()
68-
{
69-
if ($this->credentials->validateExistence('socket_file')) {
70-
return $this->credentials->getCredential('socket_file');
71-
}
72-
7338
return $this->credentials->getCredential('host');
7439
}
7540

7641
/**
77-
* Return setted socket port from
78-
* credentials handler
79-
* @return string
80-
*/
81-
private function getPort()
82-
{
83-
if ($this->credentials->validateExistence('port')) {
84-
return $this->credentials->getCredential('port');
85-
}
86-
87-
return 0;
88-
}
89-
90-
/**
91-
* Returns number of incoming backlogs
92-
* @return int
93-
*/
94-
private function getBacklog()
95-
{
96-
return $this->credentials->getCredential('backlog');
97-
}
98-
99-
/**
100-
* Bytes length which will be fetched from remote host
101-
* @return int
102-
*/
103-
private function getReadLength()
104-
{
105-
return $this->credentials->getCredential('read_length');
106-
}
107-
108-
/**
109-
* Return flag value responded for reading status
110-
* @return int
111-
*/
112-
private function getReadFlag()
113-
{
114-
return $this->credentials->getCredential('read_flag');
115-
}
116-
117-
/**
118-
* @return \Qonsillium\Actions\SocketCreator
42+
* @return \Qonsillium\Actions\SocketConnector
11943
*/
120-
public function getCreator(): SocketCreator
44+
public function getConnector(string $type)
12145
{
122-
return new SocketCreator($this->getDomain(), $this->getType(), $this->getProtocol());
46+
return new SocketConnector($this->getAddress(), $type);
12347
}
12448

12549
/**
@@ -130,36 +54,12 @@ public function getAcceptor(): SocketAcceptor
13054
return new SocketAcceptor();
13155
}
13256

133-
/**
134-
* @return \Qonsillium\Actions\SocketConnector
135-
*/
136-
public function getConnector(): SocketConnector
137-
{
138-
return new SocketConnector($this->getAddress(), $this->getPort());
139-
}
140-
141-
/**
142-
* @return \Qonsillium\Actions\SocketListener
143-
*/
144-
public function getListener(): SocketListener
145-
{
146-
return new SocketListener($this->getBacklog());
147-
}
148-
149-
/**
150-
* @return \Qonsillium\Actions\SocketBinder
151-
*/
152-
public function getBinder(): SocketBinder
153-
{
154-
return new SocketBinder($this->getAddress(), $this->getPort());
155-
}
156-
15757
/**
15858
* @return \Qonsillium\Actions\SocketReader
15959
*/
16060
public function getReader(): SocketReader
16161
{
162-
return new SocketReader($this->getReadLength(), $this->getReadFlag());
62+
return new SocketReader();
16363
}
16464

16565
/**

src/Actions/AbstractSocketAction.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
abstract class AbstractSocketAction
66
{
7+
/**
8+
* Created socket resource
9+
* @var resource
10+
*/
11+
protected $socket;
12+
713
/**
814
* Make socket action when need
915
* to make class like function
@@ -14,6 +20,23 @@ public function __invoke()
1420
return $this->make();
1521
}
1622

23+
/**
24+
* @param resource $socket
25+
* @return void
26+
*/
27+
public function setSocket($socket)
28+
{
29+
$this->socket = $socket;
30+
}
31+
32+
/**
33+
* @return resource
34+
*/
35+
public function getSocket()
36+
{
37+
return $this->socket;
38+
}
39+
1740
/**
1841
* Make socket action
1942
* @return mixed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Qonsillium\Actions\Connections;
4+
5+
class ClientConnection extends Connection
6+
{
7+
/**
8+
* Create client connection to server socket
9+
* @return resource
10+
*/
11+
public function create()
12+
{
13+
return stream_socket_client(
14+
$this->getAddress(),
15+
$errno,
16+
$errstr
17+
);
18+
}
19+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Qonsillium\Actions\Connections;
4+
5+
abstract class Connection
6+
{
7+
/**
8+
* Address where will be connected socket
9+
* @var string
10+
*/
11+
protected string $address;
12+
13+
/**
14+
* Socket connection timeout
15+
* @var int
16+
*/
17+
protected int $timeout;
18+
19+
/**
20+
* @param string $address
21+
* @return void
22+
*/
23+
public function setAddress(string $address)
24+
{
25+
$this->address = $address;
26+
}
27+
28+
/**
29+
* @return string
30+
*/
31+
public function getAddress()
32+
{
33+
return $this->address;
34+
}
35+
36+
/**
37+
* @param int $timeout
38+
* @return void
39+
*/
40+
public function setTimeout(int $timeout)
41+
{
42+
$this->timeout = $timeout;
43+
}
44+
45+
/**
46+
* @return int
47+
*/
48+
public function getTimeout()
49+
{
50+
return $this->timeout;
51+
}
52+
53+
/**
54+
* Create client or server socket connection
55+
* @return resource
56+
*/
57+
abstract public function create();
58+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Qonsillium\Actions\Connections;
4+
5+
class ConnectionFactory
6+
{
7+
/**
8+
* Get server or client connection creator
9+
* @param string $type
10+
* @return \Qonsillium\Actions\Connections\Connection
11+
*/
12+
public static function getConnection(string $type): Connection
13+
{
14+
if ($type === 'server') {
15+
return new ServerConnection();
16+
} elseif ($type === 'client') {
17+
return new ClientConnection();
18+
}
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Qonsillium\Actions\Connections;
4+
5+
class ServerConnection extends Connection
6+
{
7+
/**
8+
* Create server socket connection
9+
* @return resource
10+
*/
11+
public function create()
12+
{
13+
return stream_socket_server(
14+
$this->getAddress(),
15+
$errno,
16+
$errstr
17+
);
18+
}
19+
}

src/Actions/SocketAcceptor.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,14 @@
22

33
namespace Qonsillium\Actions;
44

5-
use \Socket;
6-
75
class SocketAcceptor extends AbstractSocketAction
86
{
97
/**
10-
* Accepted socket connection
11-
* @var \Socket
12-
*/
13-
private $acceptedSocket;
14-
15-
/**
16-
* @param \Socket $socket
17-
* @return void
18-
*/
19-
public function setAcceptSocket(Socket $socket)
20-
{
21-
$this->acceptedSocket = $socket;
22-
}
23-
24-
/**
25-
* @return \Socket
26-
*/
27-
public function getAcceptedSocket()
28-
{
29-
return $this->acceptedSocket;
30-
}
31-
32-
/**
33-
* Accept socket connection
8+
* Accept socket connections
349
* @return resource
3510
*/
3611
public function make()
3712
{
38-
return socket_accept($this->getAcceptedSocket());
13+
return stream_socket_accept($this->getSocket());
3914
}
4015
}

0 commit comments

Comments
 (0)