Skip to content

Commit de6d57f

Browse files
authored
Merge pull request #3 from rubyqorn/dev-socket-implementation
Created socket manipulator class
2 parents 019d095 + 18ad3b7 commit de6d57f

File tree

4 files changed

+176
-12
lines changed

4 files changed

+176
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
2+
/.vscode/
23
composer.lock.json

src/Socket.php

+84-12
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,100 @@
22

33
namespace WebSocket;
44

5-
use Websocket\Credential\ICredential;
6-
75
class Socket
86
{
9-
protected ?ICredential $credential;
7+
/**
8+
* @var string
9+
*/
10+
protected string $host;
11+
12+
/**
13+
* @var string
14+
*/
15+
protected string $port;
16+
17+
/**
18+
* Current listening socket
19+
* @var resource
20+
*/
21+
protected $socket;
22+
23+
/**
24+
* Readed message from listening
25+
* socket
26+
* @var string
27+
*/
28+
protected string $content;
29+
30+
/**
31+
* Socket constructor method
32+
* @param string $host
33+
* @param string $port
34+
*/
35+
public function __construct(string $host, string $port)
36+
{
37+
$this->host = $host;
38+
$this->port = $port;
39+
$this->socket = $this->create();
40+
}
1041

1142
/**
12-
* DI of SocketCredentials
13-
* @param \Websocket\Credential\ICredential $credential
14-
* @return void
43+
* Creates and returns a socket resource, also
44+
* referred to as an endpoint of communication
45+
* @return resource
1546
*/
16-
public function setCredentials(ICredential $credential)
47+
public function create()
1748
{
18-
$this->credential = $credential;
49+
return socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
50+
}
51+
52+
/**
53+
* Initiates a connection on a socket
54+
* @return bool
55+
*/
56+
public function connect()
57+
{
58+
return socket_connect(
59+
$this->socket, $this->host, $this->port
60+
);
61+
}
62+
63+
/**
64+
* Write to a socket specified message
65+
* @param string $message
66+
* @return int
67+
*/
68+
public function write(string $message)
69+
{
70+
return socket_write(
71+
$this->socket, $message, strlen($message)
72+
);
73+
}
74+
75+
/**
76+
* Reads a maximum of length bytes from a socket
77+
* @return string
78+
*/
79+
public function read()
80+
{
81+
$write = null;
82+
$exception = null;
83+
84+
while(socket_select([$this->socket], $write, $exception, 0)) {
85+
socket_recv(
86+
$this->socket, $this->content, strlen($this->content), 0
87+
);
88+
}
89+
90+
return $this->content;
1991
}
2092

2193
/**
22-
* Return instance of SocketCredentials
23-
* @return SocketCredentials
94+
* Socket detructor method which
95+
* close current listening socket
2496
*/
25-
public function getCredentials()
97+
public function __destruct()
2698
{
27-
return $this->credential;
99+
socket_close($this->socket);
28100
}
29101
}

src/SocketEndpoint.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace WebSocket;
4+
5+
use Websocket\Credential\ICredential;
6+
use WebSocket\SocketMessage;
7+
use WebSocket\Socket;
8+
9+
class SocketEndpoint
10+
{
11+
protected ?ICredential $credential;
12+
13+
/**
14+
* DI of SocketCredentials
15+
* @param \Websocket\Credential\ICredential $credential
16+
* @return void
17+
*/
18+
public function setCredentials(ICredential $credential)
19+
{
20+
$this->credential = $credential;
21+
}
22+
23+
/**
24+
* Return instance of SocketCredentials
25+
* @return SocketCredentials
26+
*/
27+
public function getCredentials()
28+
{
29+
return $this->credential;
30+
}
31+
32+
/**
33+
* Socket factory method which manipulate
34+
* PHP socket API
35+
* @return \WebSocket\Socket
36+
*/
37+
public function getSocket()
38+
{
39+
return new Socket(
40+
$this->getCredentials()->getCredential('host'),
41+
$this->getCredentials()->getCredential('port')
42+
);
43+
}
44+
45+
/**
46+
* Write content from current listening socket
47+
* @param string $message
48+
* @return int
49+
*/
50+
protected function writeToSocket(string $message)
51+
{
52+
return $this->getSocket()->write($message);
53+
}
54+
55+
/**
56+
* Read content from current listening socket
57+
* @return string
58+
*/
59+
protected function readFromSocket()
60+
{
61+
return $this->getSocket()->read();
62+
}
63+
64+
/**
65+
* Send message on listening socket and
66+
* read response from socket
67+
* @param string $message
68+
* @return string
69+
*/
70+
public function send(string $message)
71+
{
72+
if (!$this->writeToSocket($message)) {
73+
return false;
74+
}
75+
76+
return $this->readFromSocket();
77+
}
78+
}

src/SocketMessage.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace WebSocket;
4+
5+
class SocketMessage
6+
{
7+
protected $message;
8+
9+
public function __construct(string $message)
10+
{
11+
$this->message = $message;
12+
}
13+
}

0 commit comments

Comments
 (0)