Skip to content
Snippets Groups Projects
Commit 8f06523c authored by Tommaso Colombo's avatar Tommaso Colombo
Browse files

Session: set state variables *before* calling state change completion methods

parent f9f6ebd8
No related branches found
No related tags found
No related merge requests found
...@@ -47,8 +47,8 @@ void Session::asyncOpen(const std::string& localName, ...@@ -47,8 +47,8 @@ void Session::asyncOpen(const std::string& localName,
m_socket.async_connect(remoteEndpoint, m_strand.wrap( m_socket.async_connect(remoteEndpoint, m_strand.wrap(
[this, self] (const boost::system::error_code& error) { [this, self] (const boost::system::error_code& error) {
if (error) { if (error) {
onOpenError(error);
abortOpen(); abortOpen();
onOpenError(error);
} }
else { else {
startOpen(self); startOpen(self);
...@@ -72,21 +72,21 @@ void Session::startOpen(const std::shared_ptr<Session>& self) ...@@ -72,21 +72,21 @@ void Session::startOpen(const std::shared_ptr<Session>& self)
boost::asio::async_write(m_socket, boost::make_iterator_range(m_sendBuffers), m_strand.wrap( boost::asio::async_write(m_socket, boost::make_iterator_range(m_sendBuffers), m_strand.wrap(
[this, self] (const boost::system::error_code& error, std::size_t) { [this, self] (const boost::system::error_code& error, std::size_t) {
if (error) { if (error) {
onOpenError(error);
abortOpen(); abortOpen();
onOpenError(error);
return; return;
} }
// 3. Receive the HELLO message header // 3. Receive the HELLO message header
boost::asio::async_read(m_socket, boost::asio::buffer(m_recvHeader.data()), m_strand.wrap( boost::asio::async_read(m_socket, boost::asio::buffer(m_recvHeader.data()), m_strand.wrap(
[this, self] (const boost::system::error_code& error, std::size_t) { [this, self] (const boost::system::error_code& error, std::size_t) {
if (error) { if (error) {
onOpenError(error);
abortOpen(); abortOpen();
onOpenError(error);
return; return;
} }
if (m_recvHeader.typeId() != HELLO_MESSAGE_ID) { if (m_recvHeader.typeId() != HELLO_MESSAGE_ID) {
onOpenError(Error::UNEXPECTED_MESSAGE_TYPE);
abortOpen(); abortOpen();
onOpenError(Error::UNEXPECTED_MESSAGE_TYPE);
return; return;
} }
// 4. Receive the HELLO message body // 4. Receive the HELLO message body
...@@ -94,8 +94,8 @@ void Session::startOpen(const std::shared_ptr<Session>& self) ...@@ -94,8 +94,8 @@ void Session::startOpen(const std::shared_ptr<Session>& self)
boost::asio::async_read(m_socket, boost::asio::buffer(*storage), m_strand.wrap( boost::asio::async_read(m_socket, boost::asio::buffer(*storage), m_strand.wrap(
[this, self, storage] (const boost::system::error_code& error, std::size_t) { [this, self, storage] (const boost::system::error_code& error, std::size_t) {
if (error) { if (error) {
onOpenError(error);
abortOpen(); abortOpen();
onOpenError(error);
return; return;
} }
m_remoteName.insert(m_remoteName.begin(), storage->begin(), storage->end()); m_remoteName.insert(m_remoteName.begin(), storage->begin(), storage->end());
...@@ -108,11 +108,14 @@ void Session::startOpen(const std::shared_ptr<Session>& self) ...@@ -108,11 +108,14 @@ void Session::startOpen(const std::shared_ptr<Session>& self)
void Session::abortOpen() void Session::abortOpen()
{ {
m_localName.clear();
m_remoteName.clear();
m_state = State::CLOSED;
// abortOpen() is called if there was an error in one of the I/O operations in asyncOpen() or // abortOpen() is called if there was an error in one of the I/O operations in asyncOpen() or
// startOpen(). We have to distinguish two cases: // startOpen(). We have to distinguish two cases:
// 1) The error occurred because asyncClose() closed m_socket // 1) The error occurred because asyncClose() closed m_socket
// 2) There was a "real" I/O error // 2) There was a "real" I/O error
if (m_state == State::CLOSE_PENDING) { if (m_state == State::CLOSE_PENDING) {
// case 1) // case 1)
onClose(); onClose();
...@@ -122,9 +125,6 @@ void Session::abortOpen() ...@@ -122,9 +125,6 @@ void Session::abortOpen()
boost::system::error_code ignoredError; boost::system::error_code ignoredError;
m_socket.close(ignoredError); m_socket.close(ignoredError);
} }
m_localName.clear();
m_remoteName.clear();
m_state = State::CLOSED;
} }
...@@ -156,10 +156,10 @@ void Session::asyncClose() ...@@ -156,10 +156,10 @@ void Session::asyncClose()
void Session::checkClose() void Session::checkClose()
{ {
if (m_state == State::CLOSE_PENDING && m_recvNPending == 0 && m_sendQueue.empty()) { if (m_state == State::CLOSE_PENDING && m_recvNPending == 0 && m_sendQueue.empty()) {
onClose();
m_localName.clear(); m_localName.clear();
m_remoteName.clear(); m_remoteName.clear();
m_state = State::CLOSED; m_state = State::CLOSED;
onClose();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment