1 while (count($clients) > 0) {
2 // create a copy, so $clients doesn't get modified by socket_select()
5 // get a list of all the clients that have data to be read from
6 // if there are no clients with data, go to next iteration
7 $left = @socket_select($read, $write = null, $except = null, 0, 150);
12 // check if there is a client trying to connect
13 if (in_array($mainSocket, $read)) {
14 // accept the client, and add him to the $clients array
15 $new_sock = socket_accept($mainSocket);
16 $clients[] = $new_sock;
18 // send the client a welcome message
19 socket_write($new_sock, "No noobs, but I'll make an exception. :)\n".
20 "There are ".(count($clients) - 1)." client(s) connected to the server.\n");
22 socket_getpeername($new_sock, $ip);
23 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:New client connected: {$ip}");
26 if (count($clients) > 2) {
27 foreach ($clients as $send_sock) {
28 if ($send_sock != $mainSocket && $send_sock != $new_sock) {
29 socket_write($send_sock, "Server: Chatter has joined from {$ip}. There are now ".(count($clients) - 1)." clients.\n");
34 // remove the listening socket from the clients-with-data array
35 $key = array_search($mainSocket, $read);
39 // loop through all the clients that have data to read from
40 foreach ($read as $read_sock) {
42 socket_getpeername($read_sock, $ip);
44 // read until newline or 1024 bytes
45 // socket_read while show errors when the client is disconnected, so silence the error messages
46 $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);
48 // check if the client is disconnected
49 if (($data === false) || (in_array(strtolower(trim($data)), $leaving))) {
51 // remove client for $clients array
52 $key = array_search($read_sock, $clients);
53 unset($clients[$key]);
54 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client from {$ip} disconnected. Left: ".(count($clients) - 1)."");
57 if (count($clients) > 1) {
58 foreach ($clients as $send_sock) {
59 if ($send_sock != $mainSocket) {
60 socket_write($send_sock, "Server: Chatter from {$ip} has logged out. ".(count($clients) - 1)." client(s) left.\n");
65 // continue to the next client to read from, if any
66 socket_write($read_sock, "Server: Good bye.\n");
67 socket_shutdown($read_sock, 2);
68 socket_close($read_sock);
70 } elseif (in_array(trim($data), $shutdown)) {
71 // Is he allowed to shutdown?
72 if (!in_array($ip, $masters)) {
73 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has tried to shutdown the server!");
74 socket_write($read_sock, "Server: You are not allowed to shutdown the server!\n");
79 // Close all connections a leave here
80 foreach ($clients as $client) {
81 // Send message to client
82 if ($client !== $mainSocket && $client != $read_sock) {
83 socket_write($client, "Server: Shutting down! Thank you for joining us.\n");
87 socket_shutdown($client, 2);
88 socket_close($client);
97 // trim off the trailing/beginning white spaces
100 // Test for HTML codes
101 $tags = strip_tags($data);
103 // check if there is any data after trimming off the spaces
104 if (!empty($data) && $tags == $data && count($clients) > 2) {
105 // Send confirmation to "chatter"
106 socket_write($read_sock, "\nServer: Message accepted.\n");
108 // send this to all the clients in the $clients array (except the first one, which is a listening socket)
109 foreach ($clients as $send_sock) {
111 // if its the listening sock or the client that we got the message from, go to the next one in the list
112 if ($send_sock == $mainSocket || $send_sock == $read_sock)
115 // write the message to the client -- add a newline character to the end of the message
116 socket_write($send_sock, "{$ip}:{$data}\n");
118 } // end of broadcast foreach
119 } elseif ($tags != $data) {
120 // HTML codes are not allowed
121 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has entered HTML code!");
122 socket_write($read_sock, "Server: HTML is forbidden!\n");
123 } elseif ((count($clients) == 2) && ($read_sock != $mainSocket)) {
124 // No one else will hear the "chatter"
125 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} speaks with himself.");
126 socket_write($read_sock, "Server: No one will hear you!\n");
128 } // end of reading foreach
131 // close the listening socket
132 socket_close($mainSocket);
139 * @author Roland Haeder <webmaster@ship-simu.org>
141 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
142 * @license GNU GPL 3.0 or any newer version
143 * @link http://www.ship-simu.org
145 * This program is free software: you can redistribute it and/or modify
146 * it under the terms of the GNU General Public License as published by
147 * the Free Software Foundation, either version 3 of the License, or
148 * (at your option) any later version.
150 * This program is distributed in the hope that it will be useful,
151 * but WITHOUT ANY WARRANTY; without even the implied warranty of
152 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
153 * GNU General Public License for more details.
155 * You should have received a copy of the GNU General Public License
156 * along with this program. If not, see <http://www.gnu.org/licenses/>.
158 class ???Listener extends BaseListener implements Listenable, Visitable {
160 * Protected constructor
164 protected function __construct () {
165 // Call parent constructor
166 parent::__construct(__CLASS__);
170 * Creates an instance of this class
172 * @param $nodeInstance A NodeHelper instance
173 * @return $listenerInstance An instance a prepared listener class
175 public final static function create???Listener (NodeHelper $nodeInstance) {
177 $listenerInstance = new ???Listener();
179 // Set the application instance
180 $listenerInstance->setNodeInstance($nodeInstance);
182 // Return the prepared instance
183 return $listenerInstance;
187 * Initializes the listener by setting up the required socket server
192 public function initListener() {
193 $this->partialStub('Need to implement this method.');
197 * "Listens" for incoming network packages
202 public function doListen() {
203 $this->partialStub('Need to implement this method.');