|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::io::{self, Read, Write}; |
| 19 | +use std::sync::{Arc, Mutex, MutexGuard}; |
| 20 | + |
| 21 | +use super::{ReadHalf, TIoChannel, WriteHalf}; |
| 22 | + |
| 23 | +/// A cloneable channel that serializes access to an underlying I/O stream. |
| 24 | +/// |
| 25 | +/// This adapter allows a bidirectional stream that cannot be cloned, such as a |
| 26 | +/// TLS session, to implement [`TIoChannel`]. Every read, write, and flush holds |
| 27 | +/// the same lock for the duration of that operation. It is intended for |
| 28 | +/// synchronous request-response traffic, where a caller writes and flushes a |
| 29 | +/// complete request before reading its response. |
| 30 | +/// |
| 31 | +/// The shared lock makes access memory-safe across threads, but it does not |
| 32 | +/// provide full-duplex progress: a blocking read holds the lock and prevents a |
| 33 | +/// concurrent write until that read finishes. |
| 34 | +#[derive(Debug)] |
| 35 | +pub struct TSharedChannel<C> { |
| 36 | + inner: Arc<Mutex<C>>, |
| 37 | +} |
| 38 | + |
| 39 | +impl<C> TSharedChannel<C> { |
| 40 | + /// Wrap `inner` in a shared channel. |
| 41 | + pub fn new(inner: C) -> Self { |
| 42 | + Self { |
| 43 | + inner: Arc::new(Mutex::new(inner)), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // io::Error::other requires Rust 1.74. |
| 48 | + #[allow(unknown_lints)] |
| 49 | + #[allow(clippy::io_other_error)] |
| 50 | + pub(crate) fn lock(&self) -> io::Result<MutexGuard<'_, C>> { |
| 51 | + self.inner |
| 52 | + .lock() |
| 53 | + .map_err(|_| io::Error::new(io::ErrorKind::Other, "shared channel lock is poisoned")) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl<C> Clone for TSharedChannel<C> { |
| 58 | + fn clone(&self) -> Self { |
| 59 | + Self { |
| 60 | + inner: Arc::clone(&self.inner), |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<C: Read> Read for TSharedChannel<C> { |
| 66 | + fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> { |
| 67 | + self.lock()?.read(buffer) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<C: Write> Write for TSharedChannel<C> { |
| 72 | + fn write(&mut self, buffer: &[u8]) -> io::Result<usize> { |
| 73 | + self.lock()?.write(buffer) |
| 74 | + } |
| 75 | + |
| 76 | + fn flush(&mut self) -> io::Result<()> { |
| 77 | + self.lock()?.flush() |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<C: Read + Write> TIoChannel for TSharedChannel<C> { |
| 82 | + fn split(self) -> crate::Result<(ReadHalf<Self>, WriteHalf<Self>)> |
| 83 | + where |
| 84 | + Self: Sized, |
| 85 | + { |
| 86 | + Ok((ReadHalf::new(self.clone()), WriteHalf::new(self))) |
| 87 | + } |
| 88 | +} |
0 commit comments