mirror of
https://gitea.farpn.net/w1cdn/farpn_udp_server.git
synced 2025-10-13 13:08:42 -05:00
Rough out db basics.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@
|
|||||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
*.db
|
||||||
|
87
Cargo.lock
generated
87
Cargo.lock
generated
@ -2,6 +2,93 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "2.9.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2261d10cca569e4643e526d8dc2e62e433cc8aba21ab764233731f8d369bf394"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fallible-iterator"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fallible-streaming-iterator"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "foldhash"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.15.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
|
||||||
|
dependencies = [
|
||||||
|
"foldhash",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashlink"
|
||||||
|
version = "0.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1"
|
||||||
|
dependencies = [
|
||||||
|
"hashbrown",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libsqlite3-sys"
|
||||||
|
version = "0.35.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "133c182a6a2c87864fe97778797e46c7e999672690dc9fa3ee8e241aa4a9c13f"
|
||||||
|
dependencies = [
|
||||||
|
"pkg-config",
|
||||||
|
"vcpkg",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "my_udp_server"
|
name = "my_udp_server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rusqlite",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pkg-config"
|
||||||
|
version = "0.3.32"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rusqlite"
|
||||||
|
version = "0.37.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "165ca6e57b20e1351573e3729b958bc62f0e48025386970b6e4d29e7a7e71f3f"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"fallible-iterator",
|
||||||
|
"fallible-streaming-iterator",
|
||||||
|
"hashlink",
|
||||||
|
"libsqlite3-sys",
|
||||||
|
"smallvec",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "smallvec"
|
||||||
|
version = "1.15.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vcpkg"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||||
|
@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
rusqlite = "0.37.0"
|
||||||
|
48
src/main.rs
48
src/main.rs
@ -1,8 +1,13 @@
|
|||||||
// UDP server
|
use std::net::UdpSocket; // UDP server
|
||||||
use std::net::UdpSocket;
|
use rusqlite::{params, Connection, Result}; // Database operations and result handling
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
|
let _ = create_database();
|
||||||
|
let _ = insert_packet("foo", 1);
|
||||||
|
|
||||||
let socket_result: Result<UdpSocket, std::io::Error> = UdpSocket::bind("127.0.0.1:7878");
|
let socket_result: Result<UdpSocket, std::io::Error> = UdpSocket::bind("127.0.0.1:7878");
|
||||||
let socket: UdpSocket = match socket_result {
|
let socket: UdpSocket = match socket_result {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
@ -17,7 +22,6 @@ fn main() -> std::io::Result<()> {
|
|||||||
// Buffer for incoming data (512 bytes)
|
// Buffer for incoming data (512 bytes)
|
||||||
let mut buffer: [u8; 512] = [0; 512];
|
let mut buffer: [u8; 512] = [0; 512];
|
||||||
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Receive data from the client
|
// Receive data from the client
|
||||||
let (bytes_received, src_addr) = socket.recv_from(&mut buffer)?;
|
let (bytes_received, src_addr) = socket.recv_from(&mut buffer)?;
|
||||||
@ -32,3 +36,41 @@ fn main() -> std::io::Result<()> {
|
|||||||
socket.send_to(&buffer[..bytes_received], src_addr)?;
|
socket.send_to(&buffer[..bytes_received], src_addr)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Database functions
|
||||||
|
fn create_database() -> Result<()> {
|
||||||
|
// Connect to SQLite database (creates the file if it doesn't exist)
|
||||||
|
let conn = Connection::open("pkt_database.db")?;
|
||||||
|
|
||||||
|
// Create a table
|
||||||
|
conn.execute(
|
||||||
|
"CREATE TABLE IF NOT EXISTS packets (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
raw TEXT NOT NULL,
|
||||||
|
age INTEGER NOT NULL,
|
||||||
|
date INTEGER NOT NULL
|
||||||
|
)",
|
||||||
|
[], // No parameters needed
|
||||||
|
)?;
|
||||||
|
|
||||||
|
println!("Database and table created successfully.");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_packet(raw: &str, age: i32) -> Result<()> {
|
||||||
|
let conn = Connection::open("pkt_database.db")?;
|
||||||
|
|
||||||
|
let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
|
||||||
|
|
||||||
|
// Insert
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO packets (raw, age, date) VALUES (?1, ?2, ?3)",
|
||||||
|
params![raw, age, ds], // Bind parameters
|
||||||
|
)?;
|
||||||
|
|
||||||
|
println!("Row inserted successfully.");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user