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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use super::*;
use std::net::Ipv4Addr;
#[cfg(test)]
use serial_test_derive::serial;
#[derive(Clone)]
pub struct Server {
inner: Arc<Inner<ServerManager>>,
server: *mut sys::ISteamGameServer,
}
unsafe impl Send for Server {}
unsafe impl Sync for Server {}
pub enum ServerMode {
NoAuthentication,
Authentication,
AuthenticationAndSecure,
}
impl Server {
pub fn init(
ip: Ipv4Addr, steam_port: u16,
game_port: u16, query_port: u16,
server_mode: ServerMode, version: &str,
) -> SResult<(Server, SingleClient<ServerManager>)> {
unsafe {
let version = CString::new(version).unwrap();
let raw_ip: u32 = ip.into();
let server_mode = match server_mode {
ServerMode::NoAuthentication => sys::EServerMode::eServerModeNoAuthentication,
ServerMode::Authentication => sys::EServerMode::eServerModeAuthentication,
ServerMode::AuthenticationAndSecure => sys::EServerMode::eServerModeAuthenticationAndSecure,
};
if !sys::SteamInternal_GameServer_Init(
raw_ip, steam_port,
game_port, query_port,
server_mode,
version.as_ptr(),
) {
return Err(SteamError::InitFailed);
}
sys::SteamAPI_ManualDispatch_Init();
let server_raw = sys::SteamAPI_SteamGameServer_v013();
let server = Arc::new(Inner {
_manager: ServerManager { _priv: () },
callbacks: Mutex::new(Callbacks {
callbacks: HashMap::new(),
call_results: HashMap::new(),
}),
networking_sockets_data: Mutex::new(NetworkingSocketsData {
sockets: Default::default(),
independent_connections: Default::default(),
connection_callback: Default::default()
})
});
Ok((Server {
inner: server.clone(),
server: server_raw,
}, SingleClient {
inner: server,
_not_sync: PhantomData,
}))
}
}
pub fn register_callback<C, F>(&self, f: F) -> CallbackHandle<ServerManager>
where C: Callback,
F: FnMut(C) + 'static + Send
{
unsafe {
register_callback(&self.inner, f)
}
}
pub fn steam_id(&self) -> SteamId {
unsafe {
SteamId(sys::SteamAPI_ISteamGameServer_GetSteamID(self.server))
}
}
pub fn authentication_session_ticket(&self) -> (AuthTicket, Vec<u8>) {
unsafe {
let mut ticket = vec![0; 1024];
let mut ticket_len = 0;
let auth_ticket = sys::SteamAPI_ISteamGameServer_GetAuthSessionTicket(self.server, ticket.as_mut_ptr() as *mut _, 1024, &mut ticket_len);
ticket.truncate(ticket_len as usize);
(AuthTicket(auth_ticket), ticket)
}
}
pub fn cancel_authentication_ticket(&self, ticket: AuthTicket) {
unsafe {
sys::SteamAPI_ISteamGameServer_CancelAuthTicket(self.server, ticket.0);
}
}
pub fn begin_authentication_session(&self, user: SteamId, ticket: &[u8]) -> Result<(), AuthSessionError> {
unsafe {
let res = sys::SteamAPI_ISteamGameServer_BeginAuthSession(
self.server,
ticket.as_ptr() as *const _, ticket.len() as _,
user.0
);
Err(match res {
sys::EBeginAuthSessionResult::k_EBeginAuthSessionResultOK => return Ok(()),
sys::EBeginAuthSessionResult::k_EBeginAuthSessionResultInvalidTicket => AuthSessionError::InvalidTicket,
sys::EBeginAuthSessionResult::k_EBeginAuthSessionResultDuplicateRequest => AuthSessionError::DuplicateRequest,
sys::EBeginAuthSessionResult::k_EBeginAuthSessionResultInvalidVersion => AuthSessionError::InvalidVersion,
sys::EBeginAuthSessionResult::k_EBeginAuthSessionResultGameMismatch => AuthSessionError::GameMismatch,
sys::EBeginAuthSessionResult::k_EBeginAuthSessionResultExpiredTicket => AuthSessionError::ExpiredTicket,
_ => unreachable!(),
})
}
}
pub fn end_authentication_session(&self, user: SteamId) {
unsafe {
sys::SteamAPI_ISteamGameServer_EndAuthSession(self.server, user.0);
}
}
pub fn set_product(&self, product: &str) {
let product = CString::new(product).unwrap();
unsafe {
sys::SteamAPI_ISteamGameServer_SetProduct(self.server, product.as_ptr() as *const _);
}
}
pub fn set_game_description(&self, desc: &str) {
let desc = CString::new(desc).unwrap();
unsafe {
sys::SteamAPI_ISteamGameServer_SetGameDescription(self.server, desc.as_ptr());
}
}
pub fn set_dedicated_server(&self, dedicated: bool) {
unsafe {
sys::SteamAPI_ISteamGameServer_SetDedicatedServer(self.server, dedicated);
}
}
pub fn log_on_anonymous(&self) {
unsafe {
sys::SteamAPI_ISteamGameServer_LogOnAnonymous(self.server);
}
}
pub fn enable_heartbeats(&self, active: bool) {
unsafe {
sys::SteamAPI_ISteamGameServer_EnableHeartbeats(self.server, active);
}
}
pub fn set_mod_dir(&self, mod_dir: &str) {
let mod_dir = CString::new(mod_dir).unwrap();
unsafe {
sys::SteamAPI_ISteamGameServer_SetModDir(self.server, mod_dir.as_ptr());
}
}
pub fn set_map_name(&self, map_name: &str) {
let map_name = CString::new(map_name).unwrap();
unsafe {
sys::SteamAPI_ISteamGameServer_SetMapName(self.server, map_name.as_ptr());
}
}
pub fn set_server_name(&self, server_name: &str) {
let server_name = CString::new(server_name).unwrap();
unsafe {
sys::SteamAPI_ISteamGameServer_SetMapName(self.server, server_name.as_ptr());
}
}
pub fn set_max_players(&self, count: i32) {
unsafe {
sys::SteamAPI_ISteamGameServer_SetMaxPlayerCount(self.server, count);
}
}
pub fn ugc(&self) -> UGC<ServerManager> {
unsafe {
let ugc = sys::SteamAPI_SteamGameServerUGC_v015();
debug_assert!(!ugc.is_null());
UGC {
ugc,
inner: self.inner.clone(),
}
}
}
}
#[test]
#[serial]
fn test() {
let (server, single) = Server::init(
[127, 0, 0, 1].into(),
23333, 23334, 23335,
ServerMode::Authentication, "0.0.1"
).unwrap();
println!("{:?}", server.steam_id());
server.set_product("steamworks-rs test");
server.set_game_description("basic server test");
server.set_dedicated_server(true);
server.log_on_anonymous();
println!("{:?}", server.steam_id());
let _cb = server.register_callback(|v: AuthSessionTicketResponse| println!("Got response: {:?}", v.result));
let _cb = server.register_callback(|v: ValidateAuthTicketResponse| println!("{:?}", v));
let id = server.steam_id();
let (auth, ticket) = server.authentication_session_ticket();
println!("{:?}", server.begin_authentication_session(id, &ticket));
for _ in 0 .. 20 {
single.run_callbacks();
::std::thread::sleep(::std::time::Duration::from_millis(50));
}
println!("END");
server.cancel_authentication_ticket(auth);
for _ in 0 .. 20 {
single.run_callbacks();
::std::thread::sleep(::std::time::Duration::from_millis(50));
}
server.end_authentication_session(id);
}
pub struct ServerManager {
_priv: (),
}
unsafe impl Manager for ServerManager {
unsafe fn get_pipe() -> sys::HSteamPipe {
sys::SteamGameServer_GetHSteamPipe()
}
}
impl Drop for ServerManager {
fn drop(&mut self) {
unsafe {
sys::SteamGameServer_Shutdown();
}
}
}