Move CInv to protocol.[ch]pp
authorGiel van Schijndel <me@mortis.eu>
Thu, 11 Aug 2011 16:49:03 +0000 (18:49 +0200)
committerGiel van Schijndel <me@mortis.eu>
Fri, 19 Aug 2011 05:24:38 +0000 (07:24 +0200)
This commit does *not* and should not modify *any* code, it only moves
it from net.h and splits it across protocol.cpp and protocol.hpp.

Signed-off-by: Giel van Schijndel <me@mortis.eu>

src/net.h
src/protocol.cpp
src/protocol.h

index 66f3949..efac1f4 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -17,7 +17,6 @@
 #include "protocol.h"
 
 class CAddrDB;
-class CInv;
 class CRequestTracker;
 class CNode;
 class CBlockIndex;
@@ -52,85 +51,6 @@ enum
     MSG_BLOCK,
 };
 
-static const char* ppszTypeName[] =
-{
-    "ERROR",
-    "tx",
-    "block",
-};
-
-class CInv
-{
-public:
-    int type;
-    uint256 hash;
-
-    CInv()
-    {
-        type = 0;
-        hash = 0;
-    }
-
-    CInv(int typeIn, const uint256& hashIn)
-    {
-        type = typeIn;
-        hash = hashIn;
-    }
-
-    CInv(const std::string& strType, const uint256& hashIn)
-    {
-        int i;
-        for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
-        {
-            if (strType == ppszTypeName[i])
-            {
-                type = i;
-                break;
-            }
-        }
-        if (i == ARRAYLEN(ppszTypeName))
-            throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
-        hash = hashIn;
-    }
-
-    IMPLEMENT_SERIALIZE
-    (
-        READWRITE(type);
-        READWRITE(hash);
-    )
-
-    friend inline bool operator<(const CInv& a, const CInv& b)
-    {
-        return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
-    }
-
-    bool IsKnownType() const
-    {
-        return (type >= 1 && type < ARRAYLEN(ppszTypeName));
-    }
-
-    const char* GetCommand() const
-    {
-        if (!IsKnownType())
-            throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
-        return ppszTypeName[type];
-    }
-
-    std::string ToString() const
-    {
-        return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
-    }
-
-    void print() const
-    {
-        printf("CInv(%s)\n", ToString().c_str());
-    }
-};
-
-
-
-
-
 class CRequestTracker
 {
 public:
index 8d2dbfd..48784b9 100644 (file)
@@ -15,6 +15,12 @@ bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, in
 bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
 
 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
+static const char* ppszTypeName[] =
+{
+    "ERROR",
+    "tx",
+    "block",
+};
 
 CMessageHeader::CMessageHeader()
 {
@@ -249,3 +255,58 @@ void CAddress::print() const
 {
     printf("CAddress(%s)\n", ToString().c_str());
 }
+
+CInv::CInv()
+{
+    type = 0;
+    hash = 0;
+}
+
+CInv::CInv(int typeIn, const uint256& hashIn)
+{
+    type = typeIn;
+    hash = hashIn;
+}
+
+CInv::CInv(const std::string& strType, const uint256& hashIn)
+{
+    int i;
+    for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
+    {
+        if (strType == ppszTypeName[i])
+        {
+            type = i;
+            break;
+        }
+    }
+    if (i == ARRAYLEN(ppszTypeName))
+        throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
+    hash = hashIn;
+}
+
+bool operator<(const CInv& a, const CInv& b)
+{
+    return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
+}
+
+bool CInv::IsKnownType() const
+{
+    return (type >= 1 && type < ARRAYLEN(ppszTypeName));
+}
+
+const char* CInv::GetCommand() const
+{
+    if (!IsKnownType())
+        throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
+    return ppszTypeName[type];
+}
+
+std::string CInv::ToString() const
+{
+    return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
+}
+
+void CInv::print() const
+{
+    printf("CInv(%s)\n", ToString().c_str());
+}
index 009f67d..53d3eef 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "serialize.h"
 #include <string>
+#include "uint256.h"
 
 extern bool fTestNet;
 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
@@ -120,4 +121,30 @@ class CAddress
         unsigned int nLastTry;
 };
 
+class CInv
+{
+    public:
+        CInv();
+        CInv(int typeIn, const uint256& hashIn);
+        CInv(const std::string& strType, const uint256& hashIn);
+
+        IMPLEMENT_SERIALIZE
+        (
+            READWRITE(type);
+            READWRITE(hash);
+        )
+
+        friend bool operator<(const CInv& a, const CInv& b);
+
+        bool IsKnownType() const;
+        const char* GetCommand() const;
+        std::string ToString() const;
+        void print() const;
+
+    // TODO: make private (improves encapsulation)
+    public:
+        int type;
+        uint256 hash;
+};
+
 #endif // __INCLUDED_PROTOCOL_H__