danicoin/src/P2p/ConnectionContext.h

84 lines
2.6 KiB
C
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <list>
#include <ostream>
#include <unordered_set>
#include <boost/uuid/uuid.hpp>
#include "Common/StringTools.h"
#include "crypto/hash.h"
namespace CryptoNote {
2015-07-30 15:22:07 +00:00
struct CryptoNoteConnectionContext {
2015-07-15 12:23:00 +00:00
uint8_t version;
2015-05-27 12:08:46 +00:00
boost::uuids::uuid m_connection_id;
uint32_t m_remote_ip = 0;
uint32_t m_remote_port = 0;
bool m_is_income = false;
time_t m_started = 0;
enum state {
state_befor_handshake = 0, //default state
state_synchronizing,
state_idle,
state_normal,
state_sync_required,
2015-07-15 12:23:00 +00:00
state_pool_sync_required,
2015-05-27 12:08:46 +00:00
state_shutdown
};
state m_state = state_befor_handshake;
2015-07-30 15:22:07 +00:00
std::list<Crypto::Hash> m_needed_objects;
std::unordered_set<Crypto::Hash> m_requested_objects;
uint32_t m_remote_blockchain_height = 0;
uint32_t m_last_response_height = 0;
2015-05-27 12:08:46 +00:00
};
2015-07-30 15:22:07 +00:00
inline std::string get_protocol_state_string(CryptoNoteConnectionContext::state s) {
2015-05-27 12:08:46 +00:00
switch (s) {
2015-07-30 15:22:07 +00:00
case CryptoNoteConnectionContext::state_befor_handshake:
2015-05-27 12:08:46 +00:00
return "state_befor_handshake";
2015-07-30 15:22:07 +00:00
case CryptoNoteConnectionContext::state_synchronizing:
2015-05-27 12:08:46 +00:00
return "state_synchronizing";
2015-07-30 15:22:07 +00:00
case CryptoNoteConnectionContext::state_idle:
2015-05-27 12:08:46 +00:00
return "state_idle";
2015-07-30 15:22:07 +00:00
case CryptoNoteConnectionContext::state_normal:
2015-05-27 12:08:46 +00:00
return "state_normal";
2015-07-30 15:22:07 +00:00
case CryptoNoteConnectionContext::state_sync_required:
2015-05-27 12:08:46 +00:00
return "state_sync_required";
case CryptoNoteConnectionContext::state_pool_sync_required:
return "state_pool_sync_required";
2015-07-30 15:22:07 +00:00
case CryptoNoteConnectionContext::state_shutdown:
2015-05-27 12:08:46 +00:00
return "state_shutdown";
default:
return "unknown";
}
}
}
namespace std {
2015-07-30 15:22:07 +00:00
inline std::ostream& operator << (std::ostream& s, const CryptoNote::CryptoNoteConnectionContext& context) {
2015-05-27 12:08:46 +00:00
return s << "[" << Common::ipAddressToString(context.m_remote_ip) << ":" <<
context.m_remote_port << (context.m_is_income ? " INC" : " OUT") << "] ";
}
}