Rename ServerState -> State.
There's no need to write `Server` every time because it's only used inside of the server code, and when the handshake service constructs a Server.
This commit is contained in:
parent
d1b3e8fe6b
commit
9ff0fd90dc
|
@ -25,7 +25,7 @@ use crate::{
|
||||||
BoxedStdError, Config,
|
BoxedStdError, Config,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{error::ErrorSlot, server::ServerState, HandshakeError, Client, Server};
|
use super::{error::ErrorSlot, HandshakeError, Client, Server};
|
||||||
|
|
||||||
/// A [`Service`] that handshakes with a remote peer and constructs a
|
/// A [`Service`] that handshakes with a remote peer and constructs a
|
||||||
/// client/server pair.
|
/// client/server pair.
|
||||||
|
@ -192,8 +192,9 @@ where
|
||||||
|
|
||||||
let (peer_tx, peer_rx) = stream.split();
|
let (peer_tx, peer_rx) = stream.split();
|
||||||
|
|
||||||
|
use super::server;
|
||||||
let server = Server {
|
let server = Server {
|
||||||
state: ServerState::AwaitingRequest,
|
state: server::State::AwaitingRequest,
|
||||||
svc: internal_service,
|
svc: internal_service,
|
||||||
client_rx: server_rx,
|
client_rx: server_rx,
|
||||||
error_slot: slot,
|
error_slot: slot,
|
||||||
|
|
|
@ -24,7 +24,7 @@ use crate::{
|
||||||
|
|
||||||
use super::{client::ClientRequest, error::ErrorSlot, PeerError, SharedPeerError};
|
use super::{client::ClientRequest, error::ErrorSlot, PeerError, SharedPeerError};
|
||||||
|
|
||||||
pub(super) enum ServerState {
|
pub(super) enum State {
|
||||||
/// Awaiting a client request or a peer message.
|
/// Awaiting a client request or a peer message.
|
||||||
AwaitingRequest,
|
AwaitingRequest,
|
||||||
/// Awaiting a peer message we can interpret as a client request.
|
/// Awaiting a peer message we can interpret as a client request.
|
||||||
|
@ -35,9 +35,9 @@ pub(super) enum ServerState {
|
||||||
|
|
||||||
/// The "server" duplex half of a peer connection.
|
/// The "server" duplex half of a peer connection.
|
||||||
pub struct Server<S, Tx> {
|
pub struct Server<S, Tx> {
|
||||||
pub(super) state: ServerState,
|
pub(super) state: State,
|
||||||
/// A timeout for a client request. This is stored separately from
|
/// A timeout for a client request. This is stored separately from
|
||||||
/// ServerState so that we can move the future out of it independently of
|
/// State so that we can move the future out of it independently of
|
||||||
/// other state handling.
|
/// other state handling.
|
||||||
pub(super) request_timer: Option<Delay>,
|
pub(super) request_timer: Option<Delay>,
|
||||||
pub(super) svc: S,
|
pub(super) svc: S,
|
||||||
|
@ -80,7 +80,7 @@ where
|
||||||
// check whether it can be interpreted as a response to the pending request.
|
// check whether it can be interpreted as a response to the pending request.
|
||||||
loop {
|
loop {
|
||||||
match self.state {
|
match self.state {
|
||||||
ServerState::AwaitingRequest => {
|
State::AwaitingRequest => {
|
||||||
trace!("awaiting client request or peer message");
|
trace!("awaiting client request or peer message");
|
||||||
match future::select(peer_rx.next(), self.client_rx.next()).await {
|
match future::select(peer_rx.next(), self.client_rx.next()).await {
|
||||||
Either::Left((None, _)) => {
|
Either::Left((None, _)) => {
|
||||||
|
@ -100,7 +100,7 @@ where
|
||||||
}
|
}
|
||||||
// We're awaiting a response to a client request,
|
// We're awaiting a response to a client request,
|
||||||
// so wait on either a peer message, or on a request timeout.
|
// so wait on either a peer message, or on a request timeout.
|
||||||
ServerState::AwaitingResponse { .. } => {
|
State::AwaitingResponse { .. } => {
|
||||||
trace!("awaiting response to client request");
|
trace!("awaiting response to client request");
|
||||||
let timer_ref = self
|
let timer_ref = self
|
||||||
.request_timer
|
.request_timer
|
||||||
|
@ -121,14 +121,14 @@ where
|
||||||
trace!("client request timed out");
|
trace!("client request timed out");
|
||||||
// Re-matching lets us take ownership of tx
|
// Re-matching lets us take ownership of tx
|
||||||
self.state = match self.state {
|
self.state = match self.state {
|
||||||
ServerState::AwaitingResponse(Request::Ping(_), _) => {
|
State::AwaitingResponse(Request::Ping(_), _) => {
|
||||||
self.fail_with(PeerError::ClientRequestTimeout);
|
self.fail_with(PeerError::ClientRequestTimeout);
|
||||||
ServerState::Failed
|
State::Failed
|
||||||
}
|
}
|
||||||
ServerState::AwaitingResponse(_, tx) => {
|
State::AwaitingResponse(_, tx) => {
|
||||||
let e = PeerError::ClientRequestTimeout;
|
let e = PeerError::ClientRequestTimeout;
|
||||||
let _ = tx.send(Err(Arc::new(e).into()));
|
let _ = tx.send(Err(Arc::new(e).into()));
|
||||||
ServerState::AwaitingRequest
|
State::AwaitingRequest
|
||||||
}
|
}
|
||||||
_ => panic!("unreachable"),
|
_ => panic!("unreachable"),
|
||||||
};
|
};
|
||||||
|
@ -137,7 +137,7 @@ where
|
||||||
}
|
}
|
||||||
// We've failed, but we need to flush all pending client
|
// We've failed, but we need to flush all pending client
|
||||||
// requests before we can return and complete the future.
|
// requests before we can return and complete the future.
|
||||||
ServerState::Failed => {
|
State::Failed => {
|
||||||
match self.client_rx.next().await {
|
match self.client_rx.next().await {
|
||||||
Some(ClientRequest(_, tx)) => {
|
Some(ClientRequest(_, tx)) => {
|
||||||
let e = self
|
let e = self
|
||||||
|
@ -172,13 +172,13 @@ where
|
||||||
// Drop the guard immediately to release the mutex.
|
// Drop the guard immediately to release the mutex.
|
||||||
std::mem::drop(guard);
|
std::mem::drop(guard);
|
||||||
|
|
||||||
// We want to close the client channel and set ServerState::Failed so
|
// We want to close the client channel and set State::Failed so
|
||||||
// that we can flush any pending client requests. However, we may have
|
// that we can flush any pending client requests. However, we may have
|
||||||
// an outstanding client request in ServerState::AwaitingResponse, so
|
// an outstanding client request in State::AwaitingResponse, so
|
||||||
// we need to deal with it first if it exists.
|
// we need to deal with it first if it exists.
|
||||||
self.client_rx.close();
|
self.client_rx.close();
|
||||||
let old_state = std::mem::replace(&mut self.state, ServerState::Failed);
|
let old_state = std::mem::replace(&mut self.state, State::Failed);
|
||||||
if let ServerState::AwaitingResponse(_, tx) = old_state {
|
if let State::AwaitingResponse(_, tx) = old_state {
|
||||||
// We know the slot has Some(e) because we just set it above,
|
// We know the slot has Some(e) because we just set it above,
|
||||||
// and the error slot is never unset.
|
// and the error slot is never unset.
|
||||||
let e = self.error_slot.try_get_error().unwrap();
|
let e = self.error_slot.try_get_error().unwrap();
|
||||||
|
@ -191,7 +191,7 @@ where
|
||||||
async fn handle_client_request(&mut self, msg: ClientRequest) {
|
async fn handle_client_request(&mut self, msg: ClientRequest) {
|
||||||
trace!(?msg);
|
trace!(?msg);
|
||||||
use Request::*;
|
use Request::*;
|
||||||
use ServerState::*;
|
use State::*;
|
||||||
let ClientRequest(req, tx) = msg;
|
let ClientRequest(req, tx) = msg;
|
||||||
|
|
||||||
// Inner match returns Result with the new state or an error.
|
// Inner match returns Result with the new state or an error.
|
||||||
|
@ -254,7 +254,7 @@ where
|
||||||
// This conversion is done by a sequence of (request, message) match arms,
|
// This conversion is done by a sequence of (request, message) match arms,
|
||||||
// each of which contains the conversion logic for that pair.
|
// each of which contains the conversion logic for that pair.
|
||||||
use Request::*;
|
use Request::*;
|
||||||
use ServerState::*;
|
use State::*;
|
||||||
let mut ignored_msg = None;
|
let mut ignored_msg = None;
|
||||||
// We want to be able to consume the state, but it's behind a mutable
|
// We want to be able to consume the state, but it's behind a mutable
|
||||||
// reference, so we can't move it out of self without swapping in a
|
// reference, so we can't move it out of self without swapping in a
|
||||||
|
|
Loading…
Reference in New Issue