adopt to winsock
[melted] / src / swig / php / client.php
1 <?php
2 /**
3 * Client class
4 *
5 * This class will handle all communications with a server
6 *
7 * PHP version 5.2.0+
8 *
9 * Licensed under The MIT License
10 * Redistributions of files must retain the above copyright notice.
11 *
12 * @filesource
13 * @copyright 2010 Skyler Sully
14 * @link %link%
15 * @since %since%
16 * @version $Revision: $
17 * @modifiedby $LastChangedBy: ssully$
18 * @lastmodified $Date: $
19 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
20 */
21 class Client
22 {
23 /**
24 * Address of the server
25 *
26 * @access public
27 * @var String
28 */
29 var $address;
30
31 /**
32 * Port to connect to on server
33 *
34 * @access public
35 * @var int
36 */
37 var $port;
38
39 /**
40 * Connection to the server
41 *
42 * @access public
43 * @var Resource
44 */
45 var $connection;
46
47 /**
48 * Tells if client is connected
49 *
50 * @access public
51 * @var boolean
52 */
53 var $connected;
54
55 /**
56 * Expect Welcome flag
57 *
58 * @access public
59 * @var boolean
60 */
61 var $expectWelcome;
62
63 /**
64 * Client Constructor
65 */
66 function __construct($address, $port, $mode = 1) {
67 $this->setup($address, $port, $mode);
68 $this->_initialize();
69 }
70
71 /**
72 * Client Destructor
73 */
74 function __destruct() {
75
76 }
77
78 /**
79 * Sets the address and port of the server
80 *
81 * @access public
82 * @return void
83 */
84 function setup($address, $port, $mode = 1) {
85 $this->address = $address;
86 $this->port = $port;
87
88 $this->connected = false;
89 $this->expectWelcome = false;
90 if( $mode > 1 ) {
91 $mode = 1;
92 }
93 $this->mode = $mode;
94 }
95
96 /**
97 * Initialization function to be extended by descendent classes
98 *
99 * @access protected
100 * @return void
101 */
102 function _initialize() {
103
104 }
105
106 /**
107 * Connects to the server using $address and $port
108 *
109 * @access public
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
113 */
114 function connect($expectWelcome = false, &$welcome = null) {
115 if( $this->connected ) {
116 $this->disconnect();
117 }
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();
126 }
127 }
128 return $this->connected;
129 }
130
131 /**
132 * Disconnects from the server
133 *
134 * @access public
135 */
136 function disconnect() {
137 if( is_resource($this->connection) ) {
138 fclose($this->connection);
139 }
140 $this->connected = false;
141 return !$this->connected;
142 }
143
144 /**
145 * Attempts to reconnect to the server
146 *
147 * @access public
148 */
149 function reconnect() {
150 $this->disconnect();
151 return $this->connect($this->expectWelcome);
152 }
153
154 /**
155 * Writes a message to the server
156 *
157 * @access public
158 * @param String - Message to write to the server
159 * @return number of bytes written or false on error
160 */
161 function write($message) {
162 if( $this->connected ) {
163 if( !is_string($message) ) {
164 return false;
165 }
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);
171 } else {
172 return $this->connected = false;
173 }
174 }
175 } else {
176 return false;
177 }
178
179 return $result;
180 }
181
182 /**
183 * Reads a message from the server
184 *
185 * @access public
186 * @return Response from server, or false on error
187 */
188 function read() {
189 $response = '';
190 if( $this->connected ) {
191 if( $this->mode ) {
192 $response = @fgets($this->connection, 65535);
193 if($response === false) {
194 if( $this->reconnect() ) {
195 return $this->read();
196 } else {
197 return $this->connected = false;
198 }
199 }
200 } else {
201 while($tmp = @fgets($this->connection)) {
202 $response .= $tmp;
203 }
204 }
205 } else {
206 $response = '*** NOT CONNECTED ***';
207 }
208
209 return trim($response);
210 }
211
212 /**
213 * Sets the blocking mode of the connection
214 *
215 * @access public
216 * @param $mode
217 * @return boolean
218 */
219 function setBlockingMode($mode) {
220 if( !is_int($mode) ) {
221 return false;
222 }
223 if( $mode == 0 ) {
224 $this->mode = 0;
225 } else {
226 $this->mode = 1;
227 }
228 stream_set_blocking($this->connection, $this->mode);
229 }
230
231 /**
232 * Returns the connection status of the server
233 *
234 * @access public
235 * @return boolean
236 */
237 function isConnected() {
238 return $this->connected;
239 }
240 }
241
242 ?>