This commit is contained in:
azw 2023-11-11 05:36:38 +00:00
parent 08caa6cf47
commit 18521debbc

View File

@ -1,6 +1,5 @@
use std::rc::{Rc, Weak}; use std::rc::{Rc, Weak};
use std::cell::RefCell; use std::cell::RefCell;
use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use actix_web::{dev::ServerHandle, use actix_web::{dev::ServerHandle,
@ -42,11 +41,11 @@ pub struct App {
im_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>, im_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>,
im_work_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>, im_work_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>,
im_mutex: Mutex<i32>, im_mutex: Mutex<i32>,
http_handlers: Arc<Mutex<HashMap<String, HttpRequestCb>>>, webapp_state: Arc::<AppState>,
} }
struct AppState { struct AppState {
app_name: String request: Arc<Mutex<Vec<Arc<Mutex<HttpContext>>>>>,
} }
#[derive(Default)] #[derive(Default)]
@ -57,28 +56,43 @@ struct IMMsgNode {
entry: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>, entry: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>,
} }
async fn index(data: Data<AppState>) -> impl Responder { struct HttpContext {
println!("http.thread.id {:?}", thread::current().id()); id: u64,
HttpResponse::Ok().body("Hello world!") req: HttpRequest,
add_tick: i64,
handled: bool,
rsp: String
} }
async fn run_app(port: u16, tx: mpsc::Sender<ServerHandle>) -> std::io::Result<()> { async fn index(data: Data<Arc::<AppState>>, req: HttpRequest) -> impl Responder {
let server = HttpServer::new(|| { println!("http.thread.id {:?}", thread::current().id());
let context = Arc::new(
Mutex::new(HttpContext{
id: 0,
req: req,
add_tick: 0,
handled: false,
rsp: "".to_string()
})
);
{
//data.request.lock();
}
{
while !context.lock().unwrap().handled {
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
}
}
let data = context.lock().unwrap().rsp.clone();
HttpResponse::Ok().body(data)
}
async fn run_app(port: u16, app_state: Arc::<AppState>,
tx: mpsc::Sender<ServerHandle>) -> std::io::Result<()> {
let server = HttpServer::new(move || {
WebApp::new() WebApp::new()
.data(AppState { .data(app_state.clone())
app_name:String::from("Actix-web"),
})
.route("/webapp/index.php",web::get().to(index)) .route("/webapp/index.php",web::get().to(index))
/*
WebApp::new()
.service(web::resource("/webapp/index.php").to(
|| async
{
"{}"
})
)
.service(hello)
*/
}) })
.bind(("0.0.0.0", port))? .bind(("0.0.0.0", port))?
.workers(1) .workers(1)
@ -106,10 +120,14 @@ impl App {
instance_id: 0, instance_id: 0,
tokio_rt: Runtime::new().unwrap(), tokio_rt: Runtime::new().unwrap(),
user_app: None, user_app: None,
http_handlers: Default::default(),
im_msgs: r9::ListHead::<IMMsgNode>::new_head(), im_msgs: r9::ListHead::<IMMsgNode>::new_head(),
im_work_msgs: r9::ListHead::<IMMsgNode>::new_head(), im_work_msgs: r9::ListHead::<IMMsgNode>::new_head(),
im_mutex: Mutex::new(1), im_mutex: Mutex::new(1),
webapp_state: Arc::new(
AppState{
//request: Default::default()
}
),
_self_wp: Default::default(), _self_wp: Default::default(),
} }
))); )));
@ -138,7 +156,7 @@ impl App {
pub fn run(&mut self) { pub fn run(&mut self) {
loop { loop {
crate::Timer::instance().borrow_mut().update(); crate::Timer::instance().borrow_mut().update();
sleep(Duration::from_millis(1)); std::thread::sleep(Duration::from_millis(1));
self.dispatch_immsg(); self.dispatch_immsg();
} }
} }
@ -217,8 +235,9 @@ impl App {
let (tx, _) = mpsc::channel(); let (tx, _) = mpsc::channel();
let port = self.user_app.as_ref().unwrap().borrow().get_http_listen_port() as u16; let port = self.user_app.as_ref().unwrap().borrow().get_http_listen_port() as u16;
let app_state = self.webapp_state.clone();
thread::spawn(move || { thread::spawn(move || {
let server_future = run_app(port, tx); let server_future = run_app(port, app_state, tx);
rt::System::new().block_on(server_future) rt::System::new().block_on(server_future)
}); });
} }