2016-10-25 07:35:10 +03:00
|
|
|
// Copyright 2016 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::ops::DerefMut;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use mioco;
|
2016-10-30 18:24:19 +03:00
|
|
|
use mioco::sync::mpsc::{sync_channel, SyncSender};
|
2016-10-29 22:36:45 +03:00
|
|
|
use mioco::tcp::{TcpStream, Shutdown};
|
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
use core::ser;
|
2016-10-29 22:36:45 +03:00
|
|
|
use handshake::Handshake;
|
2016-10-26 08:06:13 +03:00
|
|
|
use msg::*;
|
|
|
|
use types::*;
|
2016-10-25 07:35:10 +03:00
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
pub struct ProtocolV1 {
|
|
|
|
conn: RefCell<TcpStream>,
|
2016-10-30 18:24:19 +03:00
|
|
|
//msg_send: Option<SyncSender<ser::Writeable>>,
|
|
|
|
stop_send: RefCell<Option<SyncSender<u8>>>,
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
impl Protocol for ProtocolV1 {
|
|
|
|
fn handle(&self, server: &NetAdapter) -> Option<ser::Error> {
|
2016-10-30 18:24:19 +03:00
|
|
|
// setup channels so we can switch between reads, writes and close
|
|
|
|
let (msg_send, msg_recv) = sync_channel(10);
|
|
|
|
let (stop_send, stop_recv) = sync_channel(1);
|
|
|
|
|
|
|
|
//self.msg_send = Some(msg_send);
|
|
|
|
let mut stop_mut = self.stop_send.borrow_mut();
|
|
|
|
*stop_mut = Some(stop_send);
|
2016-10-29 22:36:45 +03:00
|
|
|
|
|
|
|
let mut conn = self.conn.borrow_mut();
|
2016-10-25 07:35:10 +03:00
|
|
|
loop {
|
2016-10-29 22:36:45 +03:00
|
|
|
select!(
|
|
|
|
r:conn => {
|
|
|
|
let header = try_to_o!(ser::deserialize::<MsgHeader>(conn.deref_mut()));
|
|
|
|
if !header.acceptable() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
},
|
2016-10-30 18:24:19 +03:00
|
|
|
r:msg_recv => {
|
|
|
|
ser::serialize(conn.deref_mut(), msg_recv.recv().unwrap());
|
|
|
|
},
|
|
|
|
r:stop_recv => {
|
|
|
|
stop_recv.recv();
|
|
|
|
conn.shutdown(Shutdown::Both);
|
|
|
|
return None;;
|
2016-10-29 22:36:45 +03:00
|
|
|
}
|
|
|
|
);
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-30 18:24:19 +03:00
|
|
|
|
|
|
|
fn close(&self) {
|
|
|
|
let stop_send = self.stop_send.borrow();
|
|
|
|
stop_send.as_ref().unwrap().send(0);
|
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
impl ProtocolV1 {
|
|
|
|
pub fn new(conn: TcpStream) -> ProtocolV1 {
|
2016-10-30 18:24:19 +03:00
|
|
|
ProtocolV1 { conn: RefCell::new(conn), /* msg_send: None, */ stop_send: RefCell::new(None) }
|
2016-10-28 00:28:02 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|