Skip to content

Commit 19bfe91

Browse files
authored
Merge pull request #4 from rubyqorn/dev-socket-implementation
Fixed unknown resource problem
2 parents de6d57f + 75f9ad9 commit 19bfe91

15 files changed

+334
-37
lines changed

.gitignore

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

README.md

100644100755
File mode changed.

composer.json

100644100755
File mode changed.

phpunit.xml

100644100755
File mode changed.

src/Client.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace WebSocket;
4+
5+
class Client extends SocketEndpoint implements ISocketImplementer
6+
{
7+
/**
8+
* Socket resource created using
9+
* acceptSocketConnection method
10+
* @var resource
11+
*/
12+
protected $createdSocket;
13+
14+
/**
15+
* Accept server socket connection
16+
* @return resource|\WebSocket\SocketError
17+
*/
18+
public function create()
19+
{
20+
$this->createdSocket = $this->connectToSocket();
21+
22+
if (!$this->createdSocket) {
23+
return new SocketError('Can\'t accepted connected socket');
24+
}
25+
26+
return $this->createdSocket;
27+
}
28+
29+
/**
30+
* Read message from accepted server socket
31+
* @return \WebSocket\SocketError|\WebSocket\SocketMessage
32+
*/
33+
public function read()
34+
{
35+
$socketResponse = new SocketMessage(
36+
$this->readFromSocket($this->createdSocket)
37+
);
38+
39+
if (!$socketResponse->getSocketResponse()) {
40+
return new SocketError('Can\'t read message from client socket');
41+
}
42+
43+
return $socketResponse;
44+
}
45+
46+
/**
47+
* Write message to server accepted socket
48+
* @param string $message
49+
* @return int|\WebSocket\SocketError
50+
*/
51+
public function write(string $message)
52+
{
53+
$writtenMessage = $this->writeToSocket($this->createdSocket, $message);
54+
55+
if (!$writtenMessage) {
56+
return new SocketError('Can\'t write to accepted client socket');
57+
}
58+
59+
return $writtenMessage;
60+
}
61+
62+
/**
63+
* Call Client destructor method when socket
64+
* connection was broken
65+
* @return void
66+
*/
67+
public function __destruct()
68+
{
69+
return $this->closeSocketConnection($this->createdSocket);
70+
}
71+
}

src/ISocketImplementer.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace WebSocket;
4+
5+
interface ISocketImplementer
6+
{
7+
/**
8+
* Initiate socket connection
9+
*/
10+
public function create();
11+
12+
/**
13+
* Read message from initiated socket
14+
*/
15+
public function read();
16+
17+
/**
18+
* Write message to initiated socket
19+
* @param string $message
20+
*/
21+
public function write(string $message);
22+
}

src/Server.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace WebSocket;
4+
5+
class Server extends SocketEndpoint implements ISocketImplementer
6+
{
7+
/**
8+
* Socket resource created using
9+
* acceptSocketConnection method
10+
* @var resource
11+
*/
12+
protected $createdSocket;
13+
14+
/**
15+
* Accept client socket connection
16+
* @return resource|\WebSocket\SocketError
17+
*/
18+
public function create()
19+
{
20+
$this->createdSocket = $this->acceptConnectionOnSocket();
21+
22+
if (!$this->createdSocket) {
23+
return new SocketError('Can\'t accepted connected socket');
24+
}
25+
26+
return $this->createdSocket;
27+
}
28+
29+
/**
30+
* Read message from accepted client socket
31+
* @return \WebSocket\SocketError|\WebSocket\SocketMessage
32+
*/
33+
public function read()
34+
{
35+
$socketResponse = new SocketMessage(
36+
$this->readFromSocket($this->createdSocket)
37+
);
38+
39+
if (!$socketResponse->getSocketResponse()) {
40+
return new SocketError('Can\'t read message from client socket');
41+
}
42+
43+
return $socketResponse;
44+
}
45+
46+
/**
47+
* Write message to client accepted socket
48+
* @param string $message
49+
* @return int|\WebSocket\SocketError
50+
*/
51+
public function write(string $message)
52+
{
53+
$writtenMessage = $this->writeToSocket($this->createdSocket, $message);
54+
55+
if (!$writtenMessage) {
56+
return new SocketError('Can\'t write to accepted client socket');
57+
}
58+
59+
return $writtenMessage;
60+
}
61+
62+
/**
63+
* Call Server destructor method when socket
64+
* connection was broken
65+
* @return void
66+
*/
67+
public function __destruct()
68+
{
69+
return $this->closeSocketConnection($this->createdSocket);
70+
}
71+
}

src/Socket.php

100644100755
+85-18
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ class Socket
2525
* socket
2626
* @var string
2727
*/
28-
protected string $content;
28+
protected string $content = '';
29+
30+
protected $clientSocket;
31+
32+
protected $serverSocket;
33+
34+
protected $createdSocket;
2935

3036
/**
3137
* Socket constructor method
@@ -50,52 +56,113 @@ public function create()
5056
}
5157

5258
/**
53-
* Initiates a connection on a socket
59+
* Binds a name to a socket
60+
* @return bool
61+
*/
62+
public function bind()
63+
{
64+
return socket_bind($this->socket, $this->host, $this->port);
65+
}
66+
67+
/**
68+
* Listens for a connection on a socket
5469
* @return bool
5570
*/
71+
public function listen()
72+
{
73+
return socket_listen($this->socket, 1);
74+
}
75+
76+
/**
77+
* Accept a connection on a socket
78+
* @return resource
79+
*/
80+
public function accept()
81+
{
82+
return socket_accept($this->socket);
83+
}
84+
85+
/**
86+
* Bind, listen and return accepted socket
87+
* connection
88+
* @return resource
89+
*/
90+
public function acceptSocketConnection()
91+
{
92+
if (!$this->bind()) {
93+
return false;
94+
}
95+
96+
if (!$this->listen()) {
97+
return false;
98+
}
99+
100+
$acceptedSocket = $this->accept();
101+
102+
if (!$acceptedSocket) {
103+
return false;
104+
}
105+
106+
return $acceptedSocket;
107+
}
108+
109+
/**
110+
* Return resource of type Socket
111+
* which was created right now. Can get
112+
* access by connect method
113+
* @return resource
114+
*/
115+
public function getConnectedSocket()
116+
{
117+
return $this->socket;
118+
}
119+
120+
/**
121+
* Initiates a connection on a socket
122+
* @return \WebSocket\Socket
123+
*/
56124
public function connect()
57125
{
58-
return socket_connect(
126+
socket_connect(
59127
$this->socket, $this->host, $this->port
60128
);
129+
130+
return $this->socket;
61131
}
62132

63133
/**
64-
* Write to a socket specified message
134+
* Write to a socket specified messages
65135
* @param string $message
66136
* @return int
67137
*/
68-
public function write(string $message)
138+
public function write($socket, string $message)
69139
{
70140
return socket_write(
71-
$this->socket, $message, strlen($message)
141+
$socket, $message, strlen($message)
72142
);
73143
}
74144

75145
/**
76146
* Reads a maximum of length bytes from a socket
147+
* @param resource $socket
77148
* @return string
78149
*/
79-
public function read()
150+
public function read($socket)
80151
{
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-
);
152+
while(socket_recv($socket, $buffer, 2048, 0)) {
153+
$this->content .= $buffer;
88154
}
89155

90156
return $this->content;
91157
}
92158

93159
/**
94-
* Socket detructor method which
95-
* close current listening socket
160+
* Close specified socket connection
161+
* @param resource $socket
162+
* @return void
96163
*/
97-
public function __destruct()
164+
public function close($socket)
98165
{
99-
socket_close($this->socket);
166+
return socket_close($socket);
100167
}
101168
}

0 commit comments

Comments
 (0)