分享

socket.php

 tttff 2010-06-11
<?php
/**
 * t_socket
 *
 * @version 0.1.2
 * @author     Jesus Lara <jesuslarag@gmail.com>
 * @license    http://www./copyleft/lesser.html  LGPL License 2.1
 * @copyright 2005-2009 Jesus Lara. All rights reserved.
 * @filesource
 * @package tomates
 * @subpackage socket
 * @uses operaciones básicas con sockets
 */
class t_socket {
        #properties
        protected $host = 'localhost';
        protected $port = 80;
        protected $username = '';
        protected $passwd = '';
        private $connected = false;
        private $errno =0;
        private $errstr = '';
        public $timeout = 20;
        protected $connex_timeout = 20;
        public $buffer = '';
        public $lastdata = '';
        public $status = 0;
        public $service_status =0;
        #para socket client php5
        public $protocol = 'tcp://'; #TCP o UDP
        public $strhost = 'tcp://localhost:80';
        #contiene el resource handle del socket creado
        public static $connection = null;
        #datos de la conexion:
        public $bytes_read = 0;
        public $bytes_write = 0;

        #arrays

        #single methods
        function status() {
                return $this->status;
        }

        function set_status($status) {
                $this->status = $status;
        }

        function connected() {
                return $this->connected;
        }

        function set_connected($status) {
                $this->connected = $status;
        }

        #constructor
        #Funcion constructora de la clase
        function __construct($host='', $port='', $timeout='') {
                $this->host = $host;
                $this->port = $port;
                $this->passwd = $password;
                $this->status = array();
                $this->connection =null;
                $this->buffer = null;
                $this->lastdata = '';
                if ($timeout!='') {
                        $this->timeout = $timeout;
                        $this->connex_timeout = $timeout;
                }
                set_time_limit($this->timeout);
                //stream_set_timeout
                //stream_get_meta_data
                //pfsockopen(string hostname, int port [,int errno] [,string errstr] [,int timeout]); conexion persistente
                //echo 'constructor ejecutado';
        }
        #destructor
        function __destruct() {
                $this->close();
                unset($this->connection);
        }

        #methods
        public function connect($persistent=false) {
                if ($persistent) {
                        $this->connection = pfsockopen($this->host,  $this->port, $errno, $errstr, $this->timeout);
                        //echo 'usando conexiones persistentes<br />';
                } else {
                        $this->connection = fsockopen($this->host,  $this->port, $errno, $errstr, $this->timeout);
                }
                if (!$this->connection) {
                        echo 'Error: ' . $this->errno . '<br /><b>' . $this->errstr . '</b>';
                        return false;
                }
                #defino el tiempo de espera de la conexion
                stream_set_timeout($this->connection, $this->connex_timeout);
                if ($this->connection) {
                        #procedo a hacer las evaluaciones necesarias del socket
                        $this->connected = true;
                        $this->service_status = 1;
                        #obtiene un array de estado, timed_out, blocked, eof (end of file), unread_bytes (cantidad de bytes en el buffer interno)
                        #mode (modo de acceso) y uri (direccion actual del recurso)
                        $this->status = stream_get_meta_data($this->connection);
                        return true;
                } else {
                        return false;
                }
        }

        public function close() {
                if ($this->connected) {
                        fclose($this->connection);
                        $this->connected = false;
                }
        }


        function read() {
                if ($this->connected) {
                        socket_set_timeout($this->connection,0,100*1000);
                        $linecount = 0;
                        while ($line = fgets($this->connection)) {
                                $this->buffer[] = trim($line);
                                // DEBUG BLOCK
                                //echo("<-" . trim($line) . "\n");
                                // END DEBUG BLOCK
                                $linecount++;
                        }
                        $this->lastdata = $line;
                        $this->bytes_read = strlen($this->lastdata);
                        $this->status = stream_get_meta_data($this->connection);
                        return $linecount;
                } else {
                        return false;
                }
        }

        function write($data) {
                if ($this->connected) {
                        // DEBUG BLOCK
                        //echo("->" . $data . "\n");
                        // END DEBUG BLOCK
                        $nbytes = fwrite($this->connection, $data, strlen($data));
                        if ($nbytes) {
                                $this->bytes_write = $nbytes;
                                return true;
                        } else {
                                return false;
                        }
                } else {
                        return false;
                }
        }

        function clear_buffer() {
                $this->buffer = '';
                unset($this->buffer);
                return true;
        }


}#end of class

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多