2017-04-23 22:39:07 +00:00
|
|
|
extern crate bytes;
|
2016-12-21 03:50:44 +00:00
|
|
|
extern crate fallible_iterator;
|
2018-06-17 04:29:27 +00:00
|
|
|
extern crate futures_cpupool;
|
2016-12-20 23:42:28 +00:00
|
|
|
extern crate postgres_protocol;
|
2018-01-10 05:15:35 +00:00
|
|
|
extern crate postgres_shared;
|
2018-06-17 04:29:27 +00:00
|
|
|
extern crate tokio_codec;
|
2017-04-23 22:39:07 +00:00
|
|
|
extern crate tokio_io;
|
2018-06-17 04:29:27 +00:00
|
|
|
extern crate tokio_tcp;
|
|
|
|
extern crate tokio_timer;
|
2017-02-24 03:48:20 +00:00
|
|
|
|
2017-03-03 06:27:12 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate futures;
|
2018-06-17 04:29:27 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate state_machine_future;
|
2017-03-03 06:27:12 +00:00
|
|
|
|
2017-02-24 03:48:20 +00:00
|
|
|
#[cfg(unix)]
|
2016-12-20 23:42:28 +00:00
|
|
|
extern crate tokio_uds;
|
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
use futures::sync::mpsc;
|
|
|
|
use futures::{Async, Future, Poll};
|
2016-12-20 23:42:28 +00:00
|
|
|
use std::io;
|
|
|
|
|
2017-07-20 04:22:27 +00:00
|
|
|
#[doc(inline)]
|
2018-06-17 04:29:27 +00:00
|
|
|
pub use postgres_shared::{error, params};
|
2018-04-15 21:38:01 +00:00
|
|
|
#[doc(inline)]
|
2018-06-17 04:29:27 +00:00
|
|
|
pub use postgres_shared::{CancelData, Notification};
|
2016-12-25 17:51:12 +00:00
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
use error::Error;
|
|
|
|
use params::ConnectParams;
|
2016-12-20 23:42:28 +00:00
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
mod proto;
|
2017-07-20 04:22:27 +00:00
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
fn bad_response() -> Error {
|
|
|
|
Error::from(io::Error::new(
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
"the server returned an unexpected response",
|
|
|
|
))
|
2016-12-20 23:42:28 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
fn disconnected() -> Error {
|
|
|
|
Error::from(io::Error::new(
|
|
|
|
io::ErrorKind::UnexpectedEof,
|
|
|
|
"server disconnected",
|
|
|
|
))
|
2016-12-20 23:42:28 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
pub struct Client(mpsc::Sender<proto::Request>);
|
2016-12-20 23:42:28 +00:00
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
impl Client {
|
|
|
|
pub fn connect(params: ConnectParams) -> Handshake {
|
|
|
|
Handshake(proto::HandshakeFuture::new(params))
|
2016-12-20 23:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
pub struct Connection(proto::Connection);
|
2016-12-21 03:50:44 +00:00
|
|
|
|
2016-12-20 23:42:28 +00:00
|
|
|
impl Connection {
|
|
|
|
pub fn cancel_data(&self) -> CancelData {
|
2018-06-17 04:29:27 +00:00
|
|
|
self.0.cancel_data()
|
2016-12-20 23:42:28 +00:00
|
|
|
}
|
2016-12-26 21:21:20 +00:00
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
pub fn parameter(&self, name: &str) -> Option<&str> {
|
|
|
|
self.0.parameter(name)
|
2018-01-29 11:05:16 +00:00
|
|
|
}
|
2016-12-20 23:42:28 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
impl Future for Connection {
|
|
|
|
type Item = ();
|
2017-03-03 06:27:12 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
fn poll(&mut self) -> Poll<(), Error> {
|
|
|
|
self.0.poll()
|
2016-12-21 03:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
pub struct Handshake(proto::HandshakeFuture);
|
2017-09-30 21:56:15 +00:00
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
impl Future for Handshake {
|
|
|
|
type Item = (Client, Connection);
|
|
|
|
type Error = Error;
|
2017-09-30 21:56:15 +00:00
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
fn poll(&mut self) -> Poll<(Client, Connection), Error> {
|
|
|
|
let (sender, connection) = try_ready!(self.0.poll());
|
2017-09-30 21:56:15 +00:00
|
|
|
|
2018-06-17 04:29:27 +00:00
|
|
|
Ok(Async::Ready((Client(sender), Connection(connection))))
|
2017-09-30 21:56:15 +00:00
|
|
|
}
|
|
|
|
}
|