5 * This class will handle all communications with a server
9 * Licensed under The MIT License
10 * Redistributions of files must retain the above copyright notice.
13 * @copyright 2010 Skyler Sully
16 * @version $Revision: $
17 * @modifiedby $LastChangedBy: ssully$
18 * @lastmodified $Date: $
19 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
24 * Address of the server
32 * Port to connect to on server
40 * Connection to the server
48 * Tells if client is connected
66 function __construct($address, $port, $mode = 1) {
67 $this->setup($address, $port, $mode);
74 function __destruct() {
79 * Sets the address and port of the server
84 function setup($address, $port, $mode = 1) {
85 $this->address
= $address;
88 $this->connected
= false
;
89 $this->expectWelcome
= false
;
97 * Initialization function to be extended by descendent classes
102 function _initialize() {
107 * Connects to the server using $address and $port
110 * @param $expect_welcome - should we expect a welcome message
111 * @param $welcome - welcome message
112 * @return boolean - true if connected, false if not connected
114 function connect($expectWelcome = false
, &$welcome = null
) {
115 if( $this->connected
) {
118 $this->connected
= false
;
119 $this->connection
= @fsockopen($this->address
, $this->port
, $errno, $errstr, 30);
120 $this->expectWelcome
= $expectWelcome;
121 if( !$this->connection
=== false
) {
122 $this->setBlockingMode($this->mode
);
123 $this->connected
= true
;
124 if( $expectWelcome === true
) {
125 $welcome = $this->read();
128 return $this->connected
;
132 * Disconnects from the server
136 function disconnect() {
137 if( is_resource($this->connection
) ) {
138 fclose($this->connection
);
140 $this->connected
= false
;
141 return !$this->connected
;
145 * Attempts to reconnect to the server
149 function reconnect() {
151 return $this->connect($this->expectWelcome
);
155 * Writes a message to the server
158 * @param String - Message to write to the server
159 * @return number of bytes written or false on error
161 function write($message) {
162 if( $this->connected
) {
163 if( !is_string($message) ) {
166 $message = trim($message).PHP_EOL
;
167 $result = @fputs($this->connection
, $message, strlen($message));
168 if( $result == false
) {
169 if( $this->reconnect() ) {
170 return $this->write($message);
172 return $this->connected
= false
;
183 * Reads a message from the server
186 * @return Response from server, or false on error
190 if( $this->connected
) {
192 $response = @fgets($this->connection
, 65535);
193 if($response === false
) {
194 if( $this->reconnect() ) {
195 return $this->read();
197 return $this->connected
= false
;
201 while($tmp = @fgets($this->connection
)) {
206 $response = '*** NOT CONNECTED ***';
209 return trim($response);
213 * Sets the blocking mode of the connection
219 function setBlockingMode($mode) {
220 if( !is_int($mode) ) {
228 stream_set_blocking($this->connection
, $this->mode
);
232 * Returns the connection status of the server
237 function isConnected() {
238 return $this->connected
;