1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 use core::pin::Pin; 15 use core::task::{Context, Poll}; 16 17 #[cfg(feature = "http3")] 18 use ylong_runtime::net::ConnectedUdpSocket; 19 20 use crate::async_impl::ssl_stream::AsyncSslStream; 21 use crate::runtime::{AsyncRead, AsyncWrite, ReadBuf, TcpStream}; 22 23 /// A stream which may be wrapped with TLS. 24 pub enum MixStream { 25 /// A raw HTTP stream. 26 Http(TcpStream), 27 /// An SSL-wrapped HTTP stream. 28 Https(AsyncSslStream<TcpStream>), 29 #[cfg(feature = "http3")] 30 /// A Udp connection 31 Udp(ConnectedUdpSocket), 32 } 33 34 impl AsyncRead for MixStream { 35 // poll_read separately. poll_readnull36 fn poll_read( 37 mut self: Pin<&mut Self>, 38 cx: &mut Context<'_>, 39 buf: &mut ReadBuf<'_>, 40 ) -> Poll<std::io::Result<()>> { 41 match &mut *self { 42 MixStream::Http(s) => Pin::new(s).poll_read(cx, buf), 43 MixStream::Https(s) => Pin::new(s).poll_read(cx, buf), 44 #[cfg(feature = "http3")] 45 MixStream::Udp(s) => Pin::new(s).poll_recv(cx, buf), 46 } 47 } 48 } 49 50 impl AsyncWrite for MixStream { 51 // poll_write separately. poll_writenull52 fn poll_write( 53 mut self: Pin<&mut Self>, 54 ctx: &mut Context<'_>, 55 buf: &[u8], 56 ) -> Poll<std::io::Result<usize>> { 57 match &mut *self { 58 MixStream::Http(s) => Pin::new(s).poll_write(ctx, buf), 59 MixStream::Https(s) => Pin::new(s).poll_write(ctx, buf), 60 #[cfg(feature = "http3")] 61 MixStream::Udp(s) => Pin::new(s).poll_send(ctx, buf), 62 } 63 } 64 poll_flushnull65 fn poll_flush(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<std::io::Result<()>> { 66 match &mut *self { 67 MixStream::Http(s) => Pin::new(s).poll_flush(ctx), 68 MixStream::Https(s) => Pin::new(s).poll_flush(ctx), 69 #[cfg(feature = "http3")] 70 MixStream::Udp(_) => Poll::Ready(Ok(())), 71 } 72 } 73 poll_shutdownnull74 fn poll_shutdown(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<std::io::Result<()>> { 75 match &mut *self { 76 MixStream::Http(s) => Pin::new(s).poll_shutdown(ctx), 77 MixStream::Https(s) => Pin::new(s).poll_shutdown(ctx), 78 #[cfg(feature = "http3")] 79 MixStream::Udp(_) => Poll::Ready(Ok(())), 80 } 81 } 82 } 83