-
Notifications
You must be signed in to change notification settings - Fork 16
/
client.rs
185 lines (167 loc) · 5.71 KB
/
client.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
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
use std::{
collections::HashMap,
thread::{self, sleep},
time::Duration,
};
use bevy::{
app::{AppExit, ScheduleRunnerPlugin},
ecs::schedule::IntoSystemConfigs,
log::LogPlugin,
prelude::{
App, Commands, Deref, DerefMut, EventReader, EventWriter, PostUpdate, ResMut, Resource,
Startup, Update,
},
utils::tracing::{info, warn},
};
use bevy_quinnet::{
client::{
certificate::CertificateVerificationMode,
client_connected,
connection::{ClientEndpointConfiguration, ConnectionEvent, ConnectionFailedEvent},
QuinnetClient, QuinnetClientPlugin,
},
shared::{channels::ChannelsConfiguration, ClientId},
};
use rand::{distributions::Alphanumeric, Rng};
use tokio::sync::mpsc;
use protocol::{ClientMessage, ServerMessage};
mod protocol;
#[derive(Resource, Debug, Clone, Default)]
struct Users {
self_id: ClientId,
names: HashMap<ClientId, String>,
}
#[derive(Resource, Deref, DerefMut)]
struct TerminalReceiver(mpsc::Receiver<String>);
pub fn on_app_exit(app_exit_events: EventReader<AppExit>, mut client: ResMut<QuinnetClient>) {
if !app_exit_events.is_empty() {
client
.connection_mut()
.send_message(ClientMessage::Disconnect {})
.unwrap();
// TODO Clean: event to let the async client send his last messages.
sleep(Duration::from_secs_f32(0.1));
}
}
fn handle_server_messages(mut users: ResMut<Users>, mut client: ResMut<QuinnetClient>) {
while let Some((_, message)) = client
.connection_mut()
.try_receive_message::<ServerMessage>()
{
match message {
ServerMessage::ClientConnected {
client_id,
username,
} => {
info!("{} joined", username);
users.names.insert(client_id, username);
}
ServerMessage::ClientDisconnected { client_id } => {
if let Some(username) = users.names.remove(&client_id) {
println!("{} left", username);
} else {
warn!("ClientDisconnected for an unknown client_id: {}", client_id)
}
}
ServerMessage::ChatMessage { client_id, message } => {
if let Some(username) = users.names.get(&client_id) {
if client_id != users.self_id {
println!("{}: {}", username, message);
}
} else {
warn!("Chat message from an unknown client_id: {}", client_id)
}
}
ServerMessage::InitClient {
client_id,
usernames,
} => {
users.self_id = client_id;
users.names = usernames;
}
}
}
}
fn handle_terminal_messages(
mut terminal_messages: ResMut<TerminalReceiver>,
mut app_exit_events: EventWriter<AppExit>,
mut client: ResMut<QuinnetClient>,
) {
while let Ok(message) = terminal_messages.try_recv() {
if message == "quit" {
app_exit_events.send(AppExit::Success);
} else {
client
.connection_mut()
.try_send_message(ClientMessage::ChatMessage { message: message });
}
}
}
fn start_terminal_listener(mut commands: Commands) {
let (from_terminal_sender, from_terminal_receiver) = mpsc::channel::<String>(100);
thread::spawn(move || loop {
let mut buffer = String::new();
std::io::stdin().read_line(&mut buffer).unwrap();
from_terminal_sender
.try_send(buffer.trim_end().to_string())
.unwrap();
});
commands.insert_resource(TerminalReceiver(from_terminal_receiver));
}
fn start_connection(mut client: ResMut<QuinnetClient>) {
client
.open_connection(
ClientEndpointConfiguration::from_strings("[::1]:6000", "[::]:0").unwrap(),
CertificateVerificationMode::SkipVerification,
ChannelsConfiguration::default(),
)
.unwrap();
// You can already send message(s) even before being connected, they will be buffered. In this example we will wait for a ConnectionEvent.
}
fn handle_client_events(
mut connection_events: EventReader<ConnectionEvent>,
mut connection_failed_events: EventReader<ConnectionFailedEvent>,
mut client: ResMut<QuinnetClient>,
) {
if !connection_events.is_empty() {
// We are connected
let username: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect();
println!("--- Joining with name: {}", username);
println!("--- Type 'quit' to disconnect");
client
.connection_mut()
.send_message(ClientMessage::Join { name: username })
.unwrap();
connection_events.clear();
}
for ev in connection_failed_events.read() {
println!(
"Failed to connect: {:?}, make sure the chat-server is running.",
ev.err
);
}
}
fn main() {
App::new()
.add_plugins((
ScheduleRunnerPlugin::default(),
LogPlugin::default(),
QuinnetClientPlugin::default(),
))
.insert_resource(Users::default())
.add_systems(Startup, (start_terminal_listener, start_connection))
.add_systems(
Update,
(
handle_client_events,
(handle_terminal_messages, handle_server_messages).run_if(client_connected),
),
)
// CoreSet::PostUpdate so that AppExit events generated in the previous stage are available
.add_systems(PostUpdate, on_app_exit)
.run();
}