Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 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 byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 19 | use std::cmp; |
| 20 | use std::io; |
| 21 | use std::io::{ErrorKind, Read, Write}; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 22 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 23 | use super::{TReadTransport, TReadTransportFactory, TWriteTransport, TWriteTransportFactory}; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 24 | |
| 25 | /// Default capacity of the read buffer in bytes. |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 26 | const READ_CAPACITY: usize = 4096; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 27 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 28 | /// Default capacity of the write buffer in bytes. |
| 29 | const WRITE_CAPACITY: usize = 4096; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 30 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 31 | /// Transport that reads framed messages. |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 32 | /// |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 33 | /// A `TFramedReadTransport` maintains a fixed-size internal read buffer. |
| 34 | /// On a call to `TFramedReadTransport::read(...)` one full message - both |
| 35 | /// fixed-length header and bytes - is read from the wrapped channel and |
| 36 | /// buffered. Subsequent read calls are serviced from the internal buffer |
| 37 | /// until it is exhausted, at which point the next full message is read |
| 38 | /// from the wrapped channel. |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 39 | /// |
| 40 | /// # Examples |
| 41 | /// |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 42 | /// Create and use a `TFramedReadTransport`. |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 43 | /// |
| 44 | /// ```no_run |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 45 | /// use std::io::Read; |
| 46 | /// use thrift::transport::{TFramedReadTransport, TTcpChannel}; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 47 | /// |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 48 | /// let mut c = TTcpChannel::new(); |
| 49 | /// c.open("localhost:9090").unwrap(); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 50 | /// |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 51 | /// let mut t = TFramedReadTransport::new(c); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 52 | /// |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 53 | /// t.read(&mut vec![0u8; 1]).unwrap(); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 54 | /// ``` |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 55 | #[derive(Debug)] |
| 56 | pub struct TFramedReadTransport<C> |
| 57 | where |
| 58 | C: Read, |
| 59 | { |
| 60 | buf: Box<[u8]>, |
| 61 | pos: usize, |
| 62 | cap: usize, |
| 63 | chan: C, |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 66 | impl<C> TFramedReadTransport<C> |
| 67 | where |
| 68 | C: Read, |
| 69 | { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 70 | /// Create a `TFramedTransport` with default-sized internal read and |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 71 | /// write buffers that wraps the given `TIoChannel`. |
| 72 | pub fn new(channel: C) -> TFramedReadTransport<C> { |
| 73 | TFramedReadTransport::with_capacity(READ_CAPACITY, channel) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /// Create a `TFramedTransport` with an internal read buffer of size |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 77 | /// `read_capacity` and an internal write buffer of size |
| 78 | /// `write_capacity` that wraps the given `TIoChannel`. |
| 79 | pub fn with_capacity(read_capacity: usize, channel: C) -> TFramedReadTransport<C> { |
| 80 | TFramedReadTransport { |
| 81 | buf: vec![0; read_capacity].into_boxed_slice(), |
| 82 | pos: 0, |
| 83 | cap: 0, |
| 84 | chan: channel, |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 89 | impl<C> Read for TFramedReadTransport<C> |
| 90 | where |
| 91 | C: Read, |
| 92 | { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 93 | fn read(&mut self, b: &mut [u8]) -> io::Result<usize> { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 94 | if self.cap - self.pos == 0 { |
| 95 | let message_size = self.chan.read_i32::<BigEndian>()? as usize; |
| 96 | if message_size > self.buf.len() { |
| 97 | return Err( |
| 98 | io::Error::new( |
| 99 | ErrorKind::Other, |
| 100 | format!( |
| 101 | "bytes to be read ({}) exceeds buffer \ |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 102 | capacity ({})", |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 103 | message_size, |
| 104 | self.buf.len() |
| 105 | ), |
| 106 | ), |
| 107 | ); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 108 | } |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 109 | self.chan.read_exact(&mut self.buf[..message_size])?; |
| 110 | self.pos = 0; |
| 111 | self.cap = message_size as usize; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 114 | let nread = cmp::min(b.len(), self.cap - self.pos); |
| 115 | b[..nread].clone_from_slice(&self.buf[self.pos..self.pos + nread]); |
| 116 | self.pos += nread; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 117 | |
| 118 | Ok(nread) |
| 119 | } |
| 120 | } |
| 121 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 122 | /// Factory for creating instances of `TFramedReadTransport`. |
| 123 | #[derive(Default)] |
| 124 | pub struct TFramedReadTransportFactory; |
| 125 | |
| 126 | impl TFramedReadTransportFactory { |
| 127 | pub fn new() -> TFramedReadTransportFactory { |
| 128 | TFramedReadTransportFactory {} |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | impl TReadTransportFactory for TFramedReadTransportFactory { |
| 133 | /// Create a `TFramedReadTransport`. |
| 134 | fn create(&self, channel: Box<Read + Send>) -> Box<TReadTransport + Send> { |
| 135 | Box::new(TFramedReadTransport::new(channel)) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /// Transport that writes framed messages. |
| 140 | /// |
| 141 | /// A `TFramedWriteTransport` maintains a fixed-size internal write buffer. All |
| 142 | /// writes are made to this buffer and are sent to the wrapped channel only |
| 143 | /// when `TFramedWriteTransport::flush()` is called. On a flush a fixed-length |
| 144 | /// header with a count of the buffered bytes is written, followed by the bytes |
| 145 | /// themselves. |
| 146 | /// |
| 147 | /// # Examples |
| 148 | /// |
| 149 | /// Create and use a `TFramedWriteTransport`. |
| 150 | /// |
| 151 | /// ```no_run |
| 152 | /// use std::io::Write; |
| 153 | /// use thrift::transport::{TFramedWriteTransport, TTcpChannel}; |
| 154 | /// |
| 155 | /// let mut c = TTcpChannel::new(); |
| 156 | /// c.open("localhost:9090").unwrap(); |
| 157 | /// |
| 158 | /// let mut t = TFramedWriteTransport::new(c); |
| 159 | /// |
| 160 | /// t.write(&[0x00]).unwrap(); |
| 161 | /// t.flush().unwrap(); |
| 162 | /// ``` |
| 163 | #[derive(Debug)] |
| 164 | pub struct TFramedWriteTransport<C> |
| 165 | where |
| 166 | C: Write, |
| 167 | { |
| 168 | buf: Box<[u8]>, |
| 169 | pos: usize, |
| 170 | channel: C, |
| 171 | } |
| 172 | |
| 173 | impl<C> TFramedWriteTransport<C> |
| 174 | where |
| 175 | C: Write, |
| 176 | { |
| 177 | /// Create a `TFramedTransport` with default-sized internal read and |
| 178 | /// write buffers that wraps the given `TIoChannel`. |
| 179 | pub fn new(channel: C) -> TFramedWriteTransport<C> { |
| 180 | TFramedWriteTransport::with_capacity(WRITE_CAPACITY, channel) |
| 181 | } |
| 182 | |
| 183 | /// Create a `TFramedTransport` with an internal read buffer of size |
| 184 | /// `read_capacity` and an internal write buffer of size |
| 185 | /// `write_capacity` that wraps the given `TIoChannel`. |
| 186 | pub fn with_capacity(write_capacity: usize, channel: C) -> TFramedWriteTransport<C> { |
| 187 | TFramedWriteTransport { |
| 188 | buf: vec![0; write_capacity].into_boxed_slice(), |
| 189 | pos: 0, |
| 190 | channel: channel, |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | impl<C> Write for TFramedWriteTransport<C> |
| 196 | where |
| 197 | C: Write, |
| 198 | { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 199 | fn write(&mut self, b: &[u8]) -> io::Result<usize> { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 200 | if b.len() > (self.buf.len() - self.pos) { |
| 201 | return Err( |
| 202 | io::Error::new( |
| 203 | ErrorKind::Other, |
| 204 | format!( |
| 205 | "bytes to be written ({}) exceeds buffer \ |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 206 | capacity ({})", |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 207 | b.len(), |
| 208 | self.buf.len() - self.pos |
| 209 | ), |
| 210 | ), |
| 211 | ); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | let nwrite = b.len(); // always less than available write buffer capacity |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 215 | self.buf[self.pos..(self.pos + nwrite)].clone_from_slice(b); |
| 216 | self.pos += nwrite; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 217 | Ok(nwrite) |
| 218 | } |
| 219 | |
| 220 | fn flush(&mut self) -> io::Result<()> { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 221 | let message_size = self.pos; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 222 | |
| 223 | if let 0 = message_size { |
| 224 | return Ok(()); |
| 225 | } else { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 226 | self.channel |
| 227 | .write_i32::<BigEndian>(message_size as i32)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | let mut byte_index = 0; |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 231 | while byte_index < self.pos { |
| 232 | let nwrite = self.channel.write(&self.buf[byte_index..self.pos])?; |
| 233 | byte_index = cmp::min(byte_index + nwrite, self.pos); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 236 | self.pos = 0; |
| 237 | self.channel.flush() |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 241 | /// Factory for creating instances of `TFramedWriteTransport`. |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 242 | #[derive(Default)] |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 243 | pub struct TFramedWriteTransportFactory; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 244 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 245 | impl TFramedWriteTransportFactory { |
| 246 | pub fn new() -> TFramedWriteTransportFactory { |
| 247 | TFramedWriteTransportFactory {} |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 251 | impl TWriteTransportFactory for TFramedWriteTransportFactory { |
| 252 | /// Create a `TFramedWriteTransport`. |
| 253 | fn create(&self, channel: Box<Write + Send>) -> Box<TWriteTransport + Send> { |
| 254 | Box::new(TFramedWriteTransport::new(channel)) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | #[cfg(test)] |
| 259 | mod tests { |
| 260 | // use std::io::{Read, Write}; |
| 261 | // |
| 262 | // use super::*; |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 263 | // use ::transport::mem::TBufferChannel; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 264 | } |