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