Skip to content

Commit

Permalink
improved HTTP protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
b23r0 committed Aug 24, 2022
1 parent 2d3c1b8 commit 904aa08
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 89 deletions.
4 changes: 2 additions & 2 deletions heroinn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
eframe = "0.18"
egui_extras = {version = "0.18" , features = ["image"]}
eframe = "0.19.0"
egui_extras = {version = "0.19.0" , features = ["image"]}
image = { version = "0.24", features = ["jpeg", "png", "ico"] }
heroinn_util = {path = "../heroinn_util"}
heroinn_core = {path = "../heroinn_core"}
Expand Down
6 changes: 4 additions & 2 deletions heroinn/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl HeroinnApp {
.selected_text(format!("{:?}", self.combox_listen_protocol))
.show_ui(ui, |ui| {
ui.selectable_value(&mut self.combox_listen_protocol, HeroinnProtocol::TCP, "TCP");
ui.selectable_value(&mut self.combox_listen_protocol, HeroinnProtocol::HTTP, "HTTP");
});
});
strip.cell(|ui|{
Expand Down Expand Up @@ -233,6 +234,7 @@ impl HeroinnApp {
.width(280.0)
.show_ui(ui, |ui| {
ui.selectable_value(&mut self.combox_listen_protocol, HeroinnProtocol::TCP, format!("{:?}" , HeroinnProtocol::TCP));
ui.selectable_value(&mut self.combox_listen_protocol, HeroinnProtocol::HTTP, format!("{:?}" , HeroinnProtocol::HTTP));
});
ui.end_row();

Expand Down Expand Up @@ -309,7 +311,7 @@ impl HeroinnApp {
fn listen_table(&mut self,ctx: &egui::Context , ui: &mut egui::Ui) {
TableBuilder::new(ui)
.striped(true)
.cell_layout(egui::Layout::left_to_right().with_cross_align(egui::Align::Center))
.cell_layout(egui::Layout::left_to_right(egui::Align::Center))
.column(Size::initial(320.0).at_least(50.0))
.column(Size::initial(320.0).at_least(50.0))
.column(Size::initial(100.0).at_least(50.0))
Expand Down Expand Up @@ -374,7 +376,7 @@ impl HeroinnApp {
fn host_table(&mut self,ctx: &egui::Context , ui: &mut egui::Ui) {
TableBuilder::new(ui)
.striped(true)
.cell_layout(egui::Layout::left_to_right().with_cross_align(egui::Align::Center))
.cell_layout(egui::Layout::left_to_right(egui::Align::Center))
.column(Size::initial(50.0).at_least(50.0))
.column(Size::initial(120.0).at_least(50.0))
.column(Size::initial(150.0).at_least(50.0))
Expand Down
4 changes: 2 additions & 2 deletions heroinn_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ simple_logger = "2.2.0"
log = "0.4"
uuid = { version = "1.1.2" , features = ["v4"]}
hostname = "^0.3"
systemstat = "0.1.11"
systemstat = "0.2.0"
os_info = "3.4"
lazy_static = "1.4.0"

Expand All @@ -21,6 +21,6 @@ windows = {version = "0.39.0" , features = ["Win32_Foundation","Win32_System_Thr


[target.'cfg(not(target_os = "windows"))'.dependencies]
nix = "0.24.0"
nix = "0.25.0"
ioctl-rs = "0.2"
libc = "0.2.126"
2 changes: 1 addition & 1 deletion heroinn_client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn master_configure() -> ConnectionInfo{

if size == 0{
return ConnectionInfo{
protocol : HeroinnProtocol::TCP.to_u8(),
protocol : HeroinnProtocol::HTTP.to_u8(),
address : String::from("127.0.0.1:8000"),
remark : String::from("Default"),
};
Expand Down
2 changes: 2 additions & 0 deletions heroinn_client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ fn main() {

let config = master_configure();

log::debug!("master config : {:?}" , config);

loop{
close_all_session_in_lock!(shell_session_mgr);

Expand Down
17 changes: 17 additions & 0 deletions heroinn_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use std::net::SocketAddr;

pub mod module;

use heroinn_util::protocol::http::WSServer;
use heroinn_util::{*, protocol::{Server} , protocol::tcp::*, packet::Message};

pub struct HeroinnServer{
tcp_server : Option<TcpServer>,
ws_server : Option<WSServer>,
protocol : HeroinnProtocol
}

Expand All @@ -31,6 +33,17 @@ impl HeroinnServer{
match TcpServer::new(format!("0.0.0.0:{}" , port).as_str() , HeroinnServer::cb_connection , cb_msg){
Ok(tcp_server) => Ok(Self{
tcp_server: Some(tcp_server) ,
ws_server : None,
protocol
}),
Err(e) => Err(e),
}
},
HeroinnProtocol::HTTP => {
match WSServer::new(format!("0.0.0.0:{}" , port).as_str() , HeroinnServer::cb_connection , cb_msg){
Ok(ws_server) => Ok(Self{
tcp_server: None,
ws_server : Some(ws_server),
protocol
}),
Err(e) => Err(e),
Expand All @@ -43,13 +56,15 @@ impl HeroinnServer{
pub fn sendto(&mut self, peer_addr : &SocketAddr, buf : & [u8]) -> Result<()>{
match self.protocol{
HeroinnProtocol::TCP => self.tcp_server.as_mut().unwrap().sendto(peer_addr, buf),
HeroinnProtocol::HTTP => self.ws_server.as_mut().unwrap().sendto(peer_addr, buf),
HeroinnProtocol::Unknow => panic!("unknow protocol"),
}
}

pub fn local_addr(&self) -> Result<SocketAddr>{
match self.protocol{
HeroinnProtocol::TCP => self.tcp_server.as_ref().unwrap().local_addr(),
HeroinnProtocol::HTTP => self.ws_server.as_ref().unwrap().local_addr(),
HeroinnProtocol::Unknow => panic!("unknow protocol"),
}
}
Expand All @@ -61,13 +76,15 @@ impl HeroinnServer{
pub fn contains_addr(&mut self , peer_addr : &SocketAddr) -> bool{
match self.protocol{
HeroinnProtocol::TCP => self.tcp_server.as_mut().unwrap().contains_addr(peer_addr),
HeroinnProtocol::HTTP => self.ws_server.as_mut().unwrap().contains_addr(peer_addr),
HeroinnProtocol::Unknow => panic!("unknow protocol"),
}
}

pub fn close(&mut self){
match self.protocol{
HeroinnProtocol::TCP => self.tcp_server.as_mut().unwrap().close(),
HeroinnProtocol::HTTP => self.ws_server.as_mut().unwrap().close(),
HeroinnProtocol::Unknow => panic!("unknow protocol"),
}
}
Expand Down
3 changes: 3 additions & 0 deletions heroinn_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,23 @@ impl HeroinnServerCommandID{
#[derive(Debug,Clone,PartialEq)]
pub enum HeroinnProtocol{
TCP,
HTTP,
Unknow
}

impl HeroinnProtocol{
pub fn to_u8(&self) -> u8{
match self{
HeroinnProtocol::TCP => 0x00,
HeroinnProtocol::HTTP => 0x01,
HeroinnProtocol::Unknow => 0xff,
}
}

pub fn from(v : u8) -> Self{
match v{
0x00 => HeroinnProtocol::TCP,
0x01 => HeroinnProtocol::HTTP,
_ => HeroinnProtocol::Unknow,
}
}
Expand Down
Loading

0 comments on commit 904aa08

Please sign in to comment.