-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8f692c
commit 4ddab4f
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace classes\socket; | ||
|
||
use \classes\ExceptionManager as Exception; | ||
use \classes\IniManager as Ini; | ||
|
||
class Server | ||
{ | ||
use \traits\EchoTrait; | ||
|
||
/** | ||
* @var resource $resource The server socket resource | ||
*/ | ||
private $resource; | ||
/** | ||
* @var string $protocol The server socket protocol | ||
*/ | ||
private $protocol; | ||
/** | ||
* @var string $address The server socket address | ||
*/ | ||
private $address; | ||
/** | ||
* @var integer $port The server socket port | ||
*/ | ||
private $port; | ||
/** | ||
* @var boolean $verbose True to print info in console else false | ||
*/ | ||
private $verbose; | ||
/** | ||
* @var integer $errorNum The error code if an error occured | ||
*/ | ||
private $errorNum; | ||
/** | ||
* @var string $errorString The error string if an error occured | ||
*/ | ||
private $errorString; | ||
|
||
/** | ||
* Constructor that load parameters in the ini conf file and run the socket server | ||
*/ | ||
public function __construct() | ||
{ | ||
cli_set_process_title('PHP socket server'); | ||
|
||
$params = Ini::getSectionParams('Socket'); | ||
$this->protocol = $params['protocol']; | ||
$this->address = $params['address']; | ||
$this->port = $params['port']; | ||
$this->verbose = $params['verbose']; | ||
$this->resource = stream_socket_server( | ||
$this->protocol . '://' . $this->address . ':' . $this->port, | ||
$this->errorNum, | ||
$this->errorString | ||
); | ||
|
||
if ($this->resource === false) { | ||
throw new Exception('Error ' . $this->errorNum . '::' . $this->errorString, Exception::$ERROR); | ||
} | ||
|
||
$this->run(); | ||
} | ||
|
||
/** | ||
* Run the server | ||
*/ | ||
private function run() | ||
{ | ||
while (1) { | ||
if ($this->verbose) { | ||
$this->runningInfo(); | ||
} | ||
|
||
sleep(60); | ||
} | ||
} | ||
|
||
private function runningInfo() | ||
{ | ||
static::out( | ||
'[' . date('Y-m-d H:i:s') . ']' | ||
. ' Server running on ' . stream_socket_get_name($this->resource, false) . PHP_EOL | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/** | ||
* Test script for the socket Server class | ||
* | ||
* @category Test | ||
* @author Romain Laneuville <[email protected]> | ||
*/ | ||
|
||
use \classes\socket\Server as Server; | ||
|
||
include_once '../autoloader.php'; | ||
|
||
try { | ||
$server = new Server(); | ||
} catch (Exception $e) { | ||
} finally { | ||
exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters