implement pings

This commit is contained in:
Nikita Vilunov 2023-02-10 18:09:29 +01:00
parent 5d57df3219
commit e0135a8ea6
4 changed files with 35 additions and 9 deletions

View File

@ -3,3 +3,4 @@ listen_on = "127.0.0.1:8080"
[irc]
listen_on = "127.0.0.1:6667"
server_name = "irc.localhost"

View File

@ -35,11 +35,15 @@ async fn main() -> Result<()> {
tracing::info!("Booting up");
tracing::info!("Loaded config: {config:?}");
let ServerConfig {
telemetry: telemetry_config,
irc: irc_config,
} = config;
let metrics = MetricsRegistry::new();
let rooms = RoomRegistry::empty();
let players = PlayerRegistry::empty(rooms.clone());
let telemetry_terminator = util::telemetry::launch(&config.telemetry, metrics.clone()).await?;
let irc = projections::irc::launch(&config.irc, players, metrics.clone()).await?;
let telemetry_terminator = util::telemetry::launch(telemetry_config, metrics.clone()).await?;
let irc = projections::irc::launch(irc_config, players, metrics.clone()).await?;
tracing::info!("Started");
sleep.await;

View File

@ -2,21 +2,24 @@ use std::net::SocketAddr;
use prometheus::{IntCounter, IntGauge, Registry as MetricsRegistry};
use serde::Deserialize;
use tokio::io::{AsyncBufReadExt, BufReader, BufWriter};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::oneshot::channel;
use crate::core::player::PlayerRegistry;
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;
#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub struct ServerConfig {
pub listen_on: SocketAddr,
pub server_name: String,
}
async fn handle_socket(
config: ServerConfig,
mut stream: TcpStream,
socket_addr: SocketAddr,
mut players: PlayerRegistry,
@ -42,10 +45,27 @@ async fn handle_socket(
},
Err(err) => log::warn!("Failed to read from socket: {err}"),
}
let parsed = client::client_message(&buffer[..]);
let parsed = client_message(&buffer[..]);
match parsed {
Ok((rest, 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) => {
log::warn!("Failed to parse IRC message: {err}");
@ -59,7 +79,7 @@ async fn handle_socket(
}
pub async fn launch(
config: &ServerConfig,
config: ServerConfig,
players: PlayerRegistry,
metrics: MetricsRegistry,
) -> Result<Terminator> {
@ -85,10 +105,11 @@ pub async fn launch(
new_conn = listener.accept() => {
match new_conn {
Ok((stream, socket_addr)) => {
let config = config.clone();
total_connections.inc();
current_connections.inc();
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}"),
}

View File

@ -24,7 +24,7 @@ pub struct ServerConfig {
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");
let listener = TcpListener::bind(config.listen_on).await?;
log::debug!("Listener started");