* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); // Set handler name $this->setHandlerName('tcp'); } /** * Creates an instance of this class * * @return $handlerInstance An instance of a Networkable class */ public static final function createTcpRawDataHandler () { // Get new instance $handlerInstance = new TcpRawDataHandler(); // Return the prepared instance return $handlerInstance; } /** * Processes raw data from given resource. This is mostly useful for TCP * package handling and is implemented in the TcpListener class * * @param $resource A valid resource identifier * @return void */ public function processRawDataFromResource ($resource) { // Check the resource if (!is_resource($resource)) { // Throw an exception throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); } // END - if // Reset error code to unhandled $this->setErrorCode(self::SOCKET_ERROR_UNHANDLED); // Init variables $decodedData = false; // Debug message //* NOISY-DEBUG: */ $this->debugOutput('HANDLER: Handling TCP package from peer ' . $resource); /* * Read the raw data from socket. If you change PHP_BINARY_READ to * PHP_NORMAL_READ, this line will endless block. We only have * simultanous threads and no real threads. */ $rawData = socket_read($resource, $this->getConfigInstance()->getConfigEntry('tcp_buffer_length'), PHP_BINARY_READ); // Debug output of read data length //* NOISY-DEBUG: */ $this->debugOutput('TCP-HANDLER: rawData[]=' . strlen($rawData)); // Is it valid? if (($rawData === false) || (socket_last_error($resource) > 0)) { // Network error or connection lost $this->setErrorCode(socket_last_error($resource)); } elseif (empty($rawData)) { // The peer did send nothing to us $this->setErrorCode(self::SOCKET_ERROR_EMPTY_DATA); } else { /* * Low-level checks of the raw data went all fine, we can now * decode the raw data. This may still fail because of invalid * encoded data. */ $decodedData = $this->getInputStreamInstance()->streamData($rawData); } // Add the (maybe above decoded) data to the stacker $this->addDecodedDataToStacker($decodedData); } } // [EOF] ?>