Use standard C99 (and Qt) types for 64-bit integers
[novacoin.git] / src / protocol.cpp
index 8d2dbfd..8e58865 100644 (file)
@@ -3,10 +3,12 @@
 // Distributed under the MIT/X11 software license, see the accompanying
 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
 
+#include <stdint.h>
+
 #include "protocol.h"
 #include "util.h"
 
-#ifndef __WXMSW__
+#ifndef WIN32
 # include <arpa/inet.h>
 #endif
 
@@ -15,6 +17,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()
 {
@@ -76,7 +84,7 @@ CAddress::CAddress()
     Init();
 }
 
-CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn)
+CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64_t nServicesIn)
 {
     Init();
     ip = ipIn;
@@ -84,7 +92,7 @@ CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn)
     nServices = nServicesIn;
 }
 
-CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn)
+CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64_t nServicesIn)
 {
     Init();
     ip = sockaddr.sin_addr.s_addr;
@@ -92,25 +100,25 @@ CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn)
     nServices = nServicesIn;
 }
 
-CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64 nServicesIn)
+CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64_t nServicesIn)
 {
     Init();
     Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
 }
 
-CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64 nServicesIn)
+CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64_t nServicesIn)
 {
     Init();
     Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
 }
 
-CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64 nServicesIn)
+CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64_t nServicesIn)
 {
     Init();
     Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
 }
 
-CAddress::CAddress(std::string strIn, bool fNameLookup, uint64 nServicesIn)
+CAddress::CAddress(std::string strIn, bool fNameLookup, uint64_t nServicesIn)
 {
     Init();
     Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
@@ -217,7 +225,7 @@ bool CAddress::IsValid() const
     if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
         return false;
 
-    return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
+    return (ip != 0 && ip != INADDR_NONE && port != htons(std::numeric_limits<unsigned short>::max()));
 }
 
 unsigned char CAddress::GetByte(int n) const
@@ -249,3 +257,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());
+}