Remove Blobs and switch to Packets (#6937)
* Remove Blobs and switch to Packets * Fix some gossip messages not respecting MTU size * Failure to serialize is not fatal * Add log macros * Remove unused extern * Apparently macro use is required * Explicitly scope macro * Fix test compile
This commit is contained in:
@ -51,3 +51,5 @@ pub mod transport;
|
||||
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
extern crate log as logger;
|
||||
|
@ -1,6 +1,8 @@
|
||||
use crate::clock::Slot;
|
||||
use std::fmt;
|
||||
use bincode::Result;
|
||||
use serde::Serialize;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
use std::{fmt, io};
|
||||
|
||||
/// Maximum over-the-wire size of a Transaction
|
||||
/// 1280 is IPv6 minimum MTU
|
||||
@ -33,6 +35,29 @@ impl Packet {
|
||||
pub fn new(data: [u8; PACKET_DATA_SIZE], meta: Meta) -> Self {
|
||||
Self { data, meta }
|
||||
}
|
||||
|
||||
pub fn from_data<T: Serialize>(dest: &SocketAddr, data: T) -> Self {
|
||||
let mut me = Packet::default();
|
||||
if let Err(e) = Self::populate_packet(&mut me, Some(dest), &data) {
|
||||
logger::error!("Couldn't write to packet {:?}. Data skipped.", e);
|
||||
}
|
||||
me
|
||||
}
|
||||
|
||||
pub fn populate_packet<T: Serialize>(
|
||||
packet: &mut Packet,
|
||||
dest: Option<&SocketAddr>,
|
||||
data: &T,
|
||||
) -> Result<()> {
|
||||
let mut wr = io::Cursor::new(&mut packet.data[..]);
|
||||
bincode::serialize_into(&mut wr, data)?;
|
||||
let len = wr.position() as usize;
|
||||
packet.meta.size = len;
|
||||
if let Some(dest) = dest {
|
||||
packet.meta.set_addr(dest);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Packet {
|
||||
|
Reference in New Issue
Block a user