|
|
|
@ -15,7 +15,7 @@ use crate::core::{FrameReader, FrameWriter, Worker}; |
|
|
|
use crate::server::{LoginResponse, ServerRequest, ServerResponse};
|
|
|
|
|
|
|
|
/// A handler for a single client connected to the server.
|
|
|
|
pub trait ClientHandler: Send {
|
|
|
|
pub trait ClientHandler {
|
|
|
|
/// Runs this handler against the given incoming stream of requests.
|
|
|
|
fn run(
|
|
|
|
self,
|
|
|
|
@ -44,7 +44,7 @@ pub trait RequestHandler { |
|
|
|
|
|
|
|
/// Blanket implementation of `ClientHandler` for `RequestHandler` types.
|
|
|
|
/// Simply handles each incoming request in turn and sends responses back.
|
|
|
|
impl<H: RequestHandler + Send> ClientHandler for H {
|
|
|
|
impl<H: RequestHandler> ClientHandler for H {
|
|
|
|
fn run(
|
|
|
|
mut self,
|
|
|
|
mut request_rx: mpsc::Receiver<ServerRequest>,
|
|
|
|
@ -71,7 +71,7 @@ struct LoginHandler<H> { |
|
|
|
inner: H,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<H: ClientHandler + 'static> LoginHandler<H> {
|
|
|
|
impl<H: ClientHandler + Send + 'static> LoginHandler<H> {
|
|
|
|
fn ipv4_address(&self) -> Ipv4Addr {
|
|
|
|
match self.peer_address.ip() {
|
|
|
|
IpAddr::V4(ipv4_addr) => ipv4_addr,
|
|
|
|
@ -135,7 +135,7 @@ struct GracefulHandler<H> { |
|
|
|
shutdown_rx: watch::Receiver<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<H: ClientHandler + 'static> GracefulHandler<H> {
|
|
|
|
impl<H: ClientHandler + Send + 'static> GracefulHandler<H> {
|
|
|
|
async fn run(mut self) -> anyhow::Result<()> {
|
|
|
|
tokio::select!(
|
|
|
|
result = self.handler.run() => {
|
|
|
|
@ -163,7 +163,7 @@ struct SenderHandler<H> { |
|
|
|
result_tx: mpsc::Sender<anyhow::Result<()>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<H: ClientHandler + 'static> SenderHandler<H> {
|
|
|
|
impl<H: ClientHandler + Send + 'static> SenderHandler<H> {
|
|
|
|
async fn run(self) {
|
|
|
|
let result = self.handler.run().await;
|
|
|
|
let _ = self.result_tx.send(result).await;
|
|
|
|
@ -176,7 +176,11 @@ pub struct ServerBuilder<F> { |
|
|
|
factory: F,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: ClientHandlerFactory> ServerBuilder<F> {
|
|
|
|
impl<F> ServerBuilder<F>
|
|
|
|
where
|
|
|
|
F: ClientHandlerFactory,
|
|
|
|
<F as ClientHandlerFactory>::Handler: Send,
|
|
|
|
{
|
|
|
|
/// `handler` will be used to handle incoming connections.
|
|
|
|
pub fn new(factory: F) -> Self {
|
|
|
|
ServerBuilder { factory }
|
|
|
|
@ -258,7 +262,11 @@ impl ServerHandle { |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: ClientHandlerFactory> Server<F> {
|
|
|
|
impl<F> Server<F>
|
|
|
|
where
|
|
|
|
F: ClientHandlerFactory,
|
|
|
|
<F as ClientHandlerFactory>::Handler: Send,
|
|
|
|
{
|
|
|
|
/// Returns the address to which this server is bound.
|
|
|
|
/// This is always localhost and a random port chosen by the OS.
|
|
|
|
pub fn address(&self) -> io::Result<SocketAddr> {
|
|
|
|
|