GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — feather/feather/protocol/src/lib.rs at develop · aheart/feather Expand file tree/ Copy pathlib.rsMore file actions 210 lines (184 loc) · 6.03 KB / Copy pathlib.rsFile metadata and controls210 lines (184 loc) · 6.03 KB 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 use anyhow::anyhow; use base::ItemStack; pub mod codec; pub mod io; pub mod packets; #[doc(inline)] pub use codec::MinecraftCodec; pub use io::Nbt; pub use io::{Readable, VarInt, VarLong, Writeable}; #[doc(inline)] pub use packets::{ client::{ClientHandshakePacket, ClientLoginPacket, ClientPlayPacket, ClientStatusPacket}, server::{ServerLoginPacket, ServerPlayPacket, ServerStatusPacket}, VariantOf, }; pub type Slot = Option<ItemStack>; /// A protocol version. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum ProtocolVersion { V1_16_2, } /// A protocol state. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub enum ProtocolState { Handshake, Status, Login, Play, } /// Reads an arbitrary packet sent by a client based on a dynamically-updated /// protocol state. As opposed to `MinecraftCodec`, this struct does not type-encode /// the current protocol state using generics. /// /// This is a wrapper around a `MinecraftCodec` but more useful in certain sitations /// (e.g. when writing a proxy.) pub struct ClientPacketCodec { state: ProtocolState, codec: MinecraftCodec, } impl Default for ClientPacketCodec { fn default() -> Self { Self::new() } } impl ClientPacketCodec { pub fn new() -> Self { Self { state: ProtocolState::Handshake, codec: MinecraftCodec::new(), } } pub fn set_state(&mut self, state: ProtocolState) { self.state = state } /// Decodes a `ClientPacket` using the provided data. pub fn decode(&mut self, data: &[u8]) -> anyhow::Result<Option<ClientPacket>> { self.codec.accept(data); match self.state { ProtocolState::Handshake => self .codec .next_packet::<ClientHandshakePacket>() .map(|opt| opt.map(ClientPacket::from)), ProtocolState::Status => self .codec .next_packet::<ClientStatusPacket>() .map(|opt| opt.map(ClientPacket::from)), ProtocolState::Login => self .codec .next_packet::<ClientLoginPacket>() .map(|opt| opt.map(ClientPacket::from)), ProtocolState::Play => self .codec .next_packet::<ClientPlayPacket>() .map(|opt| opt.map(ClientPacket::from)), } } /// Encodes a `ClientPacket` into a buffer. pub fn encode(&mut self, packet: &ClientPacket, buffer: &mut Vec<u8>) { match packet { ClientPacket::Handshake(packet) => self.codec.encode(packet, buffer).unwrap(), ClientPacket::Status(packet) => self.codec.encode(packet, buffer).unwrap(), ClientPacket::Login(packet) => self.codec.encode(packet, buffer).unwrap(), ClientPacket::Play(packet) => self.codec.encode(packet, buffer).unwrap(), } } } /// Similar to `ClientPacketCodec` but for server-sent packets. pub struct ServerPacketCodec { state: ProtocolState, codec: MinecraftCodec, } impl Default for ServerPacketCodec { fn default() -> Self { Self::new() } } impl ServerPacketCodec { pub fn new() -> Self { Self { state: ProtocolState::Handshake, codec: MinecraftCodec::new(), } } pub fn set_state(&mut self, state: ProtocolState) { self.state = state } /// Decodes a `ServerPacket` using the provided data. pub fn decode(&mut self, data: &[u8]) -> anyhow::Result<Option<ServerPacket>> { self.codec.accept(data); match self.state { ProtocolState::Handshake => Err(anyhow!("server sent data during handshake state")), ProtocolState::Status => self .codec .next_packet::<ServerStatusPacket>() .map(|opt| opt.map(ServerPacket::from)), ProtocolState::Login => self .codec .next_packet::<ServerLoginPacket>() .map(|opt| opt.map(ServerPacket::from)), ProtocolState::Play => self .codec .next_packet::<ServerPlayPacket>() .map(|opt| opt.map(ServerPacket::from)), } } /// Encodes a `ServerPacket` into a buffer. pub fn encode(&mut self, packet: &ServerPacket, buffer: &mut Vec<u8>) { match packet { ServerPacket::Status(packet) => self.codec.encode(packet, buffer).unwrap(), ServerPacket::Login(packet) => self.codec.encode(packet, buffer).unwrap(), ServerPacket::Play(packet) => self.codec.encode(packet, buffer).unwrap(), } } } /// A packet sent by the client from any one of the packet stages. #[derive(Debug, Clone)] pub enum ClientPacket { Handshake(ClientHandshakePacket), Status(ClientStatusPacket), Login(ClientLoginPacket), Play(ClientPlayPacket), } impl From<ClientHandshakePacket> for ClientPacket { fn from(packet: ClientHandshakePacket) -> Self { ClientPacket::Handshake(packet) } } impl From<ClientStatusPacket> for ClientPacket { fn from(packet: ClientStatusPacket) -> Self { ClientPacket::Status(packet) } } impl From<ClientLoginPacket> for ClientPacket { fn from(packet: ClientLoginPacket) -> Self { ClientPacket::Login(packet) } } impl From<ClientPlayPacket> for ClientPacket { fn from(packet: ClientPlayPacket) -> Self { ClientPacket::Play(packet) } } /// A packet sent by the server from any one of the packet stages. #[derive(Debug, Clone)] pub enum ServerPacket { Status(ServerStatusPacket), Login(ServerLoginPacket), Play(ServerPlayPacket), } impl From<ServerStatusPacket> for ServerPacket { fn from(packet: ServerStatusPacket) -> Self { ServerPacket::Status(packet) } } impl From<ServerLoginPacket> for ServerPacket { fn from(packet: ServerLoginPacket) -> Self { ServerPacket::Login(packet) } } impl From<ServerPlayPacket> for ServerPacket { fn from(packet: ServerPlayPacket) -> Self { ServerPacket::Play(packet) } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |