mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-02-23 15:49:54 +01:00
Add TLS support (#81)
When `-- features tls` is specified for tarpc, RPC communication can also occur over a `TlsStream<TcpStream>` instead of a `TcpStream`. * The functional tests have been refactored to use a common set of functions for constructing the client and server structs so that all the tests are shared across non-tls and tls test runs. * Update pre-push to test TLS * The `cfg_attr` logic caused many false warnings from clippy, so for now the crate docs for TLS are not tested.
This commit is contained in:
55
src/stream_type.rs
Normal file
55
src/stream_type.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use std::io;
|
||||
use tokio_core::io::Io;
|
||||
use tokio_core::net::TcpStream;
|
||||
#[cfg(feature = "tls")]
|
||||
use tokio_tls::TlsStream;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum StreamType {
|
||||
Tcp(TcpStream),
|
||||
#[cfg(feature = "tls")]
|
||||
Tls(TlsStream<TcpStream>),
|
||||
}
|
||||
|
||||
impl From<TcpStream> for StreamType {
|
||||
fn from(stream: TcpStream) -> Self {
|
||||
StreamType::Tcp(stream)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "tls")]
|
||||
impl From<TlsStream<TcpStream>> for StreamType {
|
||||
fn from(stream: TlsStream<TcpStream>) -> Self {
|
||||
StreamType::Tls(stream)
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Read for StreamType {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
match *self {
|
||||
StreamType::Tcp(ref mut stream) => stream.read(buf),
|
||||
#[cfg(feature = "tls")]
|
||||
StreamType::Tls(ref mut stream) => stream.read(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Write for StreamType {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
match *self {
|
||||
StreamType::Tcp(ref mut stream) => stream.write(buf),
|
||||
#[cfg(feature = "tls")]
|
||||
StreamType::Tls(ref mut stream) => stream.write(buf),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
match *self {
|
||||
StreamType::Tcp(ref mut stream) => stream.flush(),
|
||||
#[cfg(feature = "tls")]
|
||||
StreamType::Tls(ref mut stream) => stream.flush(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Io for StreamType {}
|
||||
Reference in New Issue
Block a user