Skip to content

Commit 457d700

Browse files
committed
Read message
1 parent 82f708c commit 457d700

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ extern crate log;
5050
extern crate phf;
5151
extern crate postgres_protocol;
5252

53-
use bufstream::BufStream;
5453
use md5::Md5;
5554
use std::cell::{Cell, RefCell};
5655
use std::collections::{VecDeque, HashMap};
@@ -64,7 +63,7 @@ use std::time::Duration;
6463
use postgres_protocol::message::frontend;
6564

6665
use error::{Error, ConnectError, SqlState, DbError};
67-
use io::{TlsStream, TlsHandshake};
66+
use io::TlsHandshake;
6867
use message::{Backend, RowDescriptionEntry, ReadMessage};
6968
use notification::{Notifications, Notification};
7069
use params::{ConnectParams, IntoConnectParams, UserInfo};
@@ -304,7 +303,7 @@ impl InnerConnection {
304303
fn read_message_with_notification(&mut self) -> std_io::Result<Backend> {
305304
debug_assert!(!self.desynchronized);
306305
loop {
307-
match try_desync!(self, self.stream.read_message()) {
306+
match try_desync!(self, ReadMessage::read_message(&mut self.stream)) {
308307
Backend::NoticeResponse { fields } => {
309308
if let Ok(err) = DbError::new_raw(fields) {
310309
self.notice_handler.handle_notice(err);

src/priv_io.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ use std::os::unix::io::{AsRawFd, RawFd};
1212
#[cfg(windows)]
1313
use std::os::windows::io::{AsRawSocket, RawSocket};
1414
use postgres_protocol::message::frontend;
15+
use postgres_protocol::message::backend::{self, ParseResult};
1516

1617
use TlsMode;
1718
use params::{ConnectParams, ConnectTarget};
1819
use error::ConnectError;
1920
use io::TlsStream;
2021

2122
const DEFAULT_PORT: u16 = 5432;
23+
const MESSAGE_HEADER_SIZE: usize = 5;
2224

2325
pub struct MessageStream {
2426
stream: BufStream<Box<TlsStream>>,
@@ -43,6 +45,27 @@ impl MessageStream {
4345
self.stream.write_all(&self.buf)
4446
}
4547

48+
pub fn read_message<'a>(&'a mut self) -> io::Result<backend::Message<'a>> {
49+
self.buf.resize(MESSAGE_HEADER_SIZE, 0);
50+
try!(self.stream.read_exact(&mut self.buf));
51+
52+
let len = match try!(backend::Message::parse(&self.buf)) {
53+
// FIXME this is dumb but an explicit return runs into borrowck issues :(
54+
ParseResult::Complete { .. } => None,
55+
ParseResult::Incomplete { required_size } => Some(required_size.unwrap()),
56+
};
57+
58+
if let Some(len) = len {
59+
self.buf.resize(len, 0);
60+
try!(self.stream.read_exact(&mut self.buf[MESSAGE_HEADER_SIZE..]));
61+
};
62+
63+
match try!(backend::Message::parse(&self.buf)) {
64+
ParseResult::Complete { message, .. } => Ok(message),
65+
ParseResult::Incomplete { .. } => unreachable!(),
66+
}
67+
}
68+
4669
pub fn flush(&mut self) -> io::Result<()> {
4770
self.stream.flush()
4871
}

src/rows.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::ascii::AsciiExt;
44
use std::borrow::Cow;
55
use std::collections::VecDeque;
6-
use std::io::Write;
76
use std::fmt;
87
use std::ops::Deref;
98
use std::slice;

0 commit comments

Comments
 (0)