This commit is contained in:
azw 2023-11-11 06:43:03 +00:00
parent f0f9c4cb5c
commit 8cd5c74b85

View File

@ -2,26 +2,23 @@ use std::rc::{Rc, Weak};
use std::cell::RefCell;
use std::time::Duration;
use tokio::runtime::Runtime;
use actix_web::{dev::ServerHandle,
rt,
web,
get,
web::Data,
HttpRequest,
HttpResponse,
HttpServer,
Responder};
use actix_web::App as WebApp;
use std::{sync::mpsc, thread};
use std::sync::{Arc, Mutex, Condvar};
use std::collections::HashMap;
use actix_web::{
dev::ServerHandle,
rt,
web,
App as WebApp,
web::Data,
HttpRequest,
HttpResponse,
HttpServer,
Responder};
use std::sync::{Arc, Mutex};
//use std::collections::HashMap;
use std::any::Any;
use r9_macro::SharedFromSelf;
use r9_macro_derive::SharedFromSelf;
pub type HttpRequestCb = Rc::<dyn FnMut (HttpRequest)>;
pub trait UserApp {
fn get_pkg_name(&self) -> String;
fn init(&mut self);
@ -65,7 +62,7 @@ struct HttpContext {
}
async fn index(data: Data<Arc::<AppState>>, req: HttpRequest) -> impl Responder {
println!("http.thread.id {:?}", thread::current().id());
println!("http.thread.id {:?}", std::thread::current().id());
let context = Arc::new(
Mutex::new(HttpContext{
id: 0,
@ -89,7 +86,7 @@ async fn index(data: Data<Arc::<AppState>>, req: HttpRequest) -> impl Responder
}
async fn run_app(port: u16, app_state: Arc::<AppState>,
tx: mpsc::Sender<ServerHandle>) -> std::io::Result<()> {
tx: std::sync::mpsc::Sender<ServerHandle>) -> std::io::Result<()> {
let server = HttpServer::new(move || {
WebApp::new()
.data(app_state.clone())
@ -141,7 +138,7 @@ impl App {
}
pub fn init(&mut self, user_app: Rc::<RefCell::<dyn UserApp>>) {
println!("main.thread.id {:?}", thread::current().id());
println!("main.thread.id {:?}", std::thread::current().id());
self.user_app = Some(user_app);
self.tokio_rt.enter();
self.init_http_server();
@ -213,7 +210,7 @@ impl App {
fn dispatch_immsg(&mut self) {
{
self.im_mutex.lock();
let _ = self.im_mutex.lock();
if !self.im_msgs.borrow().empty() {
r9::ListHead::replace_init(&self.im_msgs, &self.im_work_msgs);
}
@ -252,11 +249,11 @@ impl App {
}
fn init_http_server(&mut self) {
let (tx, _) = mpsc::channel();
let (tx, _) = std::sync::mpsc::channel();
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 || {
std::thread::spawn(move || {
let server_future = run_app(port, app_state, tx);
rt::System::new().block_on(server_future)
});