|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use tokio::io; |
| 4 | + |
| 5 | +use lafere::packet::{ |
| 6 | + BodyBytes, BodyBytesMut, Flags, Packet, PacketBytes, PacketError, |
| 7 | + PacketHeader, |
| 8 | +}; |
| 9 | +use lafere::server::Message; |
| 10 | +use lafere::{client, server}; |
| 11 | + |
| 12 | +use bytes::{Bytes, BytesMut, BytesRead, BytesWrite}; |
| 13 | + |
| 14 | +#[derive(Debug, Clone)] |
| 15 | +struct MyPacket<B> { |
| 16 | + header: MyHeader, |
| 17 | + bytes: B, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, Default, Clone, PartialEq, Eq)] |
| 21 | +struct MyHeader { |
| 22 | + length: u32, |
| 23 | + flags: Flags, |
| 24 | + id: u32, |
| 25 | +} |
| 26 | + |
| 27 | +impl<B> MyPacket<B> |
| 28 | +where |
| 29 | + B: PacketBytes, |
| 30 | +{ |
| 31 | + pub fn empty() -> Self { |
| 32 | + Self { |
| 33 | + header: MyHeader::default(), |
| 34 | + bytes: B::new(MyHeader::LEN as usize), |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + pub fn body(&self) -> BodyBytes<'_> { |
| 39 | + self.bytes.body() |
| 40 | + } |
| 41 | + |
| 42 | + pub fn body_mut(&mut self) -> BodyBytesMut<'_> { |
| 43 | + self.bytes.body_mut() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl<B> Packet<B> for MyPacket<B> |
| 48 | +where |
| 49 | + B: PacketBytes, |
| 50 | +{ |
| 51 | + type Header = MyHeader; |
| 52 | + |
| 53 | + fn header(&self) -> &Self::Header { |
| 54 | + &self.header |
| 55 | + } |
| 56 | + |
| 57 | + fn header_mut(&mut self) -> &mut Self::Header { |
| 58 | + &mut self.header |
| 59 | + } |
| 60 | + |
| 61 | + fn empty() -> Self { |
| 62 | + Self { |
| 63 | + header: MyHeader::default(), |
| 64 | + bytes: B::new(MyHeader::LEN as usize), |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + fn from_bytes_and_header( |
| 69 | + bytes: B, |
| 70 | + header: Self::Header, |
| 71 | + ) -> Result<Self, PacketError> { |
| 72 | + Ok(Self { header, bytes }) |
| 73 | + } |
| 74 | + |
| 75 | + fn into_bytes(mut self) -> B { |
| 76 | + self.header.length = self.bytes.body().len() as u32; |
| 77 | + self.header.write_to(self.bytes.header_mut()); |
| 78 | + self.bytes |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl MyHeader { |
| 83 | + fn write_to(&self, mut bytes: BytesMut) { |
| 84 | + bytes.write_u32(self.length); |
| 85 | + bytes.write_u8(self.flags.as_u8()); |
| 86 | + bytes.write_u32(self.id); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl PacketHeader for MyHeader { |
| 91 | + const LEN: u32 = 4 + 4 + 1; |
| 92 | + |
| 93 | + fn from_bytes(mut bytes: Bytes) -> Result<Self, PacketError> { |
| 94 | + Ok(Self { |
| 95 | + length: bytes.read_u32(), |
| 96 | + flags: Flags::from_u8(bytes.read_u8())?, |
| 97 | + id: bytes.read_u32(), |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + fn body_len(&self) -> u32 { |
| 102 | + self.length |
| 103 | + } |
| 104 | + |
| 105 | + fn flags(&self) -> &Flags { |
| 106 | + &self.flags |
| 107 | + } |
| 108 | + |
| 109 | + fn set_flags(&mut self, flags: Flags) { |
| 110 | + self.flags = flags; |
| 111 | + } |
| 112 | + |
| 113 | + fn id(&self) -> u32 { |
| 114 | + self.id |
| 115 | + } |
| 116 | + |
| 117 | + fn set_id(&mut self, id: u32) { |
| 118 | + self.id = id; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +#[tokio::main] |
| 123 | +async fn main() { |
| 124 | + let (client, server) = io::duplex(1024); |
| 125 | + |
| 126 | + let mut alice = client::Connection::<MyPacket<_>>::new( |
| 127 | + client, |
| 128 | + client::Config { |
| 129 | + timeout: Duration::from_secs(1), |
| 130 | + body_limit: 1024, |
| 131 | + }, |
| 132 | + None, |
| 133 | + ); |
| 134 | + |
| 135 | + let mut bob = server::Connection::<MyPacket<_>>::new( |
| 136 | + server, |
| 137 | + server::Config { |
| 138 | + timeout: Duration::from_secs(1), |
| 139 | + body_limit: 1024, |
| 140 | + }, |
| 141 | + ); |
| 142 | + |
| 143 | + let client_task = tokio::spawn(async move { |
| 144 | + alice.enable_server_requests().await.unwrap(); |
| 145 | + |
| 146 | + // listen for a request from Bob |
| 147 | + let msg = alice.receive().await.unwrap(); |
| 148 | + match msg { |
| 149 | + Message::Request(req, resp_sender) => { |
| 150 | + assert_eq!(req.body().as_slice(), b"Hello, Alice!"); |
| 151 | + |
| 152 | + let mut resp = MyPacket::empty(); |
| 153 | + resp.body_mut().write(b"Hello, Back!"); |
| 154 | + resp_sender.send(resp).unwrap(); |
| 155 | + } |
| 156 | + _ => unreachable!(), |
| 157 | + } |
| 158 | + |
| 159 | + // send a request to Bob |
| 160 | + let mut req = MyPacket::empty(); |
| 161 | + req.body_mut().write(b"Hello, Bob!"); |
| 162 | + |
| 163 | + let resp = alice.request(req).await.unwrap(); |
| 164 | + assert_eq!(resp.body().as_slice(), b"Hello, Back!"); |
| 165 | + |
| 166 | + assert!(alice.receive().await.is_none()); |
| 167 | + }); |
| 168 | + |
| 169 | + let server_task = tokio::spawn(async move { |
| 170 | + // wait for Alice to enable server requests |
| 171 | + let msg = bob.receive().await.unwrap(); |
| 172 | + assert!(matches!(msg, Message::EnableServerRequests)); |
| 173 | + |
| 174 | + // send a request to Alice |
| 175 | + let mut req = MyPacket::empty(); |
| 176 | + req.body_mut().write(b"Hello, Alice!"); |
| 177 | + |
| 178 | + let resp = bob.request(req).await.unwrap(); |
| 179 | + assert_eq!(resp.body().as_slice(), b"Hello, Back!"); |
| 180 | + |
| 181 | + // listen for a request from Alice |
| 182 | + let msg = bob.receive().await.unwrap(); |
| 183 | + match msg { |
| 184 | + Message::Request(req, resp_sender) => { |
| 185 | + assert_eq!(req.body().as_slice(), b"Hello, Bob!"); |
| 186 | + |
| 187 | + let mut resp = MyPacket::empty(); |
| 188 | + resp.body_mut().write(b"Hello, Back!"); |
| 189 | + resp_sender.send(resp).unwrap(); |
| 190 | + } |
| 191 | + _ => unreachable!(), |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + tokio::try_join!(client_task, server_task).unwrap(); |
| 196 | +} |
| 197 | + |
| 198 | +#[test] |
| 199 | +fn server_requests_main() { |
| 200 | + main(); |
| 201 | +} |
0 commit comments