Browse Source

Some stuff...

peercoin
Intel 11 years ago
parent
commit
ff224b7955
  1. 9
      src/server/poolserver/Server/Server.cpp
  2. 54
      src/server/poolserver/Stratum/Client.cpp
  3. 46
      src/server/poolserver/Stratum/Client.h
  4. 15
      src/server/poolserver/Stratum/Job.h
  5. 76
      src/server/poolserver/Stratum/Server.h
  6. 2
      src/server/shared/Bitcoin/Bitcoin.h
  7. 10
      src/server/shared/Bitcoin/Block.h
  8. 8
      src/server/shared/Bitcoin/Script.h
  9. 8
      src/server/shared/Bitcoin/Serialization.cpp
  10. 4
      src/server/shared/Bitcoin/Transaction.cpp
  11. 4
      src/server/shared/Bitcoin/Transaction.h
  12. 16
      src/server/shared/ByteBuffer.h
  13. 3
      src/server/shared/Common.h
  14. 8
      src/server/shared/Crypto.cpp
  15. 6
      src/server/shared/Crypto.h
  16. 8
      src/server/shared/JSON/JSON.h
  17. 9
      src/server/shared/JSONRPC/JSONRPC.cpp
  18. 27
      src/server/shared/Util.cpp
  19. 1
      src/server/shared/Util.h

9
src/server/poolserver/Server/Server.cpp

@ -34,7 +34,8 @@ int Server::Run()
sLog.Info(LOG_SERVER, "Server is starting..."); sLog.Info(LOG_SERVER, "Server is starting...");
//InitDatabase(); //InitDatabase();
std::vector<byte> test = Util::ASCIIToBin("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
/*std::vector<byte> test = Util::ASCIIToBin("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
sLog.Info(LOG_SERVER, "Hash: %s", Util::BinToASCII(Crypto::SHA256D(test)).c_str()); sLog.Info(LOG_SERVER, "Hash: %s", Util::BinToASCII(Crypto::SHA256D(test)).c_str());
sLog.Info(LOG_SERVER, "RevHash: %s", Util::BinToASCII(Crypto::SHA256D(Util::Reverse(test))).c_str()); sLog.Info(LOG_SERVER, "RevHash: %s", Util::BinToASCII(Crypto::SHA256D(Util::Reverse(test))).c_str());
@ -56,7 +57,7 @@ int Server::Run()
block.tx.resize(1); block.tx.resize(1);
block.tx[0] = trans; block.tx[0] = trans;
block.BuildMerkleTree(); block.BuildMerkleTree();
sLog.Info(LOG_SERVER, "Hash: %s", Util::BinToASCII(block.merkleRootHash).c_str()); sLog.Info(LOG_SERVER, "Hash: %s", Util::BinToASCII(block.merkleRootHash).c_str());*/
/*ByteBuffer buf(Util::ASCIIToBin("01000000010c432f4fb3e871a8bda638350b3d5c698cf431db8d6031b53e3fb5159e59d4a90000000000ffffffff0100f2052a010000001976a9143744841e13b90b4aca16fe793a7f88da3a23cc7188ac00000000")); /*ByteBuffer buf(Util::ASCIIToBin("01000000010c432f4fb3e871a8bda638350b3d5c698cf431db8d6031b53e3fb5159e59d4a90000000000ffffffff0100f2052a010000001976a9143744841e13b90b4aca16fe793a7f88da3a23cc7188ac00000000"));
@ -78,8 +79,8 @@ int Server::Run()
// Init Bitcoin RPC // Init Bitcoin RPC
JSONRPCConnectionInfo coninfo; JSONRPCConnectionInfo coninfo;
coninfo.Host = "84.240.15.208"; coninfo.Host = "84.240.15.208";
coninfo.Port = "9902"; coninfo.Port = "8332";
coninfo.User = "ppcuser"; coninfo.User = "user";
coninfo.Pass = "DYAL6bC4RUHksL6ikdx7"; coninfo.Pass = "DYAL6bC4RUHksL6ikdx7";
JSONRPC* bitcoinrpc = new JSONRPC(); JSONRPC* bitcoinrpc = new JSONRPC();

54
src/server/poolserver/Stratum/Client.cpp

@ -0,0 +1,54 @@
#include "Server.h"
#include "Client.h"
namespace Stratum
{
void Client::OnMiningSubscribe(JSON msg)
{
_subscribed = true;
_extranonce = _server->GetExtranonce();
Job job = GetJob();
_jobs.push_back(job);
JSON response;
response.Set("id", msg["id"].Get<uint32>());
response.Set("error", NULL);
JSON miningdiff;
miningdiff.Add("mining.set_difficulty");
JSON lala;
lala.Set("", 1);
miningdiff.Add(lala);
JSON notify;
notify.Add("mining.notify");
notify.Add("abc");
JSON something;
something.Add(miningdiff);
something.Add(notify);
JSON result;
result.Add(something);
ByteBuffer noncebuf;
noncebuf << _extranonce;
JSON omg;
omg.Set("", Util::BinToASCII(noncebuf.Binary()));
result.Add(omg);
JSON shit;
shit.Set("", 4);
result.Add(shit);
response.Set("result", result);
SendMessage(response);
}
Job Client::GetJob()
{
Job job;
job.work = _server->GetWork();
return job;
}
}

46
src/server/poolserver/Stratum/Client.h

@ -4,6 +4,8 @@
#include "Common.h" #include "Common.h"
#include "Log.h" #include "Log.h"
#include "JSON.h" #include "JSON.h"
#include "Server.h"
#include "Job.h"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
@ -15,10 +17,12 @@ using namespace boost::asio::ip;
namespace Stratum namespace Stratum
{ {
class Server;
class Client class Client
{ {
public: public:
Client(asio::io_service& io_service) : _socket(io_service) Client(Server* server, asio::io_service& io_service) : _server(server), _socket(io_service), _subscribed(false)
{ {
} }
@ -36,17 +40,43 @@ namespace Stratum
boost::bind(&Client::_OnReceive, this, asio::placeholders::error, asio::placeholders::bytes_transferred)); boost::bind(&Client::_OnReceive, this, asio::placeholders::error, asio::placeholders::bytes_transferred));
} }
void SendJob()
{
}
void SendMessage(JSON msg) void SendMessage(JSON msg)
{ {
std::string data = msg.ToString(); std::string data = msg.ToString();
sLog.Debug(LOG_SERVER, "Sending: %s", data.c_str());
_socket.send(boost::asio::buffer(data.c_str(), data.length())); _socket.send(boost::asio::buffer(data.c_str(), data.length()));
} }
void OnMiningSubscribe(JSON msg);
void OnMiningAuthorize(JSON msg)
{
std::string username = msg["result"][0].Get<std::string>();
JSON response;
response.Set("id", msg["id"].Get<uint32>());
response.Set("error", NULL);
response.Set("result", true);
SendMessage(response);
}
void OnMessage(JSON msg) void OnMessage(JSON msg)
{ {
std::string method = msg.Get<std::string>("method"); std::string method = msg["method"].Get<std::string>();
sLog.Debug(LOG_SERVER, "Method: %s", method.c_str()); sLog.Debug(LOG_SERVER, "Method: %s", method.c_str());
if (method.compare("mining.subscribe") == 0)
OnMiningSubscribe(msg);
else if (method.compare("mining.authorize") == 0)
OnMiningAuthorize(msg);
else
sLog.Error(LOG_SERVER, "Method '%s' not found.", method.c_str());
} }
Job GetJob();
public: public:
void _OnReceive(const boost::system::error_code& error, size_t bytes_transferred) void _OnReceive(const boost::system::error_code& error, size_t bytes_transferred)
{ {
@ -57,8 +87,20 @@ namespace Stratum
OnMessage(JSON::FromString(iss.str())); OnMessage(JSON::FromString(iss.str()));
} }
private: private:
// Networking
asio::streambuf _recvBuffer; asio::streambuf _recvBuffer;
tcp::socket _socket; tcp::socket _socket;
// Pointer to server
Stratum::Server* _server;
// Authorization
std::vector<std::string> _workers;
// Jobs
bool _subscribed;
uint32 _extranonce;
std::vector<Job> _jobs;
}; };
} }

15
src/server/poolserver/Stratum/Job.h

@ -0,0 +1,15 @@
#ifndef STRATUM_JOB_H_
#define STRATUM_JOB_H_
#include "Bitcoin.h"
namespace Stratum
{
class Job
{
public:
Bitcoin::BlockPtr work;
};
}
#endif

76
src/server/poolserver/Stratum/Server.h

@ -9,6 +9,7 @@
#include "JSONRPC.h" #include "JSONRPC.h"
#include "Bitcoin.h" #include "Bitcoin.h"
#include "Util.h" #include "Util.h"
#include "ByteBuffer.h"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
@ -28,7 +29,7 @@ namespace Stratum
class Server class Server
{ {
public: public:
Server(asio::io_service& io_service) : _io_service(io_service), _acceptor(io_service), _blockCheckTimer(io_service), _blockHeight(0) Server(asio::io_service& io_service) : _io_service(io_service), _acceptor(io_service), _blockCheckTimer(io_service), _blockHeight(0), _extranonce(0)
{ {
} }
@ -65,10 +66,20 @@ namespace Stratum
(*it)->SendMessage(msg); (*it)->SendMessage(msg);
} }
uint32 GetExtranonce()
{
return _extranonce++;
}
Bitcoin::BlockPtr GetWork()
{
return _currentWork;
}
private: private:
void _StartAccept() void _StartAccept()
{ {
Client* client = new Client(_io_service); Client* client = new Client(this, _io_service);
_acceptor.async_accept(client->GetSocket(), boost::bind(&Server::_OnAccept, this, client, asio::placeholders::error)); _acceptor.async_accept(client->GetSocket(), boost::bind(&Server::_OnAccept, this, client, asio::placeholders::error));
} }
@ -91,10 +102,32 @@ namespace Stratum
{ {
JSON response = _bitcoinrpc->Query("getblocktemplate"); JSON response = _bitcoinrpc->Query("getblocktemplate");
Bitcoin::Block* block = new Bitcoin::Block(); Bitcoin::BlockPtr block = Bitcoin::BlockPtr(new Bitcoin::Block());
block->version = response.Get<uint32>("version");
block->prevBlockHash = Util::ASCIIToBin(response.Get<std::string>("previousblockhash")); block->version = response["version"].Get<uint32>();;
block->prevBlockHash = Util::ASCIIToBin(response["previousblockhash"].Get<std::string>());
// Set bits
ByteBuffer bitbuf(Util::ASCIIToBin(response["bits"].Get<std::string>()));
bitbuf >> block->bits;
// Add coinbase tx
block->tx.push_back(CreateCoinbaseTX(_blockHeight, _pubkey, response["coinbasevalue"].Get<int64>()));
// Add other transactions
JSON trans = response["transactions"];
for (uint64 i = 0; i < trans.Size(); ++i) {
ByteBuffer txbuf(Util::ASCIIToBin(trans[i]["data"].Get<std::string>()));
Bitcoin::Transaction tx;
txbuf >> tx;
block->tx.push_back(tx);
}
// Genrate merkle tree
block->BuildMerkleTree();
// Set current work
_currentWork = block;
} }
void _CheckBlocks() void _CheckBlocks()
@ -102,7 +135,7 @@ namespace Stratum
sLog.Debug(LOG_STRATUM, "Checking for new blocks..."); sLog.Debug(LOG_STRATUM, "Checking for new blocks...");
JSON response = _bitcoinrpc->Query("getinfo"); JSON response = _bitcoinrpc->Query("getinfo");
uint32 curBlock = response.Get<uint32>("blocks"); uint32 curBlock = response["blocks"].Get<uint32>();
if (curBlock > _blockHeight) { if (curBlock > _blockHeight) {
sLog.Debug(LOG_STRATUM, "New block on network! Height: %u", curBlock); sLog.Debug(LOG_STRATUM, "New block on network! Height: %u", curBlock);
@ -113,6 +146,33 @@ namespace Stratum
_blockCheckTimer.expires_from_now(boost::posix_time::milliseconds(sConfig.Get<uint32>("StratumBlockCheckTime"))); _blockCheckTimer.expires_from_now(boost::posix_time::milliseconds(sConfig.Get<uint32>("StratumBlockCheckTime")));
_blockCheckTimer.async_wait(boost::bind(&Server::_CheckBlocks, this)); _blockCheckTimer.async_wait(boost::bind(&Server::_CheckBlocks, this));
} }
Bitcoin::Transaction CreateCoinbaseTX(uint32 blockHeight, BinaryData pubkey, int64 value)
{
ByteBuffer scriptsig;
scriptsig << _blockHeight;
Bitcoin::OutPoint outpoint;
outpoint.hash.resize(32, 0);
outpoint.n = 0xFFFFFFFF;
Bitcoin::TxIn txin;
txin.prevout = outpoint;
txin.script = scriptsig.Binary();
txin.n = 0;
Bitcoin::TxOut txout;
txout.value = value;
txout.scriptPubKey = Bitcoin::Script(pubkey) + Bitcoin::OP_CHECKSIG;
Bitcoin::Transaction tx;
tx.version = 1;
tx.in.push_back(txin);
tx.out.push_back(txout);
tx.lockTime = 0;
return tx;
}
private: private:
// Network // Network
std::list<Client*> _clients; std::list<Client*> _clients;
@ -123,11 +183,13 @@ namespace Stratum
JSONRPC* _bitcoinrpc; JSONRPC* _bitcoinrpc;
// Bitcoin info // Bitcoin info
BinaryData _pubkey;
asio::deadline_timer _blockCheckTimer; asio::deadline_timer _blockCheckTimer;
uint32 _blockHeight; uint32 _blockHeight;
// Work // Work
Bitcoin::Block* _currentWork; Bitcoin::BlockPtr _currentWork;
uint32 _extranonce;
}; };
} }

2
src/server/shared/Bitcoin/Bitcoin.h

@ -3,5 +3,7 @@
#include "Block.h" #include "Block.h"
#include "Transaction.h" #include "Transaction.h"
#include "VarInt.h"
#include "Script.h"
#endif #endif

10
src/server/shared/Bitcoin/Block.h

@ -7,16 +7,14 @@
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include "Log.h"
namespace Bitcoin namespace Bitcoin
{ {
class BlockHeader class BlockHeader
{ {
public: public:
uint32 version; uint32 version;
std::vector<byte> prevBlockHash; BinaryData prevBlockHash;
std::vector<byte> merkleRootHash; BinaryData merkleRootHash;
uint32 time; uint32 time;
uint32 bits; uint32 bits;
uint32 nonce; uint32 nonce;
@ -29,7 +27,7 @@ namespace Bitcoin
std::vector<Transaction> tx; std::vector<Transaction> tx;
// Other data // Other data
std::vector<std::vector<byte> > merkleTree; std::vector<BinaryData> merkleTree;
void BuildMerkleTree() void BuildMerkleTree()
{ {
@ -61,6 +59,8 @@ namespace Bitcoin
} }
}; };
typedef boost::shared_ptr<Block> BlockPtr;
// Block Serialization (Implementation in Serialization.cpp) // Block Serialization (Implementation in Serialization.cpp)
ByteBuffer& operator<<(ByteBuffer& a, Block& b); ByteBuffer& operator<<(ByteBuffer& a, Block& b);
ByteBuffer& operator>>(ByteBuffer& a, Block& b); ByteBuffer& operator>>(ByteBuffer& a, Block& b);

8
src/server/shared/Bitcoin/Script.h

@ -23,18 +23,18 @@ namespace Bitcoin
{ {
public: public:
Script() {} Script() {}
Script(std::vector<byte> data) : script(data) {} Script(BinaryData data) : script(data) {}
std::vector<byte> script; BinaryData script;
const Script operator+(const Script& other) const Script operator+(const Script& other)
{ {
std::vector<byte> tmp = script; BinaryData tmp = script;
tmp.insert(tmp.end(), other.script.begin(), other.script.end()); tmp.insert(tmp.end(), other.script.begin(), other.script.end());
return Script(tmp); return Script(tmp);
} }
const Script operator+(const std::vector<byte> data) const Script operator+(const BinaryData data)
{ {
Script tmp(script); Script tmp(script);

8
src/server/shared/Bitcoin/Serialization.cpp

@ -48,7 +48,7 @@ ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, Script& b)
{ {
VarInt size; VarInt size;
a >> size; a >> size;
b.script = a.ReadBytes(size); b.script = a.ReadBinary(size);
return a; return a;
} }
@ -61,7 +61,7 @@ ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, OutPoint& b)
} }
ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, OutPoint& b) ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, OutPoint& b)
{ {
b.hash = a.ReadBytes(32); b.hash = a.ReadBinary(32);
a >> b.n; a >> b.n;
return a; return a;
} }
@ -163,8 +163,8 @@ ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Block& b)
ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, Block& b) ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, Block& b)
{ {
a >> b.version; a >> b.version;
b.prevBlockHash = a.ReadBytes(32); b.prevBlockHash = a.ReadBinary(32);
b.merkleRootHash = a.ReadBytes(32); b.merkleRootHash = a.ReadBinary(32);
a >> b.time; a >> b.time;
a >> b.bits; a >> b.bits;
a >> b.nonce; a >> b.nonce;

4
src/server/shared/Bitcoin/Transaction.cpp

@ -3,10 +3,10 @@
namespace Bitcoin namespace Bitcoin
{ {
std::vector<byte> Transaction::GetHash() BinaryData Transaction::GetHash()
{ {
ByteBuffer buf; ByteBuffer buf;
buf << *this; buf << *this;
return Crypto::SHA256D(buf.Bytes()); return Crypto::SHA256D(buf.Binary());
} }
} }

4
src/server/shared/Bitcoin/Transaction.h

@ -12,7 +12,7 @@ namespace Bitcoin
class OutPoint class OutPoint
{ {
public: public:
std::vector<byte> hash; BinaryData hash;
uint32 n; uint32 n;
}; };
@ -51,7 +51,7 @@ namespace Bitcoin
std::vector<TxOut> out; std::vector<TxOut> out;
uint32 lockTime; uint32 lockTime;
std::vector<byte> GetHash(); BinaryData GetHash();
}; };
// Transaction Serialization (Implementation in Serialization.cpp) // Transaction Serialization (Implementation in Serialization.cpp)

16
src/server/shared/ByteBuffer.h

@ -8,14 +8,14 @@ class ByteBuffer
{ {
public: public:
ByteBuffer(): pointer(0) {} ByteBuffer(): pointer(0) {}
ByteBuffer(std::vector<byte> data): pointer(0), vec(data) {} ByteBuffer(BinaryData data): pointer(0), vec(data) {}
ByteBuffer& operator<<(ByteBuffer& b) ByteBuffer& operator<<(ByteBuffer& b)
{ {
Append(b.vec); Append(b.vec);
return *this; return *this;
} }
ByteBuffer& operator<<(std::vector<byte>& b) ByteBuffer& operator<<(BinaryData& b)
{ {
Append(b); Append(b);
return *this; return *this;
@ -42,18 +42,18 @@ public:
vec.push_back(data >> (i * 8)); vec.push_back(data >> (i * 8));
} }
void Append(std::vector<byte> data) void Append(BinaryData data)
{ {
vec.insert(vec.end(), data.begin(), data.end()); vec.insert(vec.end(), data.begin(), data.end());
} }
std::vector<byte> ReadBytes(size_t size) BinaryData ReadBinary(size_t size)
{ {
if (vec.size() < pointer+size) if (vec.size() < pointer+size)
return std::vector<byte>(); return BinaryData();
pointer += size; pointer += size;
return std::vector<byte>(vec.begin()+pointer-size, vec.begin()+pointer); return BinaryData(vec.begin()+pointer-size, vec.begin()+pointer);
} }
template<typename T> template<typename T>
@ -73,13 +73,13 @@ public:
return data; return data;
} }
std::vector<byte> Bytes() BinaryData Binary()
{ {
return vec; return vec;
} }
uint64 pointer; uint64 pointer;
std::vector<byte> vec; BinaryData vec;
}; };
#endif #endif

3
src/server/shared/Common.h

@ -2,6 +2,7 @@
#define COMMON_H_ #define COMMON_H_
#include <boost/cstdint.hpp> #include <boost/cstdint.hpp>
#include <vector>
typedef uint8_t uint8; typedef uint8_t uint8;
typedef uint16_t uint16; typedef uint16_t uint16;
@ -15,4 +16,6 @@ typedef int64_t int64;
typedef uint8_t byte; typedef uint8_t byte;
typedef std::vector<byte> BinaryData;
#endif #endif

8
src/server/shared/Crypto.cpp

@ -2,7 +2,7 @@
namespace Crypto namespace Crypto
{ {
std::vector<byte> SHA256(std::vector<byte> data) BinaryData SHA256(BinaryData data)
{ {
std::vector<byte> hash; std::vector<byte> hash;
hash.resize(SHA256_DIGEST_LENGTH); hash.resize(SHA256_DIGEST_LENGTH);
@ -13,12 +13,12 @@ namespace Crypto
return std::vector<byte>(hash.begin(), hash.end()); return std::vector<byte>(hash.begin(), hash.end());
} }
std::vector<byte> SHA256(std::string data) BinaryData SHA256(std::string data)
{ {
return SHA256(std::vector<byte>(data.begin(), data.end())); return SHA256(BinaryData(data.begin(), data.end()));
} }
std::vector<byte> SHA256D(std::vector<byte> data) BinaryData SHA256D(BinaryData data)
{ {
return SHA256(SHA256(data)); return SHA256(SHA256(data));
} }

6
src/server/shared/Crypto.h

@ -9,9 +9,9 @@
namespace Crypto namespace Crypto
{ {
std::vector<byte> SHA256(std::vector<byte> data); BinaryData SHA256(BinaryData data);
std::vector<byte> SHA256(std::string data); BinaryData SHA256(std::string data);
std::vector<byte> SHA256D(std::vector<byte> data); BinaryData SHA256D(BinaryData data);
} }
#endif #endif

8
src/server/shared/JSON/JSON.h

@ -1,6 +1,7 @@
#ifndef JSON_H_ #ifndef JSON_H_
#define JSON_H_ #define JSON_H_
#include "Common.h"
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include <boost/cstdint.hpp> #include <boost/cstdint.hpp>
@ -25,9 +26,9 @@ public:
} }
template<class T> template<class T>
T Get(std::string key) T Get()
{ {
return pt->get<T>(key); return pt->get_value<T>();
} }
JSON operator[] (std::string key) JSON operator[] (std::string key)
@ -37,7 +38,7 @@ public:
return json; return json;
} }
JSON operator[] (int index) JSON operator[] (uint32 index)
{ {
JSON json(false); JSON json(false);
boost::property_tree::ptree::iterator it = pt->begin(); boost::property_tree::ptree::iterator it = pt->begin();
@ -71,6 +72,7 @@ public:
bool _base; bool _base;
}; };
template<> template<>
inline void JSON::Set<JSON>(std::string key, JSON value) inline void JSON::Set<JSON>(std::string key, JSON value)
{ {

9
src/server/shared/JSONRPC/JSONRPC.cpp

@ -107,18 +107,21 @@ JSON JSONRPC::Query(std::string method, JSON params)
std::string jsonresponse = ""; std::string jsonresponse = "";
if (_sock.available())
boost::asio::read_until(_sock, response, '\n');
if (response.size() > 0) { if (response.size() > 0) {
std::ostringstream oss; std::ostringstream oss;
oss << &response; oss << &response;
jsonresponse += oss.str(); jsonresponse += oss.str();
} }
// Read until EOF, writing data to output as we go. // Read until EOF, writing data to output as we go.
while(boost::asio::read(_sock, response, boost::asio::transfer_at_least(1), error)){ /*while (_sock.available() && boost::asio::read(_sock, response, error)){
std::ostringstream oss; std::ostringstream oss;
oss << &response; oss << &response;
jsonresponse += oss.str(); jsonresponse += oss.str();
} }*/
_sock.close(); _sock.close();

27
src/server/shared/Util.cpp

@ -18,6 +18,19 @@ std::string Util::Date(const char* format, bool utc)
return ss.str(); return ss.str();
} }
uint32 Util::Date(bool utc)
{
boost::posix_time::ptime now;
if (utc)
now = boost::posix_time::second_clock::universal_time();
else
now = boost::posix_time::second_clock::local_time();
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
boost::posix_time::time_duration diff = now - epoch;
return diff.total_seconds();
}
std::string Util::ToBase64(std::string input, bool linebreaks) std::string Util::ToBase64(std::string input, bool linebreaks)
{ {
uint32_t writePaddChars = (3 - input.length() % 3) % 3; uint32_t writePaddChars = (3 - input.length() % 3) % 3;
@ -55,9 +68,9 @@ uint8 Util::ASCIIToHex(char ch)
return 0; // Invalid return 0; // Invalid
} }
std::vector<byte> Util::ASCIIToBin(std::string str) BinaryData Util::ASCIIToBin(std::string str)
{ {
std::vector<byte> data; BinaryData data;
data.resize(str.size()/2, 0); data.resize(str.size()/2, 0);
for (uint64 i = 0; i < str.size(); ++i) { for (uint64 i = 0; i < str.size(); ++i) {
if (i%2) if (i%2)
@ -68,7 +81,7 @@ std::vector<byte> Util::ASCIIToBin(std::string str)
return data; return data;
} }
std::string Util::BinToASCII(std::vector<byte> data) std::string Util::BinToASCII(BinaryData data)
{ {
std::string str; std::string str;
for (uint64 i = 0; i < data.size(); ++i) for (uint64 i = 0; i < data.size(); ++i)
@ -79,16 +92,16 @@ std::string Util::BinToASCII(std::vector<byte> data)
return str; return str;
} }
std::vector<byte> Util::Reverse(std::vector<byte> data) BinaryData Util::Reverse(BinaryData data)
{ {
std::vector<byte> out = data; BinaryData out = data;
std::reverse(out.begin(), out.end()); std::reverse(out.begin(), out.end());
return out; return out;
} }
std::vector<byte> Util::Join(std::vector<byte> v1, std::vector<byte> v2) BinaryData Util::Join(BinaryData v1, BinaryData v2)
{ {
std::vector<byte> v3 = v1; BinaryData v3 = v1;
v3.insert(v3.end(), v2.begin(), v2.end()); v3.insert(v3.end(), v2.begin(), v2.end());
return v3; return v3;
} }

1
src/server/shared/Util.h

@ -18,6 +18,7 @@
namespace Util namespace Util
{ {
std::string Date(const char* format, bool utc = false); std::string Date(const char* format, bool utc = false);
uint32 Date(bool utc = true);
template <typename T> template <typename T>
class SynchronisedQueue class SynchronisedQueue

Loading…
Cancel
Save