Overview

Packages

  • Auth
  • Config
  • Controller
  • Date
  • Db
  • Feed
    • Abstract
    • Writers
  • File
    • Decorator
  • Form
    • Element
  • Image
  • Log
    • Writer
  • Net
    • Exception
  • None
  • PHP
  • PHPMailer
  • Session
  • Util
  • Validate
    • Validator
  • Zend
    • Registry

Classes

  • Net_HTTPDownload
  • Net_Socket
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Pry Framework
  4:  *
  5:  * LICENSE
  6:  *
  7:  * This source file is subject to the new BSD license that is bundled
  8:  * with this package in the file LICENSE.txt.
  9:  * 
 10:  * @version $Revision: 213 $
 11:  */
 12: 
 13: /**
 14:  * Wrapper pour l'utilsation de socket
 15:  * @category Pry
 16:  * @package Net
 17:  * @version 1.0
 18:  * @author Olivier ROGER <oroger.fr>
 19:  *
 20:  */
 21: class Net_Socket
 22: {
 23:     /** Etat connecté */
 24:     const CONNECTED         = 1;
 25:     /** Etat non connecté */
 26:     const DISCONNECTED      = 0;
 27:     
 28:     /**
 29:      * Hote vers lequel se connecter. Ip ou domaine
 30:      * @var string 
 31:      */
 32:     protected $host         = "";
 33:     
 34:     /**
 35:      * Port de connexion
 36:      * @var int 
 37:      */
 38:     protected $port         = 0;
 39:     
 40:     /**
 41:      * Timeout de connexion en seconde. Egalement utilisé pour les timeout de lecture/écriture
 42:      * @var int 
 43:      */
 44:     protected $timeout      = 5;
 45:     
 46:     /**
 47:      * Socket
 48:      * @var resource 
 49:      */
 50:     protected $socket       = null;
 51:     
 52:     /**
 53:      * Etat de la socket
 54:      * @var int 
 55:      */
 56:     protected $state        = 0;
 57:     
 58:     /**
 59:      * Socket bloquante ou non
 60:      * @var boolean 
 61:      */
 62:     protected $isBlocking   = false;
 63:     
 64:     /**
 65:      * Création de la socket. Initialise également l'error handler pour transformer les 
 66:      * erreur de socket en exception.
 67:      * @param type $blocking 
 68:      */
 69:     public function __construct($blocking = true)
 70:     {
 71:         $this->isBlocking   = $blocking;
 72:         Net_Exception_Handler::initialize();
 73:         $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
 74:     }
 75:     
 76:     /**
 77:      * Connexion de la socket sur $host:$port avec un delay de $timeout
 78:      * @param type $host
 79:      * @param type $port
 80:      * @param type $timeout
 81:      * @return boolean
 82:      * @throws RuntimeException 
 83:      */
 84:     public function connect($host,$port,$timeout = 15)
 85:     {
 86:         $this->host         = $host;
 87:         $this->port         = $port;
 88:         $this->timeout      = $timeout;
 89:         if(!empty($this->socket) && is_resource($this->socket))
 90:         {
 91:             //Timeout de lecture
 92:             socket_set_option($this->socket,SOL_SOCKET,SO_RCVTIMEO,array(
 93:                     'sec'=>$this->timeout,
 94:                     'usec'=>0)
 95:             );
 96:             //Timeout d'écriture
 97:             socket_set_option($this->socket,SOL_SOCKET,SO_SNDTIMEO,array(
 98:                     'sec'=>$this->timeout,
 99:                     'usec'=>0)
100:             );
101: 
102:             $connect = socket_connect($this->socket, $this->host, $this->port);
103:             if(!$connect)
104:             {
105:                 socket_close($this->socket);
106:                 throw new RuntimeException('Can\'t connect to '.$this->host.':'.$this->port.'. 
107:                     Reason : '.socket_strerror(socket_last_error()));
108:             }
109:         }
110:         else
111:         {
112:             return false;
113:         }
114:         
115:         $this->state = self::CONNECTED;
116:         return true;
117:     }
118:     
119:     /**
120:      * Déconnecte la socket
121:      * @return boolean 
122:      */
123:     public function disconnect()
124:     {
125:         if($this->isConnected())
126:         {
127:             socket_close($this->socket);
128:             $this->state = self::DISCONNECTED;
129:             return true;
130:         }
131:         
132:         return false;
133:     }
134:     
135:     /**
136:      * Ecrit sur la socket le contenu de $buffer
137:      * @param string $buffer
138:      * @return mixed Nombre d'octet ou false en cas d'erreur
139:      * @throws InvalidArgumentException 
140:      */
141:     public function write($buffer)
142:     {
143:         if($this->isConnected())
144:         {
145:             return socket_write($this->socket,$buffer, strlen($buffer));
146:         }
147:         throw new InvalidArgumentException('No active connection');
148:     }
149:     
150:     /**
151:      * Lecture sur la socket
152:      * @param int $size nombre d'octet à lire
153:      * @return boolean
154:      * @throws RuntimeException 
155:      */
156:     public function read($size = 2048)
157:     {
158:         if($this->isConnected())
159:         {
160:             $datas = socket_read($this->socket, $size, PHP_BINARY_READ);
161:             if($datas !== false)
162:             {
163:                 return $datas;
164:             }
165:             else
166:             {
167:                 Throw new RuntimeException("Lecture impossible : ".socket_strerror(socket_last_error($this->socket)));
168:             }
169:             
170:             return false;
171:         }
172:     }
173:     
174:     /**
175:      * Vérifie si la socket est connecté
176:      * @return boolean 
177:      */
178:     public function isConnected()
179:     {
180:         return (is_resource($this->socket) && $this->state == self::CONNECTED);
181:     }
182:     
183:     public function __destruct()
184:     {
185:         Net_Exception_Handler::uninitialize();
186:     }
187: }
188: 
Pry Framework API documentation generated by ApiGen 2.6.1