1 | <?php |
||
22 | abstract class AbstractSocket implements SocketInterface |
||
23 | { |
||
24 | /** |
||
25 | * Tcp socket type |
||
26 | */ |
||
27 | const SOCKET_TYPE_TCP = 'tcp'; |
||
28 | |||
29 | /** |
||
30 | * Udp socket type |
||
31 | */ |
||
32 | const SOCKET_TYPE_UDP = 'udp'; |
||
33 | |||
34 | /** |
||
35 | * Unix socket type |
||
36 | */ |
||
37 | const SOCKET_TYPE_UNIX = 'unix'; |
||
38 | |||
39 | /** |
||
40 | * Unix datagram socket type |
||
41 | */ |
||
42 | const SOCKET_TYPE_UDG = 'udg'; |
||
43 | |||
44 | /** |
||
45 | * Unknown type of socket |
||
46 | */ |
||
47 | const SOCKET_TYPE_UNKNOWN = ''; |
||
48 | |||
49 | /** |
||
50 | * This socket resource |
||
51 | * |
||
52 | * @var resource |
||
53 | */ |
||
54 | private $resource; |
||
55 | |||
56 | /** |
||
57 | * I/O interface |
||
58 | * |
||
59 | * @var IoInterface |
||
60 | */ |
||
61 | private $ioInterface; |
||
62 | |||
63 | /** |
||
64 | * Socket address |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $remoteAddress; |
||
69 | |||
70 | /** |
||
71 | * Context for this socket |
||
72 | * |
||
73 | * @var Context |
||
74 | */ |
||
75 | private $context; |
||
76 | |||
77 | /** |
||
78 | * AbstractSocket constructor. |
||
79 | */ |
||
80 | 123 | public function __construct() |
|
81 | { |
||
82 | 123 | $this->setDisconnectedState(); |
|
83 | 123 | $this->context = new Context(); |
|
84 | 123 | } |
|
85 | |||
86 | /** |
||
87 | * Set disconnected state for socket |
||
88 | * |
||
89 | * @return void |
||
90 | */ |
||
91 | 123 | private function setDisconnectedState() |
|
92 | { |
||
93 | 123 | $this->ioInterface = new DisconnectedIo($this); |
|
94 | 123 | } |
|
95 | |||
96 | /** {@inheritdoc} */ |
||
97 | 75 | public function open($address, $context = null) |
|
98 | { |
||
99 | 75 | $this->resource = $this->createSocketResource( |
|
100 | 75 | $address, |
|
101 | 75 | $context ?: stream_context_get_default() |
|
102 | 75 | ); |
|
103 | |||
104 | 69 | if (!is_resource($this->resource)) { |
|
105 | 5 | throw new ConnectionException( |
|
106 | 5 | $this, |
|
107 | 'Can not allocate socket resource.' |
||
108 | 5 | ); |
|
109 | } |
||
110 | |||
111 | 64 | $this->remoteAddress = $address; |
|
112 | |||
113 | // https://bugs.php.net/bug.php?id=51056 |
||
114 | 64 | stream_set_blocking($this->resource, 0); |
|
115 | |||
116 | // https://bugs.php.net/bug.php?id=52602 |
||
117 | 64 | stream_set_timeout($this->resource, 0, 0); |
|
118 | |||
119 | 64 | $this->ioInterface = $this->createIoInterface( |
|
120 | 64 | $this->resolveSocketType(), |
|
121 | $address |
||
122 | 64 | ); |
|
123 | |||
124 | 60 | $this->context->reset(); |
|
125 | 60 | } |
|
126 | |||
127 | /** {@inheritdoc} */ |
||
128 | 12 | public function close() |
|
129 | { |
||
130 | 12 | if ($this->resource) { |
|
131 | 8 | $this->setDisconnectedState(); |
|
132 | 8 | stream_socket_shutdown($this->resource, STREAM_SHUT_RDWR); |
|
133 | 8 | fclose($this->resource); |
|
134 | 8 | $this->resource = null; |
|
135 | 8 | $this->remoteAddress = null; |
|
136 | 8 | } |
|
137 | 12 | } |
|
138 | |||
139 | /** {@inheritdoc} */ |
||
140 | 31 | public function read(FramePickerInterface $picker, $isOutOfBand = false) |
|
141 | { |
||
142 | try { |
||
143 | 31 | return $this->ioInterface->read($picker, $this->context, $isOutOfBand); |
|
144 | 10 | } catch (ConnectionException $e) { |
|
145 | 5 | $this->setDisconnectedState(); |
|
146 | 5 | throw $e; |
|
147 | } |
||
148 | } |
||
149 | |||
150 | /** {@inheritdoc} */ |
||
151 | 10 | public function write($data, $isOutOfBand = false) |
|
152 | { |
||
153 | try { |
||
154 | 10 | return $this->ioInterface->write($data, $this->context, $isOutOfBand); |
|
155 | 10 | } catch (ConnectionException $e) { |
|
156 | 5 | $this->setDisconnectedState(); |
|
157 | 5 | throw $e; |
|
158 | } |
||
159 | } |
||
160 | |||
161 | /** {@inheritdoc} */ |
||
162 | 46 | public function getStreamResource() |
|
166 | |||
167 | /** |
||
168 | * @inheritDoc |
||
169 | */ |
||
170 | 10 | public function __toString() |
|
171 | { |
||
172 | 10 | return $this->remoteAddress ? |
|
180 | |||
181 | /** |
||
182 | * Create certain socket resource |
||
183 | * |
||
184 | * @param string $address Network address to open in form transport://path:port |
||
185 | * @param resource $context Valid stream context created by function stream_context_create or null |
||
186 | * |
||
187 | * @return resource |
||
188 | */ |
||
189 | abstract protected function createSocketResource($address, $context); |
||
190 | |||
191 | /** |
||
192 | * Create I/O interface for socket |
||
193 | * |
||
194 | * @param string $type Type of this socket, one of SOCKET_TYPE_* consts |
||
195 | * @param string $address Address passed to open method |
||
196 | * |
||
197 | * @return IoInterface |
||
198 | */ |
||
199 | abstract protected function createIoInterface($type, $address); |
||
200 | |||
201 | /** |
||
202 | * Get current socket type |
||
203 | * |
||
204 | * @return string One of SOCKET_TYPE_* consts |
||
205 | */ |
||
206 | 64 | private function resolveSocketType() |
|
230 | } |
||
231 |