[ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of Clearbricks. 4 # Copyright (c) 2006 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # Clearbricks is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Clearbricks is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Clearbricks; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 /** 24 @defgroup CB_NET Clearbricks network classes 25 @ingroup CLEARBRICKS 26 */ 27 28 /** 29 @ingroup CB_NET 30 @brief Sockets handler 31 32 Base sockets client class connections. Once socket is open, you can read 33 results with a non recursive iterator in foreach() loop. 34 */ 35 class netSocket 36 { 37 protected $_host; ///< <b>string</b> Server host 38 protected $_port; ///< <b>integer</b> Server port 39 protected $_transport = ''; ///< <b>string</b> Server transport 40 protected $_timeout; ///< <b>integer</b> Connection timeout 41 42 protected $_handle; ///< <b>resource</b> Resource handler 43 44 /** 45 Class constructor 46 47 @param host <b>string</b> Server host 48 @param port <b>integer</b> Server port 49 @param timeout <b>integer</b> Connection timeout 50 */ 51 public function __construct($host,$port,$timeout=10) 52 { 53 $this->_host = $host; 54 $this->_port = abs((integer) $port); 55 $this->_timeout = abs((integer) $timeout); 56 } 57 58 /** 59 Object destructor 60 61 Calls close() method 62 */ 63 public function __destruct() 64 { 65 $this->close(); 66 } 67 68 /** 69 Returns host if <var>$host</var> is not set, sets it otherwise. 70 71 @param host <b>string</b> Server host 72 */ 73 public function host($host=null) 74 { 75 if ($host) { 76 $this->_host = $host; 77 return true; 78 } 79 return $this->_host; 80 } 81 82 /** 83 Returns port if <var>$port</var> is not set, sets it otherwise. 84 85 @param port <b>integer</b> Server port 86 */ 87 public function port($port=null) 88 { 89 if ($port) { 90 $this->_port = abs((integer) $port); 91 return true; 92 } 93 return $this->_port; 94 } 95 96 /** 97 Returns timeout if <var>$timeout</var> is not set, sets it otherwise. 98 99 @param timeout <b>integer</b> Connection timeout 100 */ 101 public function timeout($timeout) 102 { 103 if ($timeout) { 104 $this->_timeout = abs((integer) $timeout); 105 return true; 106 } 107 return $this->_timeout; 108 } 109 110 /** 111 Opens socket connection. Returns an object of type netSocketIterator which 112 can be iterate with a simple foreach loop. 113 114 @return <b>netSocketIterator</b> 115 */ 116 public function open() 117 { 118 $handle = @fsockopen($this->_transport.$this->_host,$this->_port,$errno,$errstr,$this->_timeout); 119 if (!$handle) { 120 throw new Exception('Socket error: '.$errstr.' ('.$errno.')'); 121 } 122 $this->_handle = $handle; 123 return $this->iterator(); 124 } 125 126 /** 127 Closes socket connection 128 */ 129 public function close() 130 { 131 if ($this->isOpen()) { 132 fclose($this->_handle); 133 $this->_handle = null; 134 } 135 } 136 137 /** 138 Sends data to current socket and returns an object of type 139 netSocketIterator which can be iterate with a simple foreach loop. 140 141 <var>$data</var> can be a string or an array of simple lines. 142 143 Example: 144 145 @verbatim 146 <?php 147 $s = new netSocket('www.google.com',80,2); 148 $s->open(); 149 $data = array( 150 'GET / HTTP/1.0' 151 ); 152 foreach($s->write($data) as $v) { 153 echo $v."\n"; 154 } 155 $s->close(); 156 ?> 157 @endverbatim 158 159 @param data <b>mixed</b> Data to send 160 @return <b>netSocketIterator</b> 161 */ 162 public function write($data) 163 { 164 if (!$this->isOpen()) { 165 return false; 166 } 167 168 if (is_array($data)) { 169 $data = implode("\r\n",$data)."\r\n\r\n"; 170 } 171 172 fwrite($this->_handle,$data); 173 return $this->iterator(); 174 } 175 176 /** 177 Flushes socket write buffer. 178 */ 179 public function flush() 180 { 181 if (!$this->isOpen()) { 182 return false; 183 } 184 185 fflush($this->_handle); 186 } 187 188 /** 189 Returns an object of type netSocketIterator 190 */ 191 protected function iterator() 192 { 193 if (!$this->isOpen()) { 194 return false; 195 } 196 return new netSocketIterator($this->_handle); 197 } 198 199 /** 200 Returns true if socket connection is open. 201 202 @return <b>boolean</b> 203 */ 204 public function isOpen() 205 { 206 return is_resource($this->_handle); 207 } 208 } 209 210 class netSocketIterator implements Iterator 211 { 212 protected $_handle; 213 protected $_index; 214 215 public function __construct(&$handle) 216 { 217 if (!is_resource($handle)) { 218 throw new Exception('Handle is not a resource'); 219 } 220 $this->_handle =& $handle; 221 $this->_index = 0; 222 } 223 224 /* Iterator methods 225 --------------------------------------------------- */ 226 public function rewind() { 227 # Nothing 228 } 229 230 public function valid() { 231 return !feof($this->_handle); 232 } 233 234 public function next() { 235 $this->_index++; 236 } 237 238 public function key() { 239 return $this->_index; 240 } 241 242 public function current() { 243 return fgets($this->_handle,4096); 244 } 245 } 246 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |