Commit a60b12de by qlintonger xeno

重设文件结构

parent a9dafd10
use crate::handle_messages::handle_other_message; use crate::config::config::STATIC_WS_PWD;
use crate::heartbeat::handle_heartbeat; use crate::handles::handle_messages::handle_other_message;
use crate::json_utils::{make_common_resp, parse_message}; use crate::handles::heartbeat::handle_heartbeat;
use crate::{config, utils}; use crate::typing::used_typed::{Connection, ConnectionMap, TaskMap};
use crate::utils::json_utils::{make_common_resp, parse_message};
use crate::utils::utils::get_connection_params;
use futures::{SinkExt, StreamExt}; use futures::{SinkExt, StreamExt};
use lazy_static::lazy_static;
use redis::Client;
use redis::Commands;
use redis_pool::SingleRedisPool;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use tokio::task::JoinHandle; use tokio::sync::Mutex as AsyncMutex;
use tokio::time; use tokio::time;
use tokio_tungstenite::{accept_hdr_async, WebSocketStream}; use tokio_tungstenite::accept_hdr_async;
use tungstenite::handshake::server::{Request, Response}; use tungstenite::handshake::server::{Request, Response};
use tungstenite::{Error, Message}; use tungstenite::{Error, Message};
use tokio::sync::Mutex as AsyncMutex;
use lazy_static::lazy_static;
use redis::{Client};
use redis_pool::{SingleRedisPool};
use redis::Commands;
use config::STATIC_WS_PWD;
lazy_static! { lazy_static! {
static ref REDIS_POOL: SingleRedisPool = { static ref REDIS_POOL: SingleRedisPool = {
...@@ -25,18 +25,6 @@ lazy_static! { ...@@ -25,18 +25,6 @@ lazy_static! {
}; };
} }
// 自定义结构体来存储发送器和接收器
#[derive(Debug)]
struct Connection {
sender: futures::stream::SplitSink<WebSocketStream<tokio::net::TcpStream>, Message>,
receiver: futures::stream::SplitStream<WebSocketStream<tokio::net::TcpStream>>,
}
// 全局连接映射,存储 fromId 到 Connection 的映射
type ConnectionMap = Arc<AsyncMutex<HashMap<String, Connection>>>;
// 全局任务映射,存储 fromId 到 JoinHandle 的映射
type TaskMap = Arc<Mutex<HashMap<String, JoinHandle<()>>>>;
lazy_static! { lazy_static! {
static ref CONNECTIONS: ConnectionMap = Arc::new(AsyncMutex::new(HashMap::new())); static ref CONNECTIONS: ConnectionMap = Arc::new(AsyncMutex::new(HashMap::new()));
static ref TASKS: TaskMap = Arc::new(Mutex::new(HashMap::new())); static ref TASKS: TaskMap = Arc::new(Mutex::new(HashMap::new()));
...@@ -90,7 +78,7 @@ fn handle_handshake( ...@@ -90,7 +78,7 @@ fn handle_handshake(
must_existed_params: &[&str], must_existed_params: &[&str],
) -> Result<HashMap<String, String>, String> { ) -> Result<HashMap<String, String>, String> {
println!("新客户端连接: {}", req.uri()); println!("新客户端连接: {}", req.uri());
let connection_params = match utils::get_connection_params(req.uri().to_string()) { let connection_params = match get_connection_params(req.uri().to_string()) {
Ok(p) => p, Ok(p) => p,
Err(e) => { Err(e) => {
let error_msg = format!("缺少重要连接数据段: {}", e); let error_msg = format!("缺少重要连接数据段: {}", e);
......
pub mod config;
\ No newline at end of file
use crate::json_utils::{make_common_resp, MessageData};
use futures::SinkExt; use futures::SinkExt;
use tungstenite::{Error, Message}; use tungstenite::{Error, Message};
use crate::utils::json_utils::{make_common_resp, MessageData};
// 处理其他类型消息 // 处理其他类型消息
pub(crate) async fn handle_other_message( pub(crate) async fn handle_other_message(
// 增加 + std::marker::Unpin 限制 // 增加 + std::marker::Unpin 限制
sender: &mut (impl SinkExt<Message, Error = Error> + std::marker::Unpin), sender: &mut (impl SinkExt<Message, Error = Error> + Unpin),
data: &MessageData, data: &MessageData,
) -> Result<(), Error> { ) -> Result<(), Error> {
println!("收到客户端消息: {:?}", data); println!("收到客户端消息: {:?}", data);
......
pub mod handle_messages;
pub mod heartbeat;
\ No newline at end of file
...@@ -2,10 +2,9 @@ extern crate core; ...@@ -2,10 +2,9 @@ extern crate core;
mod client; mod client;
mod utils; mod utils;
mod json_utils;
mod heartbeat;
mod handle_messages;
mod config; mod config;
mod handles;
mod typing;
use client::handle_client; use client::handle_client;
use tokio::net::TcpListener; use tokio::net::TcpListener;
......
pub mod used_typed;
\ No newline at end of file
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tokio::task::JoinHandle;
use tokio_tungstenite::WebSocketStream;
use tungstenite::Message;
use tokio::sync::Mutex as AsyncMutex;
// 自定义结构体来存储发送器和接收器
#[derive(Debug)]
pub struct Connection {
pub(crate) sender: futures::stream::SplitSink<WebSocketStream<tokio::net::TcpStream>, Message>,
pub(crate) receiver: futures::stream::SplitStream<WebSocketStream<tokio::net::TcpStream>>,
}
// 全局连接映射,存储 fromId 到 Connection 的映射
pub type ConnectionMap = Arc<AsyncMutex<HashMap<String, Connection>>>;
// 全局任务映射,存储 fromId 到 JoinHandle 的映射
pub type TaskMap = Arc<Mutex<HashMap<String, JoinHandle<()>>>>;
\ No newline at end of file
pub mod json_utils;
pub mod utils;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment