Updated 'core'.
[hub.git] / application / hub / main / listener / tcp / class_
1 while (count($clients) > 0) {
2         // create a copy, so $clients doesn't get modified by socket_select()
3         $read = $clients;
4
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);
8         if ($left < 1) {
9                 continue;
10         }
11
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;
17
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");
21
22                 socket_getpeername($new_sock, $ip);
23                 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:New client connected: {$ip}");
24
25                 // Notify all chatter
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");
30                                 }
31                         }
32                 }
33
34                 // remove the listening socket from the clients-with-data array
35                 $key = array_search($mainSocket, $read);
36                 unset($read[$key]);
37         }
38
39         // loop through all the clients that have data to read from
40         foreach ($read as $read_sock) {
41                 // Get client data
42                 socket_getpeername($read_sock, $ip);
43
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);
47
48                 // check if the client is disconnected
49                 if (($data === FALSE) || (in_array(strtolower(trim($data)), $leaving))) {
50
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)."");
55
56                         // Notify all chatter
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");
61                                         }
62                                 }
63                         }
64
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);
69                         continue;
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");
75                                 $data = "";
76                                 continue;
77                         }
78
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");
84                                 }
85
86                                 // Quit him
87                                 socket_shutdown($client, 2);
88                                 socket_close($client);
89                         } // end foreach
90
91                         // Leave the loop
92                         $data = "";
93                         $clients = array();
94                         continue;
95                 }
96
97                 // trim off the trailing/beginning white spaces
98                 $data = trim($data);
99
100                 // Test for HTML codes
101                 $tags = strip_tags($data);
102
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");
107
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) {
110
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)
113                                         continue;
114
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");
117
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");
127                 }
128         } // end of reading foreach
129 }
130
131 // close the listening socket
132 socket_close($mainSocket);
133
134 ?>
135 <?php
136 /**
137  * A TCP ??? listener
138  *
139  * @author              Roland Haeder <webmaster@ship-simu.org>
140  * @version             0.0.0
141  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
142  * @license             GNU GPL 3.0 or any newer version
143  * @link                http://www.ship-simu.org
144  *
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.
149  *
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.
154  *
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/>.
157  */
158 class ???Listener extends BaseListener implements Listenable, Visitable {
159         /**
160          * Protected constructor
161          *
162          * @return      void
163          */
164         protected function __construct () {
165                 // Call parent constructor
166                 parent::__construct(__CLASS__);
167         }
168
169         /**
170          * Creates an instance of this class
171          *
172          * @param       $nodeInstance           A NodeHelper instance
173          * @return      $listenerInstance       An instance a prepared listener class
174          */
175         public final static function create???Listener (NodeHelper $nodeInstance) {
176                 // Get new instance
177                 $listenerInstance = new ???Listener();
178
179                 // Set the application instance
180                 $listenerInstance->setNodeInstance($nodeInstance);
181
182                 // Return the prepared instance
183                 return $listenerInstance;
184         }
185
186         /**
187          * Initializes the listener by setting up the required socket server
188          *
189          * @return      void
190          * @todo        0% done
191          */
192         public function initListener() {
193                 $this->partialStub('Need to implement this method.');
194         }
195
196         /**
197          * "Listens" for incoming network packages
198          *
199          * @return      void
200          * @todo        0% done
201          */
202         public function doListen() {
203                 $this->partialStub('Need to implement this method.');
204         }
205 }
206
207 // [EOF]
208 ?>