1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Wire encoding/decoding for Lightning messages according to [BOLT #1], and for
11 //! custom message through the [`CustomMessageReader`] trait.
13 //! [BOLT #1]: https://github.com/lightningnetwork/lightning-rfc/blob/master/01-messaging.md
17 use util::ser::{Readable, Writeable, Writer};
19 /// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
21 pub trait CustomMessageReader {
22 /// The type of the message decoded by the implementation.
23 type CustomMessage: Type;
24 /// Decodes a custom message to `CustomMessageType`. If the given message type is known to the
25 /// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
26 /// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
27 /// occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
28 fn read<R: io::Read>(&self, message_type: u16, buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError>;
31 /// A Lightning message returned by [`read()`] when decoding bytes received over the wire. Each
32 /// variant contains a message from [`msgs`] or otherwise the message type if unknown.
33 #[allow(missing_docs)]
35 pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
37 Error(msgs::ErrorMessage),
38 Warning(msgs::WarningMessage),
41 OpenChannel(msgs::OpenChannel),
42 AcceptChannel(msgs::AcceptChannel),
43 FundingCreated(msgs::FundingCreated),
44 FundingSigned(msgs::FundingSigned),
45 FundingLocked(msgs::FundingLocked),
46 Shutdown(msgs::Shutdown),
47 ClosingSigned(msgs::ClosingSigned),
48 UpdateAddHTLC(msgs::UpdateAddHTLC),
49 UpdateFulfillHTLC(msgs::UpdateFulfillHTLC),
50 UpdateFailHTLC(msgs::UpdateFailHTLC),
51 UpdateFailMalformedHTLC(msgs::UpdateFailMalformedHTLC),
52 CommitmentSigned(msgs::CommitmentSigned),
53 RevokeAndACK(msgs::RevokeAndACK),
54 UpdateFee(msgs::UpdateFee),
55 ChannelReestablish(msgs::ChannelReestablish),
56 AnnouncementSignatures(msgs::AnnouncementSignatures),
57 ChannelAnnouncement(msgs::ChannelAnnouncement),
58 NodeAnnouncement(msgs::NodeAnnouncement),
59 ChannelUpdate(msgs::ChannelUpdate),
60 QueryShortChannelIds(msgs::QueryShortChannelIds),
61 ReplyShortChannelIdsEnd(msgs::ReplyShortChannelIdsEnd),
62 QueryChannelRange(msgs::QueryChannelRange),
63 ReplyChannelRange(msgs::ReplyChannelRange),
64 GossipTimestampFilter(msgs::GossipTimestampFilter),
65 /// A message that could not be decoded because its type is unknown.
67 /// A message that was produced by a [`CustomMessageReader`] and is to be handled by a
68 /// [`::ln::peer_handler::CustomMessageHandler`].
72 impl<T> Message<T> where T: core::fmt::Debug + Type {
73 /// Returns the type that was used to decode the message payload.
74 pub fn type_id(&self) -> u16 {
76 &Message::Init(ref msg) => msg.type_id(),
77 &Message::Error(ref msg) => msg.type_id(),
78 &Message::Warning(ref msg) => msg.type_id(),
79 &Message::Ping(ref msg) => msg.type_id(),
80 &Message::Pong(ref msg) => msg.type_id(),
81 &Message::OpenChannel(ref msg) => msg.type_id(),
82 &Message::AcceptChannel(ref msg) => msg.type_id(),
83 &Message::FundingCreated(ref msg) => msg.type_id(),
84 &Message::FundingSigned(ref msg) => msg.type_id(),
85 &Message::FundingLocked(ref msg) => msg.type_id(),
86 &Message::Shutdown(ref msg) => msg.type_id(),
87 &Message::ClosingSigned(ref msg) => msg.type_id(),
88 &Message::UpdateAddHTLC(ref msg) => msg.type_id(),
89 &Message::UpdateFulfillHTLC(ref msg) => msg.type_id(),
90 &Message::UpdateFailHTLC(ref msg) => msg.type_id(),
91 &Message::UpdateFailMalformedHTLC(ref msg) => msg.type_id(),
92 &Message::CommitmentSigned(ref msg) => msg.type_id(),
93 &Message::RevokeAndACK(ref msg) => msg.type_id(),
94 &Message::UpdateFee(ref msg) => msg.type_id(),
95 &Message::ChannelReestablish(ref msg) => msg.type_id(),
96 &Message::AnnouncementSignatures(ref msg) => msg.type_id(),
97 &Message::ChannelAnnouncement(ref msg) => msg.type_id(),
98 &Message::NodeAnnouncement(ref msg) => msg.type_id(),
99 &Message::ChannelUpdate(ref msg) => msg.type_id(),
100 &Message::QueryShortChannelIds(ref msg) => msg.type_id(),
101 &Message::ReplyShortChannelIdsEnd(ref msg) => msg.type_id(),
102 &Message::QueryChannelRange(ref msg) => msg.type_id(),
103 &Message::ReplyChannelRange(ref msg) => msg.type_id(),
104 &Message::GossipTimestampFilter(ref msg) => msg.type_id(),
105 &Message::Unknown(type_id) => type_id,
106 &Message::Custom(ref msg) => msg.type_id(),
110 /// Returns whether the message's type is even, indicating both endpoints must support it.
111 pub fn is_even(&self) -> bool {
112 (self.type_id() & 1) == 0
116 /// Reads a message from the data buffer consisting of a 2-byte big-endian type and a
117 /// variable-length payload conforming to the type.
121 /// Returns an error if the message payload code not be decoded as the specified type.
122 pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, custom_reader: H)
123 -> Result<Message<T>, (msgs::DecodeError, Option<u16>)> where
124 T: core::fmt::Debug + Type + Writeable,
125 H::Target: CustomMessageReader<CustomMessage = T>,
127 let message_type = <u16 as Readable>::read(buffer).map_err(|e| (e, None))?;
128 do_read(buffer, message_type, custom_reader).map_err(|e| (e, Some(message_type)))
131 fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u16, custom_reader: H)
132 -> Result<Message<T>, msgs::DecodeError> where
133 T: core::fmt::Debug + Type + Writeable,
134 H::Target: CustomMessageReader<CustomMessage = T>,
137 msgs::Init::TYPE => {
138 Ok(Message::Init(Readable::read(buffer)?))
140 msgs::ErrorMessage::TYPE => {
141 Ok(Message::Error(Readable::read(buffer)?))
143 msgs::WarningMessage::TYPE => {
144 Ok(Message::Warning(Readable::read(buffer)?))
146 msgs::Ping::TYPE => {
147 Ok(Message::Ping(Readable::read(buffer)?))
149 msgs::Pong::TYPE => {
150 Ok(Message::Pong(Readable::read(buffer)?))
152 msgs::OpenChannel::TYPE => {
153 Ok(Message::OpenChannel(Readable::read(buffer)?))
155 msgs::AcceptChannel::TYPE => {
156 Ok(Message::AcceptChannel(Readable::read(buffer)?))
158 msgs::FundingCreated::TYPE => {
159 Ok(Message::FundingCreated(Readable::read(buffer)?))
161 msgs::FundingSigned::TYPE => {
162 Ok(Message::FundingSigned(Readable::read(buffer)?))
164 msgs::FundingLocked::TYPE => {
165 Ok(Message::FundingLocked(Readable::read(buffer)?))
167 msgs::Shutdown::TYPE => {
168 Ok(Message::Shutdown(Readable::read(buffer)?))
170 msgs::ClosingSigned::TYPE => {
171 Ok(Message::ClosingSigned(Readable::read(buffer)?))
173 msgs::UpdateAddHTLC::TYPE => {
174 Ok(Message::UpdateAddHTLC(Readable::read(buffer)?))
176 msgs::UpdateFulfillHTLC::TYPE => {
177 Ok(Message::UpdateFulfillHTLC(Readable::read(buffer)?))
179 msgs::UpdateFailHTLC::TYPE => {
180 Ok(Message::UpdateFailHTLC(Readable::read(buffer)?))
182 msgs::UpdateFailMalformedHTLC::TYPE => {
183 Ok(Message::UpdateFailMalformedHTLC(Readable::read(buffer)?))
185 msgs::CommitmentSigned::TYPE => {
186 Ok(Message::CommitmentSigned(Readable::read(buffer)?))
188 msgs::RevokeAndACK::TYPE => {
189 Ok(Message::RevokeAndACK(Readable::read(buffer)?))
191 msgs::UpdateFee::TYPE => {
192 Ok(Message::UpdateFee(Readable::read(buffer)?))
194 msgs::ChannelReestablish::TYPE => {
195 Ok(Message::ChannelReestablish(Readable::read(buffer)?))
197 msgs::AnnouncementSignatures::TYPE => {
198 Ok(Message::AnnouncementSignatures(Readable::read(buffer)?))
200 msgs::ChannelAnnouncement::TYPE => {
201 Ok(Message::ChannelAnnouncement(Readable::read(buffer)?))
203 msgs::NodeAnnouncement::TYPE => {
204 Ok(Message::NodeAnnouncement(Readable::read(buffer)?))
206 msgs::ChannelUpdate::TYPE => {
207 Ok(Message::ChannelUpdate(Readable::read(buffer)?))
209 msgs::QueryShortChannelIds::TYPE => {
210 Ok(Message::QueryShortChannelIds(Readable::read(buffer)?))
212 msgs::ReplyShortChannelIdsEnd::TYPE => {
213 Ok(Message::ReplyShortChannelIdsEnd(Readable::read(buffer)?))
215 msgs::QueryChannelRange::TYPE => {
216 Ok(Message::QueryChannelRange(Readable::read(buffer)?))
218 msgs::ReplyChannelRange::TYPE => {
219 Ok(Message::ReplyChannelRange(Readable::read(buffer)?))
221 msgs::GossipTimestampFilter::TYPE => {
222 Ok(Message::GossipTimestampFilter(Readable::read(buffer)?))
225 if let Some(custom) = custom_reader.read(message_type, buffer)? {
226 Ok(Message::Custom(custom))
228 Ok(Message::Unknown(message_type))
234 /// Writes a message to the data buffer encoded as a 2-byte big-endian type and a variable-length
239 /// Returns an I/O error if the write could not be completed.
240 pub(crate) fn write<M: Type + Writeable, W: Writer>(message: &M, buffer: &mut W) -> Result<(), io::Error> {
241 message.type_id().write(buffer)?;
242 message.write(buffer)
246 /// Defines a constant type identifier for reading messages from the wire.
248 /// The type identifying the message payload.
253 pub(crate) use self::encode::Encode;
255 /// Defines a type identifier for sending messages over the wire.
257 /// Messages implementing this trait specify a type and must be [`Writeable`].
258 pub trait Type: core::fmt::Debug + Writeable {
259 /// Returns the type identifying the message payload.
260 fn type_id(&self) -> u16;
263 impl<T: core::fmt::Debug + Writeable> Type for T where T: Encode {
264 fn type_id(&self) -> u16 {
269 impl Encode for msgs::Init {
270 const TYPE: u16 = 16;
273 impl Encode for msgs::ErrorMessage {
274 const TYPE: u16 = 17;
277 impl Encode for msgs::WarningMessage {
281 impl Encode for msgs::Ping {
282 const TYPE: u16 = 18;
285 impl Encode for msgs::Pong {
286 const TYPE: u16 = 19;
289 impl Encode for msgs::OpenChannel {
290 const TYPE: u16 = 32;
293 impl Encode for msgs::AcceptChannel {
294 const TYPE: u16 = 33;
297 impl Encode for msgs::FundingCreated {
298 const TYPE: u16 = 34;
301 impl Encode for msgs::FundingSigned {
302 const TYPE: u16 = 35;
305 impl Encode for msgs::FundingLocked {
306 const TYPE: u16 = 36;
309 impl Encode for msgs::Shutdown {
310 const TYPE: u16 = 38;
313 impl Encode for msgs::ClosingSigned {
314 const TYPE: u16 = 39;
317 impl Encode for msgs::UpdateAddHTLC {
318 const TYPE: u16 = 128;
321 impl Encode for msgs::UpdateFulfillHTLC {
322 const TYPE: u16 = 130;
325 impl Encode for msgs::UpdateFailHTLC {
326 const TYPE: u16 = 131;
329 impl Encode for msgs::UpdateFailMalformedHTLC {
330 const TYPE: u16 = 135;
333 impl Encode for msgs::CommitmentSigned {
334 const TYPE: u16 = 132;
337 impl Encode for msgs::RevokeAndACK {
338 const TYPE: u16 = 133;
341 impl Encode for msgs::UpdateFee {
342 const TYPE: u16 = 134;
345 impl Encode for msgs::ChannelReestablish {
346 const TYPE: u16 = 136;
349 impl Encode for msgs::AnnouncementSignatures {
350 const TYPE: u16 = 259;
353 impl Encode for msgs::ChannelAnnouncement {
354 const TYPE: u16 = 256;
357 impl Encode for msgs::NodeAnnouncement {
358 const TYPE: u16 = 257;
361 impl Encode for msgs::ChannelUpdate {
362 const TYPE: u16 = 258;
365 impl Encode for msgs::QueryShortChannelIds {
366 const TYPE: u16 = 261;
369 impl Encode for msgs::ReplyShortChannelIdsEnd {
370 const TYPE: u16 = 262;
373 impl Encode for msgs::QueryChannelRange {
374 const TYPE: u16 = 263;
377 impl Encode for msgs::ReplyChannelRange {
378 const TYPE: u16 = 264;
381 impl Encode for msgs::GossipTimestampFilter {
382 const TYPE: u16 = 265;
389 use core::convert::TryInto;
390 use ::ln::peer_handler::IgnoringMessageHandler;
392 // Big-endian wire encoding of Pong message (type = 19, byteslen = 2).
393 const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8];
396 fn read_empty_buffer() {
398 let mut reader = io::Cursor::new(buffer);
399 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
403 fn read_incomplete_type() {
404 let buffer = &ENCODED_PONG[..1];
405 let mut reader = io::Cursor::new(buffer);
406 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
410 fn read_empty_payload() {
411 let buffer = &ENCODED_PONG[..2];
412 let mut reader = io::Cursor::new(buffer);
413 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
417 fn read_invalid_message() {
418 let buffer = &ENCODED_PONG[..4];
419 let mut reader = io::Cursor::new(buffer);
420 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
424 fn read_known_message() {
425 let buffer = &ENCODED_PONG[..];
426 let mut reader = io::Cursor::new(buffer);
427 let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
429 Message::Pong(_) => (),
430 _ => panic!("Expected pong message; found message type: {}", message.type_id()),
435 fn read_unknown_message() {
436 let buffer = &::core::u16::MAX.to_be_bytes();
437 let mut reader = io::Cursor::new(buffer);
438 let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
440 Message::Unknown(::core::u16::MAX) => (),
441 _ => panic!("Expected message type {}; found: {}", ::core::u16::MAX, message.type_id()),
446 fn write_message_with_type() {
447 let message = msgs::Pong { byteslen: 2u16 };
448 let mut buffer = Vec::new();
449 assert!(write(&message, &mut buffer).is_ok());
451 let type_length = ::core::mem::size_of::<u16>();
452 let (type_bytes, payload_bytes) = buffer.split_at(type_length);
453 assert_eq!(u16::from_be_bytes(type_bytes.try_into().unwrap()), msgs::Pong::TYPE);
454 assert_eq!(payload_bytes, &ENCODED_PONG[type_length..]);
458 fn read_message_encoded_with_write() {
459 let message = msgs::Pong { byteslen: 2u16 };
460 let mut buffer = Vec::new();
461 assert!(write(&message, &mut buffer).is_ok());
463 let mut reader = io::Cursor::new(buffer);
464 let decoded_message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
465 match decoded_message {
466 Message::Pong(msgs::Pong { byteslen: 2u16 }) => (),
467 Message::Pong(msgs::Pong { byteslen }) => {
468 panic!("Expected byteslen {}; found: {}", message.byteslen, byteslen);
470 _ => panic!("Expected pong message; found message type: {}", decoded_message.type_id()),
475 fn type_id(&self) -> u16 { unreachable!(); }
479 fn is_even_message_type() {
480 let message = Message::<()>::Unknown(42);
481 assert!(message.is_even());
485 fn is_odd_message_type() {
486 let message = Message::<()>::Unknown(43);
487 assert!(!message.is_even());
491 fn read_lnd_init_msg() {
492 // Taken from lnd v0.9.0-beta.
493 let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 162, 161];
494 check_init_msg(buffer, false);
498 fn read_clightning_init_msg() {
499 // Taken from c-lightning v0.8.0.
500 let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 170, 162, 1, 32, 6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15];
501 check_init_msg(buffer, true);
504 fn check_init_msg(buffer: Vec<u8>, expect_unknown: bool) {
505 let mut reader = io::Cursor::new(buffer);
506 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
508 Message::Init(msgs::Init { features }) => {
509 assert!(features.supports_variable_length_onion());
510 assert!(features.supports_upfront_shutdown_script());
511 assert!(features.supports_gossip_queries());
512 assert_eq!(expect_unknown, features.supports_unknown_bits());
513 assert!(!features.requires_unknown_bits());
514 assert!(!features.initial_routing_sync());
516 _ => panic!("Expected init message, found message type: {}", decoded_msg.type_id())
521 fn read_lnd_node_announcement() {
522 // Taken from lnd v0.9.0-beta.
523 let buffer = vec![1, 1, 91, 164, 146, 213, 213, 165, 21, 227, 102, 33, 105, 179, 214, 21, 221, 175, 228, 93, 57, 177, 191, 127, 107, 229, 31, 50, 21, 81, 179, 71, 39, 18, 35, 2, 89, 224, 110, 123, 66, 39, 148, 246, 177, 85, 12, 19, 70, 226, 173, 132, 156, 26, 122, 146, 71, 213, 247, 48, 93, 190, 185, 177, 12, 172, 0, 3, 2, 162, 161, 94, 103, 195, 37, 2, 37, 242, 97, 140, 2, 111, 69, 85, 39, 118, 30, 221, 99, 254, 120, 49, 103, 22, 170, 227, 111, 172, 164, 160, 49, 68, 138, 116, 16, 22, 206, 107, 51, 153, 255, 97, 108, 105, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 172, 21, 0, 2, 38, 7];
524 let mut reader = io::Cursor::new(buffer);
525 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
527 Message::NodeAnnouncement(msgs::NodeAnnouncement { contents: msgs::UnsignedNodeAnnouncement { features, ..}, ..}) => {
528 assert!(features.supports_variable_length_onion());
529 assert!(features.supports_upfront_shutdown_script());
530 assert!(features.supports_gossip_queries());
531 assert!(!features.requires_unknown_bits());
533 _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())
538 fn read_lnd_chan_announcement() {
539 // Taken from lnd v0.9.0-beta.
540 let buffer = vec![1, 0, 82, 238, 153, 33, 128, 87, 215, 2, 28, 241, 140, 250, 98, 255, 56, 5, 79, 240, 214, 231, 172, 35, 240, 171, 44, 9, 78, 91, 8, 193, 102, 5, 17, 178, 142, 106, 180, 183, 46, 38, 217, 212, 25, 236, 69, 47, 92, 217, 181, 221, 161, 205, 121, 201, 99, 38, 158, 216, 186, 193, 230, 86, 222, 6, 206, 67, 22, 255, 137, 212, 141, 161, 62, 134, 76, 48, 241, 54, 50, 167, 187, 247, 73, 27, 74, 1, 129, 185, 197, 153, 38, 90, 255, 138, 39, 161, 102, 172, 213, 74, 107, 88, 150, 90, 0, 49, 104, 7, 182, 184, 194, 219, 181, 172, 8, 245, 65, 226, 19, 228, 101, 145, 25, 159, 52, 31, 58, 93, 53, 59, 218, 91, 37, 84, 103, 17, 74, 133, 33, 35, 2, 203, 101, 73, 19, 94, 175, 122, 46, 224, 47, 168, 128, 128, 25, 26, 25, 214, 52, 247, 43, 241, 117, 52, 206, 94, 135, 156, 52, 164, 143, 234, 58, 185, 50, 185, 140, 198, 174, 71, 65, 18, 105, 70, 131, 172, 137, 0, 164, 51, 215, 143, 117, 119, 217, 241, 197, 177, 227, 227, 170, 199, 114, 7, 218, 12, 107, 30, 191, 236, 203, 21, 61, 242, 48, 192, 90, 233, 200, 199, 111, 162, 68, 234, 54, 219, 1, 233, 66, 5, 82, 74, 84, 211, 95, 199, 245, 202, 89, 223, 102, 124, 62, 166, 253, 253, 90, 180, 118, 21, 61, 110, 37, 5, 96, 167, 0, 0, 6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15, 0, 2, 65, 0, 0, 1, 0, 0, 2, 37, 242, 97, 140, 2, 111, 69, 85, 39, 118, 30, 221, 99, 254, 120, 49, 103, 22, 170, 227, 111, 172, 164, 160, 49, 68, 138, 116, 16, 22, 206, 107, 3, 54, 61, 144, 88, 171, 247, 136, 208, 99, 9, 135, 37, 201, 178, 253, 136, 0, 185, 235, 68, 160, 106, 110, 12, 46, 21, 125, 204, 18, 75, 234, 16, 3, 42, 171, 28, 52, 224, 11, 30, 30, 253, 156, 148, 175, 203, 121, 250, 111, 122, 195, 84, 122, 77, 183, 56, 135, 101, 88, 41, 60, 191, 99, 232, 85, 2, 36, 17, 156, 11, 8, 12, 189, 177, 68, 88, 28, 15, 207, 21, 179, 151, 56, 226, 158, 148, 3, 120, 113, 177, 243, 184, 17, 173, 37, 46, 222, 16];
541 let mut reader = io::Cursor::new(buffer);
542 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
544 Message::ChannelAnnouncement(msgs::ChannelAnnouncement { contents: msgs::UnsignedChannelAnnouncement { features, ..}, ..}) => {
545 assert!(!features.requires_unknown_bits());
547 _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())
551 #[derive(Eq, PartialEq, Debug)]
552 struct TestCustomMessage {}
554 const CUSTOM_MESSAGE_TYPE : u16 = 9000;
556 impl Type for TestCustomMessage {
557 fn type_id(&self) -> u16 {
562 impl Writeable for TestCustomMessage {
563 fn write<W: Writer>(&self, _: &mut W) -> Result<(), io::Error> {
568 struct TestCustomMessageReader {}
570 impl CustomMessageReader for TestCustomMessageReader {
571 type CustomMessage = TestCustomMessage;
572 fn read<R: io::Read>(
576 ) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
577 if message_type == CUSTOM_MESSAGE_TYPE {
578 return Ok(Some(TestCustomMessage{}));
586 fn read_custom_message() {
587 let buffer = vec![35, 40];
588 let mut reader = io::Cursor::new(buffer);
589 let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap();
591 Message::Custom(custom) => {
592 assert_eq!(custom.type_id(), CUSTOM_MESSAGE_TYPE);
593 assert_eq!(custom, TestCustomMessage {});
595 _ => panic!("Expected custom message, found message type: {}", decoded_msg.type_id()),
600 fn read_with_custom_reader_unknown_message_type() {
601 let buffer = vec![35, 42];
602 let mut reader = io::Cursor::new(buffer);
603 let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap();
605 Message::Unknown(_) => {},
606 _ => panic!("Expected unknown message, found message type: {}", decoded_msg.type_id()),
611 fn custom_reader_unknown_message_type() {
612 let buffer = Vec::new();
613 let mut reader = io::Cursor::new(buffer);
614 let res = TestCustomMessageReader{}.read(CUSTOM_MESSAGE_TYPE + 1, &mut reader).unwrap();
615 assert!(res.is_none());