|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\IO\Stream; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\IO\Stream\Exception\UnableToOpenStreamException; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* SocketStream |
|
31
|
|
|
* |
|
32
|
|
|
* Implements a handler for creating socket based streams. |
|
33
|
|
|
* @author Celestino Diaz <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
class SocketStream implements Stream { |
|
36
|
|
|
|
|
37
|
|
|
/** @var \Brickoo\Component\IO\Stream\SocketStreamConfig */ |
|
38
|
|
|
private $streamConfig; |
|
39
|
|
|
|
|
40
|
|
|
/** @var resource */ |
|
41
|
|
|
private $resource; |
|
42
|
|
|
|
|
43
|
|
|
/** @param SocketStreamConfig $streamConfig */ |
|
44
|
1 |
|
public function __construct(SocketStreamConfig $streamConfig) { |
|
45
|
1 |
|
$this->streamConfig = $streamConfig; |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns the socket stream configuration. |
|
50
|
|
|
* @return SocketStreamConfig |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function getConfiguration() { |
|
53
|
1 |
|
return $this->streamConfig; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Reconfigure the socket stream context. |
|
58
|
|
|
* @param SocketStreamConfig $streamConfig |
|
59
|
|
|
* @return \Brickoo\Component\IO\Stream\SocketStream |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function reconfigure(SocketStreamConfig $streamConfig) { |
|
62
|
1 |
|
$this->streamConfig = $streamConfig; |
|
63
|
1 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** {@inheritdoc} */ |
|
67
|
5 |
|
public function open() { |
|
68
|
5 |
|
if ($this->hasResource()) { |
|
69
|
1 |
|
return $this->resource; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
$errorCode = null; |
|
73
|
5 |
|
$errorMessage = null; |
|
74
|
5 |
|
$configuration = $this->getConfiguration(); |
|
75
|
|
|
|
|
76
|
5 |
|
if (!($resource = @stream_socket_client( |
|
77
|
5 |
|
$configuration->getSocketAddress(), |
|
78
|
5 |
|
$errorCode, $errorMessage, |
|
79
|
5 |
|
$configuration->getConnectionTimeout(), |
|
80
|
5 |
|
$configuration->getConnectionType(), |
|
81
|
5 |
|
stream_context_create($configuration->getContext()) |
|
82
|
5 |
|
))) { |
|
83
|
1 |
|
throw new UnableToOpenStreamException($errorMessage, $errorCode); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
$this->resource = $resource; |
|
87
|
4 |
|
return $resource; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** {@inheritdoc} */ |
|
91
|
2 |
|
public function close() { |
|
92
|
2 |
|
if ($this->hasResource()) { |
|
93
|
2 |
|
fclose($this->resource); |
|
94
|
2 |
|
$this->resource = null; |
|
95
|
2 |
|
} |
|
96
|
2 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** Release stream resource on destruction. */ |
|
99
|
1 |
|
public function __destruct() { |
|
100
|
1 |
|
$this->close(); |
|
101
|
1 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Checks if the resource has been already created. |
|
105
|
|
|
* @return boolean check result |
|
106
|
|
|
*/ |
|
107
|
2 |
|
private function hasResource() { |
|
108
|
2 |
|
return is_resource($this->resource); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|