Skip to content

Commit

Permalink
Migrate protobuf to version 3.7.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
panhania committed Nov 14, 2024
1 parent 79ad8ef commit 3fba7a7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 53 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ homepage = "https://github.com/google/fleetspeak-rs"
repository = "https://github.com/google/fleetspeak-rs"

[workspace.dependencies]
protobuf = { version = "2.27.1" }
protobuf-codegen-pure = { version = "2.27.1" }
protobuf = { version = "3.7.1" }
protobuf-codegen = { version = "3.7.1" }
2 changes: 1 addition & 1 deletion crates/fleetspeak-proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ exclude = [
protobuf = { workspace = true }

[build-dependencies]
protobuf-codegen-pure = { workspace = true }
protobuf-codegen = { workspace = true }
47 changes: 23 additions & 24 deletions crates/fleetspeak-proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,34 @@
// Use of this source code is governed by an MIT-style license that can be found
// in the LICENSE file or at https://opensource.org/licenses/MIT.

use std::io::Result;
use std::path::PathBuf;

fn cargo_out_dir() -> PathBuf {
let out_dir = std::env::var("OUT_DIR")
.expect("output folder not specified");
const PROTOS: &'static [&'static str] = &[
"vendor/fleetspeak/fleetspeak/src/common/proto/fleetspeak/common.proto",
"vendor/fleetspeak/fleetspeak/src/client/channel/proto/fleetspeak_channel/channel.proto",
];

PathBuf::from(out_dir)
}
fn main() {
let outdir: PathBuf = std::env::var("OUT_DIR")
.expect("no output directory")
.into();

fn proto_out_dir() -> PathBuf {
cargo_out_dir().join("proto")
}
for proto in PROTOS {
println!("cargo:rerun-if-changed={}", proto);
}

fn main() -> Result<()> {
let proto_out_dir = proto_out_dir();
std::fs::create_dir_all(&proto_out_dir)?;
let proto_out_dir = outdir.join("proto");
std::fs::create_dir_all(&proto_out_dir).unwrap();

protobuf_codegen_pure::Codegen::new()
.out_dir(&proto_out_dir.to_str().unwrap())
.include("vendor/fleetspeak/fleetspeak/src")
.include("vendor/protobuf/src")
.input("vendor/fleetspeak/fleetspeak/src/common/proto/fleetspeak/common.proto")
.input("vendor/fleetspeak/fleetspeak/src/client/channel/proto/fleetspeak_channel/channel.proto")
.customize(protobuf_codegen_pure::Customize {
gen_mod_rs: Some(true),
..Default::default()
})
.run()?;
let customize = protobuf_codegen::Customize::default()
.gen_mod_rs(true)
.generate_accessors(true);

Ok(())
protobuf_codegen::Codegen::new()
.pure()
.out_dir(&proto_out_dir)
.include("vendor/fleetspeak/fleetspeak/src")
.inputs(PROTOS)
.customize(customize)
.run().unwrap();
}
38 changes: 12 additions & 26 deletions crates/fleetspeak/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,14 @@ pub fn write_startup<W>(output: &mut W, version: &str) -> std::io::Result<()>
where
W: Write,
{
use protobuf::Message as _;

let mut data = fleetspeak_proto::channel::StartupData::new();
data.set_pid(i64::from(std::process::id()));
data.set_version(String::from(version));

let mut proto = fleetspeak_proto::common::Message::new();
proto.set_message_type(String::from("StartupData"));
proto.mut_destination().set_service_name(String::from("system"));
proto.mut_data().set_type_url(type_url(&data));
proto.mut_data().set_value(data.write_to_bytes()?);
*proto.mut_data() = protobuf::well_known_types::any::Any::pack(&data)?;

write_proto(output, proto)
}
Expand All @@ -138,7 +135,7 @@ where
proto.set_message_type(message.kind.unwrap_or_else(String::new));
proto.mut_destination().set_service_name(message.service);
// TODO: Consider a way of providing the type URL of the data being sent.
proto.mut_data().set_value(message.data);
proto.mut_data().value = message.data;

write_proto(output, proto)
}
Expand Down Expand Up @@ -174,7 +171,7 @@ where
// should we error-out or return a default value? For the time being we
// stick to the default approach, but if this proves to be not working
// well in practice, it might be reconsidered.
let mut data = if proto.has_data() {
let data = if proto.has_data() {
proto.take_data()
} else {
log::warn!("empty message from '{}'", service);
Expand All @@ -184,7 +181,7 @@ where
Ok(Message {
service: service,
kind: Some(proto.message_type),
data: data.take_value(),
data: data.value,
})
}

Expand All @@ -202,7 +199,14 @@ where
{
use protobuf::Message as _;

output.write_u32::<LittleEndian>(proto.compute_size())?;
// Fleetspeak is not able to send messages bigger than 2 MiB anyway, so we
// generally do not expect overflows here.
let size = u32::try_from(proto.compute_size())
.map_err(|error| {
std::io::Error::new(std::io::ErrorKind::InvalidData, error)
})?;

output.write_u32::<LittleEndian>(size)?;
proto.write_to_writer(output)?;
write_magic(output)?;
output.flush()?;
Expand Down Expand Up @@ -277,16 +281,6 @@ impl From<InvalidMagicError> for std::io::Error {

const MAGIC: u32 = 0xf1ee1001;

/// Computes a type URL of the given Protocol Buffers message.
///
/// This function should probably be part of the `protobuf` package but for some
/// reason it is not and we have to implement it ourselves.
fn type_url<M: protobuf::Message>(message: &M) -> String {
format!("{}/{}", TYPE_URL_PREFIX, message.descriptor().full_name())
}

const TYPE_URL_PREFIX: &'static str = "type.googleapis.com";

#[cfg(test)]
mod tests {
use std::io::Cursor;
Expand Down Expand Up @@ -320,12 +314,4 @@ mod tests {
let mut cur_out = Cursor::new(&mut buf_out[..]);
assert!(handshake(&mut cur_in, &mut cur_out).is_err());
}

#[test]
fn type_url_startup_data() {
assert_eq! {
type_url(&fleetspeak_proto::channel::StartupData::new()),
"type.googleapis.com/fleetspeak.channel.StartupData"
};
}
}

0 comments on commit 3fba7a7

Please sign in to comment.