1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Net connection |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 29/09/2017 |
6
|
|
|
* Time: 12:14 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\Net; |
10
|
|
|
|
11
|
|
|
use Carno\Coroutine\Context; |
12
|
|
|
use Carno\Net\Contracts\Conn; |
13
|
|
|
use Carno\Net\Contracts\Server; |
14
|
|
|
|
15
|
|
|
class Connection implements Conn |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
protected $id = 0; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $seq = 0; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Context |
29
|
|
|
*/ |
30
|
|
|
protected $ctx = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Address |
34
|
|
|
*/ |
35
|
|
|
protected $localA = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Address |
39
|
|
|
*/ |
40
|
|
|
protected $remoteA = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Server |
44
|
|
|
*/ |
45
|
|
|
protected $serverObj = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $workerID = -1; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $serviced = ''; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Events |
59
|
|
|
*/ |
60
|
|
|
protected $netEvents = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Connection constructor. |
64
|
|
|
* @param Context $previous |
65
|
|
|
*/ |
66
|
|
|
public function __construct(Context $previous = null) |
67
|
|
|
{ |
68
|
|
|
$this->ctx = $previous ?? new Context; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Context |
73
|
|
|
*/ |
74
|
|
|
public function ctx() : Context |
75
|
|
|
{ |
76
|
|
|
return $this->ctx; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $idx |
81
|
|
|
* @return static |
82
|
|
|
*/ |
83
|
|
|
public function setID(int $idx) : self |
84
|
|
|
{ |
85
|
|
|
$this->id = $idx; |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $idx |
91
|
|
|
* @return static |
92
|
|
|
*/ |
93
|
|
|
public function setSEQ(int $idx) : self |
94
|
|
|
{ |
95
|
|
|
$this->seq = $idx; |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $host |
101
|
|
|
* @param int $port |
102
|
|
|
* @return static |
103
|
|
|
*/ |
104
|
|
|
public function setLocal(string $host, int $port) : self |
105
|
|
|
{ |
106
|
|
|
$this->localA = new Address($host, $port); |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $host |
112
|
|
|
* @param int $port |
113
|
|
|
* @return static |
114
|
|
|
*/ |
115
|
|
|
public function setRemote(string $host, int $port) : self |
116
|
|
|
{ |
117
|
|
|
$this->remoteA = new Address($host, $port); |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param Server $instance |
123
|
|
|
* @return static |
124
|
|
|
*/ |
125
|
|
|
public function setServer(Server $instance) : self |
126
|
|
|
{ |
127
|
|
|
$this->serverObj = $instance; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param int $id |
133
|
|
|
* @return static |
134
|
|
|
*/ |
135
|
|
|
public function setWorker(int $id) : self |
136
|
|
|
{ |
137
|
|
|
$this->workerID = $id; |
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $name |
143
|
|
|
* @return static |
144
|
|
|
*/ |
145
|
|
|
public function setServiced(string $name) : self |
146
|
|
|
{ |
147
|
|
|
$this->serviced = $name; |
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param Events $events |
153
|
|
|
* @return static |
154
|
|
|
*/ |
155
|
|
|
public function setEvents(Events $events) : self |
156
|
|
|
{ |
157
|
|
|
$this->netEvents = $events; |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return int |
163
|
|
|
*/ |
164
|
|
|
public function id() : int |
165
|
|
|
{ |
166
|
|
|
return $this->id; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
public function seq() : int |
173
|
|
|
{ |
174
|
|
|
return $this->seq; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return Address |
179
|
|
|
*/ |
180
|
|
|
public function local() : Address |
181
|
|
|
{ |
182
|
|
|
return $this->localA; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return Address |
187
|
|
|
*/ |
188
|
|
|
public function remote() : Address |
189
|
|
|
{ |
190
|
|
|
return $this->remoteA; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return Server |
195
|
|
|
*/ |
196
|
|
|
public function server() : Server |
197
|
|
|
{ |
198
|
|
|
return $this->serverObj; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return int |
203
|
|
|
*/ |
204
|
|
|
public function worker() : int |
205
|
|
|
{ |
206
|
|
|
return $this->workerID; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function serviced() : string |
213
|
|
|
{ |
214
|
|
|
return $this->serviced; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return Events |
219
|
|
|
*/ |
220
|
|
|
public function events() : Events |
221
|
|
|
{ |
222
|
|
|
return $this->netEvents; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|