mirror of
https://github.com/rustdesk/hbb_common.git
synced 2025-07-02 07:56:50 +00:00
[enhance] implement inline func and neat import.
This commit is contained in:
parent
6be5600b77
commit
d8f907a0d9
@ -1,8 +1,5 @@
|
|||||||
use crate::tcp;
|
use crate::{config, tcp, websocket, ResultType};
|
||||||
use crate::websocket;
|
|
||||||
use sodiumoxide::crypto::secretbox::Key;
|
use sodiumoxide::crypto::secretbox::Key;
|
||||||
use crate::config;
|
|
||||||
use crate::ResultType;
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
// support Websocket and tcp.
|
// support Websocket and tcp.
|
||||||
@ -12,6 +9,7 @@ pub enum Stream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Stream {
|
impl Stream {
|
||||||
|
#[inline]
|
||||||
pub fn set_send_timeout(&mut self, ms: u64) {
|
pub fn set_send_timeout(&mut self, ms: u64) {
|
||||||
match self {
|
match self {
|
||||||
Stream::WebSocket(s) => s.set_send_timeout(ms),
|
Stream::WebSocket(s) => s.set_send_timeout(ms),
|
||||||
@ -19,6 +17,7 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn set_raw(&mut self) {
|
pub fn set_raw(&mut self) {
|
||||||
match self {
|
match self {
|
||||||
Stream::WebSocket(s) => s.set_raw(),
|
Stream::WebSocket(s) => s.set_raw(),
|
||||||
@ -26,6 +25,7 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub async fn send_bytes(&mut self, bytes: bytes::Bytes) -> ResultType<()> {
|
pub async fn send_bytes(&mut self, bytes: bytes::Bytes) -> ResultType<()> {
|
||||||
match self {
|
match self {
|
||||||
Stream::WebSocket(s) => s.send_bytes(bytes).await,
|
Stream::WebSocket(s) => s.send_bytes(bytes).await,
|
||||||
@ -33,6 +33,7 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub async fn send_raw(&mut self, bytes: Vec<u8>) -> ResultType<()> {
|
pub async fn send_raw(&mut self, bytes: Vec<u8>) -> ResultType<()> {
|
||||||
match self {
|
match self {
|
||||||
Stream::WebSocket(s) => s.send_raw(bytes).await,
|
Stream::WebSocket(s) => s.send_raw(bytes).await,
|
||||||
@ -40,6 +41,7 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn set_key(&mut self, key: Key) {
|
pub fn set_key(&mut self, key: Key) {
|
||||||
match self {
|
match self {
|
||||||
Stream::WebSocket(s) => s.set_key(key),
|
Stream::WebSocket(s) => s.set_key(key),
|
||||||
@ -47,6 +49,7 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn is_secured(&self) -> bool {
|
pub fn is_secured(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Stream::WebSocket(s) => s.is_secured(),
|
Stream::WebSocket(s) => s.is_secured(),
|
||||||
@ -54,6 +57,7 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub async fn next_timeout(
|
pub async fn next_timeout(
|
||||||
&mut self,
|
&mut self,
|
||||||
timeout: u64,
|
timeout: u64,
|
||||||
@ -65,6 +69,7 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// establish connect from websocket
|
/// establish connect from websocket
|
||||||
|
#[inline]
|
||||||
pub async fn connect_websocket(
|
pub async fn connect_websocket(
|
||||||
url: impl AsRef<str>,
|
url: impl AsRef<str>,
|
||||||
local_addr: Option<SocketAddr>,
|
local_addr: Option<SocketAddr>,
|
||||||
@ -78,6 +83,7 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// send message
|
/// send message
|
||||||
|
#[inline]
|
||||||
pub async fn send(&mut self, msg: &impl protobuf::Message) -> ResultType<()> {
|
pub async fn send(&mut self, msg: &impl protobuf::Message) -> ResultType<()> {
|
||||||
match self {
|
match self {
|
||||||
Self::WebSocket(ws) => ws.send(msg).await,
|
Self::WebSocket(ws) => ws.send(msg).await,
|
||||||
@ -86,6 +92,7 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// receive message
|
/// receive message
|
||||||
|
#[inline]
|
||||||
pub async fn next(&mut self) -> Option<Result<bytes::BytesMut, std::io::Error>> {
|
pub async fn next(&mut self) -> Option<Result<bytes::BytesMut, std::io::Error>> {
|
||||||
match self {
|
match self {
|
||||||
Self::WebSocket(ws) => ws.next().await,
|
Self::WebSocket(ws) => ws.next().await,
|
||||||
@ -93,6 +100,7 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn local_addr(&self) -> SocketAddr {
|
pub fn local_addr(&self) -> SocketAddr {
|
||||||
match self {
|
match self {
|
||||||
Self::WebSocket(ws) => ws.local_addr(),
|
Self::WebSocket(ws) => ws.local_addr(),
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::tcp::Encrypt;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Socks5Server, protobuf::Message, sodiumoxide::crypto::secretbox::Key, ResultType,
|
config::Socks5Server, protobuf::Message, sodiumoxide::crypto::secretbox::Key, tcp::Encrypt,
|
||||||
|
ResultType,
|
||||||
};
|
};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::{SinkExt, StreamExt};
|
use futures::{SinkExt, StreamExt};
|
||||||
@ -57,10 +57,12 @@ impl WsFramedStream {
|
|||||||
Ok(ws)
|
Ok(ws)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn set_raw(&mut self) {
|
pub fn set_raw(&mut self) {
|
||||||
self.encrypt = None;
|
self.encrypt = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub async fn from_tcp_stream(stream: TcpStream, addr: SocketAddr) -> ResultType<Self> {
|
pub async fn from_tcp_stream(stream: TcpStream, addr: SocketAddr) -> ResultType<Self> {
|
||||||
let ws_stream =
|
let ws_stream =
|
||||||
WebSocketStream::from_raw_socket(MaybeTlsStream::Plain(stream), Role::Client, None)
|
WebSocketStream::from_raw_socket(MaybeTlsStream::Plain(stream), Role::Client, None)
|
||||||
@ -74,18 +76,22 @@ impl WsFramedStream {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn local_addr(&self) -> SocketAddr {
|
pub fn local_addr(&self) -> SocketAddr {
|
||||||
self.addr
|
self.addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn set_send_timeout(&mut self, ms: u64) {
|
pub fn set_send_timeout(&mut self, ms: u64) {
|
||||||
self.send_timeout = ms;
|
self.send_timeout = ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn set_key(&mut self, key: Key) {
|
pub fn set_key(&mut self, key: Key) {
|
||||||
self.encrypt = Some(Encrypt::new(key));
|
self.encrypt = Some(Encrypt::new(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn is_secured(&self) -> bool {
|
pub fn is_secured(&self) -> bool {
|
||||||
self.encrypt.is_some()
|
self.encrypt.is_some()
|
||||||
}
|
}
|
||||||
@ -104,7 +110,6 @@ impl WsFramedStream {
|
|||||||
self.send_bytes(Bytes::from(msg)).await
|
self.send_bytes(Bytes::from(msg)).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub async fn send_bytes(&mut self, bytes: Bytes) -> ResultType<()> {
|
pub async fn send_bytes(&mut self, bytes: Bytes) -> ResultType<()> {
|
||||||
let msg = WsMessage::Binary(bytes);
|
let msg = WsMessage::Binary(bytes);
|
||||||
if self.send_timeout > 0 {
|
if self.send_timeout > 0 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user