1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Async sockets |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
namespace AsyncSockets\Socket; |
11
|
|
|
|
12
|
|
|
use AsyncSockets\Frame\FramePickerInterface; |
13
|
|
|
use AsyncSockets\Socket\Io\Context; |
14
|
|
|
use AsyncSockets\Socket\Io\DatagramMemorizedIo; |
15
|
|
|
use AsyncSockets\Socket\Io\IoInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class UdpClientSocket |
19
|
|
|
*/ |
20
|
|
|
class UdpClientSocket implements SocketInterface, WithoutConnectionInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Original server socket |
24
|
|
|
* |
25
|
|
|
* @var SocketInterface |
26
|
|
|
*/ |
27
|
|
|
private $origin; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* I/O interface |
31
|
|
|
* |
32
|
|
|
* @var IoInterface |
33
|
|
|
*/ |
34
|
|
|
private $ioInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Socket context |
38
|
|
|
* |
39
|
|
|
* @var Context |
40
|
|
|
*/ |
41
|
|
|
private $context; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* UdpClientSocket constructor. |
45
|
|
|
* |
46
|
|
|
* @param SocketInterface $origin Original server socket |
47
|
|
|
* @param string $remoteAddress Client address |
48
|
|
|
* @param string $data Data for this client |
49
|
|
|
*/ |
50
|
11 |
|
public function __construct(SocketInterface $origin, $remoteAddress, $data) |
51
|
|
|
{ |
52
|
11 |
|
$this->origin = $origin; |
53
|
11 |
|
$this->ioInterface = new DatagramMemorizedIo($this, $remoteAddress, $data); |
54
|
11 |
|
$this->context = new Context(); |
55
|
11 |
|
} |
56
|
|
|
|
57
|
|
|
/** {@inheritdoc} */ |
58
|
3 |
|
public function open($address, $context = null) |
59
|
|
|
{ |
60
|
|
|
// empty body |
61
|
3 |
|
} |
62
|
|
|
|
63
|
|
|
/** {@inheritdoc} */ |
64
|
1 |
|
public function close() |
65
|
|
|
{ |
66
|
|
|
// empty body |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** {@inheritdoc} */ |
70
|
1 |
|
public function read(FramePickerInterface $picker, $isOutOfBand = false) |
71
|
|
|
{ |
72
|
1 |
|
return $this->ioInterface->read($picker, $this->context, $isOutOfBand); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** {@inheritdoc} */ |
76
|
1 |
|
public function write($data, $isOutOfBand = false) |
77
|
|
|
{ |
78
|
1 |
|
return $this->ioInterface->write($data, $this->context, $isOutOfBand); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** {@inheritdoc} */ |
82
|
4 |
|
public function getStreamResource() |
83
|
|
|
{ |
84
|
4 |
|
return $this->origin->getStreamResource(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritDoc |
89
|
|
|
*/ |
90
|
1 |
|
public function __toString() |
91
|
|
|
{ |
92
|
1 |
|
return (string) $this->origin; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
3 |
|
public function isServer() |
99
|
|
|
{ |
100
|
3 |
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritDoc |
105
|
|
|
*/ |
106
|
1 |
|
public function isClient() |
107
|
|
|
{ |
108
|
1 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritDoc |
113
|
|
|
*/ |
114
|
1 |
|
public function isConnected() |
115
|
|
|
{ |
116
|
1 |
|
return $this->ioInterface->isConnected(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|