forked from lavina/lavina
implement pings
This commit is contained in:
parent
5d57df3219
commit
e0135a8ea6
|
@ -3,3 +3,4 @@ listen_on = "127.0.0.1:8080"
|
||||||
|
|
||||||
[irc]
|
[irc]
|
||||||
listen_on = "127.0.0.1:6667"
|
listen_on = "127.0.0.1:6667"
|
||||||
|
server_name = "irc.localhost"
|
|
@ -35,11 +35,15 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
tracing::info!("Booting up");
|
tracing::info!("Booting up");
|
||||||
tracing::info!("Loaded config: {config:?}");
|
tracing::info!("Loaded config: {config:?}");
|
||||||
|
let ServerConfig {
|
||||||
|
telemetry: telemetry_config,
|
||||||
|
irc: irc_config,
|
||||||
|
} = config;
|
||||||
let metrics = MetricsRegistry::new();
|
let metrics = MetricsRegistry::new();
|
||||||
let rooms = RoomRegistry::empty();
|
let rooms = RoomRegistry::empty();
|
||||||
let players = PlayerRegistry::empty(rooms.clone());
|
let players = PlayerRegistry::empty(rooms.clone());
|
||||||
let telemetry_terminator = util::telemetry::launch(&config.telemetry, metrics.clone()).await?;
|
let telemetry_terminator = util::telemetry::launch(telemetry_config, metrics.clone()).await?;
|
||||||
let irc = projections::irc::launch(&config.irc, players, metrics.clone()).await?;
|
let irc = projections::irc::launch(irc_config, players, metrics.clone()).await?;
|
||||||
tracing::info!("Started");
|
tracing::info!("Started");
|
||||||
|
|
||||||
sleep.await;
|
sleep.await;
|
||||||
|
|
|
@ -2,21 +2,24 @@ use std::net::SocketAddr;
|
||||||
|
|
||||||
use prometheus::{IntCounter, IntGauge, Registry as MetricsRegistry};
|
use prometheus::{IntCounter, IntGauge, Registry as MetricsRegistry};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use tokio::io::{AsyncBufReadExt, BufReader, BufWriter};
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter};
|
||||||
use tokio::net::{TcpListener, TcpStream};
|
use tokio::net::{TcpListener, TcpStream};
|
||||||
use tokio::sync::oneshot::channel;
|
use tokio::sync::oneshot::channel;
|
||||||
|
|
||||||
use crate::core::player::PlayerRegistry;
|
use crate::core::player::PlayerRegistry;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::protos::irc::*;
|
use crate::protos::irc::client::{client_message, ClientMessage};
|
||||||
|
use crate::protos::irc::server::{ServerMessage, ServerMessageBody};
|
||||||
use crate::util::Terminator;
|
use crate::util::Terminator;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
pub listen_on: SocketAddr,
|
pub listen_on: SocketAddr,
|
||||||
|
pub server_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_socket(
|
async fn handle_socket(
|
||||||
|
config: ServerConfig,
|
||||||
mut stream: TcpStream,
|
mut stream: TcpStream,
|
||||||
socket_addr: SocketAddr,
|
socket_addr: SocketAddr,
|
||||||
mut players: PlayerRegistry,
|
mut players: PlayerRegistry,
|
||||||
|
@ -42,10 +45,27 @@ async fn handle_socket(
|
||||||
},
|
},
|
||||||
Err(err) => log::warn!("Failed to read from socket: {err}"),
|
Err(err) => log::warn!("Failed to read from socket: {err}"),
|
||||||
}
|
}
|
||||||
let parsed = client::client_message(&buffer[..]);
|
let parsed = client_message(&buffer[..]);
|
||||||
match parsed {
|
match parsed {
|
||||||
Ok((rest, msg)) => {
|
Ok((rest, msg)) => {
|
||||||
log::info!("Incoming IRC message: {msg:?}");
|
log::info!("Incoming IRC message: {msg:?}");
|
||||||
|
match msg {
|
||||||
|
ClientMessage::Ping { token } => {
|
||||||
|
let response = ServerMessage {
|
||||||
|
tags: vec![],
|
||||||
|
sender: None,
|
||||||
|
body: ServerMessageBody::Pong {
|
||||||
|
from: config.server_name.as_bytes().to_vec(),
|
||||||
|
token,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut buffer = vec![];
|
||||||
|
response.write(&mut buffer).unwrap();
|
||||||
|
writer.write_all(buffer.as_slice()).await;
|
||||||
|
writer.flush().await;
|
||||||
|
},
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::warn!("Failed to parse IRC message: {err}");
|
log::warn!("Failed to parse IRC message: {err}");
|
||||||
|
@ -59,7 +79,7 @@ async fn handle_socket(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn launch(
|
pub async fn launch(
|
||||||
config: &ServerConfig,
|
config: ServerConfig,
|
||||||
players: PlayerRegistry,
|
players: PlayerRegistry,
|
||||||
metrics: MetricsRegistry,
|
metrics: MetricsRegistry,
|
||||||
) -> Result<Terminator> {
|
) -> Result<Terminator> {
|
||||||
|
@ -85,10 +105,11 @@ pub async fn launch(
|
||||||
new_conn = listener.accept() => {
|
new_conn = listener.accept() => {
|
||||||
match new_conn {
|
match new_conn {
|
||||||
Ok((stream, socket_addr)) => {
|
Ok((stream, socket_addr)) => {
|
||||||
|
let config = config.clone();
|
||||||
total_connections.inc();
|
total_connections.inc();
|
||||||
current_connections.inc();
|
current_connections.inc();
|
||||||
log::debug!("Incoming connection from {socket_addr}");
|
log::debug!("Incoming connection from {socket_addr}");
|
||||||
let handle = tokio::task::spawn(handle_socket(stream, socket_addr, players.clone(), current_connections.clone()));
|
let handle = tokio::task::spawn(handle_socket(config, stream, socket_addr, players.clone(), current_connections.clone()));
|
||||||
},
|
},
|
||||||
Err(err) => log::warn!("Failed to accept new connection: {err}"),
|
Err(err) => log::warn!("Failed to accept new connection: {err}"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ pub struct ServerConfig {
|
||||||
pub listen_on: SocketAddr,
|
pub listen_on: SocketAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn launch(config: &ServerConfig, metrics: MetricsRegistry) -> Result<Terminator> {
|
pub async fn launch(config: ServerConfig, metrics: MetricsRegistry) -> Result<Terminator> {
|
||||||
log::info!("Starting the telemetry service");
|
log::info!("Starting the telemetry service");
|
||||||
let listener = TcpListener::bind(config.listen_on).await?;
|
let listener = TcpListener::bind(config.listen_on).await?;
|
||||||
log::debug!("Listener started");
|
log::debug!("Listener started");
|
||||||
|
|
Loading…
Reference in New Issue