1
- use std:: {
2
- net:: { SocketAddr , UdpSocket } ,
3
- sync:: Arc ,
4
- } ;
1
+ use std:: { sync:: Arc , thread} ;
5
2
6
3
use client:: { graphics, metrics, net, Config } ;
7
- use quinn :: rustls :: pki_types :: { CertificateDer , PrivatePkcs8KeyDer } ;
4
+ use common :: proto ;
8
5
use save:: Save ;
9
6
10
7
use ash:: khr;
11
8
use server:: Server ;
12
- use tracing:: { error, error_span, info} ;
9
+ use tracing:: { debug , error, error_span, info, Instrument } ;
13
10
use winit:: {
14
11
application:: ApplicationHandler ,
15
12
event_loop:: { ActiveEventLoop , EventLoop } ,
@@ -21,62 +18,58 @@ fn main() {
21
18
let metrics = crate :: metrics:: init ( ) ;
22
19
23
20
let dirs = directories:: ProjectDirs :: from ( "" , "" , "hypermine" ) . unwrap ( ) ;
24
- let mut config = Config :: load ( & dirs) ;
25
-
26
- if config. server . is_none ( ) {
27
- // spawn an in-process server
28
- let socket =
29
- UdpSocket :: bind ( "[::1]:0" . parse :: < SocketAddr > ( ) . unwrap ( ) ) . expect ( "binding socket" ) ;
30
- config. server = Some ( socket. local_addr ( ) . unwrap ( ) ) ;
31
-
32
- let certified_key = rcgen:: generate_simple_self_signed ( vec ! [ "localhost" . into( ) ] ) . unwrap ( ) ;
33
- let key = certified_key. key_pair . serialize_der ( ) ;
34
- let cert = certified_key. cert . der ( ) . to_vec ( ) ;
35
- let sim_cfg = config. local_simulation . clone ( ) ;
36
-
37
- let save = dirs. data_local_dir ( ) . join ( & config. save ) ;
38
- info ! ( "using save file {}" , save. display( ) ) ;
39
- std:: fs:: create_dir_all ( save. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
40
- let save = match Save :: open ( & save, config. local_simulation . chunk_size ) {
41
- Ok ( x) => x,
42
- Err ( e) => {
43
- error ! ( "couldn't open save: {}" , e) ;
44
- return ;
45
- }
46
- } ;
47
-
48
- let server = match Server :: new (
49
- server:: NetParams {
50
- certificate_chain : vec ! [ CertificateDer :: from( cert) ] ,
51
- private_key : PrivatePkcs8KeyDer :: from ( key) . into ( ) ,
52
- socket,
53
- } ,
54
- sim_cfg,
55
- save,
56
- ) {
57
- Ok ( server) => server,
58
- Err ( e) => {
59
- eprintln ! ( "{e:#}" ) ;
60
- std:: process:: exit ( 1 ) ;
61
- }
62
- } ;
63
-
64
- std:: thread:: spawn ( move || {
65
- #[ tokio:: main( flavor = "current_thread" ) ]
66
- async fn run_server ( server : Server ) {
67
- server. run ( ) . await ;
68
- }
69
-
70
- let span = error_span ! ( "server" ) ;
71
- let _guard = span. enter ( ) ;
72
- run_server ( server) ;
73
- } ) ;
74
- }
21
+ let config = Arc :: new ( Config :: load ( & dirs) ) ;
22
+
23
+ let net = match config. server {
24
+ None => {
25
+ // spawn an in-process server
26
+ let sim_cfg = config. local_simulation . clone ( ) ;
27
+
28
+ let save = dirs. data_local_dir ( ) . join ( & config. save ) ;
29
+ info ! ( "using save file {}" , save. display( ) ) ;
30
+ std:: fs:: create_dir_all ( save. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
31
+ let save = match Save :: open ( & save, config. local_simulation . chunk_size ) {
32
+ Ok ( x) => x,
33
+ Err ( e) => {
34
+ error ! ( "couldn't open save: {}" , e) ;
35
+ return ;
36
+ }
37
+ } ;
38
+
39
+ let mut server = match Server :: new ( None , sim_cfg, save) {
40
+ Ok ( server) => server,
41
+ Err ( e) => {
42
+ eprintln ! ( "{e:#}" ) ;
43
+ std:: process:: exit ( 1 ) ;
44
+ }
45
+ } ;
46
+
47
+ let ( handle, backend) = server:: Handle :: loopback ( ) ;
48
+ let name = ( * config. name ) . into ( ) ;
49
+
50
+ thread:: spawn ( move || {
51
+ let runtime = tokio:: runtime:: Builder :: new_current_thread ( )
52
+ . enable_time ( )
53
+ . build ( )
54
+ . unwrap ( ) ;
55
+ let _guard = runtime. enter ( ) ;
56
+ server
57
+ . connect ( proto:: ClientHello { name } , backend)
58
+ . unwrap ( ) ;
59
+ runtime. block_on ( server. run ( ) . instrument ( error_span ! ( "server" ) ) ) ;
60
+ debug ! ( "server thread terminated" ) ;
61
+ } ) ;
62
+
63
+ handle
64
+ }
65
+ Some ( _) => net:: spawn ( config. clone ( ) ) ,
66
+ } ;
75
67
let mut app = App {
76
- config : Arc :: new ( config ) ,
68
+ config,
77
69
dirs,
78
70
metrics,
79
71
window : None ,
72
+ net : Some ( net) ,
80
73
} ;
81
74
82
75
let event_loop = EventLoop :: new ( ) . unwrap ( ) ;
@@ -89,6 +82,7 @@ struct App {
89
82
dirs : directories:: ProjectDirs ,
90
83
metrics : Arc < metrics:: Recorder > ,
91
84
window : Option < graphics:: Window > ,
85
+ net : Option < server:: Handle > ,
92
86
}
93
87
94
88
impl ApplicationHandler for App {
@@ -98,11 +92,13 @@ impl ApplicationHandler for App {
98
92
// Initialize Vulkan with the extensions needed to render to the window
99
93
let core = Arc :: new ( graphics:: Core :: new ( window. required_extensions ( ) ) ) ;
100
94
101
- // Kick off networking
102
- let net = net:: spawn ( self . config . clone ( ) ) ;
103
-
104
95
// Finish creating the window, including the Vulkan resources used to render to it
105
- let mut window = graphics:: Window :: new ( window, core. clone ( ) , self . config . clone ( ) , net) ;
96
+ let mut window = graphics:: Window :: new (
97
+ window,
98
+ core. clone ( ) ,
99
+ self . config . clone ( ) ,
100
+ self . net . take ( ) . unwrap ( ) ,
101
+ ) ;
106
102
107
103
// Initialize widely-shared graphics resources
108
104
let gfx = Arc :: new (
0 commit comments