1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) 2017, All Contributors (see CONTRIBUTORS file)
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#![feature(fn_traits)]

extern crate byteorder;
extern crate pumpkinscript;

mod packet;
pub use packet::{PacketReader, PacketWriter};

use std::io;
use std::io::{Write, Read};
pub use pumpkinscript::{Encodable, Receivable};

pub trait Send {
    fn send<E : Encodable>(&mut self, encodable: E) -> io::Result<()>;
}

impl<T : Write> Send for PacketWriter<T> {
    fn send<E: Encodable>(&mut self, encodable: E) -> io::Result<()> {
        self.write(&encodable.encode()).map(|_| ())
    }
}

pub trait MessageHandler {
    fn handle_message(&mut self, data: &[u8]);
}

impl<T : FnMut(&[u8])> MessageHandler for T {
    fn handle_message(&mut self, data: &[u8]) {
        self.call_mut((data,))
    }
}

// This trait provides data receiving primitives
pub trait Receive {
    // Receive and handle one message. Returns after one message has
    // been handled
    fn receive<H : MessageHandler>(&mut self, handler: H) -> io::Result<()>;
}

impl<T : Read> Receive for T {
    fn receive<H: MessageHandler>(&mut self, mut handler: H) -> io::Result<()> {
        let mut reader = PacketReader::new(self);
        match reader.read() {
            Ok(data) => {
                handler.handle_message(&data);
                Ok(())
            },
            Err(err) => Err(err),
        }
    }
}