Connection.php

Gesponsorde koppelingen

PHP script bestanden

  1. Connection.php
  2. Listening.php
  3. Poller.php
  4. HttpRequest.php
  5. StringBuffer.php
  6. Accept.php
  7. Status.php
  8. Error.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
* Streams library
* Copyright (c) 2012, Remco Pander
*
* This library consists of a set of classes that can be used to create high-performing
* applications using full-duplex connections. It uses the built-in stream functions
* provided with php.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* @package    Streams
* @author     Remco Pander <[email protected]>
* @version    v1 initial release
* @copyright  Copyright (c) 2012, Remco Pander
*/


namespace Streams;
use \LogicException;
use \InvalidArgumentException;

class Connection
{
        // Global constants
        const CERT_FILE_PATH     = './';
        const CONN_MAX_TIME      = 5;
        
        // Connection details
        protected $r_Socket      = null;
        protected $i_Status      = 0;
        private $s_rAddress      = null;
        private $s_rPort         = null;
        private $b_Secure        = false;
        private $s_bindTo        = null;
        private $i_timeConnected = 0;
        
        // Buffers for data
        private  $readBuffer     = null;
        private  $writeBuffer    = null;
        
        // Callbacks
        public  $onConnect       = null;
        public  $onTerminate     = null;
        public  $onRead          = null;
        public  $onWrite         = null;
        public  $onError         = null;
        
        // Create a new connection
        public function __construct ($s_Address, $i_Port, $s_bindAddress = null, $b_Secure = false)
        {

                // Resolve address
                if (!@gethostbyname ($s_Address) && !@gethostbyaddr ($s_Address) || $i_Port > 65535 || $i_Port < 2)
                {

                        throw new InvalidArgumentException ('Address "' . $s_Address . ':' . $i_Port . '" could not be resolved or is invalid.');
                }

                
                $this -> s_rAddress = $s_Address;
                $this -> i_rPort    = $i_Port;
                $this -> s_bindTo   = $s_bindAddress;
                $this -> b_Secure   = $b_Secure;
                
                if ($this -> b_Secure)
                {

                        if (!extension_loaded ('openssl'))
                        {

                                throw new LogicException ('Cannot create secure connection withouth the OpenSSL extension loaded.');
                        }

                        
                        if (!file_exists (self :: CERT_FILE_PATH))
                        {

                                throw new LogicException ('Cannot create secure connection withouth a certificate file.');
                        }
                }

                
                // Create buffers
                $this -> readBuffer  = new StringBuffer ();
                $this -> writeBuffer = new StringBuffer ();
        }

        
        // Destructor, closes the connection if it is still open
        public function __destruct ()
        {

                // Check if we are connected.
                if ($this -> isConnected ())
                {

                        $this -> disconnect ();
                }
else
                {
                        $this -> _forceClose ();
                }

                
                // Empty buffer
                $this -> readBuffer  = null;
                $this -> writeBuffer = null;
        }

        
        public function __get ($s_Member)
        {

                switch ($s_Member)
                {
                        case
'readBuffer':
                                return $this -> readBuffer;
                                
                        case
'writeBuffer':
                                return $this -> writeBuffer;
                                
                        default:

                                throw new LogicException ('Acces to "' . $s_Member . '" is not allowed.');
                }
        }

        
        // Returns address
        public function __toString ()
        {

                return $this -> s_rAddress . ':' . $this -> i_rPort;
        }

        
        // Returns the time we connected
        public function getTimeConnected ()
        {

                return $this -> i_timeConnected;
        }

        
        // Returns the current status
        public function getStatus ()
        {

                return $this -> i_Status;
        }

                
        // Returns whether the socket is connected or not
        public function isConnected ()
        {

                return ($this -> i_Status == Status :: CONNECTED);
        }

        
        // Returns whether this socket is using a secure connection
        public function isSecure ()
        {

                return $this -> b_Secure;
        }

        
        // Checks if the socket has data pending to write
        public function hasWriteBuffer ()
        {

                return $this -> writeBuffer -> hasBuffer ();
        }

        
        // Checks if the socket has data pending in its read buffer
        public function hasReadBuffer ()
        {

                return $this -> readBuffer -> hasBuffer ();
        }

                
        // Queries the remote machine to get it's address
        public function getRemoteAddress ()
        {

                if (!$this -> isConnected ())
                {

                        throw new LogicException ('Cannot receive remote address on socket that is not connected.');
                }

                
                return stream_socket_get_name ($this -> r_Socket, true);
        }

        
        // Queries the local machine to get it's address
        public function getLocalAddress ()
        {

                if (!$this -> isConnected ())
                {

                        throw new LogicException ('Cannot receive local address on socket that is not connected.');
                }

                
                return stream_socket_get_name ($this -> r_Socket, false);
        }

        
        // Sets the encoding for the stream
        public function setEncoding ($s_Encoding)
        {

                if (!function_exists ('mb_list_encodings')  || !in_array ($s_Encoding, mb_list_encodings ()))
                {

                        return false;
                }

                
                return stream_encoding ($this -> r_Socket, $s_Encoding);
        }

        
        // Sets the internal buffer's line endings to something else
        public function setLineEnding ($s_lineEnd)
        {

                $this -> readBuffer  -> setLineEnding ($s_lineEnd);
                $this -> writeBuffer -> setLineEnding ($s_lineEnd);
                
                return true;
        }

                
        // Connects to the remote machine
        public function connect ()
        {

                if ($this -> isConnected ())
                {

                        return true;
                }

                
                if ($this -> i_Status == Status :: CONNECTING)
                {

                        if (!feof ($this -> r_Socket))
                        {

                                $this -> i_Status        = Status :: CONNECTED;
                                $this -> i_timeConnected = time ();
                                $this -> _doCallback ('onConnect', array ($this, $this -> i_timeConnected));
                                
                                return true;
                        }
else /** call from poller that connection failed **/
                        {
                                $this -> i_Status = Status :: ERROR;
                                $this -> _doCallback ('onError', array ($this, Error :: CONNFAILED));
                                
                                return false;
                        }
                }

                
                // Prepare context
                $a_Opts = array ();
                $s_Scheme = 'tcp://';
                
                // Set options for ssl connection
                if ($this -> b_Secure)
                {

                        $s_Scheme       = 'ssl://';
                        $a_Opts ['ssl'] = array ('verify_peer'       => false,
                                                 'allow_self_signed' => true,
                                                 'cert_file'         => self :: CERT_FILE_PATH,
                                                 'passphrase'        => ''
                                                );
                }

                
                // Set bind address
                if (!empty ($this -> s_bindTo))
                {

                        $a_Opts ['socket'] = array ('bindto' => $this -> s_bindTo);
                }


                // Now, create the actual resource.
                $i_Flags          = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
                $r_Context        = stream_context_create ($a_Opts);
                $this -> r_Socket = stream_socket_client  ($s_Scheme . $this -> s_rAddress . ':' . $this -> i_rPort, $i_Errno, $s_Errstr, null, $i_Flags, $r_Context);
                
                // Check if we succeeded
                if (!is_resource ($this -> r_Socket))
                {

                        $this -> i_Status = Status :: ERROR;
                        $this -> _doCallback ('onError', array ($this, Error :: INITFAILED));
                        return false;
                }

                
                // Change blocking status.
                stream_set_blocking ($this -> r_Socket, false);
                $this -> i_Status = Status :: CONNECTING;
                
                // Insert to our poller object
                Poller :: getInstance () -> addConnection ($this -> r_Socket, $this);
                
                return false;
        }

        
        // Terminates the connection to the remote machine
        public function disconnect ()
        {

                if (!$this -> isConnected ())
                {

                        return true;
                }

                
                // Check if there is data pending..
                if ($this -> writeBuffer -> hasBuffer ())
                {

                        // Bypass write () because we do not want to send any callbacks anymore
                        // and do not care for errors

                        if (($i_Written = @fwrite ($this -> r_Socket, $this -> writeBuffer -> getBuffer ())) !== false)
                        {

                                $this -> writeBuffer -> removeLength ($i_Written);
                        }        
                }

                
                // Close it.
                $this -> _forceClose ();
                $this -> i_Status = Status :: DISCONNECTED_LOCAL;
                $this -> _doCallback ('onTerminate', array ($this, Status :: DISCONNECTED_LOCAL));
                
                return true;
        }

        
        // Attempts to write data from the write buffer to the remote machine
        // Returns amount of bytes written or false

        public function write ()
        {

                if (!$this -> isConnected ())
                {

                        throw new LogicException ('Cannot perform write on socket that is not connected.');
                }

                
                if (($i_Written = fwrite ($this -> r_Socket, $this -> writeBuffer -> getBuffer ())) === false)
                {

                        $this -> i_Status = Status :: ERROR;
                        $this -> _forceClose ();
                        $this -> _doCallback ('onError', array ($this, Error :: WRITEERR));
                        
                        return false;                        
                }

                
                $this -> writeBuffer -> removeLength ($i_Written);
                $this -> _doCallback ('onWrite', array ($this, $this -> writeBuffer, $i_Written));
                
                return true;
        }

        
        // Read as much data from the stream as possible
        // Returns amount of bytes written or false

        public function read ()
        {

                if (!$this -> isConnected ())
                {

                        throw new LogicException ('Cannot perform read on socket that is not connected.');
                }

                
                // Check for end-of-file
                if (feof ($this -> r_Socket))
                {

                        $this -> _forceClose ();
                        $this -> i_Status = Status :: DISCONNECTED_REMOTE;
                        $this -> _doCallback ('onTerminate', array ($this, Status :: DISCONNECTED_REMOTE));
                        
                        return false;
                }

                
                // Perform read action..
                if (($s_readString = fread ($this -> r_Socket, 8192)) == false)
                {

                        $this -> i_Status = Status :: ERROR;
                        $this -> _forceClose ();
                        $this -> _doCallback ('onError', array ($this, Error :: READERR));
                        
                        return false;
                }

                
                // Append to read buffer
                $this -> readBuffer -> appendBuffer ($s_readString);
                $this -> _doCallback ('onRead', array ($this, $this -> readBuffer, strlen ($s_readString)));
                
                return true;
        }

        
        // Private function to close the resource
        private function _forceClose ()
        {

                if (!is_resource ($this -> r_Socket))
                {

                        return;
                }

                                
                // Close the socket resource
                stream_socket_shutdown ($this -> r_Socket, STREAM_SHUT_RDWR);
                fclose                 ($this -> r_Socket);                
                $this -> r_Socket        = null;
                $this -> i_timeConnected = 0;
                
                // Remove from poller
                Poller :: getInstance () -> removeConnection ($this);
        }

        
        // Attempts to callback to someone
        private function _doCallback ($s_Event, array $a_Args = array ())
        {

                if (isset ($this -> $s_Event) && is_callable ($this -> $s_Event))
                {

                        call_user_func_array ($this -> $s_Event, $a_Args);
                }

                
                return;
        }
}

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.