| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | stubClassLoader::load('net::stubbles::lang::exceptions::stubIllegalStateException', |
|---|
| 10 | 'net::stubbles::peer::stubConnectionException' |
|---|
| 11 | ); |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | class stubSocket extends stubBaseObject |
|---|
| 19 | { |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | protected $host = null; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | protected $port = 80; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | protected $timeout = 30; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | protected $fp = null; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | public function __construct($host, $port = 80) |
|---|
| 52 | { |
|---|
| 53 | $this->host = $host; |
|---|
| 54 | $this->port = $port; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | public function __destruct() |
|---|
| 61 | { |
|---|
| 62 | $this->disconnect(); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | public function connect() |
|---|
| 72 | { |
|---|
| 73 | if ($this->isConnected() === true) { |
|---|
| 74 | return true; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | if (null == $this->host) { |
|---|
| 78 | return false; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | $errno = 0; |
|---|
| 82 | $errstr = ''; |
|---|
| 83 | $this->fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); |
|---|
| 84 | if (false === $this->fp) { |
|---|
| 85 | $this->fp = null; |
|---|
| 86 | throw new stubConnectionException('Connecting to ' . $this->host . ':' . $this->port . ' within ' . $this->timeout . ' seconds failed: ' . $errstr . ' (' . $errno . ').'); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | socket_set_timeout($this->fp, $this->timeout); |
|---|
| 90 | return true; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | public function disconnect() |
|---|
| 97 | { |
|---|
| 98 | if ($this->isConnected() === true) { |
|---|
| 99 | fclose($this->fp); |
|---|
| 100 | $this->fp = null; |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | public function setTimeout($timeout) |
|---|
| 110 | { |
|---|
| 111 | $this->timeout = $timeout; |
|---|
| 112 | if ($this->isConnected() === true) { |
|---|
| 113 | socket_set_timeout($this->fp, $this->timeout); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | public function read($length = 4096) |
|---|
| 126 | { |
|---|
| 127 | if ($this->isConnected() === false) { |
|---|
| 128 | throw new stubIllegalStateException('Can not read on unconnected socket.'); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | $data = fgets($this->fp, $length); |
|---|
| 132 | if (false === $data) { |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | if ($this->eof() === true) { |
|---|
| 136 | return null; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | throw new stubConnectionException('Reading of ' . $length . ' bytes failed.'); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | return $data; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | public function readLine($length = 4096) |
|---|
| 152 | { |
|---|
| 153 | return rtrim($this->read($length)); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | public function readBinary($length = 1024) |
|---|
| 165 | { |
|---|
| 166 | if ($this->isConnected() === false) { |
|---|
| 167 | throw new stubIllegalStateException('Can not read on unconnected socket.'); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | $data = fread($this->fp, $length); |
|---|
| 171 | if (false === $data) { |
|---|
| 172 | throw new stubConnectionException('Reading of ' . $length . ' bytes failed.'); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | return $data; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | public function write($data) |
|---|
| 187 | { |
|---|
| 188 | if ($this->isConnected() === false) { |
|---|
| 189 | throw new stubIllegalStateException('Can not write on unconnected socket.'); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | $length = fputs($this->fp, $data, strlen($data)); |
|---|
| 193 | if (false === $length) { |
|---|
| 194 | throw new stubConnectionException('"Writing of ' . strlen($data) . ' bytes failed.'); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | return $length; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | public function getHost() |
|---|
| 206 | { |
|---|
| 207 | return $this->host; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | public function getPort() |
|---|
| 216 | { |
|---|
| 217 | return $this->port; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | public function getTimeout() |
|---|
| 226 | { |
|---|
| 227 | return $this->timeout; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | public function isConnected() |
|---|
| 236 | { |
|---|
| 237 | return is_resource($this->fp); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | public function eof() |
|---|
| 246 | { |
|---|
| 247 | if ($this->isConnected() === true) { |
|---|
| 248 | return feof($this->fp); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | return true; |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | ?> |
|---|