2016-01-18 15:33:29 +00:00
|
|
|
// Copyright (c) 2011-2016 The Cryptonote developers
|
2015-09-18 11:55:31 +00:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
#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:
|
|
|
|
|
2015-08-19 17:06:24 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|