protocol: move plugin message writing from Server to Conn; add write_fml2_handshake_plugin_message

This commit is contained in:
ice_iix 2021-01-23 09:16:42 -08:00
parent a29b6b2c34
commit 6ae092b16c
3 changed files with 62 additions and 34 deletions

View File

@ -226,7 +226,7 @@ pub mod fml2 {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
Ok(Registry {
name: Serializable::read_from(buf)?,
marker: "".to_string() // not in ModList
marker: "".to_string(), // not in ModList
})
}

View File

@ -1099,7 +1099,47 @@ impl Conn {
}
self.write_all(&buf)?;
Result::Ok(())
Ok(())
}
pub fn write_plugin_message(&mut self, channel: &str, data: &[u8]) -> Result<(), Error> {
if is_network_debug() {
debug!(
"Sending plugin message: channel={}, data={:?}",
channel, data
);
}
if self.protocol_version >= 47 {
self.write_packet(packet::play::serverbound::PluginMessageServerbound {
channel: channel.to_string(),
data: data.to_vec(),
})?;
} else {
self.write_packet(packet::play::serverbound::PluginMessageServerbound_i16 {
channel: channel.to_string(),
data: LenPrefixedBytes::<VarShort>::new(data.to_vec()),
})?;
}
Ok(())
}
pub fn write_fmlhs_plugin_message(&mut self, msg: &forge::FmlHs) -> Result<(), Error> {
let mut buf: Vec<u8> = vec![];
msg.write_to(&mut buf).unwrap();
self.write_plugin_message("FML|HS", &buf)
}
pub fn write_fml2_handshake_plugin_message(
&mut self,
msg: &forge::fml2::FmlHandshake,
) -> Result<(), Error> {
let mut buf: Vec<u8> = vec![];
msg.write_to(&mut buf).unwrap();
// TODO: double-wrap
self.write_plugin_message("fml:loginwrapper", &buf)
}
#[allow(clippy::type_complexity)]

View File

@ -267,7 +267,6 @@ impl Server {
let packet =
forge::fml2::FmlHandshake::packet_by_id(id, &mut data)?;
use forge::fml2::FmlHandshake::*;
use protocol::Serializable;
match packet {
ModList {
mod_names,
@ -275,12 +274,13 @@ impl Server {
registries,
} => {
info!("ModList mod_names={:?} channels={:?} registries={:?}", mod_names, channels, registries);
ModListReply {
mod_names,
channels,
registries,
}
.write_to(&mut write)?
write.write_fml2_handshake_plugin_message(
&ModListReply {
mod_names,
channels,
registries,
},
)?;
}
ServerRegistry {
name,
@ -288,7 +288,9 @@ impl Server {
snapshot: _,
} => {
info!("ServerRegistry {:?}", name);
Acknowledgement.write_to(&mut write)?
write.write_fml2_handshake_plugin_message(
&Acknowledgement,
)?;
}
ConfigurationData { filename, contents } => {
info!(
@ -296,7 +298,9 @@ impl Server {
filename,
String::from_utf8_lossy(&contents)
);
Acknowledgement.write_to(&mut write)?
write.write_fml2_handshake_plugin_message(
&Acknowledgement,
)?;
}
_ => unimplemented!(),
}
@ -1008,33 +1012,17 @@ impl Server {
}
}
// TODO: remove wrappers and directly call on Conn
fn write_fmlhs_plugin_message(&mut self, msg: &forge::FmlHs) {
use crate::protocol::Serializable;
let mut buf: Vec<u8> = vec![];
msg.write_to(&mut buf).unwrap();
self.write_plugin_message("FML|HS", &buf);
let _ = self.conn.as_mut().unwrap().write_fmlhs_plugin_message(msg); // TODO handle errors
}
fn write_plugin_message(&mut self, channel: &str, data: &[u8]) {
if protocol::is_network_debug() {
debug!(
"Sending plugin message: channel={}, data={:?}",
channel, data
);
}
if self.protocol_version >= 47 {
self.write_packet(packet::play::serverbound::PluginMessageServerbound {
channel: channel.to_string(),
data: data.to_vec(),
});
} else {
self.write_packet(packet::play::serverbound::PluginMessageServerbound_i16 {
channel: channel.to_string(),
data: crate::protocol::LenPrefixedBytes::<protocol::VarShort>::new(data.to_vec()),
});
}
let _ = self
.conn
.as_mut()
.unwrap()
.write_plugin_message(channel, data); // TODO handle errors
}
fn on_game_join_worldnames_ishard(