Spaces:
Build error
Build error
| //! Memory Service Binary Entry Point | |
| //! | |
| //! Starts the TCP server that handles the binary protocol. | |
| use memory_service::{MemoryService, MemoryProtocolCodec, ServiceConfig}; | |
| use tokio::net::TcpListener; | |
| use tracing::{info, error, Level}; | |
| use tracing_subscriber::EnvFilter; | |
| use std::sync::Arc; | |
| async fn main() -> anyhow::Result<()> { | |
| // Initialize logging | |
| tracing_subscriber::fmt() | |
| .with_env_filter(EnvFilter::from_default_env().add_directive(Level::INFO.into())) | |
| .json() | |
| .init(); | |
| info!("Starting Memory Service"); | |
| // Load configuration | |
| let config = ServiceConfig::load()?; | |
| info!("Configuration loaded: bind_addr={}", config.bind_addr); | |
| // Initialize memory service | |
| let service = Arc::new(MemoryService::new(&config).await?); | |
| info!("Memory service initialized"); | |
| // Start TCP listener | |
| let listener = TcpListener::bind(&config.bind_addr).await?; | |
| info!("Listening on {}", config.bind_addr); | |
| // Accept connections | |
| loop { | |
| match listener.accept().await { | |
| Ok((socket, addr)) => { | |
| info!("New connection from {}", addr); | |
| let service = Arc::clone(&service); | |
| tokio::spawn(async move { | |
| if let Err(e) = handle_connection(socket, service).await { | |
| error!("Connection error: {}", e); | |
| } | |
| }); | |
| } | |
| Err(e) => { | |
| error!("Accept error: {}", e); | |
| } | |
| } | |
| } | |
| } | |
| async fn handle_connection( | |
| socket: tokio::net::TcpStream, | |
| service: Arc<MemoryService>, | |
| ) -> anyhow::Result<()> { | |
| use futures::StreamExt; | |
| use tokio_util::codec::Decoder; | |
| let codec = MemoryProtocolCodec::new(); | |
| let mut framed = codec.framed(socket); | |
| while let Some(result) = framed.next().await { | |
| match result { | |
| Ok(frame) => { | |
| let response = service.handle_frame(frame).await?; | |
| // Send response | |
| use futures::SinkExt; | |
| framed.send(response).await?; | |
| } | |
| Err(e) => { | |
| error!("Frame decode error: {}", e); | |
| break; | |
| } | |
| } | |
| } | |
| Ok(()) | |
| } | |