@@ -48,6 +48,7 @@ extern crate hex;
4848#[ macro_use]
4949extern crate log;
5050extern crate phf;
51+ extern crate postgres_protocol;
5152
5253use bufstream:: BufStream ;
5354use md5:: Md5 ;
@@ -60,6 +61,7 @@ use std::mem;
6061use std:: result;
6162use std:: sync:: Arc ;
6263use std:: time:: Duration ;
64+ use postgres_protocol:: message:: frontend;
6365
6466use error:: { Error , ConnectError , SqlState , DbError } ;
6567use io:: { TlsStream , TlsHandshake } ;
@@ -205,6 +207,7 @@ struct StatementInfo {
205207
206208struct InnerConnection {
207209 stream : BufStream < Box < TlsStream > > ,
210+ io_buf : Vec < u8 > ,
208211 notice_handler : Box < HandleNotice > ,
209212 notifications : VecDeque < Notification > ,
210213 cancel_data : CancelData ,
@@ -246,6 +249,7 @@ impl InnerConnection {
246249
247250 let mut conn = InnerConnection {
248251 stream : BufStream :: new ( stream) ,
252+ io_buf : vec ! [ ] ,
249253 next_stmt_id : 0 ,
250254 notice_handler : Box :: new ( LoggingNoticeHandler ) ,
251255 notifications : VecDeque :: new ( ) ,
@@ -274,10 +278,10 @@ impl InnerConnection {
274278 options. push ( ( "database" . to_owned ( ) , database) ) ;
275279 }
276280
277- try!( conn. write_messages ( & [ Frontend :: StartupMessage {
278- version : message :: PROTOCOL_VERSION ,
279- parameters : & options ,
280- } ] ) ) ;
281+ try!( conn. write_message ( & frontend :: StartupMessage {
282+ parameters : & options ,
283+ } ) ) ;
284+ try! ( conn . stream . flush ( ) ) ;
281285
282286 try!( conn. handle_auth ( user) ) ;
283287
@@ -296,6 +300,14 @@ impl InnerConnection {
296300 Ok ( conn)
297301 }
298302
303+ fn write_message < M > ( & mut self , message : & M ) -> std_io:: Result < ( ) >
304+ where M : frontend:: Message
305+ {
306+ self . io_buf . clear ( ) ;
307+ try!( message. write ( & mut self . io_buf ) ) ;
308+ self . stream . write_all ( & self . io_buf )
309+ }
310+
299311 fn write_messages ( & mut self , messages : & [ Frontend ] ) -> std_io:: Result < ( ) > {
300312 debug_assert ! ( !self . desynchronized) ;
301313 for message in messages {
@@ -380,7 +392,8 @@ impl InnerConnection {
380392 let pass = try!( user. password . ok_or_else ( || {
381393 ConnectError :: ConnectParams ( "a password was requested but not provided" . into ( ) )
382394 } ) ) ;
383- try!( self . write_messages ( & [ Frontend :: PasswordMessage { password : & pass } ] ) ) ;
395+ try!( self . write_message ( & frontend:: PasswordMessage { password : & pass } ) ) ;
396+ try!( self . stream . flush ( ) ) ;
384397 }
385398 Backend :: AuthenticationMD5Password { salt } => {
386399 let pass = try!( user. password . ok_or_else ( || {
@@ -394,7 +407,8 @@ impl InnerConnection {
394407 hasher. input ( output. as_bytes ( ) ) ;
395408 hasher. input ( & salt) ;
396409 let output = format ! ( "md5{}" , hasher. result_str( ) ) ;
397- try!( self . write_messages ( & [ Frontend :: PasswordMessage { password : & output } ] ) ) ;
410+ try!( self . write_message ( & frontend:: PasswordMessage { password : & output } ) ) ;
411+ try!( self . stream . flush ( ) ) ;
398412 }
399413 Backend :: AuthenticationKerberosV5 |
400414 Backend :: AuthenticationSCMCredential |
0 commit comments