@@ -6,11 +6,10 @@ use std::collections::{HashMap, VecDeque};
6
6
use std:: io;
7
7
use tokio_codec:: Framed ;
8
8
9
- use disconnected;
10
- use error:: { self , Error } ;
9
+ use error:: { self , DbError , Error } ;
11
10
use proto:: codec:: PostgresCodec ;
12
11
use tls:: TlsStream ;
13
- use { bad_response, CancelData } ;
12
+ use { bad_response, disconnected , AsyncMessage , CancelData , Notification } ;
14
13
15
14
pub struct Request {
16
15
pub messages : Vec < u8 > ,
@@ -71,10 +70,10 @@ impl Connection {
71
70
self . stream . poll ( )
72
71
}
73
72
74
- fn poll_read ( & mut self ) -> Result < ( ) , Error > {
73
+ fn poll_read ( & mut self ) -> Result < Option < AsyncMessage > , Error > {
75
74
if self . state != State :: Active {
76
75
trace ! ( "poll_read: done" ) ;
77
- return Ok ( ( ) ) ;
76
+ return Ok ( None ) ;
78
77
}
79
78
80
79
loop {
@@ -85,14 +84,22 @@ impl Connection {
85
84
}
86
85
Async :: NotReady => {
87
86
trace ! ( "poll_read: waiting on response" ) ;
88
- return Ok ( ( ) ) ;
87
+ return Ok ( None ) ;
89
88
}
90
89
} ;
91
90
92
91
let message = match message {
93
- Message :: NoticeResponse ( _) | Message :: NotificationResponse ( _) => {
94
- // FIXME handle these
95
- continue ;
92
+ Message :: NoticeResponse ( body) => {
93
+ let error = DbError :: new ( & mut body. fields ( ) ) ?;
94
+ return Ok ( Some ( AsyncMessage :: Notice ( error) ) ) ;
95
+ }
96
+ Message :: NotificationResponse ( body) => {
97
+ let notification = Notification {
98
+ process_id : body. process_id ( ) ,
99
+ channel : body. channel ( ) ?. to_string ( ) ,
100
+ payload : body. message ( ) ?. to_string ( ) ,
101
+ } ;
102
+ return Ok ( Some ( AsyncMessage :: Notification ( notification) ) ) ;
96
103
}
97
104
Message :: ParameterStatus ( body) => {
98
105
self . parameters
@@ -127,7 +134,7 @@ impl Connection {
127
134
self . responses . push_front ( sender) ;
128
135
self . pending_response = Some ( message) ;
129
136
trace ! ( "poll_read: waiting on socket" ) ;
130
- return Ok ( ( ) ) ;
137
+ return Ok ( None ) ;
131
138
}
132
139
}
133
140
}
@@ -225,18 +232,26 @@ impl Connection {
225
232
Err ( e) => Err ( Error :: from ( e) ) ,
226
233
}
227
234
}
235
+
236
+ pub fn poll_message ( & mut self ) -> Poll < Option < AsyncMessage > , Error > {
237
+ let message = self . poll_read ( ) ?;
238
+ let want_flush = self . poll_write ( ) ?;
239
+ if want_flush {
240
+ self . poll_flush ( ) ?;
241
+ }
242
+ match message {
243
+ Some ( message) => Ok ( Async :: Ready ( Some ( message) ) ) ,
244
+ None => self . poll_shutdown ( ) . map ( |r| r. map ( |( ) | None ) ) ,
245
+ }
246
+ }
228
247
}
229
248
230
249
impl Future for Connection {
231
250
type Item = ( ) ;
232
251
type Error = Error ;
233
252
234
253
fn poll ( & mut self ) -> Poll < ( ) , Error > {
235
- self . poll_read ( ) ?;
236
- let want_flush = self . poll_write ( ) ?;
237
- if want_flush {
238
- self . poll_flush ( ) ?;
239
- }
240
- self . poll_shutdown ( )
254
+ while let Some ( _) = try_ready ! ( self . poll_message( ) ) { }
255
+ Ok ( Async :: Ready ( ( ) ) )
241
256
}
242
257
}
0 commit comments