XMPPCodec Input shall be just one Packet

This commit is contained in:
Astro 2017-06-03 02:17:12 +02:00
parent 005013f37c
commit aece3798c1
2 changed files with 20 additions and 16 deletions

View file

@ -76,7 +76,7 @@ impl Future for TcpClient {
TcpClientState::RecvStart(ref mut opt_xmpp_stream) => { TcpClientState::RecvStart(ref mut opt_xmpp_stream) => {
let mut xmpp_stream = opt_xmpp_stream.take().unwrap(); let mut xmpp_stream = opt_xmpp_stream.take().unwrap();
match xmpp_stream.poll() { match xmpp_stream.poll() {
Ok(Async::Ready(Some(events))) => println!("Recv start: {:?}", events), Ok(Async::Ready(Some(Packet::StreamStart))) => println!("Recv start!"),
Ok(Async::Ready(_)) => return Err(std::io::Error::from(ErrorKind::InvalidData)), Ok(Async::Ready(_)) => return Err(std::io::Error::from(ErrorKind::InvalidData)),
Ok(Async::NotReady) => { Ok(Async::NotReady) => {
*opt_xmpp_stream = Some(xmpp_stream); *opt_xmpp_stream = Some(xmpp_stream);

View file

@ -68,12 +68,14 @@ impl XMPPCodec {
} }
impl Codec for XMPPCodec { impl Codec for XMPPCodec {
type In = Vec<Packet>; type In = Packet;
type Out = Packet; type Out = Packet;
fn decode(&mut self, buf: &mut EasyBuf) -> Result<Option<Self::In>, Error> { fn decode(&mut self, buf: &mut EasyBuf) -> Result<Option<Self::In>, Error> {
println!("XMPPCodec.decode {:?}", buf.len()); println!("XMPPCodec.decode {:?}", buf.len());
match from_utf8(buf.as_slice()) { let buf_len = buf.len();
let chunk = buf.drain_to(buf_len);
match from_utf8(chunk.as_slice()) {
Ok(s) => Ok(s) =>
self.parser.feed_str(s), self.parser.feed_str(s),
Err(e) => Err(e) =>
@ -81,7 +83,7 @@ impl Codec for XMPPCodec {
} }
let mut new_root = None; let mut new_root = None;
let mut results = Vec::new(); let mut result = None;
for event in &mut self.parser { for event in &mut self.parser {
match &mut self.root { match &mut self.root {
&mut None => { &mut None => {
@ -89,10 +91,13 @@ impl Codec for XMPPCodec {
match event { match event {
Ok(xml::Event::ElementStart(start_tag)) => { Ok(xml::Event::ElementStart(start_tag)) => {
new_root = Some(XMPPRoot::new(start_tag)); new_root = Some(XMPPRoot::new(start_tag));
results.push(Packet::StreamStart); result = Some(Packet::StreamStart);
break
},
Err(e) => {
result = Some(Packet::Error(Box::new(e)));
break
}, },
Err(e) =>
results.push(Packet::Error(Box::new(e))),
_ => _ =>
(), (),
} }
@ -103,10 +108,13 @@ impl Codec for XMPPCodec {
None => (), None => (),
Some(Ok(stanza)) => { Some(Ok(stanza)) => {
println!("stanza: {}", stanza); println!("stanza: {}", stanza);
results.push(Packet::Stanza(stanza)); result = Some(Packet::Stanza(stanza));
break
}, },
Some(Err(e)) => Some(Err(e)) => {
results.push(Packet::Error(Box::new(e))), result = Some(Packet::Error(Box::new(e)));
break
}
}; };
}, },
} }
@ -117,11 +125,7 @@ impl Codec for XMPPCodec {
} }
} }
if results.len() == 0 { Ok(result)
Ok(None)
} else {
Ok(Some(results))
}
} }
fn encode(&mut self, msg: Self::Out, buf: &mut Vec<u8>) -> Result<(), Error> { fn encode(&mut self, msg: Self::Out, buf: &mut Vec<u8>) -> Result<(), Error> {
@ -147,6 +151,6 @@ impl Codec for XMPPCodec {
} }
fn decode_eof(&mut self, _buf: &mut EasyBuf) -> Result<Self::In, Error> { fn decode_eof(&mut self, _buf: &mut EasyBuf) -> Result<Self::In, Error> {
Ok(vec!()) Err(Error::from(ErrorKind::UnexpectedEof))
} }
} }