-
Notifications
You must be signed in to change notification settings - Fork 16
/
server.rs
139 lines (129 loc) · 4.88 KB
/
server.rs
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
use std::collections::HashMap;
use bevy::{app::ScheduleRunnerPlugin, log::LogPlugin, prelude::*};
use bevy_quinnet::{
server::{
certificate::CertificateRetrievalMode, ConnectionLostEvent, Endpoint, QuinnetServerPlugin,
QuinnetServer, ServerEndpointConfiguration,
},
shared::{channels::ChannelsConfiguration, ClientId},
};
use protocol::{ClientMessage, ServerMessage};
mod protocol;
#[derive(Resource, Debug, Clone, Default)]
struct Users {
names: HashMap<ClientId, String>,
}
fn handle_client_messages(mut server: ResMut<QuinnetServer>, mut users: ResMut<Users>) {
let endpoint = server.endpoint_mut();
for client_id in endpoint.clients() {
while let Some((_, message)) = endpoint.try_receive_message_from::<ClientMessage>(client_id)
{
match message {
ClientMessage::Join { name } => {
if users.names.contains_key(&client_id) {
warn!(
"Received a Join from an already connected client: {}",
client_id
)
} else {
info!("{} connected", name);
users.names.insert(client_id, name.clone());
// Initialize this client with existing state
endpoint
.send_message(
client_id,
ServerMessage::InitClient {
client_id: client_id,
usernames: users.names.clone(),
},
)
.unwrap();
// Broadcast the connection event
endpoint
.send_group_message(
users.names.keys().into_iter(),
ServerMessage::ClientConnected {
client_id: client_id,
username: name,
},
)
.unwrap();
}
}
ClientMessage::Disconnect {} => {
// We tell the server to disconnect this user
endpoint.disconnect_client(client_id).unwrap();
handle_disconnect(endpoint, &mut users, client_id);
}
ClientMessage::ChatMessage { message } => {
info!(
"Chat message | {:?}: {}",
users.names.get(&client_id),
message
);
endpoint.try_send_group_message(
users.names.keys().into_iter(),
ServerMessage::ChatMessage {
client_id: client_id,
message: message,
},
);
}
}
}
}
}
fn handle_server_events(
mut connection_lost_events: EventReader<ConnectionLostEvent>,
mut server: ResMut<QuinnetServer>,
mut users: ResMut<Users>,
) {
// The server signals us about users that lost connection
for client in connection_lost_events.read() {
handle_disconnect(server.endpoint_mut(), &mut users, client.id);
}
}
/// Shared disconnection behaviour, whether the client lost connection or asked to disconnect
fn handle_disconnect(endpoint: &mut Endpoint, users: &mut ResMut<Users>, client_id: ClientId) {
// Remove this user
if let Some(username) = users.names.remove(&client_id) {
// Broadcast its deconnection
endpoint
.send_group_message(
users.names.keys().into_iter(),
ServerMessage::ClientDisconnected {
client_id: client_id,
},
)
.unwrap();
info!("{} disconnected", username);
} else {
warn!(
"Received a Disconnect from an unknown or disconnected client: {}",
client_id
)
}
}
fn start_listening(mut server: ResMut<QuinnetServer>) {
server
.start_endpoint(
ServerEndpointConfiguration::from_string("[::]:6000").unwrap(),
CertificateRetrievalMode::GenerateSelfSigned {
server_hostname: "::1".to_string(),
},
ChannelsConfiguration::default(),
)
.unwrap();
}
fn main() {
App::new()
.add_plugins((
ScheduleRunnerPlugin::default(),
LogPlugin::default(),
QuinnetServerPlugin::default(),
))
.insert_resource(Users::default())
.add_systems(Startup, start_listening)
.add_systems(Update, (handle_client_messages, handle_server_events))
.run();
}