You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.5 KiB

//! Server connection information.
use std::io;
use std::net::SocketAddr;
use mio::event::Evented;
use mio::net::TcpListener;
use mio::{Poll, PollOpt, Ready, Token};
use crate::client::Client;
use crate::result::*;
use crate::state::*;
/// Connection information for the server.
#[derive(Debug)]
pub struct Server {
/// listen socket
pub socket: TcpListener,
/// token identifier (0)
pub token: Token,
/// ip address/port
pub addr: SocketAddr,
}
impl Server {
/// Bind to the provided address
pub fn listen<'a>(addr: String, token: Token) -> RudeResult<Server> {
let addr: SocketAddr = try_log!(addr.parse(), "Unable to parse server address: {}", &addr);
let socket: TcpListener = try_log!(
TcpListener::bind(&addr),
"Unable to bind to address: {}",
&addr,
);
Ok(Server {
socket,
token,
addr,
})
}
/// Accept a new client connection
pub fn accept(&self, token: Token) -> RudeResult<Client> {
let (socket, addr) = self.socket.accept()?;
Ok(Client {
socket,
token,
addr,
state: State::Login(Login::Username),
})
}
}
impl Evented for Server {
fn register(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt,
) -> io::Result<()> {
self.socket.register(poll, token, interest, opts)
}
fn reregister(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt,
) -> io::Result<()> {
self.socket.reregister(poll, token, interest, opts)
}
fn deregister(&self, poll: &Poll) -> io::Result<()> {
self.socket.deregister(poll)
}
}