46
46
use function is_resource ;
47
47
use function microtime ;
48
48
use function ord ;
49
- use function random_int ;
50
49
use function str_repeat ;
51
50
use function stream_get_meta_data ;
52
51
use function stream_select ;
@@ -61,6 +60,8 @@ final class Socket
61
60
{
62
61
private const BEGIN_REQUEST = 1 ;
63
62
63
+ private const ABORT_REQUEST = 2 ;
64
+
64
65
private const END_REQUEST = 3 ;
65
66
66
67
private const PARAMS = 4 ;
@@ -93,7 +94,7 @@ final class Socket
93
94
94
95
public const STREAM_SELECT_USEC = 200000 ;
95
96
96
- /** @var int */
97
+ /** @var SocketId */
97
98
private $ id ;
98
99
99
100
/** @var ConfiguresSocketConnection */
@@ -127,19 +128,21 @@ final class Socket
127
128
private $ status ;
128
129
129
130
/**
131
+ * @param SocketId $socketId
130
132
* @param ConfiguresSocketConnection $connection
131
133
* @param EncodesPacket $packetEncoder
132
134
* @param EncodesNameValuePair $nameValuePairEncoder
133
135
*
134
136
* @throws Exception
135
137
*/
136
138
public function __construct (
139
+ SocketId $ socketId ,
137
140
ConfiguresSocketConnection $ connection ,
138
141
EncodesPacket $ packetEncoder ,
139
142
EncodesNameValuePair $ nameValuePairEncoder
140
143
)
141
144
{
142
- $ this ->id = random_int ( 1 , ( 1 << 16 ) - 1 ) ;
145
+ $ this ->id = $ socketId ;
143
146
$ this ->connection = $ connection ;
144
147
$ this ->packetEncoder = $ packetEncoder ;
145
148
$ this ->nameValuePairEncoder = $ nameValuePairEncoder ;
@@ -151,7 +154,7 @@ public function __construct(
151
154
152
155
public function getId () : int
153
156
{
154
- return $ this ->id ;
157
+ return $ this ->id -> getValue () ;
155
158
}
156
159
157
160
public function usesConnection ( ConfiguresSocketConnection $ connection ) : bool
@@ -331,17 +334,21 @@ private function getRequestPackets( ProvidesRequestData $request ) : string
331
334
$ requestPackets = $ this ->packetEncoder ->encodePacket (
332
335
self ::BEGIN_REQUEST ,
333
336
chr ( 0 ) . chr ( self ::RESPONDER ) . chr ( 1 ) . str_repeat ( chr ( 0 ), 5 ),
334
- $ this ->id
337
+ $ this ->id -> getValue ()
335
338
);
336
339
337
340
$ paramsRequest = $ this ->nameValuePairEncoder ->encodePairs ( $ request ->getParams () );
338
341
339
342
if ( $ paramsRequest )
340
343
{
341
- $ requestPackets .= $ this ->packetEncoder ->encodePacket ( self ::PARAMS , $ paramsRequest , $ this ->id );
344
+ $ requestPackets .= $ this ->packetEncoder ->encodePacket (
345
+ self ::PARAMS ,
346
+ $ paramsRequest ,
347
+ $ this ->id ->getValue ()
348
+ );
342
349
}
343
350
344
- $ requestPackets .= $ this ->packetEncoder ->encodePacket ( self ::PARAMS , '' , $ this ->id );
351
+ $ requestPackets .= $ this ->packetEncoder ->encodePacket ( self ::PARAMS , '' , $ this ->id -> getValue () );
345
352
346
353
if ( $ request ->getContent () )
347
354
{
@@ -355,14 +362,14 @@ private function getRequestPackets( ProvidesRequestData $request ) : string
355
362
$ offset ,
356
363
self ::REQ_MAX_CONTENT_SIZE
357
364
),
358
- $ this ->id
365
+ $ this ->id -> getValue ()
359
366
);
360
367
$ offset += self ::REQ_MAX_CONTENT_SIZE ;
361
368
}
362
369
while ( $ offset < $ request ->getContentLength () );
363
370
}
364
371
365
- $ requestPackets .= $ this ->packetEncoder ->encodePacket ( self ::STDIN , '' , $ this ->id );
372
+ $ requestPackets .= $ this ->packetEncoder ->encodePacket ( self ::STDIN , '' , $ this ->id -> getValue () );
366
373
367
374
return $ requestPackets ;
368
375
}
@@ -429,7 +436,7 @@ public function fetchResponse( ?int $timeoutMs = null ) : ProvidesResponseData
429
436
continue ;
430
437
}
431
438
432
- if ( self ::END_REQUEST === $ packetType && $ packet ['requestId ' ] === $ this ->id )
439
+ if ( self ::END_REQUEST === $ packetType && $ packet ['requestId ' ] === $ this ->id -> getValue () )
433
440
{
434
441
break ;
435
442
}
@@ -577,7 +584,7 @@ public function collectResource( array &$resources ) : void
577
584
{
578
585
if ( null !== $ this ->resource )
579
586
{
580
- $ resources [ (string )$ this ->id ] = $ this ->resource ;
587
+ $ resources [ (string )$ this ->id -> getValue () ] = $ this ->resource ;
581
588
}
582
589
}
583
590
}
0 commit comments