- update: packet router to have the various services needed for the packets to be local to it.

- add: character service grpc client calls
This commit is contained in:
2025-01-04 17:45:54 -05:00
parent 00468e9600
commit 4a826d2a46
7 changed files with 189 additions and 78 deletions

View File

@@ -18,7 +18,9 @@ use tracing::{debug, error, info, warn};
use utils::consul_registration;
use utils::service_discovery::get_service_address;
use warp::Filter;
use crate::character_client::CharacterClient;
use crate::connection_service::ConnectionService;
use crate::router::PacketRouter;
mod packet_type;
mod packet;
@@ -31,53 +33,20 @@ mod handlers;
mod bufferpool;
mod metrics;
mod auth_client;
mod character_client;
mod connection_state;
mod connection_service;
pub mod auth {
tonic::include_proto!("auth"); // Path matches the package name in auth.proto
}
pub mod char {
tonic::include_proto!("character"); // Path matches the package name in auth.proto
}
const BUFFER_POOL_SIZE: usize = 1000;
const MAX_CONCURRENT_CONNECTIONS: usize = 100;
async fn handle_connection(stream: &mut TcpStream, pool: Arc<BufferPool>, auth_client: Arc<Mutex<AuthClient>>, connection_service: Arc<ConnectionService>, connection_id: String) -> Result<(), Box<dyn Error + Send + Sync>> {
ACTIVE_CONNECTIONS.inc();
while let Some(mut buffer) = pool.acquire().await {
// Read data into the buffer
let n = stream.read(&mut buffer).await?;
if n == 0 {
break; // Connection closed
}
PACKETS_RECEIVED.inc();
// Process the packet
match Packet::from_raw(&buffer[..n]) {
Ok(packet) => {
debug!("Parsed Packet: {:?}", packet);
// Handle the parsed packet (route it, process it, etc.)
router::route_packet(stream, packet, auth_client.clone(), connection_service.clone(), connection_id.clone()).await?;
}
Err(e) => warn!("Failed to parse packet: {}", e),
}
pool.release(buffer).await;
}
if let Some(state) = connection_service.get_connection(&connection_id) {
let session_id = state.session_id.unwrap_or_default();
if !session_id.is_empty() {
let mut auth_client = auth_client.lock().await;
auth_client.logout(&session_id).await?;
} else {
warn!("No session found for {}", stream.peer_addr()?);
}
}
ACTIVE_CONNECTIONS.dec();
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
@@ -97,6 +66,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let health_check_url = format!("http://{}:{}/health", service_address, health_port);
let health_check_endpoint_addr = format!("{}:{}", service_address, health_port);
let auth_node = get_service_address(&consul_url, "auth-service").await?;
let character_node = get_service_address(&consul_url, "character-service").await?;
// Register service with Consul
let service_id = consul_registration::get_or_generate_service_id(env!("CARGO_PKG_NAME"));
@@ -121,6 +91,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let auth_url = format!("http://{}:{}", auth_address.ServiceAddress, auth_address.ServicePort);
let auth_client = Arc::new(Mutex::new(AuthClient::connect(&auth_url).await?));
let character_address = character_node.get(0).unwrap();
let character_url = format!("http://{}:{}", character_address.ServiceAddress, character_address.ServicePort);
let character_client = Arc::new(Mutex::new(CharacterClient::connect(&character_url).await?));
let full_addr = format!("{}:{}", &addr, port);
tokio::spawn(async move {
@@ -129,12 +103,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let buffer_pool = BufferPool::new(BUFFER_POOL_SIZE);
let connection_service = Arc::new(ConnectionService::new());
let packet_router = PacketRouter {
auth_client,
character_client,
connection_service,
};
info!("Packet service listening on {}", full_addr);
loop {
let (mut socket, addr) = listener.accept().await.unwrap();
let auth_client = auth_client.clone();
let connection_service = connection_service.clone();
let packet_router = packet_router.clone();
info!("New connection from {}", addr);
let pool = buffer_pool.clone();
@@ -143,11 +122,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Spawn a new task for each connection
tokio::spawn(async move {
let _permit = permit;
let connection_id = connection_service.add_connection();
if let Err(e) = handle_connection(&mut socket, pool, auth_client, connection_service.clone(), connection_id.clone()).await {
let connection_id = packet_router.connection_service.add_connection();
if let Err(e) = packet_router.handle_connection(&mut socket, pool, connection_id.clone()).await {
error!("Error handling connection: {}", e);
}
connection_service.remove_connection(&connection_id);
packet_router.connection_service.remove_connection(&connection_id);
});
}
});