3 * base include file for SimpleTest
5 * @subpackage MockObjects
6 * @version $Id: socket.php,v 1.21 2004/06/30 22:13:08 lastcraft Exp $
10 * include SimpleTest files
12 require_once(dirname(__FILE__
) . '/options.php');
16 * Stashes an error for later. Useful for constructors
17 * until PHP gets exceptions.
19 * @subpackage WebTester
22 var $_error = 'Constructor not chained';
25 * Sets the error to empty.
28 function StickyError() {
33 * Test for an outstanding error.
34 * @return boolean True if there is an error.
38 return ($this->_error
!= '');
42 * Accessor for an outstanding error.
43 * @return string Empty string if no error otherwise
52 * Sets the internal error.
53 * @param string Error message to stash.
56 function _setError($error) {
57 $this->_error
= $error;
61 * Resets the error state to no error.
64 function _clearError() {
70 * Wrapper for TCP/IP socket.
72 * @subpackage WebTester
74 class SimpleSocket
extends StickyError
{
80 * Opens a socket for reading and writing.
81 * @param string $host Hostname to send request to.
82 * @param integer $port Port on remote machine to open.
83 * @param integer $timeout Connection timeout in seconds.
86 function SimpleSocket($host, $port, $timeout) {
88 $this->_is_open
= false
;
90 if (! ($this->_handle
= $this->_openSocket($host, $port, $error_number, $error, $timeout))) {
91 $this->_setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
94 $this->_is_open
= true
;
95 SimpleTestCompatibility
::setTimeout($this->_handle
, $timeout);
99 * Writes some data to the socket and saves alocal copy.
100 * @param string $message String to send to socket.
101 * @return boolean True if successful.
104 function write($message) {
105 if ($this->isError() ||
! $this->isOpen()) {
108 $count = fwrite($this->_handle
, $message);
110 if ($count === false
) {
111 $this->_setError('Cannot write to socket');
116 fflush($this->_handle
);
117 $this->_sent
.= $message;
122 * Reads data from the socket.
123 * @param integer $block_size Size of chunk to read.
124 * @return integer Incoming bytes. False
128 function read($block_size = 255) {
129 if ($this->isError() ||
! $this->isOpen()) {
132 $raw = fread($this->_handle
, $block_size);
133 if ($raw === false
) {
134 $this->_setError('Cannot read from socket');
141 * Accessor for socket open state.
142 * @return boolean True if open.
146 return $this->_is_open
;
150 * Closes the socket preventing further reads.
151 * Cannot be reopened once closed.
152 * @return boolean True if successful.
156 $this->_is_open
= false
;
157 return fclose($this->_handle
);
161 * Accessor for content so far.
162 * @return string Bytes sent only.
170 * Actually opens the low level socket.
171 * @param string $host Host to connect to.
172 * @param integer $port Port on host.
173 * @param integer $error_number Recipient of error code.
174 * @param string $error Recipoent of error message.
175 * @param integer $timeout Maximum time to wait for connection.
178 function _openSocket($host, $port, &$error_number, &$error, $timeout) {
179 return @fsockopen
($host, $port, $error_number, $error, $timeout);
184 * Wrapper for TCP/IP socket over TLS.
185 * @package SimpleTest
186 * @subpackage WebTester
188 class SimpleSecureSocket
extends SimpleSocket
{
191 * Opens a secure socket for reading and writing.
192 * @param string $host Hostname to send request to.
193 * @param integer $port Port on remote machine to open.
194 * @param integer $timeout Connection timeout in seconds.
197 function SimpleSecureSocket($host, $port, $timeout) {
198 $this->SimpleSocket($host, $port, $timeout);
202 * Actually opens the low level socket.
203 * @param string $host Host to connect to.
204 * @param integer $port Port on host.
205 * @param integer $error_number Recipient of error code.
206 * @param string $error Recipient of error message.
207 * @param integer $timeout Maximum time to wait for connection.
210 function _openSocket($host, $port, &$error_number, &$error, $timeout) {
211 return parent
::_openSocket("tls://$host", $port, $error_number, $error, $timeout);