/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2023 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
use std::{
net::{TcpStream, ToSocketAddrs},
pin::Pin,
task::{Context, Poll},
};
use async_tungstenite::{
tungstenite::{handshake::client::Response, Message},
WebSocketStream,
};
use futures::sink::Sink;
use futures_rustls::{client::TlsStream, rustls::ServerName, TlsConnector};
use smol::{prelude::*, Async};
use url::Url;
use crate::{Error, Result as DrkResult};
#[allow(clippy::large_enum_variant)]
pub enum WsStream {
Tcp(WebSocketStream>),
Tls(WebSocketStream>>),
}
impl Sink for WsStream {
type Error = async_tungstenite::tungstenite::Error;
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> {
match &mut *self {
WsStream::Tcp(s) => Pin::new(s).poll_ready(cx),
WsStream::Tls(s) => Pin::new(s).poll_ready(cx),
}
}
fn start_send(mut self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> {
match &mut *self {
WsStream::Tcp(s) => Pin::new(s).start_send(item),
WsStream::Tls(s) => Pin::new(s).start_send(item),
}
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> {
match &mut *self {
WsStream::Tcp(s) => Pin::new(s).poll_flush(cx),
WsStream::Tls(s) => Pin::new(s).poll_flush(cx),
}
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> {
match &mut *self {
WsStream::Tcp(s) => Pin::new(s).poll_close(cx),
WsStream::Tls(s) => Pin::new(s).poll_close(cx),
}
}
}
impl Stream for WsStream {
type Item = async_tungstenite::tungstenite::Result;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll