|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rvdv\Nntp\Socket; |
|
4
|
|
|
|
|
5
|
|
|
use Rvdv\Nntp\Exception\SocketException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @author Robin van der Vleuten <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
class Socket implements SocketInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var resource |
|
14
|
|
|
*/ |
|
15
|
|
|
private $stream; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
*/ |
|
20
|
1 |
|
public function setBlocking($blocking) |
|
21
|
|
|
{ |
|
22
|
1 |
|
if (!stream_set_blocking($this->stream, $blocking ? 1 : 0)) { |
|
23
|
|
|
throw new SocketException(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
return $this; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function setReadBuffer($buffer) |
|
33
|
|
|
{ |
|
34
|
1 |
|
if (stream_set_read_buffer($this->stream, $buffer) !== 0) { |
|
35
|
|
|
throw new SocketException(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function enableCrypto($enable, $cryptoType = STREAM_CRYPTO_METHOD_TLS_CLIENT) |
|
45
|
|
|
{ |
|
46
|
|
|
if (!stream_socket_enable_crypto($this->stream, $enable, $cryptoType)) { |
|
47
|
|
|
throw new SocketException(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function connect($address, $timeout = null) |
|
57
|
|
|
{ |
|
58
|
1 |
|
if (!$this->stream = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT)) { |
|
59
|
|
|
throw new SocketException(sprintf('Connection to %s failed: %s', $address, $errstr)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Use unbuffered read operations on the underlying stream resource. |
|
63
|
|
|
// Reading chunks from the stream may otherwise leave unread bytes in |
|
64
|
|
|
// PHP's stream buffers which some event loop implementations do not |
|
65
|
|
|
// trigger events on (edge triggered). |
|
66
|
|
|
// This does not affect the default event loop implementation (level |
|
67
|
|
|
// triggered), so we can ignore platforms not supporting this (HHVM). |
|
68
|
1 |
|
$this->setReadBuffer(0); |
|
69
|
|
|
|
|
70
|
1 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function disconnect() |
|
77
|
|
|
{ |
|
78
|
1 |
|
if (!fclose($this->stream)) { |
|
79
|
|
|
throw new SocketException(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function eof() |
|
89
|
|
|
{ |
|
90
|
1 |
|
return feof($this->stream); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function gets($length = null) |
|
97
|
|
|
{ |
|
98
|
|
|
if (($data = fgets($this->stream, $length)) === false) { |
|
99
|
|
|
throw new SocketException(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $data; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function read($length) |
|
109
|
|
|
{ |
|
110
|
1 |
|
if (($data = fread($this->stream, $length)) === false) { |
|
111
|
|
|
throw new SocketException(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
return $data; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function write($data) |
|
121
|
|
|
{ |
|
122
|
1 |
|
if (($bytes = fwrite($this->stream, $data)) === false) { |
|
123
|
|
|
throw new SocketException(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
return $bytes; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|