Skip to content

Commit

Permalink
Merge pull request #4 from rubyqorn/dev-socket-implementation
Browse files Browse the repository at this point in the history
Fixed unknown resource problem
  • Loading branch information
rubyqorn authored Oct 17, 2020
2 parents de6d57f + 75f9ad9 commit 19bfe91
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor/
/.vscode/
composer.lock.json
composer.lock
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified composer.json
100644 → 100755
Empty file.
Empty file modified phpunit.xml
100644 → 100755
Empty file.
71 changes: 71 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace WebSocket;

class Client extends SocketEndpoint implements ISocketImplementer
{
/**
* Socket resource created using
* acceptSocketConnection method
* @var resource
*/
protected $createdSocket;

/**
* Accept server socket connection
* @return resource|\WebSocket\SocketError
*/
public function create()
{
$this->createdSocket = $this->connectToSocket();

if (!$this->createdSocket) {
return new SocketError('Can\'t accepted connected socket');
}

return $this->createdSocket;
}

/**
* Read message from accepted server socket
* @return \WebSocket\SocketError|\WebSocket\SocketMessage
*/
public function read()
{
$socketResponse = new SocketMessage(
$this->readFromSocket($this->createdSocket)
);

if (!$socketResponse->getSocketResponse()) {
return new SocketError('Can\'t read message from client socket');
}

return $socketResponse;
}

/**
* Write message to server accepted socket
* @param string $message
* @return int|\WebSocket\SocketError
*/
public function write(string $message)
{
$writtenMessage = $this->writeToSocket($this->createdSocket, $message);

if (!$writtenMessage) {
return new SocketError('Can\'t write to accepted client socket');
}

return $writtenMessage;
}

/**
* Call Client destructor method when socket
* connection was broken
* @return void
*/
public function __destruct()
{
return $this->closeSocketConnection($this->createdSocket);
}
}
22 changes: 22 additions & 0 deletions src/ISocketImplementer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace WebSocket;

interface ISocketImplementer
{
/**
* Initiate socket connection
*/
public function create();

/**
* Read message from initiated socket
*/
public function read();

/**
* Write message to initiated socket
* @param string $message
*/
public function write(string $message);
}
71 changes: 71 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace WebSocket;

class Server extends SocketEndpoint implements ISocketImplementer
{
/**
* Socket resource created using
* acceptSocketConnection method
* @var resource
*/
protected $createdSocket;

/**
* Accept client socket connection
* @return resource|\WebSocket\SocketError
*/
public function create()
{
$this->createdSocket = $this->acceptConnectionOnSocket();

if (!$this->createdSocket) {
return new SocketError('Can\'t accepted connected socket');
}

return $this->createdSocket;
}

/**
* Read message from accepted client socket
* @return \WebSocket\SocketError|\WebSocket\SocketMessage
*/
public function read()
{
$socketResponse = new SocketMessage(
$this->readFromSocket($this->createdSocket)
);

if (!$socketResponse->getSocketResponse()) {
return new SocketError('Can\'t read message from client socket');
}

return $socketResponse;
}

/**
* Write message to client accepted socket
* @param string $message
* @return int|\WebSocket\SocketError
*/
public function write(string $message)
{
$writtenMessage = $this->writeToSocket($this->createdSocket, $message);

if (!$writtenMessage) {
return new SocketError('Can\'t write to accepted client socket');
}

return $writtenMessage;
}

/**
* Call Server destructor method when socket
* connection was broken
* @return void
*/
public function __destruct()
{
return $this->closeSocketConnection($this->createdSocket);
}
}
103 changes: 85 additions & 18 deletions src/Socket.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ class Socket
* socket
* @var string
*/
protected string $content;
protected string $content = '';

protected $clientSocket;

protected $serverSocket;

protected $createdSocket;

/**
* Socket constructor method
Expand All @@ -50,52 +56,113 @@ public function create()
}

/**
* Initiates a connection on a socket
* Binds a name to a socket
* @return bool
*/
public function bind()
{
return socket_bind($this->socket, $this->host, $this->port);
}

/**
* Listens for a connection on a socket
* @return bool
*/
public function listen()
{
return socket_listen($this->socket, 1);
}

/**
* Accept a connection on a socket
* @return resource
*/
public function accept()
{
return socket_accept($this->socket);
}

/**
* Bind, listen and return accepted socket
* connection
* @return resource
*/
public function acceptSocketConnection()
{
if (!$this->bind()) {
return false;
}

if (!$this->listen()) {
return false;
}

$acceptedSocket = $this->accept();

if (!$acceptedSocket) {
return false;
}

return $acceptedSocket;
}

/**
* Return resource of type Socket
* which was created right now. Can get
* access by connect method
* @return resource
*/
public function getConnectedSocket()
{
return $this->socket;
}

/**
* Initiates a connection on a socket
* @return \WebSocket\Socket
*/
public function connect()
{
return socket_connect(
socket_connect(
$this->socket, $this->host, $this->port
);

return $this->socket;
}

/**
* Write to a socket specified message
* Write to a socket specified messages
* @param string $message
* @return int
*/
public function write(string $message)
public function write($socket, string $message)
{
return socket_write(
$this->socket, $message, strlen($message)
$socket, $message, strlen($message)
);
}

/**
* Reads a maximum of length bytes from a socket
* @param resource $socket
* @return string
*/
public function read()
public function read($socket)
{
$write = null;
$exception = null;

while(socket_select([$this->socket], $write, $exception, 0)) {
socket_recv(
$this->socket, $this->content, strlen($this->content), 0
);
while(socket_recv($socket, $buffer, 2048, 0)) {
$this->content .= $buffer;
}

return $this->content;
}

/**
* Socket detructor method which
* close current listening socket
* Close specified socket connection
* @param resource $socket
* @return void
*/
public function __destruct()
public function close($socket)
{
socket_close($this->socket);
return socket_close($socket);
}
}
Loading

0 comments on commit 19bfe91

Please sign in to comment.