1 | <?php |
||
47 | class SocketClient |
||
48 | { |
||
49 | |||
50 | const BUFFER_LENGTH = 4096; |
||
51 | |||
52 | /** |
||
53 | * Resource. |
||
54 | * |
||
55 | * @var resource |
||
56 | */ |
||
57 | protected $resource; |
||
58 | |||
59 | /** |
||
60 | * Address. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $address; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Options used to create a stream context |
||
69 | * @see http://php.net/manual/en/function.stream-context-create.php |
||
70 | * |
||
71 | 6 | * @var array |
|
72 | */ |
||
73 | 6 | protected $options; |
|
74 | 6 | ||
75 | /** |
||
76 | * Constructor takes address as argument. |
||
77 | * |
||
78 | * @param string $address |
||
79 | */ |
||
80 | public function __construct($address, $options = null) |
||
85 | 6 | ||
86 | 3 | /** |
|
87 | 3 | * Connect. |
|
88 | 3 | * |
|
89 | * @param integer $timeout Timeout for connection |
||
90 | * @param boolean $persistent Persitent connection |
||
91 | * @return void |
||
92 | 6 | */ |
|
93 | 6 | public function connect($timeout = 30, $persistent = false) |
|
123 | |||
124 | /** |
||
125 | * Reconnect and optionally use different address. |
||
126 | * |
||
127 | * @param string $address |
||
128 | * @param integer $timeout |
||
129 | 3 | * @param bool $persistent |
|
130 | */ |
||
131 | 3 | public function reconnect($address = null, $timeout = 30, $persistent = false) |
|
141 | |||
142 | /** |
||
143 | * Close stream. |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | public function close() |
||
151 | |||
152 | 3 | /** |
|
153 | * Set stream blocking mode. |
||
154 | 3 | * |
|
155 | * @param boolean $flag Flag |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function setBlocking($flag = true) |
||
163 | |||
164 | 3 | /** |
|
165 | * Read from stream. |
||
166 | 3 | * |
|
167 | 3 | * @param integer $length Bytes to read |
|
168 | 3 | * @return string |
|
169 | 3 | */ |
|
170 | public function read($length = self::BUFFER_LENGTH) |
||
174 | |||
175 | /** |
||
176 | * Write to stream. |
||
177 | * |
||
178 | * @param string $string String |
||
179 | * @param integer $length Limit |
||
180 | * @return void |
||
181 | */ |
||
182 | public function write($string, $length = null) |
||
190 | |||
191 | /** |
||
192 | * Enable/disable cryptography on stream. |
||
193 | * |
||
194 | * @param boolean $enable Flag |
||
195 | * @param integer $cryptoType One of the STREAM_CRYPTO_METHOD_* constants. |
||
196 | * @return void |
||
197 | * @throws InvalidArgumentException |
||
198 | */ |
||
199 | public function crypto($enable, $cryptoType = null) |
||
212 | 3 | ||
213 | /** |
||
214 | * Get socket stream. |
||
215 | * |
||
216 | * @return resource |
||
217 | */ |
||
218 | public function getResource() |
||
222 | |||
223 | /** |
||
224 | * Return address. |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getAddress() |
||
232 | } |
||
233 |