2020-02-04 15:42:04 +00:00
|
|
|
use crate::{
|
2020-04-08 14:26:42 +00:00
|
|
|
protocols::Protocols,
|
2020-02-21 13:08:34 +00:00
|
|
|
types::{
|
2020-04-08 14:26:42 +00:00
|
|
|
Cid, Frame, Pid, Sid, STREAM_ID_OFFSET1, STREAM_ID_OFFSET2, VELOREN_MAGIC_NUMBER,
|
2020-02-21 13:08:34 +00:00
|
|
|
VELOREN_NETWORK_VERSION,
|
2020-02-20 16:04:58 +00:00
|
|
|
},
|
2020-02-04 15:42:04 +00:00
|
|
|
};
|
2020-04-08 14:26:42 +00:00
|
|
|
use async_std::sync::RwLock;
|
|
|
|
use futures::{
|
|
|
|
channel::{mpsc, oneshot},
|
|
|
|
sink::SinkExt,
|
|
|
|
stream::StreamExt,
|
|
|
|
};
|
2020-02-04 15:42:04 +00:00
|
|
|
use tracing::*;
|
2020-03-22 13:47:21 +00:00
|
|
|
//use futures::prelude::*;
|
2020-02-04 15:42:04 +00:00
|
|
|
|
2020-02-20 16:04:58 +00:00
|
|
|
pub(crate) struct Channel {
|
2020-03-22 13:47:21 +00:00
|
|
|
cid: Cid,
|
|
|
|
local_pid: Pid,
|
|
|
|
remote_pid: RwLock<Option<Pid>>,
|
|
|
|
send_state: RwLock<ChannelState>,
|
|
|
|
recv_state: RwLock<ChannelState>,
|
2020-02-04 15:42:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum ChannelState {
|
|
|
|
None,
|
|
|
|
Handshake,
|
|
|
|
Pid,
|
|
|
|
Shutdown,
|
|
|
|
}
|
2020-02-04 15:42:04 +00:00
|
|
|
|
2020-02-20 16:04:58 +00:00
|
|
|
impl Channel {
|
2020-04-08 14:26:42 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2020-02-04 15:42:04 +00:00
|
|
|
const WRONG_NUMBER: &'static [u8] = "Handshake does not contain the magic number requiered by \
|
|
|
|
veloren server.\nWe are not sure if you are a valid \
|
|
|
|
veloren client.\nClosing the connection"
|
|
|
|
.as_bytes();
|
2020-04-08 14:26:42 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2020-03-22 13:47:21 +00:00
|
|
|
const WRONG_VERSION: &'static str = "Handshake does contain a correct magic number, but \
|
2020-02-04 15:42:04 +00:00
|
|
|
invalid version.\nWe don't know how to communicate with \
|
2020-03-22 13:47:21 +00:00
|
|
|
you.\nClosing the connection";
|
2020-02-04 15:42:04 +00:00
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
pub fn new(cid: u64, local_pid: Pid) -> Self {
|
2020-02-10 17:25:47 +00:00
|
|
|
Self {
|
2020-03-22 13:47:21 +00:00
|
|
|
cid,
|
2020-02-04 15:42:04 +00:00
|
|
|
local_pid,
|
2020-03-22 13:47:21 +00:00
|
|
|
remote_pid: RwLock::new(None),
|
|
|
|
send_state: RwLock::new(ChannelState::None),
|
|
|
|
recv_state: RwLock::new(ChannelState::None),
|
2020-02-04 15:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
/// (prot|part)_(in|out)_(sender|receiver)
|
|
|
|
/// prot: TO/FROM PROTOCOL = TCP
|
|
|
|
/// part: TO/FROM PARTICIPANT
|
|
|
|
/// in: FROM
|
|
|
|
/// out: TO
|
|
|
|
/// sender: mpsc::Sender
|
|
|
|
/// receiver: mpsc::Receiver
|
|
|
|
pub async fn run(
|
|
|
|
self,
|
2020-04-08 14:26:42 +00:00
|
|
|
protocol: Protocols,
|
2020-03-22 13:47:21 +00:00
|
|
|
part_in_receiver: mpsc::UnboundedReceiver<Frame>,
|
|
|
|
part_out_sender: mpsc::UnboundedSender<(Cid, Frame)>,
|
2020-04-08 14:26:42 +00:00
|
|
|
configured_sender: mpsc::UnboundedSender<(Cid, Pid, Sid, oneshot::Sender<()>)>,
|
2020-03-10 00:07:36 +00:00
|
|
|
) {
|
2020-03-22 13:47:21 +00:00
|
|
|
let (prot_in_sender, prot_in_receiver) = mpsc::unbounded::<Frame>();
|
|
|
|
let (prot_out_sender, prot_out_receiver) = mpsc::unbounded::<Frame>();
|
2020-02-10 17:25:47 +00:00
|
|
|
|
2020-04-08 14:26:42 +00:00
|
|
|
let handler_future = self.frame_handler(
|
|
|
|
prot_in_receiver,
|
|
|
|
prot_out_sender,
|
|
|
|
part_out_sender,
|
|
|
|
configured_sender,
|
2020-03-22 13:47:21 +00:00
|
|
|
);
|
2020-04-08 14:26:42 +00:00
|
|
|
match protocol {
|
|
|
|
Protocols::Tcp(tcp) => {
|
|
|
|
futures::join!(
|
|
|
|
tcp.read(prot_in_sender),
|
|
|
|
tcp.write(prot_out_receiver, part_in_receiver),
|
|
|
|
handler_future,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Protocols::Udp(udp) => {
|
|
|
|
futures::join!(
|
|
|
|
udp.read(prot_in_sender),
|
|
|
|
udp.write(prot_out_receiver, part_in_receiver),
|
|
|
|
handler_future,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
}
|
2020-03-22 13:47:21 +00:00
|
|
|
|
|
|
|
//return part_out_receiver;
|
2020-02-10 17:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
pub async fn frame_handler(
|
|
|
|
&self,
|
|
|
|
mut frames: mpsc::UnboundedReceiver<Frame>,
|
|
|
|
mut frame_sender: mpsc::UnboundedSender<Frame>,
|
|
|
|
mut external_frame_sender: mpsc::UnboundedSender<(Cid, Frame)>,
|
2020-04-08 14:26:42 +00:00
|
|
|
mut configured_sender: mpsc::UnboundedSender<(Cid, Pid, Sid, oneshot::Sender<()>)>,
|
2020-03-10 00:07:36 +00:00
|
|
|
) {
|
2020-03-22 13:47:21 +00:00
|
|
|
const ERR_S: &str = "Got A Raw Message, these are usually Debug Messages indicating that \
|
|
|
|
something went wrong on network layer and connection will be closed";
|
|
|
|
while let Some(frame) = frames.next().await {
|
|
|
|
match frame {
|
|
|
|
Frame::Handshake {
|
|
|
|
magic_number,
|
|
|
|
version,
|
|
|
|
} => {
|
2020-04-08 14:26:42 +00:00
|
|
|
trace!(?magic_number, ?version, "recv handshake");
|
2020-03-22 13:47:21 +00:00
|
|
|
if self
|
|
|
|
.verify_handshake(magic_number, version, &mut frame_sender)
|
|
|
|
.await
|
|
|
|
.is_ok()
|
|
|
|
{
|
|
|
|
debug!("handshake completed");
|
|
|
|
*self.recv_state.write().await = ChannelState::Handshake;
|
|
|
|
if *self.send_state.read().await == ChannelState::Handshake {
|
|
|
|
self.send_pid(&mut frame_sender).await;
|
|
|
|
} else {
|
|
|
|
self.send_handshake(&mut frame_sender).await;
|
2020-03-04 15:52:30 +00:00
|
|
|
}
|
2020-03-22 13:47:21 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
Frame::ParticipantId { pid } => {
|
|
|
|
if self.remote_pid.read().await.is_some() {
|
|
|
|
error!(?pid, "invalid message, cant change participantId");
|
2020-03-10 00:07:36 +00:00
|
|
|
return;
|
2020-02-20 16:04:58 +00:00
|
|
|
}
|
2020-03-22 13:47:21 +00:00
|
|
|
*self.remote_pid.write().await = Some(pid);
|
|
|
|
*self.recv_state.write().await = ChannelState::Pid;
|
|
|
|
debug!(?pid, "Participant send their ID");
|
|
|
|
let stream_id_offset = if *self.send_state.read().await != ChannelState::Pid {
|
|
|
|
self.send_pid(&mut frame_sender).await;
|
|
|
|
STREAM_ID_OFFSET2
|
|
|
|
} else {
|
|
|
|
STREAM_ID_OFFSET1
|
2020-02-20 16:04:58 +00:00
|
|
|
};
|
2020-03-22 13:47:21 +00:00
|
|
|
info!(?pid, "this channel is now configured!");
|
2020-04-08 14:26:42 +00:00
|
|
|
let (sender, receiver) = oneshot::channel();
|
2020-03-22 13:47:21 +00:00
|
|
|
configured_sender
|
2020-04-08 14:26:42 +00:00
|
|
|
.send((self.cid, pid, stream_id_offset, sender))
|
2020-03-22 13:47:21 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-04-08 14:26:42 +00:00
|
|
|
receiver.await.unwrap();
|
|
|
|
//TODO: this is sync anyway, because we need to wait. so find a better way than
|
|
|
|
// there channels like direct method call... otherwise a
|
|
|
|
// frame might jump in before its officially configured yet
|
|
|
|
debug!(
|
|
|
|
"STOP, if you read this, fix this error. make this a function isntead a \
|
|
|
|
channel here"
|
|
|
|
);
|
2020-03-22 13:47:21 +00:00
|
|
|
},
|
|
|
|
Frame::Shutdown => {
|
|
|
|
info!("shutdown signal received");
|
|
|
|
*self.recv_state.write().await = ChannelState::Shutdown;
|
|
|
|
},
|
|
|
|
/* Sending RAW is only used for debug purposes in case someone write a
|
|
|
|
* new API against veloren Server! */
|
|
|
|
Frame::Raw(bytes) => match std::str::from_utf8(bytes.as_slice()) {
|
|
|
|
Ok(string) => error!(?string, ERR_S),
|
|
|
|
_ => error!(?bytes, ERR_S),
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
trace!("forward frame");
|
|
|
|
external_frame_sender.send((self.cid, frame)).await.unwrap();
|
|
|
|
},
|
|
|
|
}
|
2020-02-04 15:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
async fn verify_handshake(
|
|
|
|
&self,
|
|
|
|
magic_number: String,
|
|
|
|
version: [u32; 3],
|
2020-04-08 14:26:42 +00:00
|
|
|
#[cfg(debug_assertions)] frame_sender: &mut mpsc::UnboundedSender<Frame>,
|
|
|
|
#[cfg(not(debug_assertions))] _: &mut mpsc::UnboundedSender<Frame>,
|
2020-03-22 13:47:21 +00:00
|
|
|
) -> Result<(), ()> {
|
|
|
|
if magic_number != VELOREN_MAGIC_NUMBER {
|
|
|
|
error!(?magic_number, "connection with invalid magic_number");
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
debug!("sending client instructions before killing");
|
|
|
|
frame_sender
|
|
|
|
.send(Frame::Raw(Self::WRONG_NUMBER.to_vec()))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
frame_sender.send(Frame::Shutdown).await.unwrap();
|
|
|
|
*self.send_state.write().await = ChannelState::Shutdown;
|
2020-03-10 00:07:36 +00:00
|
|
|
}
|
2020-03-22 13:47:21 +00:00
|
|
|
return Err(());
|
2020-02-10 17:25:47 +00:00
|
|
|
}
|
2020-03-22 13:47:21 +00:00
|
|
|
if version != VELOREN_NETWORK_VERSION {
|
|
|
|
error!(?version, "connection with wrong network version");
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
debug!("sending client instructions before killing");
|
|
|
|
frame_sender
|
|
|
|
.send(Frame::Raw(
|
|
|
|
format!(
|
|
|
|
"{} Our Version: {:?}\nYour Version: {:?}\nClosing the connection",
|
|
|
|
Self::WRONG_VERSION,
|
|
|
|
VELOREN_NETWORK_VERSION,
|
|
|
|
version,
|
|
|
|
)
|
|
|
|
.as_bytes()
|
|
|
|
.to_vec(),
|
|
|
|
))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
frame_sender.send(Frame::Shutdown {}).await.unwrap();
|
|
|
|
*self.send_state.write().await = ChannelState::Shutdown;
|
|
|
|
}
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
Ok(())
|
2020-02-10 17:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
pub(crate) async fn send_handshake(&self, part_in_sender: &mut mpsc::UnboundedSender<Frame>) {
|
|
|
|
part_in_sender
|
|
|
|
.send(Frame::Handshake {
|
|
|
|
magic_number: VELOREN_MAGIC_NUMBER.to_string(),
|
|
|
|
version: VELOREN_NETWORK_VERSION,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
*self.send_state.write().await = ChannelState::Handshake;
|
2020-02-10 17:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
pub(crate) async fn send_pid(&self, part_in_sender: &mut mpsc::UnboundedSender<Frame>) {
|
|
|
|
part_in_sender
|
|
|
|
.send(Frame::ParticipantId {
|
|
|
|
pid: self.local_pid,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
*self.send_state.write().await = ChannelState::Pid;
|
2020-02-10 17:25:47 +00:00
|
|
|
}
|
2020-03-22 13:47:21 +00:00
|
|
|
/*
|
|
|
|
pub async fn run(&mut self) {
|
|
|
|
//let (incomming_sender, incomming_receiver) = mpsc::unbounded();
|
|
|
|
futures::join!(self.listen_manager(), self.send_outgoing());
|
2020-02-10 17:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
pub async fn listen_manager(&self) {
|
|
|
|
let (mut listen_sender, mut listen_receiver) = mpsc::unbounded::<Address>();
|
2020-03-10 00:07:36 +00:00
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
while self.closed.load(Ordering::Relaxed) {
|
|
|
|
while let Some(address) = listen_receiver.next().await {
|
|
|
|
let (end_sender, end_receiver) = oneshot::channel::<()>();
|
|
|
|
task::spawn(channel_creator(address, end_receiver));
|
2020-02-20 16:04:58 +00:00
|
|
|
}
|
2020-02-10 17:25:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
pub async fn send_outgoing(&self) {
|
|
|
|
//let prios = prios::PrioManager;
|
|
|
|
while self.closed.load(Ordering::Relaxed) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}*/
|
2020-02-04 15:42:04 +00:00
|
|
|
}
|