123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- require_once(dirname(__FILE__) . '/compatibility.php');
- class SimpleStickyError {
- var $_error = 'Constructor not chained';
-
- function SimpleStickyError() {
- $this->_clearError();
- }
-
- function isError() {
- return ($this->_error != '');
- }
-
- function getError() {
- return $this->_error;
- }
-
- function _setError($error) {
- $this->_error = $error;
- }
-
- function _clearError() {
- $this->_setError('');
- }
- }
- class SimpleSocket extends SimpleStickyError {
- var $_handle;
- var $_is_open = false;
- var $_sent = '';
- var $lock_size;
-
- function SimpleSocket($host, $port, $timeout, $block_size = 255) {
- $this->SimpleStickyError();
- $error_number = 0;
- $error = '';
- if (! ($this->_handle = $this->_openSocket($host, $port, $error_number, $error, $timeout))) {
- $this->_setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
- return;
- }
- $this->_is_open = true;
- $this->_block_size = $block_size;
- SimpleTestCompatibility::setTimeout($this->_handle, $timeout);
- }
-
- function write($message) {
- if ($this->isError() || ! $this->isOpen()) {
- return false;
- }
- $count = fwrite($this->_handle, $message);
- if (! $count) {
- if ($count === false) {
- $this->_setError('Cannot write to socket');
- $this->close();
- }
- return false;
- }
- fflush($this->_handle);
- $this->_sent .= $message;
- return true;
- }
-
- function read() {
- if ($this->isError() || ! $this->isOpen()) {
- return false;
- }
- $raw = @fread($this->_handle, $this->_block_size);
- if ($raw === false) {
- $this->_setError('Cannot read from socket');
- $this->close();
- }
- return $raw;
- }
-
- function isOpen() {
- return $this->_is_open;
- }
-
- function close() {
- $this->_is_open = false;
- return fclose($this->_handle);
- }
-
- function getSent() {
- return $this->_sent;
- }
-
- function _openSocket($host, $port, &$error_number, &$error, $timeout) {
- return @fsockopen($host, $port, $error_number, $error, $timeout);
- }
- }
- class SimpleSecureSocket extends SimpleSocket {
-
- function SimpleSecureSocket($host, $port, $timeout) {
- $this->SimpleSocket($host, $port, $timeout);
- }
-
- function _openSocket($host, $port, &$error_number, &$error, $timeout) {
- return parent::_openSocket("tls://$host", $port, $error_number, $error, $timeout);
- }
- }
- ?>
|