完善服务端功能
This commit is contained in:
@@ -4,4 +4,77 @@
|
||||
// 由 ling 创建于 2025/1/18.
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
fn main() {}
|
||||
use async_trait::async_trait;
|
||||
use chrono::Local;
|
||||
use colored::{Color, Colorize};
|
||||
use fern::Dispatch;
|
||||
use log::{Level, LevelFilter};
|
||||
use std::sync::Arc;
|
||||
use LingTransmit::server::event::ServerEvent;
|
||||
use LingTransmit::server::Client::Client;
|
||||
use LingTransmit::server::Server;
|
||||
use LingTransmit::ssl::ServerCert;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let cert = include_bytes!("../../ssl/test_cert.pem");
|
||||
let pri = include_bytes!("../../ssl/test.pem");
|
||||
let passwd = include_str!("../../ssl/pass.txt");
|
||||
init_log();
|
||||
let server_cert = ServerCert::init_buffer_password(
|
||||
&cert.to_vec(),
|
||||
&pri.to_vec(),
|
||||
passwd,
|
||||
)
|
||||
.expect("解析证书失败");
|
||||
let server = Server::new_tcp("0.0.0.0:11451", server_cert, Arc::new(Event {}))
|
||||
.await
|
||||
.expect("启动服务端失败");
|
||||
server.start_accept().await;
|
||||
}
|
||||
fn get_time() -> String {
|
||||
let now = Local::now();
|
||||
now.format("%Y-%m-%d %H:%M:%S").to_string()
|
||||
}
|
||||
fn init_log() {
|
||||
let console_dispatch = Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
let (title, color) = match record.level() {
|
||||
Level::Error => ("Error", Color::Red),
|
||||
Level::Warn => ("Warning", Color::Yellow),
|
||||
Level::Info => ("Info", Color::Green),
|
||||
Level::Debug => ("Debug", Color::BrightWhite),
|
||||
Level::Trace => ("Trace", Color::White),
|
||||
};
|
||||
|
||||
out.finish(format_args!(
|
||||
"{}",
|
||||
format!("[{} {}]\t{}", get_time(), title, message).color(color)
|
||||
))
|
||||
})
|
||||
.chain(std::io::stdout())
|
||||
.level(LevelFilter::Trace)
|
||||
.apply();
|
||||
}
|
||||
|
||||
struct Event {}
|
||||
|
||||
#[async_trait]
|
||||
impl ServerEvent for Event {
|
||||
async fn client_linker_listener(&self, client: Arc<Client>) {
|
||||
println!("客户端连入,ID:{}", client.id);
|
||||
}
|
||||
|
||||
async fn client_close_listener(&self, client: Arc<Client>) {
|
||||
println!("客户端挂断,ID:{}", client.id)
|
||||
}
|
||||
|
||||
async fn client_user_data(&self, client: Arc<Client>, packet: Vec<u8>) -> std::io::Result<()> {
|
||||
println!(
|
||||
"客户端发送数据,ID:{},数据长度:{}",
|
||||
client.id,
|
||||
packet.len()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user