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
//! saffron's two-player networking code for turn-based games.
//!
//! # What sfn-tpn is made for
//!
//! This crate provides an interface for adding multiplayer to two-player turn-based games.
//! In particular, it is made for games that have strict turns.
//! That is, each player is allowed to take a turn if and only if it is not the other player's turn,
//! and players alternate turns.
//!
//! Think chess, checkers, Connect 4, (two-player) Blokus, and such.
//! Nonexamples could include games that allow actions on the other player's turn, like Trap Cards
//! from Yu-Gi-Oh, though you might be able to define the concept of a turn such that it works with
//! sfn-tpn.
//!
//! # What sfn-tpn can do
//!
//! This crate exposes a [`NetcodeInterface`] with functionality for
//!
//! - connecting two game instances (peer-to-peer via [iroh](https://www.iroh.computer/))
//! - sending byte buffers of a constant size between the two game instances
//! - doing so in a strictly turn-based manner (as described above)
//!
//! # What sfn-tpn can not do
//!
//! - connect multiple game instances
//! - anything not turn-based
//! - run on systems not supported by Tokio and iroh
//! - in particular, wasm is not supported because of threading shenanigans
//!
//! # Examples
//!
//! - See the examples directory at <https://github.com/wade-cheng/sfn-tpn>
//! - <https://github.com/wade-cheng/pieceboard>
use ;
/// Config used to create a new [`NetcodeInterface`].
///
/// The user was either given a ticket, or is generating a new ticket.
/// The interface for netcode.
///
/// Runs [Tokio](https://tokio.rs/) and [iroh](https://www.iroh.computer/)
/// under the hood in a separate thread. So, methods must be called from the
/// context of a Tokio runtime. The procedure for operation is as follows.
///
/// A [`new`][`NetcodeInterface::new`] `NetcodeInterface` should be created on
/// the two players' machines. The first, the "server," must provide a oneshot
/// sender that receives a newly generated ticket. The second, the "client,"
/// must provide a ticket string from that server.
///
/// The server moves second and the client moves first.
///
/// If it is the user's turn, they may:
///
/// - [`send_turn`][`NetcodeInterface::send_turn`] once
/// - it will no longer be the user's turn
///
/// If it is not the user's turn, they may:
///
/// - [`try_recv_turn`][`NetcodeInterface::try_recv_turn`] repeatedly
/// - if it returns `Ok`, it will be the user's turn.
///
/// Turns are represented as byte buffers of a constant size. Both players'
/// buffer sizes must be the same.
///
/// Deviations from this procedure are undefined behavior.