Move message size constant to `Message` type

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-10-18 13:06:45 +02:00
parent c306392d13
commit f5e881b5a9
No known key found for this signature in database
2 changed files with 14 additions and 15 deletions

View File

@ -1,8 +1,7 @@
use std::mem::size_of;
use crate::service::message::*;
use crate::service::*;
use crate::wire;
use std::mem::size_of;
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub enum PingState {
@ -118,14 +117,14 @@ impl Session {
pub fn ping(&mut self, reactor: &mut Reactor) -> Result<(), SessionError> {
if let SessionState::Negotiated { ping, .. } = &mut self.state {
let ponglen = self.rng.u16(0..wire::message::MAX_PAYLOAD_SIZE_BYTES);
let msg =
Message::Ping {
ponglen,
zeroes: message::ZeroBytes::new(self.rng.u16(
0..(wire::message::MAX_PAYLOAD_SIZE_BYTES - (size_of::<u16>() as u16)),
)),
};
let ponglen = self.rng.u16(0..Message::MAX_SIZE);
let msg = Message::Ping {
ponglen,
zeroes: message::ZeroBytes::new(
self.rng
.u16(0..(Message::MAX_SIZE - size_of::<u16>() as u16)),
),
};
reactor.write(self.addr, msg);
*ping = PingState::AwaitingResponse(ponglen);

View File

@ -7,10 +7,6 @@ use crate::prelude::*;
use crate::service::message::*;
use crate::wire;
/// The maximum supported message size in bytes.
pub const MAX_PAYLOAD_SIZE_BYTES: wire::Size =
wire::Size::MAX - (mem::size_of::<MessageType>() as wire::Size);
/// Message type.
#[repr(u16)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -48,6 +44,10 @@ impl TryFrom<u16> for MessageType {
}
impl Message {
/// The maximum supported message size in bytes.
pub const MAX_SIZE: wire::Size =
wire::Size::MAX - (mem::size_of::<MessageType>() as wire::Size);
pub fn type_id(&self) -> u16 {
match self {
Self::Initialize { .. } => MessageType::Initialize,