mirror of
https://github.com/guilhermewerner/mini-redis
synced 2025-06-15 22:45:48 +00:00
Create Simple TCP Listener
This commit is contained in:
1
Source/Client.rs
Normal file
1
Source/Client.rs
Normal file
@ -0,0 +1 @@
|
||||
fn main() {}
|
28
Source/Server.rs
Normal file
28
Source/Server.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use mini_redis::{Connection, Frame};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Bind the listener to the address
|
||||
let listener = TcpListener::bind("127.0.0.1:6379").await.unwrap();
|
||||
|
||||
loop {
|
||||
// The second item contains the IP and port of the new connection.
|
||||
let (socket, _) = listener.accept().await.unwrap();
|
||||
process(socket).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn process(socket: TcpStream) {
|
||||
// The `Connection` lets us read/write redis **frames** instead of
|
||||
// byte streams. The `Connection` type is defined by mini-redis.
|
||||
let mut connection = Connection::new(socket);
|
||||
|
||||
if let Some(frame) = connection.read_frame().await.unwrap() {
|
||||
println!("GOT: {:?}", frame);
|
||||
|
||||
// Respond with an error
|
||||
let response = Frame::Error("unimplemented".to_string());
|
||||
connection.write_frame(&response).await.unwrap();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user