1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\RemoteRequest\Pointers; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\RemoteRequest\Interfaces\IConnectionParams; |
7
|
|
|
use kalanis\RemoteRequest\Protocols\Helper; |
8
|
|
|
use kalanis\RemoteRequest\RequestException; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class SocketProcessor |
13
|
|
|
* @package kalanis\RemoteRequest\Pointers |
14
|
|
|
* Query to the remote server - read into provided output |
15
|
|
|
* @codeCoverageIgnore because accessing remote source via internal socket |
16
|
|
|
*/ |
17
|
|
|
class SocketProcessor extends Processor |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param resource $filePointer |
21
|
|
|
* @param IConnectionParams $params |
22
|
|
|
* @throws RequestException |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
|
|
protected function writeRequest($filePointer, IConnectionParams $params): parent |
26
|
|
|
{ |
27
|
|
|
$input = $this->remoteQuery ? strval(stream_get_contents($this->remoteQuery->getData(), -1, 0)) : ''; |
28
|
|
|
$result = socket_sendto($filePointer, $input, strlen($input), 0, $params->getHost(), intval($params->getPort())); |
29
|
|
|
if (!$result) { |
30
|
|
|
$errorCode = socket_last_error(); |
31
|
|
|
$errorMessage = socket_strerror($errorCode); |
32
|
|
|
throw new RequestException($this->getRRLang()->rrPointSentProblem($errorMessage), $errorCode); |
33
|
|
|
} |
34
|
|
|
return $this; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param resource $filePointer |
39
|
|
|
* @throws RequestException |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
protected function readResponse($filePointer): parent |
43
|
|
|
{ |
44
|
|
|
$this->remoteResponse = null; |
45
|
|
|
$reply = null; |
46
|
|
|
$result = socket_recv($filePointer, $reply, $this->bytesPerSegment(), MSG_WAITALL); |
47
|
|
|
if (false === $result) { // because could return size 0 bytes |
48
|
|
|
$errorCode = socket_last_error(); |
49
|
|
|
$errorMessage = socket_strerror($errorCode); |
50
|
|
|
throw new RequestException($this->getRRLang()->rrPointReceivedProblem($errorMessage), $errorCode); |
51
|
|
|
} |
52
|
|
|
if (!is_null($reply)) { |
53
|
|
|
$response = Helper::getTempStorage(); |
54
|
|
|
fwrite($response, $reply); |
55
|
|
|
rewind($response); |
56
|
|
|
$this->remoteResponse = $response; |
57
|
|
|
} |
58
|
|
|
return $this; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* How many bytes of loaded segment |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
|
|
protected function bytesPerSegment(): int |
66
|
|
|
{ |
67
|
|
|
return 2045; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|