danicoin/src/P2p/LevinProtocol.h

114 lines
3 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
2015-07-30 15:22:07 +00:00
#include "CryptoNote.h"
#include <Common/MemoryInputStream.h>
#include <Common/VectorOutputStream.h>
#include "Serialization/KVBinaryInputStreamSerializer.h"
#include "Serialization/KVBinaryOutputStreamSerializer.h"
2015-05-27 12:08:46 +00:00
namespace System {
class TcpConnection;
}
namespace CryptoNote {
enum class LevinError: int32_t {
OK = 0,
ERROR_CONNECTION = -1,
ERROR_CONNECTION_NOT_FOUND = -2,
ERROR_CONNECTION_DESTROYED = -3,
ERROR_CONNECTION_TIMEDOUT = -4,
ERROR_CONNECTION_NO_DUPLEX_PROTOCOL = -5,
ERROR_CONNECTION_HANDLER_NOT_DEFINED = -6,
ERROR_FORMAT = -7,
};
2015-07-30 15:22:07 +00:00
const int32_t LEVIN_PROTOCOL_RETCODE_SUCCESS = 1;
2015-05-27 12:08:46 +00:00
class LevinProtocol {
public:
LevinProtocol(System::TcpConnection& connection);
2015-07-30 15:22:07 +00:00
template <typename Request, typename Response>
bool invoke(uint32_t command, const Request& request, Response& response) {
sendMessage(command, encode(request), true);
Command cmd;
readCommand(cmd);
if (!cmd.isResponse) {
return false;
}
return decode(cmd.buf, response);
2015-05-27 12:08:46 +00:00
}
2015-07-30 15:22:07 +00:00
template <typename Request>
void notify(uint32_t command, const Request& request, int) {
sendMessage(command, encode(request), false);
2015-05-27 12:08:46 +00:00
}
struct Command {
uint32_t command;
bool isNotify;
bool isResponse;
2015-07-30 15:22:07 +00:00
BinaryArray buf;
2015-05-27 12:08:46 +00:00
2015-07-30 15:22:07 +00:00
bool needReply() const;
2015-05-27 12:08:46 +00:00
};
bool readCommand(Command& cmd);
2015-07-30 15:22:07 +00:00
void sendMessage(uint32_t command, const BinaryArray& out, bool needResponse);
void sendReply(uint32_t command, const BinaryArray& out, int32_t returnCode);
2015-05-27 12:08:46 +00:00
template <typename T>
2015-07-30 15:22:07 +00:00
static bool decode(const BinaryArray& buf, T& value) {
2015-07-15 12:23:00 +00:00
try {
2015-07-30 15:22:07 +00:00
Common::MemoryInputStream stream(buf.data(), buf.size());
2015-07-15 12:23:00 +00:00
KVBinaryInputStreamSerializer serializer(stream);
serialize(value, serializer);
} catch (std::exception&) {
2015-05-27 12:08:46 +00:00
return false;
}
2015-07-15 12:23:00 +00:00
return true;
2015-05-27 12:08:46 +00:00
}
template <typename T>
2015-07-30 15:22:07 +00:00
static BinaryArray encode(const T& value) {
BinaryArray result;
2015-07-15 12:23:00 +00:00
KVBinaryOutputStreamSerializer serializer;
serialize(const_cast<T&>(value), serializer);
2015-07-30 15:22:07 +00:00
Common::VectorOutputStream stream(result);
serializer.dump(stream);
return result;
2015-05-27 12:08:46 +00:00
}
private:
bool readStrict(uint8_t* ptr, size_t size);
void writeStrict(const uint8_t* ptr, size_t size);
2015-05-27 12:08:46 +00:00
System::TcpConnection& m_conn;
};
}