mirror of
https://github.com/guilhermewerner/mini-redis
synced 2025-06-15 14:35:13 +00:00
Create Simple TCP Listener
This commit is contained in:
25
Cargo.toml
Normal file
25
Cargo.toml
Normal file
@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "MiniRedis"
|
||||
version = "0.0.1"
|
||||
description = "Mini Redis"
|
||||
repository = "https://github.com/GuilhermeWerner/MiniRedis"
|
||||
authors = ["GuilhermeWerner <guilhermeqwerner@gmail.com>"]
|
||||
license = "MIT"
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[[bin]]
|
||||
name="Client"
|
||||
path = "Source/Client.rs"
|
||||
|
||||
[[bin]]
|
||||
name="Server"
|
||||
path = "Source/Server.rs"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.0.2", features = ["full"] }
|
||||
mini-redis = "0.4"
|
||||
|
||||
[[example]]
|
||||
name="Hello"
|
||||
path = "Examples/Hello.rs"
|
17
Examples/Hello.rs
Normal file
17
Examples/Hello.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use mini_redis::{client, Result};
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn main() -> Result<()> {
|
||||
// Open a connection to the mini-redis address.
|
||||
let mut client = client::connect("127.0.0.1:6379").await?;
|
||||
|
||||
// Set the key "hello" with value "world"
|
||||
client.set("hello", "world".into()).await?;
|
||||
|
||||
// Get key "hello"
|
||||
let result = client.get("hello").await?;
|
||||
|
||||
println!("{:?}", result);
|
||||
|
||||
Ok(())
|
||||
}
|
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