Skip to content

Commit 51b3a35

Browse files
committed
Deleted send method from SocketEndpoint. Added bind, listen and accept methods for socket server creation
1 parent 18ad3b7 commit 51b3a35

File tree

3 files changed

+86
-33
lines changed

3 files changed

+86
-33
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/vendor/
22
/.vscode/
3-
composer.lock.json
3+
composer.lock

src/Socket.php

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Socket
2525
* socket
2626
* @var string
2727
*/
28-
protected string $content;
28+
protected string $content = '';
2929

3030
/**
3131
* Socket constructor method
@@ -49,6 +49,57 @@ public function create()
4949
return socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
5050
}
5151

52+
/**
53+
* Binds a name to a socket
54+
* @return bool
55+
*/
56+
public function bind()
57+
{
58+
return socket_bind($this->socket, $this->host, $this->port);
59+
}
60+
61+
/**
62+
* Listens for a connection on a socket
63+
* @return bool
64+
*/
65+
public function listen()
66+
{
67+
return socket_listen($this->socket, 1);
68+
}
69+
70+
/**
71+
* Accept a connection on a socket
72+
* @return resource
73+
*/
74+
public function accept()
75+
{
76+
return socket_accept($this->socket);
77+
}
78+
79+
/**
80+
* Bind, listen and return accepted socket
81+
* connection
82+
* @return resource
83+
*/
84+
public function acceptSocketConnection()
85+
{
86+
if (!$this->bind()) {
87+
return false;
88+
}
89+
90+
if (!$this->listen()) {
91+
return false;
92+
}
93+
94+
$acceptedSocket = $this->accept();
95+
96+
if (!$acceptedSocket) {
97+
return false;
98+
}
99+
100+
return $acceptedSocket;
101+
}
102+
52103
/**
53104
* Initiates a connection on a socket
54105
* @return bool
@@ -61,30 +112,26 @@ public function connect()
61112
}
62113

63114
/**
64-
* Write to a socket specified message
115+
* Write to a socket specified messages
65116
* @param string $message
66117
* @return int
67118
*/
68-
public function write(string $message)
119+
public function write($socket, string $message)
69120
{
70121
return socket_write(
71-
$this->socket, $message, strlen($message)
122+
$socket, $message, strlen($message)
72123
);
73124
}
74125

75126
/**
76127
* Reads a maximum of length bytes from a socket
128+
* @param resource $socket
77129
* @return string
78130
*/
79-
public function read()
131+
public function read($socket)
80132
{
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-
);
133+
while(socket_recv($socket, $buffer, 2048, 0)) {
134+
$this->content .= $buffer;
88135
}
89136

90137
return $this->content;

src/SocketEndpoint.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace WebSocket;
44

55
use Websocket\Credential\ICredential;
6-
use WebSocket\SocketMessage;
76
use WebSocket\Socket;
87

98
class SocketEndpoint
@@ -43,36 +42,43 @@ public function getSocket()
4342
}
4443

4544
/**
46-
* Write content from current listening socket
47-
* @param string $message
48-
* @return int
45+
* Bind, listen and accept connection on socket.
46+
* Usually use when wanted to create server socket
47+
* @return resource|bool
4948
*/
50-
protected function writeToSocket(string $message)
51-
{
52-
return $this->getSocket()->write($message);
49+
public function acceptConnectionOnSocket()
50+
{
51+
return $this->getSocket()->acceptSocketConnection();
5352
}
5453

5554
/**
56-
* Read content from current listening socket
57-
* @return string
55+
* Connect to created to socket. Usually use
56+
* when wanted to create client socket
57+
* @return resource|bool
5858
*/
59-
protected function readFromSocket()
59+
public function connectToSocket()
6060
{
61-
return $this->getSocket()->read();
61+
return $this->getSocket()->connect();
6262
}
6363

6464
/**
65-
* Send message on listening socket and
66-
* read response from socket
65+
* Write content from current listening socket
66+
* @param resource $socket
6767
* @param string $message
68-
* @return string
68+
* @return int
6969
*/
70-
public function send(string $message)
71-
{
72-
if (!$this->writeToSocket($message)) {
73-
return false;
74-
}
70+
protected function writeToSocket($socket, string $message)
71+
{
72+
return $this->getSocket()->write($socket, $message);
73+
}
7574

76-
return $this->readFromSocket();
75+
/**
76+
* Read content from current listening socket
77+
* @param resource $socket
78+
* @return string
79+
*/
80+
protected function readFromSocket($socket)
81+
{
82+
return $this->getSocket()->read($socket);
7783
}
7884
}

0 commit comments

Comments
 (0)