]> git.mxchange.org Git - hub.git/blob - application/hub/main/listener/tcp/class_
Better TCP listener implemented, client pool added, some code moves
[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 ?>