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
use super::{
ActiveUpload, Arc, Client, ClientContext, DownloadStatus, PeerMessage,
RwLock, RwLockExt, collect_failed_tokens, error, next_upload_token, thread,
};
use crate::types::UploadStatus;
use std::sync::atomic::{AtomicBool, AtomicU64};
/// The peer behind a registry key, which may carry a `:direct` suffix the
/// server never sees.
pub fn downloader_of(requester_key: &str) -> &str {
requester_key
.strip_suffix(":direct")
.unwrap_or(requester_key)
}
impl Client {
/// Whether the server listed `username` as privileged. Privileged peers are
/// served before everyone else.
#[must_use]
pub fn is_privileged(&self, username: &str) -> bool {
self.context
.read_safe()
.is_ok_and(|ctx| ctx.is_privileged(username))
}
/// Ask the server how much of our own privilege time is left (code 92). The
/// answer arrives asynchronously; read it with
/// [`Client::own_privilege_seconds`].
///
/// # Errors
/// [`crate::error::SoulseekRs::NotConnected`] without a server connection.
pub fn check_privileges(&self) -> crate::error::Result<()> {
let Some(handle) = &self.server_handle else {
return Err(crate::error::SoulseekRs::NotConnected);
};
let _ = handle.send(super::ServerMessage::CheckPrivileges);
Ok(())
}
/// Seconds of our own privileges left, or `None` until the server has
/// answered a [`Client::check_privileges`].
#[must_use]
pub fn own_privilege_seconds(&self) -> Option<u32> {
self.context
.read_safe()
.ok()
.and_then(|ctx| ctx.own_privileges)
}
/// Queued-upload states recorded since the last call, for a caller that
/// samples [`Client::uploads`] and would otherwise miss a peer that queued
/// and was served between two samples.
#[must_use]
pub fn take_upload_events(&self) -> Vec<crate::types::UploadInfo> {
self.context
.write_safe()
.map(|mut ctx| ctx.take_upload_events())
.unwrap_or_default()
}
/// How many uploads run at once. Lowering it does not interrupt transfers
/// already in flight; the queue simply refills more slowly.
pub fn set_upload_slots(&self, slots: usize) {
if let Ok(mut ctx) = self.context.write_safe() {
ctx.upload_slots = slots.max(1);
}
}
/// Drop everything `username` was queued or offered, then hand their slot to
/// whoever is next.
pub(crate) fn release_upload_slots(
client_context: &Arc<RwLock<ClientContext>>,
username: &str,
) {
let freed = client_context
.write_safe()
.is_ok_and(|mut ctx| ctx.release_upload_slots(username));
if freed {
Self::pump_upload_queue(client_context);
}
}
/// Fill every free upload slot from the queue and send the offers.
///
/// Called whenever the queue or the slot count could have changed: a new
/// request, a finished transfer, or a privileged list that re-ranked who is
/// waiting.
pub(crate) fn pump_upload_queue(
client_context: &Arc<RwLock<ClientContext>>,
) {
let (registry, offers) = match client_context.write_safe() {
Ok(mut ctx) => ctx.pump_uploads(next_upload_token),
Err(e) => {
error!("[client] pump_upload_queue write: {}", e);
return;
}
};
let Some(registry) = registry else {
return;
};
for offer in offers {
let _ = registry.send_to_peer(
&offer.requester_key,
PeerMessage::ServeUpload {
token: offer.token,
filename: offer.virtual_path,
size: offer.size,
},
);
}
}
/// Consume the upload job for `token` and stream the file to `host:port`
/// on a background thread.
pub(crate) fn spawn_serve(
client_context: &Arc<RwLock<ClientContext>>,
own_username: &str,
token: u32,
host: String,
port: u32,
) {
let Ok(mut ctx) = client_context.write_safe() else {
return;
};
let Some(job) = ctx.uploads.remove(&token) else {
return;
};
let bytes_sent = Arc::new(AtomicU64::new(0));
let cancel = Arc::new(AtomicBool::new(false));
ctx.active_uploads.insert(
token,
ActiveUpload {
username: job.downloader.clone(),
filename: job.virtual_path.clone(),
size: job.size,
bytes_sent: bytes_sent.clone(),
cancel: cancel.clone(),
status: UploadStatus::InProgress,
started: std::time::Instant::now(),
},
);
drop(ctx);
let own = own_username.to_string();
let real_path = job.real_path;
let context = client_context.clone();
thread::spawn(move || {
let result = crate::peer::upload_peer::serve_file(
&host,
port,
&own,
token,
&real_path,
&bytes_sent,
&cancel,
);
let status = match &result {
Ok(()) => UploadStatus::Completed,
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {
UploadStatus::Cancelled
}
Err(e) => {
error!("[client] serve {}: {}", real_path.display(), e);
UploadStatus::Failed(e.to_string())
}
};
if let Ok(mut ctx) = context.write_safe()
&& let Some(upload) = ctx.active_uploads.get_mut(&token)
{
upload.status = status;
}
// The slot this transfer held is free now, so whoever is next in
// line gets it without waiting for another request to arrive.
Self::pump_upload_queue(&context);
});
}
pub(crate) fn process_failed_uploads(
client_context: Arc<RwLock<ClientContext>>,
username: &str,
filename: Option<&str>,
) {
let failed_tokens = match client_context.read_safe() {
Ok(context) => {
collect_failed_tokens(&context.downloads, username, filename)
}
Err(e) => {
error!("[client] process_failed_uploads read: {}", e);
return;
}
};
if failed_tokens.is_empty() {
return;
}
match client_context.write_safe() {
Ok(mut context) => {
for token in failed_tokens {
context.downloads.update_status(
token,
DownloadStatus::Failed(Some(
"The upload failed on the other side".to_string(),
)),
);
context.downloads.remove(token);
}
}
Err(e) => {
error!("[client] process_failed_uploads write: {}", e);
}
}
}
}