Skip to content

Commit e898c76

Browse files
committed
clean up api
1 parent f0a1254 commit e898c76

File tree

4 files changed

+84
-23
lines changed

4 files changed

+84
-23
lines changed

server-broadcast.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function onConnect( $client ) {
2727

2828
printf( "[%s] Connected at port %d\n", $client->getAddress(), $client->getPort() );
2929

30-
\Sock\SocketServerBroadcast::broadcast( array( 'data' => "Connected\n", 'type' => 'msg' ) );
30+
$client->connected();
3131

3232
$read = '';
3333
while( true ) {
@@ -36,14 +36,12 @@ function onConnect( $client ) {
3636
if( $read == '' ) {
3737
break;
3838
}
39-
40-
\Sock\SocketServerBroadcast::broadcast( array( 'data' => $read, 'type' => 'msg' ) );
39+
$client->sendBroadcast( $read );
4140
}
4241

43-
\Sock\SocketServerBroadcast::broadcast( array( 'type' => 'disc' ) );
42+
$client->disconnected();
4443

4544
printf( "[%s] Disconnected\n", $client->getAddress() );
46-
$client->close();
4745
}
4846

4947
require "sock/SocketServerBroadcast.php";

sock/SocketClient.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function getPort() {
3838
}
3939

4040
public function close() {
41+
socket_shutdown( $this->connection, 0);
4142
socket_close( $this->connection );
4243
}
4344
}

sock/SocketClientBroadcast.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace Sock;
3+
4+
require_once "SocketException.php";
5+
require_once "SocketClient.php";
6+
7+
class SocketClientBroadcast extends SocketClient {
8+
9+
protected $server;
10+
11+
public function __construct( $connection, SocketServerBroadcast $server ) {
12+
parent::__construct( $connection );
13+
$this->server = $server;
14+
}
15+
16+
public function sendBroadcast($message) {
17+
$this->server->broadcast( array( 'data' => $message, 'type' => 'msg' ) );
18+
}
19+
20+
public function disconnected() {
21+
$this->server->broadcast( array( 'type' => 'disc' ) );
22+
$this->close();
23+
}
24+
25+
public function connected() {
26+
// don't need this file open in child processes
27+
unset($this->server->pipe);
28+
$this->server->broadcast( array( 'data' => "Connected\n", 'type' => 'msg' ) );
29+
}
30+
31+
}

sock/SocketServerBroadcast.php

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,35 @@
33
namespace Sock;
44

55
require_once "SocketServer.php";
6+
require_once "SocketClientBroadcast.php";
67

78
class SocketServerBroadcast extends SocketServer {
89

910
const PIPENAME = '/tmp/broadcastserver.pid';
1011

11-
private static $pid;
12-
13-
protected $pipe;
12+
protected $pid;
13+
public $pipe;
1414

1515
private $connections = array();
1616

1717
public function __construct( $port = 4444, $address = '127.0.0.1' ) {
1818
parent::__construct( $port, $address );
19-
self::$pid = posix_getpid();
19+
$this->pid = posix_getpid();
2020
if(!file_exists(self::PIPENAME)) {
2121
umask(0);
2222
if( ! posix_mkfifo(self::PIPENAME, 0666 ) ) {
23-
die('Cant create a pipe: ' . self::PIPENAME);
23+
die('Cant create a pipe: "' . self::PIPENAME . '"');
2424
}
2525
}
26-
2726
$this->pipe = fopen(self::PIPENAME, 'r+');
2827
}
2928

3029
public function handleProcess() {
31-
$len = $this->bytesToInt( fread($this->pipe, 4) );
30+
$header = fread($this->pipe, 4);
31+
$len = $this->bytesToInt( $header );
32+
3233
$message = unserialize( fread( $this->pipe, $len ) );
34+
3335
if( $message['type'] == 'msg' ) {
3436
$client = $this->connections[ $message['pid'] ];
3537
$msg = sprintf('[%s] (%d):%s', $client->getAddress(), $message['pid'], $message['data'] );
@@ -59,19 +61,39 @@ protected function beforeServerLoop() {
5961
parent::beforeServerLoop();
6062
socket_set_nonblock( $this->sockServer );
6163
pcntl_signal(SIGUSR1, array($this, 'handleProcess'), true);
64+
//pcntl_signal(SIGINT, array($this, 'stop'), true);
65+
}
66+
67+
protected function stop() {
68+
echo "\nClosing active connections...\n";
69+
$this->_listenLoop = false;
70+
foreach( $this->connections as $conn ) {
71+
$conn->close();
72+
}
73+
echo "Shutting down server...\n";
74+
fclose($this->pipe);
75+
$this->pipe = fopen(self::PIPENAME, 'w');
76+
fclose($this->pipe);
77+
echo unlink( self::PIPENAME ) ? 'removed' : 'not removed';
78+
pcntl_wait($status);
6279
}
6380

6481
protected function serverLoop() {
6582
while( $this->_listenLoop ) {
6683
if( ( $client = @socket_accept( $this->sockServer ) ) === false ) {
6784
$info = array();
68-
if( pcntl_sigtimedwait(array(SIGUSR1),$info,1) > 0 ) {
69-
$this->handleProcess();
85+
if( pcntl_sigtimedwait(array(SIGUSR1, SIGINT),$info,1) > 0 ) {
86+
if( $info['signo'] == SIGINT ) {
87+
$this->stop();
88+
}
89+
else{
90+
$this->handleProcess();
91+
}
7092
}
7193
continue;
7294
}
7395

74-
$socketClient = new SocketClient( $client );
96+
$socketClient = new SocketClientBroadcast( $client, $this );
7597

7698
if( is_array( $this->connectionHandler ) ) {
7799
$object = $this->connectionHandler[0];
@@ -83,26 +105,35 @@ protected function serverLoop() {
83105
$childPid = $function( $socketClient );
84106
}
85107

108+
if( ! $childPid ) {
109+
// force child process to exit from loop
110+
return;
111+
}
112+
86113
$this->connections[ $childPid ] = $socketClient;
87114
}
88-
unlink(self::PIPENAME);
115+
89116
}
90117

91-
static function broadcast( Array $msg ) {
118+
public function broadcast( Array $msg ) {
92119
$msg['pid'] = posix_getpid();
93-
$message = serialize( $msg );
94-
$f = fopen(self::PIPENAME, 'w');
95-
fwrite($f, self::strlenInBytes($message) . $message);
120+
$message = serialize( $msg );
121+
$f = fopen(self::PIPENAME, 'w+');
122+
if( !$f ) {
123+
echo "ERROR: Can't open PIPE for writting\n";
124+
return;
125+
}
126+
fwrite($f, $this->strlenInBytes($message) . $message );
96127
fclose($f);
97-
posix_kill(self::$pid, SIGUSR1);
128+
posix_kill($this->pid, SIGUSR1);
98129
}
99130

100-
static function strlenInBytes($str) {
131+
protected function strlenInBytes($str) {
101132
$len = strlen($str);
102133
$chars = chr( $len & 0xFF );
103134
$chars .= chr( ($len >> 8 ) & 0xFF );
104135
$chars .= chr( ($len >> 16 ) & 0xFF );
105136
$chars .= chr( ($len >> 24 ) & 0xFF );
106137
return $chars;
107138
}
108-
}
139+
}

0 commit comments

Comments
 (0)