issun 0.10.0

A mini game engine for logic-focused games - Build games in ISSUN (一寸) of time
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! NetworkBackend trait and implementations

use super::types::{NetworkMetadata, NetworkScope, NodeId};
use crate::error::Result;
use async_trait::async_trait;
use tokio::sync::mpsc;

/// Type-erased network event (for receiving)
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct RawNetworkEvent {
    pub metadata: NetworkMetadata,
    pub scope: NetworkScope,
    pub type_name: String,
    pub payload: Vec<u8>, // bincode serialized
}

/// Network backend trait for event transmission
#[async_trait]
pub trait NetworkBackend: Send + Sync + 'static {
    /// Get this node's ID
    fn node_id(&self) -> NodeId;

    /// Send event to network
    async fn send(&self, event: RawNetworkEvent) -> Result<()>;

    /// Receive stream of raw events
    fn receive_stream(&self) -> mpsc::Receiver<RawNetworkEvent>;

    /// Connect to network
    async fn connect(&mut self, addr: &str) -> Result<()>;

    /// Disconnect from network
    async fn disconnect(&mut self) -> Result<()>;

    /// Check if connected
    fn is_connected(&self) -> bool;
}

/// Local-only backend (no network)
pub struct LocalOnlyBackend {
    node_id: NodeId,
}

impl LocalOnlyBackend {
    pub fn new() -> Self {
        Self {
            node_id: NodeId::from_u64(0),
        }
    }
}

impl Default for LocalOnlyBackend {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl NetworkBackend for LocalOnlyBackend {
    fn node_id(&self) -> NodeId {
        self.node_id
    }

    async fn send(&self, _event: RawNetworkEvent) -> Result<()> {
        // No-op for local-only
        Ok(())
    }

    fn receive_stream(&self) -> mpsc::Receiver<RawNetworkEvent> {
        // Return empty channel
        let (_tx, rx) = mpsc::channel(1);
        rx
    }

    async fn connect(&mut self, _addr: &str) -> Result<()> {
        Ok(())
    }

    async fn disconnect(&mut self) -> Result<()> {
        Ok(())
    }

    fn is_connected(&self) -> bool {
        false
    }
}

/// QUIC client backend for connecting to relay server
pub struct QuicClientBackend {
    node_id: NodeId,
    connection: Option<quinn::Connection>,
    send_tx: mpsc::Sender<RawNetworkEvent>,
    recv_rx: std::sync::Arc<std::sync::Mutex<Option<mpsc::Receiver<RawNetworkEvent>>>>,
    connected: std::sync::Arc<std::sync::atomic::AtomicBool>,
}

impl QuicClientBackend {
    /// Create a new QUIC client backend
    pub fn new() -> Self {
        let (send_tx, _send_rx) = mpsc::channel(1000);

        Self {
            node_id: NodeId::random(),
            connection: None,
            send_tx,
            recv_rx: std::sync::Arc::new(std::sync::Mutex::new(None)),
            connected: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
        }
    }

    /// Connect to relay server
    pub async fn connect_to_server(addr: &str) -> Result<Self> {
        use std::sync::Arc;

        // Install default crypto provider if not already installed
        let _ = rustls::crypto::ring::default_provider().install_default();

        // Generate random node ID
        let node_id = NodeId::random();

        // Configure QUIC client
        // For development, accept invalid certificates
        let crypto = rustls::ClientConfig::builder()
            .dangerous()
            .with_custom_certificate_verifier(Arc::new(SkipServerVerification))
            .with_no_client_auth();

        let client_config = quinn::ClientConfig::new(Arc::new(
            quinn::crypto::rustls::QuicClientConfig::try_from(crypto).map_err(|e| {
                crate::error::IssunError::NetworkError(format!("TLS config error: {}", e))
            })?,
        ));

        let mut endpoint = quinn::Endpoint::client("[::]:0".parse().unwrap()).map_err(|e| {
            crate::error::IssunError::NetworkError(format!("Failed to create endpoint: {}", e))
        })?;

        endpoint.set_default_client_config(client_config);

        // Connect to server
        let connection = endpoint
            .connect(addr.parse().unwrap(), "localhost")
            .map_err(|e| {
                crate::error::IssunError::NetworkError(format!("Connection failed: {}", e))
            })?
            .await
            .map_err(|e| {
                crate::error::IssunError::NetworkError(format!("Connection failed: {}", e))
            })?;

        // Perform handshake
        let mut send_stream = connection.open_uni().await.map_err(|e| {
            crate::error::IssunError::NetworkError(format!("Failed to open stream: {}", e))
        })?;

        send_stream
            .write_all(&node_id.as_u64().to_le_bytes())
            .await
            .map_err(|e| {
                crate::error::IssunError::NetworkError(format!("Handshake failed: {}", e))
            })?;

        send_stream.finish().map_err(|e| {
            crate::error::IssunError::NetworkError(format!("Handshake failed: {}", e))
        })?;

        // Receive handshake ack
        let mut recv_stream = connection.accept_uni().await.map_err(|e| {
            crate::error::IssunError::NetworkError(format!("Handshake failed: {}", e))
        })?;

        let mut buf = [0u8; 8];
        recv_stream.read_exact(&mut buf).await.map_err(|e| {
            crate::error::IssunError::NetworkError(format!("Handshake failed: {}", e))
        })?;

        // Create channels
        let (send_tx, mut send_rx) = mpsc::channel::<RawNetworkEvent>(1000);
        let (recv_tx, recv_rx) = mpsc::channel::<RawNetworkEvent>(1000);

        let connected = Arc::new(std::sync::atomic::AtomicBool::new(true));

        // Spawn send worker
        let conn_send = connection.clone();
        let connected_send = connected.clone();
        tokio::spawn(async move {
            while let Some(event) = send_rx.recv().await {
                if !connected_send.load(std::sync::atomic::Ordering::SeqCst) {
                    break;
                }

                if let Err(e) = Self::send_event(&conn_send, &event).await {
                    eprintln!("Failed to send event: {:?}", e);
                }
            }
        });

        // Spawn receive worker
        let conn_recv = connection.clone();
        let connected_recv = connected.clone();
        tokio::spawn(async move {
            loop {
                if !connected_recv.load(std::sync::atomic::Ordering::SeqCst) {
                    break;
                }

                match Self::receive_event(&conn_recv).await {
                    Ok(Some(event)) => {
                        let _ = recv_tx.send(event).await;
                    }
                    Ok(None) => {
                        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
                    }
                    Err(e) => {
                        eprintln!("Failed to receive event: {:?}", e);
                        break;
                    }
                }
            }
        });

        Ok(Self {
            node_id,
            connection: Some(connection),
            send_tx,
            recv_rx: Arc::new(std::sync::Mutex::new(Some(recv_rx))),
            connected,
        })
    }

    async fn send_event(connection: &quinn::Connection, event: &RawNetworkEvent) -> Result<()> {
        let mut stream = connection.open_uni().await.map_err(|e| {
            crate::error::IssunError::NetworkError(format!("Failed to open stream: {}", e))
        })?;

        // Create frame
        let payload = bincode::serialize(event).map_err(|e| {
            crate::error::IssunError::NetworkError(format!("Serialization failed: {}", e))
        })?;

        let mut frame = Vec::with_capacity(8 + payload.len());
        frame.extend_from_slice(&[0x49, 0x53]); // Magic: "IS"
        frame.push(0x01); // Version
        frame.push(0x01); // Frame type: Event
        frame.extend_from_slice(&(payload.len() as u32).to_le_bytes());
        frame.extend_from_slice(&payload);

        stream
            .write_all(&frame)
            .await
            .map_err(|e| crate::error::IssunError::NetworkError(format!("Write failed: {}", e)))?;

        stream
            .finish()
            .map_err(|e| crate::error::IssunError::NetworkError(format!("Finish failed: {}", e)))?;

        Ok(())
    }

    async fn receive_event(connection: &quinn::Connection) -> Result<Option<RawNetworkEvent>> {
        // Try to accept incoming stream with timeout
        let recv_result = tokio::time::timeout(
            tokio::time::Duration::from_millis(10),
            connection.accept_uni(),
        )
        .await;

        let mut stream = match recv_result {
            Ok(Ok(stream)) => stream,
            Ok(Err(e)) => {
                return Err(crate::error::IssunError::NetworkError(format!(
                    "Accept failed: {}",
                    e
                )));
            }
            Err(_) => return Ok(None), // Timeout
        };

        // Read frame header
        let mut header = [0u8; 8];
        stream.read_exact(&mut header).await.map_err(|e| {
            crate::error::IssunError::NetworkError(format!("Read header failed: {}", e))
        })?;

        // Parse header
        if header[0] != 0x49 || header[1] != 0x53 {
            return Err(crate::error::IssunError::NetworkError(
                "Invalid magic bytes".to_string(),
            ));
        }

        let payload_len = u32::from_le_bytes([header[4], header[5], header[6], header[7]]) as usize;

        // Read payload
        let mut payload = vec![0u8; payload_len];
        stream.read_exact(&mut payload).await.map_err(|e| {
            crate::error::IssunError::NetworkError(format!("Read payload failed: {}", e))
        })?;

        // Deserialize event
        let event: RawNetworkEvent = bincode::deserialize(&payload).map_err(|e| {
            crate::error::IssunError::NetworkError(format!("Deserialization failed: {}", e))
        })?;

        Ok(Some(event))
    }
}

impl Default for QuicClientBackend {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl NetworkBackend for QuicClientBackend {
    fn node_id(&self) -> NodeId {
        self.node_id
    }

    async fn send(&self, event: RawNetworkEvent) -> Result<()> {
        self.send_tx
            .send(event)
            .await
            .map_err(|e| crate::error::IssunError::NetworkError(format!("Send failed: {}", e)))
    }

    fn receive_stream(&self) -> mpsc::Receiver<RawNetworkEvent> {
        // Take ownership of the receiver from the Arc<Mutex<Option>>
        // This can only be called once - EventBus::with_network() takes ownership
        if let Ok(mut guard) = self.recv_rx.lock() {
            if let Some(rx) = guard.take() {
                return rx;
            }
        }

        // If already taken or lock failed, return empty channel
        let (_tx, rx) = mpsc::channel(1);
        rx
    }

    async fn connect(&mut self, addr: &str) -> Result<()> {
        let backend = Self::connect_to_server(addr).await?;
        *self = backend;
        Ok(())
    }

    async fn disconnect(&mut self) -> Result<()> {
        self.connected
            .store(false, std::sync::atomic::Ordering::SeqCst);
        if let Some(connection) = self.connection.take() {
            connection.close(0u32.into(), b"disconnect");
        }
        Ok(())
    }

    fn is_connected(&self) -> bool {
        self.connected.load(std::sync::atomic::Ordering::SeqCst)
    }
}

/// Skip server certificate verification for development
#[derive(Debug)]
struct SkipServerVerification;

impl rustls::client::danger::ServerCertVerifier for SkipServerVerification {
    fn verify_server_cert(
        &self,
        _end_entity: &rustls::pki_types::CertificateDer<'_>,
        _intermediates: &[rustls::pki_types::CertificateDer<'_>],
        _server_name: &rustls::pki_types::ServerName<'_>,
        _ocsp_response: &[u8],
        _now: rustls::pki_types::UnixTime,
    ) -> std::result::Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
        Ok(rustls::client::danger::ServerCertVerified::assertion())
    }

    fn verify_tls12_signature(
        &self,
        _message: &[u8],
        _cert: &rustls::pki_types::CertificateDer<'_>,
        _dss: &rustls::DigitallySignedStruct,
    ) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
    }

    fn verify_tls13_signature(
        &self,
        _message: &[u8],
        _cert: &rustls::pki_types::CertificateDer<'_>,
        _dss: &rustls::DigitallySignedStruct,
    ) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
    }

    fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
        // Return all signature schemes supported by ring crypto provider
        rustls::crypto::ring::default_provider()
            .signature_verification_algorithms
            .supported_schemes()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_local_only_backend() {
        let mut backend = LocalOnlyBackend::new();

        assert_eq!(backend.node_id(), NodeId::from_u64(0));
        assert!(!backend.is_connected());

        backend.connect("dummy").await.unwrap();
        assert!(!backend.is_connected());

        backend.disconnect().await.unwrap();
    }
}