|
1 |
| -use futures::{BoxFuture, Future, IntoFuture, Async, Sink, Stream as FuturesStream}; |
| 1 | +use bytes::{BytesMut, BufMut}; |
| 2 | +use futures::{BoxFuture, Future, IntoFuture, Sink, Stream as FuturesStream, Poll}; |
2 | 3 | use futures::future::Either;
|
3 | 4 | use postgres_shared::params::Host;
|
4 | 5 | use postgres_protocol::message::backend::{self, ParseResult};
|
5 | 6 | use postgres_protocol::message::frontend;
|
6 | 7 | use std::io::{self, Read, Write};
|
7 |
| -use tokio_core::io::{Io, Codec, EasyBuf, Framed}; |
| 8 | +use tokio_io::{AsyncRead, AsyncWrite}; |
| 9 | +use tokio_io::codec::{Encoder, Decoder, Framed}; |
8 | 10 | use tokio_core::net::TcpStream;
|
9 | 11 | use tokio_core::reactor::Handle;
|
10 | 12 | use tokio_dns;
|
@@ -132,65 +134,85 @@ impl Write for Stream {
|
132 | 134 | }
|
133 | 135 | }
|
134 | 136 |
|
135 |
| -impl Io for Stream { |
136 |
| - fn poll_read(&mut self) -> Async<()> { |
| 137 | +impl AsyncRead for Stream { |
| 138 | + unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool { |
137 | 139 | match self.0 {
|
138 |
| - InnerStream::Tcp(ref mut s) => s.poll_read(), |
| 140 | + InnerStream::Tcp(ref s) => s.prepare_uninitialized_buffer(buf), |
139 | 141 | #[cfg(unix)]
|
140 |
| - InnerStream::Unix(ref mut s) => s.poll_read(), |
| 142 | + InnerStream::Unix(ref s) => s.prepare_uninitialized_buffer(buf), |
141 | 143 | }
|
142 | 144 | }
|
143 | 145 |
|
144 |
| - fn poll_write(&mut self) -> Async<()> { |
| 146 | + fn read_buf<B>(&mut self, buf: &mut B) -> Poll<usize, io::Error> |
| 147 | + where B: BufMut |
| 148 | + { |
145 | 149 | match self.0 {
|
146 |
| - InnerStream::Tcp(ref mut s) => s.poll_write(), |
| 150 | + InnerStream::Tcp(ref mut s) => s.read_buf(buf), |
147 | 151 | #[cfg(unix)]
|
148 |
| - InnerStream::Unix(ref mut s) => s.poll_write(), |
| 152 | + InnerStream::Unix(ref mut s) => s.read_buf(buf), |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +impl AsyncWrite for Stream { |
| 158 | + fn shutdown(&mut self) -> Poll<(), io::Error> { |
| 159 | + match self.0 { |
| 160 | + InnerStream::Tcp(ref mut s) => s.shutdown(), |
| 161 | + #[cfg(unix)] |
| 162 | + InnerStream::Unix(ref mut s) => s.shutdown(), |
149 | 163 | }
|
150 | 164 | }
|
151 | 165 | }
|
152 | 166 |
|
153 | 167 | pub struct PostgresCodec;
|
154 | 168 |
|
155 |
| -impl Codec for PostgresCodec { |
156 |
| - type In = backend::Message<Vec<u8>>; |
157 |
| - type Out = Vec<u8>; |
| 169 | +impl Decoder for PostgresCodec { |
| 170 | + type Item = backend::Message<Vec<u8>>; |
| 171 | + type Error = io::Error; |
158 | 172 |
|
159 | 173 | // FIXME ideally we'd avoid re-copying the data
|
160 |
| - fn decode(&mut self, buf: &mut EasyBuf) -> io::Result<Option<Self::In>> { |
| 174 | + fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<Self::Item>> { |
161 | 175 | match backend::Message::parse_owned(buf.as_ref())? {
|
162 | 176 | ParseResult::Complete { message, consumed } => {
|
163 |
| - buf.drain_to(consumed); |
| 177 | + buf.split_to(consumed); |
164 | 178 | Ok(Some(message))
|
165 | 179 | }
|
166 | 180 | ParseResult::Incomplete { .. } => Ok(None),
|
167 | 181 | }
|
168 | 182 | }
|
| 183 | +} |
| 184 | + |
| 185 | +impl Encoder for PostgresCodec { |
| 186 | + type Item = Vec<u8>; |
| 187 | + type Error = io::Error; |
169 | 188 |
|
170 |
| - fn encode(&mut self, msg: Vec<u8>, buf: &mut Vec<u8>) -> io::Result<()> { |
171 |
| - buf.extend_from_slice(&msg); |
| 189 | + fn encode(&mut self, msg: Vec<u8>, buf: &mut BytesMut) -> io::Result<()> { |
| 190 | + buf.extend(&msg); |
172 | 191 | Ok(())
|
173 | 192 | }
|
174 | 193 | }
|
175 | 194 |
|
176 | 195 | struct SslCodec;
|
177 | 196 |
|
178 |
| -impl Codec for SslCodec { |
179 |
| - type In = u8; |
180 |
| - type Out = Vec<u8>; |
| 197 | +impl Decoder for SslCodec { |
| 198 | + type Item = u8; |
| 199 | + type Error = io::Error; |
181 | 200 |
|
182 |
| - fn decode(&mut self, buf: &mut EasyBuf) -> io::Result<Option<u8>> { |
183 |
| - if buf.as_slice().is_empty() { |
| 201 | + fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<u8>> { |
| 202 | + if buf.is_empty() { |
184 | 203 | Ok(None)
|
185 | 204 | } else {
|
186 |
| - let byte = buf.as_slice()[0]; |
187 |
| - buf.drain_to(1); |
188 |
| - Ok(Some(byte)) |
| 205 | + Ok(Some(buf.split_to(1)[0])) |
189 | 206 | }
|
190 | 207 | }
|
| 208 | +} |
| 209 | + |
| 210 | +impl Encoder for SslCodec { |
| 211 | + type Item = Vec<u8>; |
| 212 | + type Error = io::Error; |
191 | 213 |
|
192 |
| - fn encode(&mut self, msg: Vec<u8>, buf: &mut Vec<u8>) -> io::Result<()> { |
193 |
| - buf.extend_from_slice(&msg); |
| 214 | + fn encode(&mut self, msg: Vec<u8>, buf: &mut BytesMut) -> io::Result<()> { |
| 215 | + buf.extend(&msg); |
194 | 216 | Ok(())
|
195 | 217 | }
|
196 | 218 | }
|
0 commit comments