rak_rs/protocol/packet/
online.rs1use std::net::SocketAddr;
16
17use super::RakPacket;
18use crate::register_packets;
19
20use binary_util::interfaces::{Reader, Writer};
21use binary_util::io::{ByteReader, ByteWriter};
22use binary_util::BinaryIo;
23
24#[derive(BinaryIo, Clone, Debug)]
29#[repr(u8)]
30pub enum OnlinePacket {
31 ConnectedPing(ConnectedPing) = 0x00,
32 ConnectedPong(ConnectedPong) = 0x03,
33 LostConnection(LostConnection) = 0x04,
34 ConnectionRequest(ConnectionRequest) = 0x09,
35 ConnectionAccept(ConnectionAccept) = 0x10,
36 NewConnection(NewConnection) = 0x13,
37 Disconnect(Disconnect) = 0x15,
38}
39
40register_packets! {
41 Online is OnlinePacket,
42 ConnectedPing,
43 ConnectedPong,
44 LostConnection,
45 ConnectionRequest,
46 ConnectionAccept,
47 NewConnection,
48 Disconnect
49}
50
51#[derive(Clone, Debug, BinaryIo)]
59pub struct ConnectedPing {
60 pub time: i64,
62}
63
64#[derive(Clone, Debug, BinaryIo)]
70pub struct ConnectedPong {
71 pub ping_time: i64,
73 pub pong_time: i64,
75}
76
77#[derive(Clone, Debug, BinaryIo)]
80pub struct ConnectionRequest {
81 pub client_id: i64,
82 pub time: i64,
83 pub security: bool,
84}
85
86#[derive(Clone, Debug)]
89pub struct ConnectionAccept {
90 pub client_address: SocketAddr,
92 pub system_index: i16,
94 pub internal_ids: Vec<SocketAddr>,
98 pub request_time: i64,
100 pub timestamp: i64,
102}
103
104impl Reader<ConnectionAccept> for ConnectionAccept {
105 fn read(buf: &mut ByteReader) -> std::io::Result<Self> {
106 let client_address = buf.read_type::<SocketAddr>()?;
107
108 let system_index = buf.read_i16()?;
110 let mut internal_ids = Vec::<SocketAddr>::new();
111
112 for _ in 0..20 {
113 if buf.as_slice().len() <= 16 {
115 break;
116 }
117 internal_ids.push(buf.read_type::<SocketAddr>()?);
118 }
119
120 let request_time = buf.read_i64()?;
121 let timestamp = buf.read_i64()?;
122
123 Ok(Self {
124 client_address,
125 system_index,
126 internal_ids,
127 request_time,
128 timestamp,
129 })
130 }
131}
132
133impl Writer for ConnectionAccept {
134 fn write(&self, buf: &mut ByteWriter) -> std::io::Result<()> {
135 buf.write_type::<SocketAddr>(&self.client_address)?;
136 buf.write_i16(self.system_index)?;
137
138 if self.internal_ids.len() > 20 {
139 return Err(std::io::Error::new(
140 std::io::ErrorKind::InvalidInput,
141 "Too many internal id's",
142 ));
143 }
144
145 for internal_id in &self.internal_ids {
146 buf.write_type::<SocketAddr>(internal_id)?;
147 }
148
149 buf.write_i64(self.request_time)?;
150 buf.write_i64(self.timestamp)?;
151
152 Ok(())
153 }
154}
155
156#[derive(Clone, Debug)]
159pub struct NewConnection {
160 pub server_address: SocketAddr,
162 pub system_address: Vec<SocketAddr>,
164 pub request_time: i64,
166 pub timestamp: i64,
168}
169
170impl Reader<NewConnection> for NewConnection {
171 fn read(buf: &mut ByteReader) -> std::io::Result<Self> {
172 let server_address = buf.read_type::<SocketAddr>()?;
173
174 let mut system_address = Vec::<SocketAddr>::new();
175
176 for _ in 0..20 {
177 if buf.as_slice().len() < 16 {
179 break;
180 }
181 system_address.push(buf.read_type::<SocketAddr>()?);
182 }
183
184 let request_time = buf.read_i64()?;
185 let timestamp = buf.read_i64()?;
186
187 Ok(Self {
188 server_address,
189 system_address,
190 request_time,
191 timestamp,
192 })
193 }
194}
195
196impl Writer for NewConnection {
197 fn write(&self, buf: &mut ByteWriter) -> std::io::Result<()> {
198 buf.write_type::<SocketAddr>(&self.server_address)?;
199
200 if self.system_address.len() > 20 {
201 return Err(std::io::Error::new(
202 std::io::ErrorKind::InvalidInput,
203 "Too many internal id's",
204 ));
205 }
206
207 for system_address in &self.system_address {
208 buf.write_type::<SocketAddr>(system_address)?;
209 }
210
211 buf.write_i64(self.request_time)?;
212 buf.write_i64(self.timestamp)?;
213
214 Ok(())
215 }
216}
217
218#[derive(Clone, Debug, BinaryIo)]
220pub struct Disconnect {}
221
222#[derive(Clone, Debug, BinaryIo)]
225pub struct LostConnection {}