Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
feather/feather/server/src/connection_worker.rs at main · feather-rs/feather · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
feather-rs
/
feather
Public
Notifications
You must be signed in to change notification settings
Fork
142
Star
2.7k
Code
Issues
66
Pull requests
38
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
feather
/
feather
/
server
/
src
/
connection_worker.rs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
250 lines (217 loc) · 7.16 KB
main
Breadcrumbs
feather
/
feather
/
server
/
src
/
connection_worker.rs
Copy path
Top
File metadata and controls
Code
Blame
250 lines (217 loc) · 7.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use
std
::
{
fmt
::
Debug
,
io
,
net
::
SocketAddr
,
sync
::
Arc
,
time
::
Duration
}
;
use
base
::
Text
;
use
flume
::
{
Receiver
,
Sender
}
;
use
futures_lite
::
FutureExt
;
use
io
::
ErrorKind
;
use
protocol
::
{
codec
::
CryptKey
,
packets
::
server
::
Disconnect
,
ClientPlayPacket
,
MinecraftCodec
,
Readable
,
ServerPlayPacket
,
Writeable
,
}
;
use
tokio
::
{
io
::
{
AsyncReadExt
,
AsyncWriteExt
}
,
net
::
{
tcp
::
{
OwnedReadHalf
,
OwnedWriteHalf
}
,
TcpStream
,
}
,
time
::
timeout
,
}
;
use
crate
::
{
initial_handler
::
{
InitialHandling
,
NewPlayer
}
,
options
::
Options
,
player_count
::
PlayerCount
,
}
;
/// Tokio task which handles a connection and processes
/// packets.
///
/// # Lifecycle
/// * A connection is made, and the `Listener` spawns a `Worker`.
/// * Connection goes through initial handling, i.e., the handshake process.
/// * If the connection was not a status ping, then the main server thread
/// is notified of the new connection via a channel.
pub
struct
Worker
{
reader
:
Reader
,
writer
:
Writer
,
options
:
Arc
<
Options
>
,
player_count
:
PlayerCount
,
packets_to_send_tx
:
Sender
<
ServerPlayPacket
>
,
received_packets_rx
:
Receiver
<
ClientPlayPacket
>
,
new_players
:
Sender
<
NewPlayer
>
,
}
impl
Worker
{
pub
fn
new
(
stream
:
TcpStream
,
_addr
:
SocketAddr
,
options
:
Arc
<
Options
>
,
player_count
:
PlayerCount
,
new_players
:
Sender
<
NewPlayer
>
,
)
->
Self
{
let
(
reader
,
writer
)
= stream
.
into_split
(
)
;
let
(
received_packets_tx
,
received_packets_rx
)
= flume
::
bounded
(
32
)
;
let
(
packets_to_send_tx
,
packets_to_send_rx
)
= flume
::
unbounded
(
)
;
let
reader =
Reader
::
new
(
reader
,
received_packets_tx
)
;
let
writer =
Writer
::
new
(
writer
,
packets_to_send_rx
)
;
Self
{
reader
,
writer
,
options
,
player_count
,
packets_to_send_tx
,
received_packets_rx
,
new_players
,
}
}
pub
fn
start
(
self
)
{
tokio
::
task
::
spawn
(
async
move
{
self
.
run
(
)
.
await
;
}
)
;
}
async
fn
run
(
mut
self
)
{
let
result =
crate
::
initial_handler
::
handle
(
&
mut
self
)
.
await
;
match
result
{
Ok
(
result
)
=>
self
.
proceed
(
result
)
.
await
,
Err
(
e
)
=> log
::
debug!
(
"Initial handling failed: {:?}"
,
e
)
,
}
}
async
fn
proceed
(
mut
self
,
result
:
InitialHandling
)
{
match
result
{
InitialHandling
::
Disconnect
=>
(
)
,
InitialHandling
::
Join
(
new_player
)
=>
{
if
self
.
player_count
.
try_add_player
(
)
.
is_err
(
)
{
self
.
write
(
ServerPlayPacket
::
Disconnect
(
Disconnect
{
reason
:
Text
::
from
(
"The server is full!"
)
.
to_string
(
)
,
}
)
)
.
await
.
ok
(
)
;
return
;
}
let
username = new_player
.
username
.
clone
(
)
;
let
_ =
self
.
new_players
.
send_async
(
new_player
)
.
await
;
self
.
split
(
username
)
;
}
}
}
pub
fn
options
(
&
self
)
->
&
Options
{
&
self
.
options
}
pub
fn
player_count
(
&
self
)
->
u32
{
self
.
player_count
.
get
(
)
}
#
[
allow
(
unused
)
]
pub
fn
enable_compression
(
&
mut
self
,
threshold
:
usize
)
{
self
.
reader
.
codec
.
enable_compression
(
threshold
)
;
self
.
writer
.
codec
.
enable_compression
(
threshold
)
;
log
::
debug!
(
"Enabled compression"
)
;
}
pub
fn
enable_encryption
(
&
mut
self
,
key
:
CryptKey
)
{
self
.
reader
.
codec
.
enable_encryption
(
key
)
;
self
.
writer
.
codec
.
enable_encryption
(
key
)
;
log
::
debug!
(
"Enabled encryption"
)
;
}
pub
async
fn
read
<
P
:
Readable
>
(
&
mut
self
)
-> anyhow
::
Result
<
P
>
{
self
.
reader
.
read
(
)
.
await
}
pub
async
fn
write
(
&
mut
self
,
packet
:
impl
Writeable
+
Debug
)
-> anyhow
::
Result
<
(
)
>
{
self
.
writer
.
write
(
packet
)
.
await
}
pub
fn
split
(
self
,
username
:
String
)
{
let
Self
{
reader
,
writer
,
player_count
,
..
}
=
self
;
let
reader = tokio
::
task
::
spawn
(
async
move
{
reader
.
run
(
)
.
await
}
)
;
let
writer = tokio
::
task
::
spawn
(
async
move
{
writer
.
run
(
)
.
await
}
)
;
tokio
::
task
::
spawn
(
async
move
{
let
result = reader
.
race
(
writer
)
.
await
.
expect
(
"task panicked"
)
;
if
let
Err
(
e
)
= result
{
let
message =
disconnected_message
(
e
)
;
log
::
debug!
(
"{} lost connection: {}"
,
username
,
message
)
;
}
player_count
.
remove_player
(
)
;
}
)
;
}
pub
fn
packets_to_send
(
&
self
)
->
Sender
<
ServerPlayPacket
>
{
self
.
packets_to_send_tx
.
clone
(
)
}
pub
fn
received_packets
(
&
self
)
->
Receiver
<
ClientPlayPacket
>
{
self
.
received_packets_rx
.
clone
(
)
}
}
struct
Reader
{
stream
:
OwnedReadHalf
,
codec
:
MinecraftCodec
,
buffer
:
[
u8
;
512
]
,
received_packets
:
Sender
<
ClientPlayPacket
>
,
}
impl
Reader
{
pub
fn
new
(
stream
:
OwnedReadHalf
,
received_packets
:
Sender
<
ClientPlayPacket
>
)
->
Self
{
Self
{
stream
,
codec
:
MinecraftCodec
::
new
(
)
,
buffer
:
[
0
;
512
]
,
received_packets
,
}
}
pub
async
fn
run
(
mut
self
)
-> anyhow
::
Result
<
(
)
>
{
loop
{
let
packet =
self
.
read
::
<
ClientPlayPacket
>
(
)
.
await
?
;
let
result =
self
.
received_packets
.
send_async
(
packet
)
.
await
;
if
result
.
is_err
(
)
{
// server dropped connection
return
Ok
(
(
)
)
;
}
}
}
pub
async
fn
read
<
P
:
Readable
>
(
&
mut
self
)
-> anyhow
::
Result
<
P
>
{
// Keep reading bytes and trying to get the packet.
loop
{
if
let
Some
(
packet
)
=
self
.
codec
.
next_packet
::
<
P
>
(
)
?
{
return
Ok
(
packet
)
;
}
let
duration =
Duration
::
from_secs
(
10
)
;
let
read_bytes =
timeout
(
duration
,
self
.
stream
.
read
(
&
mut
self
.
buffer
)
)
.
await
??
;
if
read_bytes ==
0
{
return
Err
(
io
::
Error
::
new
(
ErrorKind
::
UnexpectedEof
,
"read 0 bytes"
)
.
into
(
)
)
;
}
let
bytes =
&
self
.
buffer
[
..read_bytes
]
;
self
.
codec
.
accept
(
bytes
)
;
}
}
}
struct
Writer
{
stream
:
OwnedWriteHalf
,
codec
:
MinecraftCodec
,
packets_to_send
:
Receiver
<
ServerPlayPacket
>
,
buffer
:
Vec
<
u8
>
,
}
impl
Writer
{
pub
fn
new
(
stream
:
OwnedWriteHalf
,
packets_to_send
:
Receiver
<
ServerPlayPacket
>
)
->
Self
{
Self
{
stream
,
codec
:
MinecraftCodec
::
new
(
)
,
packets_to_send
,
buffer
:
Vec
::
new
(
)
,
}
}
pub
async
fn
run
(
mut
self
)
-> anyhow
::
Result
<
(
)
>
{
while
let
Ok
(
packet
)
=
self
.
packets_to_send
.
recv_async
(
)
.
await
{
self
.
write
(
packet
)
.
await
?
;
}
Ok
(
(
)
)
}
pub
async
fn
write
(
&
mut
self
,
packet
:
impl
Writeable
+
Debug
)
-> anyhow
::
Result
<
(
)
>
{
self
.
codec
.
encode
(
&
packet
,
&
mut
self
.
buffer
)
?
;
self
.
stream
.
write_all
(
&
self
.
buffer
)
.
await
?
;
self
.
buffer
.
clear
(
)
;
Ok
(
(
)
)
}
}
fn
disconnected_message
(
e
:
anyhow
::
Error
)
->
String
{
if
let
Some
(
io_error
)
= e
.
downcast_ref
::
<
io
::
Error
>
(
)
{
if
io_error
.
kind
(
)
==
ErrorKind
::
UnexpectedEof
{
return
"disconnected"
.
to_owned
(
)
;
}
}
format
!
(
"{:?}"
,
e
)
}
You can’t perform that action at this time.