blob: 275bcd45982c029ad5349d704924f8c3e4e91623 [file] [log] [blame]
Allen George8b96bfb2016-11-02 08:01:08 -04001// 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
18use std::convert::From;
19use std::io;
20use std::io::{ErrorKind, Read, Write};
Marcin Pajkowski98ce2c82019-11-05 00:20:15 +010021use std::net::{Shutdown, TcpStream, ToSocketAddrs};
Allen George8b96bfb2016-11-02 08:01:08 -040022
Allen George0e22c362017-01-30 07:15:00 -050023use super::{ReadHalf, TIoChannel, WriteHalf};
Allen Georgeb0d14132020-03-29 11:48:55 -040024use crate::{new_transport_error, TransportErrorKind};
Allen George8b96bfb2016-11-02 08:01:08 -040025
Allen George0e22c362017-01-30 07:15:00 -050026/// Bidirectional TCP/IP channel.
Allen George8b96bfb2016-11-02 08:01:08 -040027///
28/// # Examples
29///
Allen George0e22c362017-01-30 07:15:00 -050030/// Create a `TTcpChannel`.
Allen George8b96bfb2016-11-02 08:01:08 -040031///
32/// ```no_run
33/// use std::io::{Read, Write};
Allen George0e22c362017-01-30 07:15:00 -050034/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -040035///
Allen George0e22c362017-01-30 07:15:00 -050036/// let mut c = TTcpChannel::new();
37/// c.open("localhost:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -040038///
39/// let mut buf = vec![0u8; 4];
Allen George0e22c362017-01-30 07:15:00 -050040/// c.read(&mut buf).unwrap();
41/// c.write(&vec![0, 1, 2]).unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -040042/// ```
43///
Allen George0e22c362017-01-30 07:15:00 -050044/// Create a `TTcpChannel` by wrapping an existing `TcpStream`.
Allen George8b96bfb2016-11-02 08:01:08 -040045///
46/// ```no_run
47/// use std::io::{Read, Write};
48/// use std::net::TcpStream;
Allen George0e22c362017-01-30 07:15:00 -050049/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -040050///
51/// let stream = TcpStream::connect("127.0.0.1:9189").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -040052///
Allen George0e22c362017-01-30 07:15:00 -050053/// // no need to call c.open() since we've already connected above
54/// let mut c = TTcpChannel::with_stream(stream);
Allen George8b96bfb2016-11-02 08:01:08 -040055///
56/// let mut buf = vec![0u8; 4];
Allen George0e22c362017-01-30 07:15:00 -050057/// c.read(&mut buf).unwrap();
58/// c.write(&vec![0, 1, 2]).unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -040059/// ```
Allen George0e22c362017-01-30 07:15:00 -050060#[derive(Debug, Default)]
61pub struct TTcpChannel {
Allen George8b96bfb2016-11-02 08:01:08 -040062 stream: Option<TcpStream>,
63}
64
Allen George0e22c362017-01-30 07:15:00 -050065impl TTcpChannel {
66 /// Create an uninitialized `TTcpChannel`.
Allen George8b96bfb2016-11-02 08:01:08 -040067 ///
Allen George0e22c362017-01-30 07:15:00 -050068 /// The returned instance must be opened using `TTcpChannel::open(...)`
Allen George8b96bfb2016-11-02 08:01:08 -040069 /// before it can be used.
Allen George0e22c362017-01-30 07:15:00 -050070 pub fn new() -> TTcpChannel {
71 TTcpChannel { stream: None }
Allen George8b96bfb2016-11-02 08:01:08 -040072 }
73
Allen George0e22c362017-01-30 07:15:00 -050074 /// Create a `TTcpChannel` that wraps an existing `TcpStream`.
Allen George8b96bfb2016-11-02 08:01:08 -040075 ///
76 /// The passed-in stream is assumed to have been opened before being wrapped
Allen George0e22c362017-01-30 07:15:00 -050077 /// by the created `TTcpChannel` instance.
78 pub fn with_stream(stream: TcpStream) -> TTcpChannel {
Allen Georgeef7a1892018-12-16 18:01:37 -050079 TTcpChannel {
80 stream: Some(stream),
81 }
Allen George8b96bfb2016-11-02 08:01:08 -040082 }
83
Marcin Pajkowski98ce2c82019-11-05 00:20:15 +010084 /// Connect to `remote_address`, which should implement `ToSocketAddrs` trait.
Allen Georgeb0d14132020-03-29 11:48:55 -040085 pub fn open<A: ToSocketAddrs>(&mut self, remote_address: A) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -040086 if self.stream.is_some() {
Allen Georgeef7a1892018-12-16 18:01:37 -050087 Err(new_transport_error(
88 TransportErrorKind::AlreadyOpen,
89 "tcp connection previously opened",
90 ))
Allen George8b96bfb2016-11-02 08:01:08 -040091 } else {
92 match TcpStream::connect(&remote_address) {
93 Ok(s) => {
94 self.stream = Some(s);
95 Ok(())
96 }
97 Err(e) => Err(From::from(e)),
98 }
99 }
100 }
101
Allen George0e22c362017-01-30 07:15:00 -0500102 /// Shut down this channel.
Allen George8b96bfb2016-11-02 08:01:08 -0400103 ///
104 /// Both send and receive halves are closed, and this instance can no
105 /// longer be used to communicate with another endpoint.
Allen Georgeb0d14132020-03-29 11:48:55 -0400106 pub fn close(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500107 self.if_set(|s| s.shutdown(Shutdown::Both))
108 .map_err(From::from)
Allen George8b96bfb2016-11-02 08:01:08 -0400109 }
110
111 fn if_set<F, T>(&mut self, mut stream_operation: F) -> io::Result<T>
Allen George0e22c362017-01-30 07:15:00 -0500112 where
113 F: FnMut(&mut TcpStream) -> io::Result<T>,
Allen George8b96bfb2016-11-02 08:01:08 -0400114 {
Allen George8b96bfb2016-11-02 08:01:08 -0400115 if let Some(ref mut s) = self.stream {
116 stream_operation(s)
117 } else {
Allen Georgeef7a1892018-12-16 18:01:37 -0500118 Err(io::Error::new(
119 ErrorKind::NotConnected,
120 "tcp endpoint not connected",
121 ))
Allen George8b96bfb2016-11-02 08:01:08 -0400122 }
123 }
124}
125
Allen George0e22c362017-01-30 07:15:00 -0500126impl TIoChannel for TTcpChannel {
Allen Georgeb0d14132020-03-29 11:48:55 -0400127 fn split(self) -> crate::Result<(ReadHalf<Self>, WriteHalf<Self>)>
Allen George0e22c362017-01-30 07:15:00 -0500128 where
129 Self: Sized,
130 {
131 let mut s = self;
132
133 s.stream
134 .as_mut()
135 .and_then(|s| s.try_clone().ok())
Allen Georgeef7a1892018-12-16 18:01:37 -0500136 .map(|cloned| {
137 let read_half = ReadHalf::new(TTcpChannel {
138 stream: s.stream.take(),
139 });
140 let write_half = WriteHalf::new(TTcpChannel {
141 stream: Some(cloned),
142 });
143 (read_half, write_half)
144 })
145 .ok_or_else(|| {
146 new_transport_error(
147 TransportErrorKind::Unknown,
148 "cannot clone underlying tcp stream",
149 )
150 })
Allen George0e22c362017-01-30 07:15:00 -0500151 }
152}
153
154impl Read for TTcpChannel {
Allen George8b96bfb2016-11-02 08:01:08 -0400155 fn read(&mut self, b: &mut [u8]) -> io::Result<usize> {
156 self.if_set(|s| s.read(b))
157 }
158}
159
Allen George0e22c362017-01-30 07:15:00 -0500160impl Write for TTcpChannel {
Allen George8b96bfb2016-11-02 08:01:08 -0400161 fn write(&mut self, b: &[u8]) -> io::Result<usize> {
Allen Georgecf7ba4c2017-12-11 11:44:11 -0500162 self.if_set(|s| s.write(b))
Allen George8b96bfb2016-11-02 08:01:08 -0400163 }
164
165 fn flush(&mut self) -> io::Result<()> {
166 self.if_set(|s| s.flush())
167 }
168}