Browse Source

Merge branch 'newtouniqueptr' of https://github.com/danielhams/cpp-ethereum into danielhams-newtouniqueptr

Conflicts:
	libethereum/Client.cpp
	libethereum/Client.h
cl-refactor
Gav Wood 11 years ago
parent
commit
43a91b7a7b
  1. 3
      alethzero/MainWin.cpp
  2. 4
      alethzero/MainWin.h
  3. 28
      libethereum/Client.cpp
  4. 20
      libethereum/Client.h
  5. 14
      libethereum/UPnP.cpp
  6. 4
      libethereum/UPnP.h

3
alethzero/MainWin.cpp

@ -26,7 +26,7 @@ Main::Main(QWidget *parent) :
setWindowFlags(Qt::Window); setWindowFlags(Qt::Window);
ui->setupUi(this); ui->setupUi(this);
g_logPost = [=](std::string const& s, char const* c) { simpleDebugOut(s, c); ui->log->addItem(QString::fromStdString(s)); }; g_logPost = [=](std::string const& s, char const* c) { simpleDebugOut(s, c); ui->log->addItem(QString::fromStdString(s)); };
m_client = new Client("AlethZero"); m_client.reset(new Client("AlethZero"));
readSettings(); readSettings();
refresh(); refresh();
@ -63,7 +63,6 @@ Main::~Main()
{ {
g_logPost = simpleDebugOut; g_logPost = simpleDebugOut;
writeSettings(); writeSettings();
delete ui;
} }
void Main::on_about_triggered() void Main::on_about_triggered()

4
alethzero/MainWin.h

@ -54,9 +54,9 @@ private:
eth::u256 total() const; eth::u256 total() const;
eth::u256 value() const; eth::u256 value() const;
Ui::Main *ui; std::unique_ptr<Ui::Main> ui;
eth::Client* m_client; std::unique_ptr<eth::Client> m_client;
QByteArray m_peers; QByteArray m_peers;
QMutex m_guiLock; QMutex m_guiLock;

28
libethereum/Client.cpp

@ -33,7 +33,8 @@ Client::Client(std::string const& _clientVersion, Address _us, std::string const
m_bc(_dbPath), m_bc(_dbPath),
m_stateDB(State::openDB(_dbPath)), m_stateDB(State::openDB(_dbPath)),
m_preMine(_us, m_stateDB), m_preMine(_us, m_stateDB),
m_postMine(_us, m_stateDB) m_postMine(_us, m_stateDB),
m_workState(Active)
{ {
Defaults::setDBPath(_dbPath); Defaults::setDBPath(_dbPath);
@ -45,26 +46,28 @@ Client::Client(std::string const& _clientVersion, Address _us, std::string const
static const char* c_threadName = "eth"; static const char* c_threadName = "eth";
m_work = new thread([&](){ m_work.reset(new thread([&](){
setThreadName(c_threadName); setThreadName(c_threadName);
while (m_workState.load(std::memory_order_acquire) != Deleting)
while (m_workState != Deleting) work(); m_workState = Deleted; work();
}); m_workState.store(Deleted, std::memory_order_release);
}));
} }
Client::~Client() Client::~Client()
{ {
if (m_workState == Active) if (m_workState.load(std::memory_order_acquire) == Active)
m_workState = Deleting; m_workState.store(Deleting, std::memory_order_release);
while (m_workState != Deleted) while (m_workState.load(std::memory_order_acquire) != Deleted)
this_thread::sleep_for(chrono::milliseconds(10)); this_thread::sleep_for(chrono::milliseconds(10));
m_work->join();
} }
void Client::startNetwork(unsigned short _listenPort, std::string const& _seedHost, unsigned short _port, NodeMode _mode, unsigned _peers, string const& _publicIP, bool _upnp) void Client::startNetwork(unsigned short _listenPort, std::string const& _seedHost, unsigned short _port, NodeMode _mode, unsigned _peers, string const& _publicIP, bool _upnp)
{ {
if (m_net) if (m_net.get())
return; return;
m_net = new PeerServer(m_clientVersion, m_bc, 0, _listenPort, _mode, _publicIP, _upnp); m_net.reset(new PeerServer(m_clientVersion, m_bc, 0, _listenPort, _mode, _publicIP, _upnp));
m_net->setIdealPeerCount(_peers); m_net->setIdealPeerCount(_peers);
if (_seedHost.size()) if (_seedHost.size())
connect(_seedHost, _port); connect(_seedHost, _port);
@ -72,15 +75,14 @@ void Client::startNetwork(unsigned short _listenPort, std::string const& _seedHo
void Client::connect(std::string const& _seedHost, unsigned short _port) void Client::connect(std::string const& _seedHost, unsigned short _port)
{ {
if (!m_net) if (!m_net.get())
return; return;
m_net->connect(_seedHost, _port); m_net->connect(_seedHost, _port);
} }
void Client::stopNetwork() void Client::stopNetwork()
{ {
delete m_net; m_net.reset(nullptr);
m_net = nullptr;
} }
void Client::startMining() void Client::startMining()

20
libethereum/Client.h

@ -23,6 +23,7 @@
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <atomic>
#include "Common.h" #include "Common.h"
#include "BlockChain.h" #include "BlockChain.h"
#include "TransactionQueue.h" #include "TransactionQueue.h"
@ -52,6 +53,13 @@ private:
Client* m_client; Client* m_client;
}; };
enum ClientWorkState
{
Active = 0,
Deleting,
Deleted
};
class Client class Client
{ {
public: public:
@ -112,7 +120,7 @@ public:
/// Stop the network subsystem. /// Stop the network subsystem.
void stopNetwork(); void stopNetwork();
/// Get access to the peer server object. This will be null if the network isn't online. /// Get access to the peer server object. This will be null if the network isn't online.
PeerServer* peerServer() const { return m_net; } PeerServer* peerServer() const { return m_net.get(); }
// Mining stuff: // Mining stuff:
@ -134,14 +142,14 @@ private:
BlockChain m_bc; ///< Maintains block database. BlockChain m_bc; ///< Maintains block database.
TransactionQueue m_tq; ///< Maintains list of incoming transactions not yet on the block chain. TransactionQueue m_tq; ///< Maintains list of incoming transactions not yet on the block chain.
Overlay m_stateDB; ///< Acts as the central point for the state database, so multiple States can share it. Overlay m_stateDB; ///< Acts as the central point for the state database, so multiple States can share it.
State m_preMine; ///< The present state of the client. State m_preMine; ///< The present state of the client.
State m_postMine; ///< The state of the client which we're mining (i.e. it'll have all the rewards added). State m_postMine; ///< The state of the client which we're mining (i.e. it'll have all the rewards added).
PeerServer* m_net = nullptr; ///< Should run in background and send us events when blocks found and allow us to send blocks as required. std::unique_ptr<PeerServer> m_net; ///< Should run in background and send us events when blocks found and allow us to send blocks as required.
std::thread* m_work; ///< The work thread. std::unique_ptr<std::thread> m_work;///< The work thread.
std::recursive_mutex m_lock; std::recursive_mutex m_lock;
enum { Active = 0, Deleting, Deleted } m_workState = Active; std::atomic<ClientWorkState> m_workState;
bool m_doMine = false; ///< Are we supposed to be mining? bool m_doMine = false; ///< Are we supposed to be mining?
MineProgress m_mineProgress; MineProgress m_mineProgress;
mutable bool m_restartMining = false; mutable bool m_restartMining = false;

14
libethereum/UPnP.cpp

@ -33,8 +33,8 @@ using namespace eth;
UPnP::UPnP() UPnP::UPnP()
{ {
m_urls = new UPNPUrls; m_urls.reset(new UPNPUrls);
m_data = new IGDdatas; m_data.reset(new IGDdatas);
m_ok = false; m_ok = false;
@ -43,8 +43,8 @@ UPnP::UPnP()
char* descXML; char* descXML;
int descXMLsize = 0; int descXMLsize = 0;
int upnperror = 0; int upnperror = 0;
memset(m_urls, 0, sizeof(struct UPNPUrls)); memset(m_urls.get(), 0, sizeof(struct UPNPUrls));
memset(m_data, 0, sizeof(struct IGDdatas)); memset(m_data.get(), 0, sizeof(struct IGDdatas));
devlist = upnpDiscover(2000, NULL/*multicast interface*/, NULL/*minissdpd socket path*/, 0/*sameport*/, 0/*ipv6*/, &upnperror); devlist = upnpDiscover(2000, NULL/*multicast interface*/, NULL/*minissdpd socket path*/, 0/*sameport*/, 0/*ipv6*/, &upnperror);
if (devlist) if (devlist)
{ {
@ -66,12 +66,12 @@ UPnP::UPnP()
#endif #endif
if (descXML) if (descXML)
{ {
parserootdesc (descXML, descXMLsize, m_data); parserootdesc (descXML, descXMLsize, m_data.get());
free (descXML); descXML = 0; free (descXML); descXML = 0;
#if MINIUPNPC_API_VERSION >= 9 #if MINIUPNPC_API_VERSION >= 9
GetUPNPUrls (m_urls, m_data, dev->descURL, 0); GetUPNPUrls (m_urls.get(), m_data.get(), dev->descURL, 0);
#else #else
GetUPNPUrls (m_urls, m_data, dev->descURL); GetUPNPUrls (m_urls.get(), m_data.get(), dev->descURL);
#endif #endif
m_ok = true; m_ok = true;
} }

4
libethereum/UPnP.h

@ -45,8 +45,8 @@ public:
std::set<int> m_reg; std::set<int> m_reg;
bool m_ok; bool m_ok;
struct UPNPUrls* m_urls; std::unique_ptr<struct UPNPUrls> m_urls;
struct IGDdatas* m_data; std::unique_ptr<struct IGDdatas> m_data;
}; };
} }

Loading…
Cancel
Save