diff options
author | hozan23 <hozan23@proton.me> | 2023-11-30 23:51:18 +0300 |
---|---|---|
committer | hozan23 <hozan23@proton.me> | 2023-11-30 23:51:18 +0300 |
commit | 5111a3d5749625c3d8e26a24a5a32c4da58f18d3 (patch) | |
tree | 0d8376af78d0ec09f3464ba4370ebb8cdd43826d | |
parent | d8425015363cc0ac4742f938dc3f8e9d0beaa933 (diff) |
net: Use ToConn and ToListener traits for objects that can be converted
to Conn and Listener.
-rw-r--r-- | jsonrpc/README.md | 4 | ||||
-rw-r--r-- | jsonrpc/examples/client.rs | 2 | ||||
-rw-r--r-- | jsonrpc/examples/server.rs | 2 | ||||
-rw-r--r-- | jsonrpc/src/client.rs | 6 | ||||
-rw-r--r-- | jsonrpc/src/lib.rs | 4 | ||||
-rw-r--r-- | jsonrpc/src/server.rs | 6 | ||||
-rw-r--r-- | net/src/connection.rs | 5 | ||||
-rw-r--r-- | net/src/lib.rs | 4 | ||||
-rw-r--r-- | net/src/listener.rs | 5 | ||||
-rw-r--r-- | net/src/transports/tcp.rs | 22 | ||||
-rw-r--r-- | net/src/transports/tls.rs | 22 | ||||
-rw-r--r-- | net/src/transports/udp.rs | 14 | ||||
-rw-r--r-- | net/src/transports/unix.rs | 25 |
13 files changed, 101 insertions, 20 deletions
diff --git a/jsonrpc/README.md b/jsonrpc/README.md index 929d645..f7ee641 100644 --- a/jsonrpc/README.md +++ b/jsonrpc/README.md @@ -30,7 +30,7 @@ let ex = Arc::new(smol::Executor::new()); // Creates a new server let listener = TcpListener::bind("127.0.0.1:60000").await.unwrap(); let config = ServerConfig::default(); -let server = Server::new(listener.into(), config, ex.clone()); +let server = Server::new(listener, config, ex.clone()); // Register the HelloWorld service register_service!(HelloWorld, say_hello); @@ -45,7 +45,7 @@ ex.run(server.start()); // Creates a new client let conn = TcpStream::connect("127.0.0.1:60000").await.unwrap(); let config = ClientConfig::default(); -let client = Client::new(conn.into(), config); +let client = Client::new(conn, config); let result: String = client.call("HelloWorld.say_hello", "world".to_string()).await.unwrap(); diff --git a/jsonrpc/examples/client.rs b/jsonrpc/examples/client.rs index 8f46a8e..b28760d 100644 --- a/jsonrpc/examples/client.rs +++ b/jsonrpc/examples/client.rs @@ -17,7 +17,7 @@ fn main() { smol::future::block_on(async { let conn = TcpStream::connect("127.0.0.1:60000").await.unwrap(); let config = ClientConfig::default(); - let client = Client::new(conn.into(), config); + let client = Client::new(conn, config); let params = Req { x: 10, y: 7 }; let result: u32 = client.call("Calc.add", params).await.unwrap(); diff --git a/jsonrpc/examples/server.rs b/jsonrpc/examples/server.rs index 4109e0d..b09232f 100644 --- a/jsonrpc/examples/server.rs +++ b/jsonrpc/examples/server.rs @@ -46,7 +46,7 @@ fn main() { // Creates a new server let listener = TcpListener::bind("127.0.0.1:60000").await.unwrap(); let config = ServerConfig::default(); - let server = Server::new(listener.into(), config, ex); + let server = Server::new(listener, config, ex); // Register the Calc service register_service!(Calc, ping, add, sub, version); diff --git a/jsonrpc/src/client.rs b/jsonrpc/src/client.rs index 0061002..69c2e6d 100644 --- a/jsonrpc/src/client.rs +++ b/jsonrpc/src/client.rs @@ -2,7 +2,7 @@ use log::debug; use serde::{de::DeserializeOwned, Serialize}; use karyons_core::util::random_32; -use karyons_net::Conn; +use karyons_net::ToConn; use crate::{ codec::{Codec, CodecConfig}, @@ -23,12 +23,12 @@ pub struct Client { impl Client { /// Creates a new RPC client by passing a Tcp, Unix, or Tls connection. - pub fn new(conn: Conn, config: ClientConfig) -> Self { + pub fn new<C: ToConn>(conn: C, config: ClientConfig) -> Self { let codec_config = CodecConfig { max_allowed_buffer_size: 0, ..Default::default() }; - let codec = Codec::new(conn, codec_config); + let codec = Codec::new(conn.to_conn(), codec_config); Self { codec, config } } diff --git a/jsonrpc/src/lib.rs b/jsonrpc/src/lib.rs index 65fb38f..da9047d 100644 --- a/jsonrpc/src/lib.rs +++ b/jsonrpc/src/lib.rs @@ -27,7 +27,7 @@ //! // Creates a new server //! let listener = TcpListener::bind("127.0.0.1:60000").await.unwrap(); //! let config = ServerConfig::default(); -//! let server = Server::new(listener.into(), config, ex.clone()); +//! let server = Server::new(listener, config, ex.clone()); //! //! // Register the HelloWorld service //! register_service!(HelloWorld, say_hello); @@ -43,7 +43,7 @@ //! // Creates a new client //! let conn = TcpStream::connect("127.0.0.1:60000").await.unwrap(); //! let config = ClientConfig::default(); -//! let client = Client::new(conn.into(), config); +//! let client = Client::new(conn, config); //! //! let result: String = client.call("HelloWorld.say_hello", "world".to_string()).await.unwrap(); //! }; diff --git a/jsonrpc/src/server.rs b/jsonrpc/src/server.rs index 44de336..4f1bada 100644 --- a/jsonrpc/src/server.rs +++ b/jsonrpc/src/server.rs @@ -8,7 +8,7 @@ use karyons_core::{ Executor, }; -use karyons_net::{Conn, Listener}; +use karyons_net::{Conn, Listener, ToListener}; use crate::{ codec::{Codec, CodecConfig}, @@ -33,9 +33,9 @@ pub struct Server<'a> { impl<'a> Server<'a> { /// Creates a new RPC server by passing a listener. It supports Tcp, Unix, and Tls. - pub fn new(listener: Listener, config: ServerConfig, ex: Executor<'a>) -> Arc<Self> { + pub fn new<T: ToListener>(listener: T, config: ServerConfig, ex: Executor<'a>) -> Arc<Self> { Arc::new(Self { - listener, + listener: listener.to_listener(), services: RwLock::new(HashMap::new()), task_group: TaskGroup::new(ex), config, diff --git a/net/src/connection.rs b/net/src/connection.rs index b1d7550..3c047a0 100644 --- a/net/src/connection.rs +++ b/net/src/connection.rs @@ -8,6 +8,11 @@ use crate::{ /// Alias for `Box<dyn Connection>` pub type Conn = Box<dyn Connection>; +/// A trait for objects which can be converted to [`Conn`]. +pub trait ToConn { + fn to_conn(self) -> Conn; +} + /// Connection is a generic network connection interface for /// [`udp::UdpConn`], [`tcp::TcpConn`], and [`unix::UnixConn`]. /// diff --git a/net/src/lib.rs b/net/src/lib.rs index 813a090..ff466af 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -5,9 +5,9 @@ mod listener; mod transports; pub use { - connection::{dial, Conn, Connection}, + connection::{dial, Conn, Connection, ToConn}, endpoint::{Addr, Endpoint, Port}, - listener::{listen, ConnListener, Listener}, + listener::{listen, ConnListener, Listener, ToListener}, transports::{ tcp::{dial_tcp, listen_tcp, TcpConn}, tls, diff --git a/net/src/listener.rs b/net/src/listener.rs index fcd1175..cab5330 100644 --- a/net/src/listener.rs +++ b/net/src/listener.rs @@ -8,6 +8,11 @@ use crate::{ /// Alias for `Box<dyn ConnListener>` pub type Listener = Box<dyn ConnListener>; +/// A trait for objects which can be converted to [`Listener`]. +pub trait ToListener { + fn to_listener(self) -> Listener; +} + /// ConnListener is a generic network listener. #[async_trait] pub trait ConnListener: Send + Sync { diff --git a/net/src/transports/tcp.rs b/net/src/transports/tcp.rs index 7cd7127..99243b5 100644 --- a/net/src/transports/tcp.rs +++ b/net/src/transports/tcp.rs @@ -7,9 +7,9 @@ use smol::{ }; use crate::{ - connection::Connection, + connection::{Connection, ToConn}, endpoint::{Addr, Endpoint, Port}, - listener::ConnListener, + listener::{ConnListener, ToListener}, Error, Result, }; @@ -95,3 +95,21 @@ impl From<TcpListener> for Box<dyn ConnListener> { Box::new(listener) } } + +impl ToConn for TcpStream { + fn to_conn(self) -> Box<dyn Connection> { + self.into() + } +} + +impl ToConn for TcpConn { + fn to_conn(self) -> Box<dyn Connection> { + Box::new(self) + } +} + +impl ToListener for TcpListener { + fn to_listener(self) -> Box<dyn ConnListener> { + self.into() + } +} diff --git a/net/src/transports/tls.rs b/net/src/transports/tls.rs index bc928b0..8a43d7d 100644 --- a/net/src/transports/tls.rs +++ b/net/src/transports/tls.rs @@ -9,9 +9,9 @@ use smol::{ }; use crate::{ - connection::Connection, + connection::{Connection, ToConn}, endpoint::{Addr, Endpoint, Port}, - listener::ConnListener, + listener::{ConnListener, ToListener}, Error, Result, }; @@ -150,3 +150,21 @@ impl From<TlsListener> for Box<dyn ConnListener> { Box::new(listener) } } + +impl ToConn for TlsStream<TcpStream> { + fn to_conn(self) -> Box<dyn Connection> { + self.into() + } +} + +impl ToConn for TlsConn { + fn to_conn(self) -> Box<dyn Connection> { + Box::new(self) + } +} + +impl ToListener for TlsListener { + fn to_listener(self) -> Box<dyn ConnListener> { + self.into() + } +} diff --git a/net/src/transports/udp.rs b/net/src/transports/udp.rs index 9576876..a8b505c 100644 --- a/net/src/transports/udp.rs +++ b/net/src/transports/udp.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use smol::net::UdpSocket; use crate::{ - connection::Connection, + connection::{Connection, ToConn}, endpoint::{Addr, Endpoint, Port}, Error, Result, }; @@ -79,3 +79,15 @@ impl From<UdpSocket> for Box<dyn Connection> { Box::new(UdpConn::new(conn)) } } + +impl ToConn for UdpSocket { + fn to_conn(self) -> Box<dyn Connection> { + self.into() + } +} + +impl ToConn for UdpConn { + fn to_conn(self) -> Box<dyn Connection> { + Box::new(self) + } +} diff --git a/net/src/transports/unix.rs b/net/src/transports/unix.rs index c546333..3867040 100644 --- a/net/src/transports/unix.rs +++ b/net/src/transports/unix.rs @@ -6,7 +6,12 @@ use smol::{ net::unix::{UnixListener, UnixStream}, }; -use crate::{connection::Connection, endpoint::Endpoint, listener::ConnListener, Error, Result}; +use crate::{ + connection::{Connection, ToConn}, + endpoint::Endpoint, + listener::{ConnListener, ToListener}, + Error, Result, +}; /// Unix domain socket implementation of the [`Connection`] trait. pub struct UnixConn { @@ -86,3 +91,21 @@ impl From<UnixListener> for Box<dyn ConnListener> { Box::new(listener) } } + +impl ToConn for UnixStream { + fn to_conn(self) -> Box<dyn Connection> { + self.into() + } +} + +impl ToConn for UnixConn { + fn to_conn(self) -> Box<dyn Connection> { + Box::new(self) + } +} + +impl ToListener for UnixListener { + fn to_listener(self) -> Box<dyn ConnListener> { + self.into() + } +} |