|
|
@@ -9,8 +9,7 @@ use std::net::{SocketAddr, TcpStream};
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
-use crate::error;
|
|
|
-use crate::net::error::{NetError, NetResult};
|
|
|
+use crate::error::{Error, Result};
|
|
|
use crate::net::message_subscriber::{MessageSubscription, MessageSubsystem};
|
|
|
use crate::net::messages;
|
|
|
use crate::system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription};
|
|
|
@@ -24,7 +23,7 @@ pub struct Channel {
|
|
|
writer: Mutex<WriteHalf<Async<TcpStream>>>,
|
|
|
address: SocketAddr,
|
|
|
message_subsystem: MessageSubsystem,
|
|
|
- stop_subscriber: SubscriberPtr<NetError>,
|
|
|
+ stop_subscriber: SubscriberPtr<Error>,
|
|
|
receive_task: StoppableTaskPtr,
|
|
|
stopped: AtomicBool,
|
|
|
}
|
|
|
@@ -61,7 +60,7 @@ impl Channel {
|
|
|
self.clone().main_receive_loop(),
|
|
|
// Ignore stop handler
|
|
|
|result| self2.handle_stop(result),
|
|
|
- NetError::ServiceStopped,
|
|
|
+ Error::ServiceStopped,
|
|
|
executor,
|
|
|
);
|
|
|
debug!(target: "net", "Channel::start() [END, address={}]", self.address());
|
|
|
@@ -75,16 +74,16 @@ impl Channel {
|
|
|
assert_eq!(self.stopped.load(Ordering::Relaxed), false);
|
|
|
// Changes memory ordering to relaxed. We don't need strict thread locking here.
|
|
|
self.stopped.store(false, Ordering::Relaxed);
|
|
|
- self.stop_subscriber.notify(NetError::ChannelStopped).await;
|
|
|
+ self.stop_subscriber.notify(Error::ChannelStopped).await;
|
|
|
self.receive_task.stop().await;
|
|
|
self.message_subsystem
|
|
|
- .trigger_error(NetError::ChannelStopped)
|
|
|
+ .trigger_error(Error::ChannelStopped)
|
|
|
.await;
|
|
|
debug!(target: "net", "Channel::stop() [END, address={}]", self.address());
|
|
|
}
|
|
|
|
|
|
/// Creates a subscription to a stopped signal.
|
|
|
- pub async fn subscribe_stop(&self) -> Subscription<NetError> {
|
|
|
+ pub async fn subscribe_stop(&self) -> Subscription<Error> {
|
|
|
debug!(target: "net",
|
|
|
"Channel::subscribe_stop() [START, address={}]",
|
|
|
self.address()
|
|
|
@@ -102,14 +101,14 @@ impl Channel {
|
|
|
/// Sends a message across a channel. Calls function 'send_message' that
|
|
|
/// creates a new payload and sends it over the TCP connection as a
|
|
|
/// packet. Returns an error if something goes wrong.
|
|
|
- pub async fn send<M: messages::Message>(&self, message: M) -> NetResult<()> {
|
|
|
+ pub async fn send<M: messages::Message>(&self, message: M) -> Result<()> {
|
|
|
debug!(target: "net",
|
|
|
"Channel::send() [START, command={:?}, address={}]",
|
|
|
M::name(),
|
|
|
self.address()
|
|
|
);
|
|
|
if self.stopped.load(Ordering::Relaxed) {
|
|
|
- return Err(NetError::ChannelStopped);
|
|
|
+ return Err(Error::ChannelStopped);
|
|
|
}
|
|
|
|
|
|
// Catch failure and stop channel, return a net error
|
|
|
@@ -118,7 +117,7 @@ impl Channel {
|
|
|
Err(err) => {
|
|
|
error!("Channel send error for [{}]: {}", self.address(), err);
|
|
|
self.stop().await;
|
|
|
- Err(NetError::ChannelStopped)
|
|
|
+ Err(Error::ChannelStopped)
|
|
|
}
|
|
|
};
|
|
|
debug!(target: "net",
|
|
|
@@ -133,7 +132,7 @@ impl Channel {
|
|
|
/// it. Then creates a message packet- the base type of the network- and
|
|
|
/// copies the payload into it. Then we send the packet over the TCP
|
|
|
/// stream.
|
|
|
- async fn send_message<M: messages::Message>(&self, message: M) -> error::Result<()> {
|
|
|
+ async fn send_message<M: messages::Message>(&self, message: M) -> Result<()> {
|
|
|
let mut payload = Vec::new();
|
|
|
message.encode(&mut payload)?;
|
|
|
let packet = messages::Packet {
|
|
|
@@ -146,7 +145,7 @@ impl Channel {
|
|
|
}
|
|
|
|
|
|
/// Subscribe to a messages on the message subsystem.
|
|
|
- pub async fn subscribe_msg<M: messages::Message>(&self) -> NetResult<MessageSubscription<M>> {
|
|
|
+ pub async fn subscribe_msg<M: messages::Message>(&self) -> Result<MessageSubscription<M>> {
|
|
|
debug!(target: "net",
|
|
|
"Channel::subscribe_msg() [START, command={:?}, address={}]",
|
|
|
M::name(),
|
|
|
@@ -167,9 +166,9 @@ impl Channel {
|
|
|
}
|
|
|
|
|
|
/// End of file error. Triggered when unexpected end of file occurs.
|
|
|
- fn is_eof_error(err: &error::Error) -> bool {
|
|
|
+ fn is_eof_error(err: Error) -> bool {
|
|
|
match err {
|
|
|
- error::Error::Io(io_err) => io_err.clone() == std::io::ErrorKind::UnexpectedEof,
|
|
|
+ Error::Io(io_err) => io_err.clone() == std::io::ErrorKind::UnexpectedEof,
|
|
|
_ => false,
|
|
|
}
|
|
|
}
|
|
|
@@ -203,7 +202,7 @@ impl Channel {
|
|
|
|
|
|
/// Run the receive loop. Start receiving messages or handle network
|
|
|
/// failure.
|
|
|
- async fn main_receive_loop(self: Arc<Self>) -> NetResult<()> {
|
|
|
+ async fn main_receive_loop(self: Arc<Self>) -> Result<()> {
|
|
|
debug!(target: "net",
|
|
|
"Channel::receive_loop() [START, address={}]",
|
|
|
self.address()
|
|
|
@@ -215,7 +214,7 @@ impl Channel {
|
|
|
let packet = match messages::read_packet(reader).await {
|
|
|
Ok(packet) => packet,
|
|
|
Err(err) => {
|
|
|
- if Self::is_eof_error(&err) {
|
|
|
+ if Self::is_eof_error(err.clone()) {
|
|
|
info!("Channel {} disconnected", self.address());
|
|
|
} else {
|
|
|
error!("Read error on channel: {}", err);
|
|
|
@@ -225,7 +224,7 @@ impl Channel {
|
|
|
self.address()
|
|
|
);
|
|
|
self.stop().await;
|
|
|
- return Err(NetError::ChannelStopped);
|
|
|
+ return Err(Error::ChannelStopped);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -238,7 +237,7 @@ impl Channel {
|
|
|
|
|
|
/// Handle network errors. Panic if error passes silently, otherwise
|
|
|
/// broadcast the error.
|
|
|
- async fn handle_stop(self: Arc<Self>, result: NetResult<()>) {
|
|
|
+ async fn handle_stop(self: Arc<Self>, result: Result<()>) {
|
|
|
debug!(target: "net", "Channel::handle_stop() [START, address={}]", self.address());
|
|
|
match result {
|
|
|
Ok(()) => panic!("Channel task should never complete without error status"),
|