From 36bdebe5e0f8cb943a06979c5e22efb14059fc98 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 7 Feb 2015 13:32:49 -0800 Subject: [PATCH] Transition into ready state. --- crypto.cc | 22 ++++++++++++++++++++-- crypto.h | 2 ++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/crypto.cc b/crypto.cc index ce10e15..af0f7e7 100644 --- a/crypto.cc +++ b/crypto.cc @@ -182,7 +182,16 @@ void CryptoPubServerConnection::OnReadable_(struct bufferevent* bev, void* this_ void CryptoPubServerConnection::OnReadable() { char buf[UINT16_MAX]; int bytes = bufferevent_read(bev_, buf, UINT16_MAX); - std::unique_ptr decoded(TLVNode::Decode(std::string(buf, bytes))); + const std::string input(buf, bytes); + + if (state_ == AWAITING_HANDSHAKE) { + OnHandshake(input); + return; + } +} + +void CryptoPubServerConnection::OnHandshake(const std::string& input) { + std::unique_ptr decoded(TLVNode::Decode(input)); if (!decoded.get()) { // TODO: re-buffer? return; @@ -288,7 +297,16 @@ void CryptoPubClient::OnReadable_(struct bufferevent* bev, void* this__) { void CryptoPubClient::OnReadable() { char buf[UINT16_MAX]; int bytes = bufferevent_read(bev_, buf, UINT16_MAX); - std::unique_ptr decoded(TLVNode::Decode(std::string(buf, bytes))); + const std::string input(buf, bytes); + + if (state_ == AWAITING_HANDSHAKE) { + OnHandshake(input); + return; + } +} + +void CryptoPubClient::OnHandshake(const std::string& input) { + std::unique_ptr decoded(TLVNode::Decode(input)); if (!decoded.get()) { // TODO: re-buffer? return; diff --git a/crypto.h b/crypto.h index 7abe225..bc0de32 100644 --- a/crypto.h +++ b/crypto.h @@ -49,6 +49,7 @@ class CryptoPubServerConnection : public CryptoBase { private: static void OnReadable_(struct bufferevent* bev, void* this__); void OnReadable(); + void OnHandshake(const std::string& input); static void OnError_(struct bufferevent* bev, const short what, void* this__); void OnError(const short what); @@ -79,6 +80,7 @@ class CryptoPubClient : public CryptoBase { private: static void OnReadable_(struct bufferevent* bev, void* this__); void OnReadable(); + void OnHandshake(const std::string& input); static void OnConnectOrError_(struct bufferevent* bev, const short what, void* this__); void OnConnect(); void OnError();