hcnet
1.0.0
The bottom layer of the virtualized server and client can easily switch protocols such as tcp/tcps/ws/wss/udp to facilitate protocol switching under any circumstances. Subsequent requirements such as online number and flow control will be processed later.
Unified use of callbacks for processing functions
/// 连接处理回调函数
# [ async_trait ]
pub trait Handler {
/// 此接口只有在服务端接受服务时进行触发
/// 接收新的端口连接, 如果处理将在此触发
async fn on_accept ( & mut self , conn : NetConn ) -> NetResult < ( ) > {
let _conn = conn ;
unreachable ! ( "Listener must impl accept" )
}
/// 此接口在可以发送消息时触发
/// 例如websocket将在握手成功后触发该函数
async fn on_open ( & mut self ) -> NetResult < ( ) > {
trace ! ( "ws on_open" ) ;
Ok ( ( ) )
}
/// 此接口在远程服务端被关闭时进行触发
async fn on_close ( & mut self , code : CloseCode , reason : String ) {
trace ! (
"on_close code = {}, reason = {reason}" ,
Into :: < u16 > :: into ( code )
) ;
}
/// ping消息收到, 将会自动返回pong消息
async fn on_ping ( & mut self , data : Vec < u8 > ) -> NetResult < Vec < u8 > > {
trace ! ( "on_ping" ) ;
Ok ( data )
}
/// pong消息
async fn on_pong ( & mut self , data : Vec < u8 > ) -> NetResult < ( ) > {
let _data = data ;
trace ! ( "on_pong" ) ;
Ok ( ( ) )
}
/// message信息收到
async fn on_message ( & mut self , msg : Message ) -> NetResult < ( ) > {
let _ = msg ;
Ok ( ( ) )
}
async fn on_request ( & mut self , req : Request < Vec < u8 > > ) -> NetResult < Response < Vec < u8 > > > {
WsHandshake :: build_request ( & req )
}
async fn on_response ( & mut self , res : Request < Vec < u8 > > ) -> NetResult < ( ) > {
let _ = res ;
Ok ( ( ) )
}
}
NetConn :: tcp_bind ( "0.0.0.0:2003" , Settings :: default ( ) ) . await NetConn :: ws_bind ( "0.0.0.0:2003" , Settings :: default ( ) ) . await Configuring the certificate information during monitoring can easily support the wss protocol.
let mut settings = Settings {
domain : Some ( "test.wmproxy.net" . to_string ( ) ) ,
cert : Some ( "key/example.com.pem" . to_string ( ) ) ,
key : Some ( "key/example.com.key" . to_string ( ) ) ,
.. Settings :: default ( )
} ;
NetConn :: ws_bind ( "0.0.0.0:2003" , settings ) . await NetConn :: kcp_bind ( "0.0.0.0:2003" ) . awaitBasically, the monitoring is the same as the general socket monitoring, and the complexity is similar. You can switch any protocol at will.
let h = conn . run_handler ( |_| ServerHandler ) . await . unwrap ( ) ;
let _ = tokio :: join! ( h ) ;For specific examples, please refer to server_echo
NetConn :: tcp_connect ( "127.0.0.1:2003" ) . await NetConn :: ws_connect ( "ws://example.com:2003" ) . await NetConn :: ws_connect ( "wss://example.com:2003" ) . await NetConn :: kcp_connect ( "wss://example.com:2003" ) . await let ( mut sender , receiver ) = NetSender :: new ( 10 , 1 ) ;
let _ = conn . run_with_handler (
ClientHandler {
sender : sender . clone ( ) ,
} ,
receiver ,
) . await ;or
let _ = conn . run_handler ( |sender| ClientHandler { sender } ) . await ;For specific examples, please refer to client_echo
Start the server first
cargo run --example server_echo tcpRestart the client
cargo run --example client_echo tcp