3 * A general ConnectionHelper class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 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 BaseConnectionHelper extends BaseHubHelper implements Registerable, ProtocolHandler {
28 private $protocol = 'invalid';
43 private $sentData = 0;
51 * Protected constructor
53 * @param $className Name of the class
56 protected function __construct ($className) {
57 // Call parent constructor
58 parent::__construct($className);
60 // Register this connection helper
61 Registry::getRegistry()->addInstance('connection', $this);
65 * Getter for port number to satify ProtocolHandler
67 * @return $port The port number
69 public final function getPort () {
74 * Setter for port number to satify ProtocolHandler
76 * @param $port The port number
79 protected final function setPort ($port) {
86 * @return $protocol Used protocol
88 public final function getProtocol () {
89 return $this->protocol;
95 * @param $protocol Used protocol
98 protected final function setProtocol ($protocol) {
99 $this->protocol = $protocol;
103 * Getter for IP address
105 * @return $address The IP address
107 public final function getAddress () {
108 return $this->address;
112 * Setter for IP address
114 * @param $address The IP address
117 protected final function setAddress ($address) {
118 $this->address = $address;
122 * Sends raw package data to the recipient
124 * @param $packageData Raw package data
126 *@ throws InvalidSocketException If we got a problem with this socket
128 public function sendRawPackageData (array $packageData) {
129 // We need to "package" all data. This is done by a implode()
130 $rawData = implode(NetworkPackage::PACKAGE_DATA_SEPERATOR, $packageData);
132 // Get socket resource
133 $socketResource = $this->getSocketResource();
136 $numBytes = @socket_write($socketResource, $rawData, $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length') - $this->offset);
138 // If there was an error, we don't continue here
139 if ($numBytes === false) {
140 // Get socket error code for verification
141 $socketError = socket_last_error($socketResource);
144 $errorMessage = socket_strerror($socketError);
146 // Shutdown this socket
147 $this->shutdownSocket($socketResource);
150 throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
153 // How much has been sent?
154 die('num='.$numBytes."\n");
158 * Getter for real class name
160 * @return $class Name of this class
162 public function __toString () {
163 // Class name representation
164 $class = $this->getAddress() . ':' . $this->getPort() . ':' . parent::__toString();