22
33namespace Wrench ;
44
5+ use Wrench \Payload \PayloadHandler ;
6+
57use Wrench \Protocol \Protocol ;
68
79use Wrench \Payload \Payload ;
@@ -37,7 +39,7 @@ class Connection extends Configurable
3739 *
3840 * Wraps the client connection resource
3941 *
40- * @var Socket
42+ * @var ServerClientSocket
4143 */
4244 protected $ socket ;
4345
@@ -77,9 +79,9 @@ class Connection extends Configurable
7779 protected $ id = null ;
7880
7981 /**
80- * The current payload
82+ * @var PayloadHandler
8183 */
82- protected $ payload ;
84+ protected $ payloadHandler ;
8385
8486 /**
8587 * Constructor
@@ -97,9 +99,11 @@ public function __construct(
9799 $ this ->manager = $ manager ;
98100 $ this ->socket = $ socket ;
99101
102+
100103 parent ::__construct ($ options );
101104
102105 $ this ->configureClientInformation ();
106+ $ this ->configurePayloadHandler ();
103107
104108 $ this ->log ('Connected ' );
105109 }
@@ -127,6 +131,14 @@ protected function configure(array $options)
127131 parent ::configure ($ options );
128132 }
129133
134+ protected function configurePayloadHandler ()
135+ {
136+ $ this ->payloadHandler = new PayloadHandler (
137+ array ($ this , 'handlePayload ' ),
138+ $ this ->options
139+ );
140+ }
141+
130142 /**
131143 * @throws RuntimeException
132144 */
@@ -266,62 +278,27 @@ protected function export($data)
266278 * @todo An endpoint MUST be capable of handling control frames in the
267279 * middle of a fragmented message.
268280 * @param string $data
281+ * @return void
269282 */
270283 public function handle ($ data )
271284 {
272- if (!$ this ->payload ) {
273- $ this ->payload = $ this ->protocol ->getPayload ();
274- }
275-
276- while ($ data ) { // Each iteration pulls off a single payload chunk
277- $ size = strlen ($ data );
278- $ remaining = $ this ->payload ->getRemainingData ();
279-
280- // If we don't yet know how much data is remaining, read data into
281- // the payload in two byte chunks (the size of a WebSocket frame
282- // header to get the initial length)
283- //
284- // Then re-loop. For extended lengths, this will happen once or four
285- // times extra, as the extended length is read in.
286- if ($ remaining === null ) {
287- $ chunk_size = 2 ;
288- } elseif ($ remaining > 0 ) {
289- $ chunk_size = $ remaining ;
290- } elseif ($ remaining === 0 ) {
291- $ chunk_size = 0 ;
292- }
293-
294- $ chunk_size = min (strlen ($ data ), $ chunk_size );
295- $ chunk = substr ($ data , 0 , $ chunk_size );
296- $ data = substr ($ data , $ chunk_size );
297-
298- $ this ->payload ->receiveData ($ chunk );
299-
300- if ($ remaining !== 0 && !$ this ->payload ->isComplete ()) {
301- continue ;
302- }
303-
304- if ($ this ->payload ->isComplete ()) {
305- $ this ->handlePayload ($ this ->payload );
306- $ this ->payload = $ this ->protocol ->getPayload ();
307- } else {
308- throw new ConnectionException ('Payload will not complete ' );
309- }
310- }
285+ $ this ->payloadHandler ->handle ($ data );
311286 }
312287
313288 /**
314289 * Handle a complete payload received from the client
315290 *
291+ * Public because called from our PayloadHandler
292+ *
316293 * @param string $payload
317294 */
318- protected function handlePayload (Payload $ payload )
295+ public function handlePayload (Payload $ payload )
319296 {
320297 $ app = $ this ->getClientApplication ();
321298
322299 $ this ->log ('Handling payload: ' . $ payload ->getPayload (), 'debug ' );
323300
324- switch ($ payload ->getType ()) {
301+ switch ($ type = $ payload ->getType ()) {
325302 case Protocol::TYPE_TEXT :
326303 if (method_exists ($ app , 'onData ' )) {
327304 $ app ->onData ($ payload , $ this );
0 commit comments