3 * A TCP connection listener
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class TcpListener extends BaseListener implements Listenable {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Creates an instance of this class
38 * @param $nodeInstance A NodeHelper instance
39 * @return $listenerInstance An instance a prepared listener class
41 public final static function createTcpListener (NodeHelper $nodeInstance) {
43 $listenerInstance = new TcpListener();
45 // Set the application instance
46 $listenerInstance->setNodeInstance($nodeInstance);
48 // Set the protocol to TCP
49 $listenerInstance->setProtocol('tcp');
51 // Return the prepared instance
52 return $listenerInstance;
56 * Initializes the listener by setting up the required socket server
59 * @throws InvalidSocketException Thrown if the socket could not be initialized
61 public function initListener() {
62 // Create a streaming socket, of type TCP/IP
63 $mainSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
65 // Is the socket resource valid?
66 if (!is_resource($mainSocket)) {
67 // Something bad happened
68 throw new InvalidSocketException(array($this, gettype($mainSocket), 0, 'invalid'), BaseListener::EXCEPTION_INVALID_SOCKET);
71 // Get socket error code for verification
72 $socketError = socket_last_error($mainSocket);
74 // Check if there was an error else
75 if ($socketError > 0) {
77 throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET);
80 // Set the option to reuse the port
81 if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
82 // Get socket error code for verification
83 $socketError = socket_last_error($mainSocket);
86 $errorMessage = socket_strerror($socketError);
88 // Shutdown this socket
89 $this->shutdownSocket($mainSocket);
92 throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
95 // "Bind" the socket to the given address, on given port so this means
96 // that all connections on this port are now our resposibility to
97 // send/recv data, disconnect, etc..
98 if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) {
99 // Get socket error code for verification
100 $socketError = socket_last_error($mainSocket);
103 $errorMessage = socket_strerror($socketError);
105 // Shutdown this socket
106 $this->shutdownSocket($mainSocket);
109 throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
112 // Start listen for connections
113 if (!socket_listen($mainSocket)) {
114 // Get socket error code for verification
115 $socketError = socket_last_error($mainSocket);
118 $errorMessage = socket_strerror($socketError);
120 // Shutdown this socket
121 $this->shutdownSocket($mainSocket);
124 throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
127 // Set the main socket
128 $this->setSocketResource($mainSocket);
130 // Initialize the client pool instance
131 $poolInstance = ObjectFactory::createObjectByConfiguredName('client_pool_class', array($this));
134 $poolInstance->addClient($mainSocket);
136 // And add it to this listener
137 $this->setPoolInstance($poolInstance);
139 // Initialize iterator for listening on packages
140 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('network_listen_iterator_class', array($poolInstance->getPoolEntriesInstance()));
142 // Rewind it and remember it in this class
143 $iteratorInstance->rewind();
144 $this->setIteratorInstance($iteratorInstance);
146 // Initialize the network package handler
147 $packageInstance = ObjectFactory::createObjectByConfiguredName('tcp_network_package_handler_class');
149 // Set it in this class
150 $this->setPackageInstance($packageInstance);
153 $this->debugOutput('LISTENER: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
157 * "Listens" for incoming network packages
162 public function doListen () {
164 $readers = $this->getPoolInstance()->getAllSockets();
166 // Check if we have some clients left
167 $left = socket_select($readers, $writers = null, $excepts = null, 0, 150);
169 // Some new clients found?
175 // Do we have changed clients?
176 if (in_array($this->getSocketResource(), $readers)) {
178 $newSocket = socket_accept($this->getSocketResource());
180 // Add it to the clients
181 $this->getPoolInstance()->addClient($newSocket);
184 // Do we have to rewind?
185 if (!$this->getIteratorInstance()->valid()) {
187 $this->getIteratorInstance()->rewind();
190 // Get the current value
191 $current = $this->getIteratorInstance()->current();
194 $this->getPackageInstance()->processPackage($current);
196 // Advance to next entry. This should be the last line
197 $this->getIteratorInstance()->next();