From a17cad3404ad44bf62f173b9001dcd298193e807 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 27 Aug 2014 09:37:00 +0200 Subject: [PATCH] A start. --- libethereum/PeerServer.h | 106 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/libethereum/PeerServer.h b/libethereum/PeerServer.h index 5d12c807c..c38d49df4 100644 --- a/libethereum/PeerServer.h +++ b/libethereum/PeerServer.h @@ -40,12 +40,116 @@ namespace eth class RLPStream; class TransactionQueue; class BlockQueue; +/* +class BasePeerServer +{ + friend class BasePeerSession; + +public: + /// Start server, listening for connections on the given port. + BasePeerServer(std::string const& _clientVersion, u256 _networkId, unsigned short _port, std::string const& _publicAddress = std::string(), bool _upnp = true); + /// Start server, listening for connections on a system-assigned port. + BasePeerServer(std::string const& _clientVersion, u256 _networkId, std::string const& _publicAddress = std::string(), bool _upnp = true); + /// Start server, but don't listen. + BasePeerServer(std::string const& _clientVersion, u256 _networkId); + + /// Will block on network process events. + virtual ~BasePeerServer(); + + /// Closes all peers. + void disconnectPeers(); + + virtual unsigned protocolVersion(); + + /// Connect to a peer explicitly. + void connect(std::string const& _addr, unsigned short _port = 30303) noexcept; + void connect(bi::tcp::endpoint const& _ep); + + /// Conduct I/O, polling, syncing, whatever. + /// Ideally all time-consuming I/O is done in a background thread or otherwise asynchronously, but you get this call every 100ms or so anyway. + /// This won't touch alter the blockchain. + void process() { if (isInitialised()) m_ioService.poll(); } + + /// @returns true iff we have the a peer of the given id. + bool havePeer(Public _id) const; + + /// Set ideal number of peers. + void setIdealPeerCount(unsigned _n) { m_idealPeerCount = _n; } + + /// Get peer information. + std::vector peers(bool _updatePing = false) const; + + /// Get number of peers connected; equivalent to, but faster than, peers().size(). + size_t peerCount() const { Guard l(x_peers); return m_peers.size(); } + + /// Ping the peers, to update the latency information. + void pingAll(); + /// Get the port we're listening on currently. + unsigned short listenPort() const { return m_public.port(); } + + /// Serialise the set of known peers. + bytes savePeers() const; + + /// Deserialise the data and populate the set of known peers. + void restorePeers(bytesConstRef _b); + + void registerPeer(std::shared_ptr _s); + +protected: + /// Called when the session has provided us with a new peer we can connect to. + void noteNewPeers() {} + + void seal(bytes& _b); + void populateAddresses(); + void determinePublic(std::string const& _publicAddress, bool _upnp); + void ensureAccepting(); + + void growPeers(); + void prunePeers(); + + /// Check to see if the network peer-state initialisation has happened. + bool isInitialised() const { return m_latestBlockSent; } + /// Initialises the network peer-state, doing the stuff that needs to be once-only. @returns true if it really was first. + bool ensureInitialised(TransactionQueue& _tq); + + std::map potentialPeers(); + + std::string m_clientVersion; + + unsigned short m_listenPort; + + ba::io_service m_ioService; + bi::tcp::acceptor m_acceptor; + bi::tcp::socket m_socket; + + UPnP* m_upnp = nullptr; + bi::tcp::endpoint m_public; + KeyPair m_key; + + u256 m_networkId; + + mutable std::mutex x_peers; + mutable std::map> m_peers; // mutable because we flush zombie entries (null-weakptrs) as regular maintenance from a const method. + + mutable std::recursive_mutex m_incomingLock; + std::map> m_incomingPeers; + std::vector m_freePeers; + + std::chrono::steady_clock::time_point m_lastPeersRequest; + unsigned m_idealPeerCount = 5; + + std::vector m_addresses; + std::vector m_peerAddresses; + + bool m_accepting = false; +}; +*/ /** * @brief The PeerServer class * @warning None of this is thread-safe. You have been warned. */ -class PeerServer +class PeerServer//: public BasePeerServer { friend class PeerSession;