Start moving protocol-specific code to protocol.[ch]pp
authorGiel van Schijndel <me@mortis.eu>
Thu, 11 Aug 2011 16:14:53 +0000 (18:14 +0200)
committerGiel van Schijndel <me@mortis.eu>
Fri, 19 Aug 2011 05:24:38 +0000 (07:24 +0200)
Move CMessageHeader from net.h to protocol.[ch]pp, with the
implementation in the .cpp compilation unit (compiling once is enough).

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.

Indentation changes aside the closest thing to a modification of code is
the addition of the 'TODO' comment (the execution of which requires code
modifications and thus doesn't belong in this commit).

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

src/main.h
src/makefile.linux-mingw
src/makefile.mingw
src/makefile.osx
src/makefile.unix
src/makefile.vc
src/net.h
src/protocol.cpp [new file with mode: 0644]
src/protocol.h [new file with mode: 0644]

index d4b8922..427067b 100644 (file)
@@ -21,7 +21,6 @@ class CKeyItem;
 class CReserveKey;
 class CWalletDB;
 
-class CMessageHeader;
 class CAddress;
 class CInv;
 class CRequestTracker;
index c03cb9d..12f6e21 100644 (file)
@@ -48,6 +48,7 @@ HEADERS = \
     main.h \
     net.h \
     noui.h \
+    protocol.h \
     rpc.h \
     script.h \
     serialize.h \
@@ -75,6 +76,7 @@ OBJS= \
     obj/keystore.o \
     obj/main.o \
     obj/net.o \
+    obj/protocol.o \
     obj/rpc.o \
     obj/script.o \
     obj/util.o \
index 55cb8a7..893700b 100644 (file)
@@ -45,6 +45,7 @@ HEADERS = \
     main.h \
     net.h \
     noui.h \
+    protocol.h \
     rpc.h \
     script.h \
     serialize.h \
@@ -72,6 +73,7 @@ OBJS= \
     obj/keystore.o \
     obj/main.o \
     obj/net.o \
+    obj/protocol.o \
     obj/rpc.o \
     obj/script.o \
     obj/util.o \
index 7172bcc..48908d9 100644 (file)
@@ -45,6 +45,7 @@ HEADERS = \
     main.h \
     net.h \
     noui.h \
+    protocol.h \
     rpc.h \
     script.h \
     serialize.h \
@@ -63,6 +64,7 @@ OBJS= \
     obj/keystore.o \
     obj/main.o \
     obj/net.o \
+    obj/protocol.o \
     obj/rpc.o \
     obj/script.o \
     obj/util.o \
index 7cc72e0..a4f13ae 100644 (file)
@@ -51,6 +51,7 @@ HEADERS = \
     main.h \
     net.h \
     noui.h \
+    protocol.h \
     rpc.h \
     script.h \
     serialize.h \
@@ -69,6 +70,7 @@ OBJS= \
     obj/keystore.o \
     obj/main.o \
     obj/net.o \
+    obj/protocol.o \
     obj/rpc.o \
     obj/script.o \
     obj/util.o \
index edaba80..a5437bc 100644 (file)
@@ -58,6 +58,7 @@ HEADERS = \
     main.h \
     net.h \
     noui.h \
+    protocol.h \
     rpc.h \
     script.h \
     serialize.h \
@@ -77,6 +78,7 @@ OBJS= \
     obj\main.o \
     obj\net.o \
     obj\rpc.o \
+    obj\protocol.o \
     obj\script.o \
     obj\util.o \
     obj\wallet.o \
index 52568ef..7a4706d 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -14,7 +14,8 @@
 #include <arpa/inet.h>
 #endif
 
-class CMessageHeader;
+#include "protocol.h"
+
 class CAddress;
 class CAddrDB;
 class CInv;
@@ -54,101 +55,6 @@ bool BindListenPort(std::string& strError=REF(std::string()));
 void StartNode(void* parg);
 bool StopNode();
 
-
-
-
-
-
-
-
-//
-// Message header
-//  (4) message start
-//  (12) command
-//  (4) size
-//  (4) checksum
-
-extern unsigned char pchMessageStart[4];
-
-class CMessageHeader
-{
-public:
-    enum { COMMAND_SIZE=12 };
-    char pchMessageStart[sizeof(::pchMessageStart)];
-    char pchCommand[COMMAND_SIZE];
-    unsigned int nMessageSize;
-    unsigned int nChecksum;
-
-    CMessageHeader()
-    {
-        memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
-        memset(pchCommand, 0, sizeof(pchCommand));
-        pchCommand[1] = 1;
-        nMessageSize = -1;
-        nChecksum = 0;
-    }
-
-    CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
-    {
-        memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
-        strncpy(pchCommand, pszCommand, COMMAND_SIZE);
-        nMessageSize = nMessageSizeIn;
-        nChecksum = 0;
-    }
-
-    IMPLEMENT_SERIALIZE
-    (
-        READWRITE(FLATDATA(pchMessageStart));
-        READWRITE(FLATDATA(pchCommand));
-        READWRITE(nMessageSize);
-        if (nVersion >= 209)
-            READWRITE(nChecksum);
-    )
-
-    std::string GetCommand()
-    {
-        if (pchCommand[COMMAND_SIZE-1] == 0)
-            return std::string(pchCommand, pchCommand + strlen(pchCommand));
-        else
-            return std::string(pchCommand, pchCommand + COMMAND_SIZE);
-    }
-
-    bool IsValid()
-    {
-        // Check start string
-        if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
-            return false;
-
-        // Check the command string for errors
-        for (char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
-        {
-            if (*p1 == 0)
-            {
-                // Must be all zeros after the first zero
-                for (; p1 < pchCommand + COMMAND_SIZE; p1++)
-                    if (*p1 != 0)
-                        return false;
-            }
-            else if (*p1 < ' ' || *p1 > 0x7E)
-                return false;
-        }
-
-        // Message size
-        if (nMessageSize > MAX_SIZE)
-        {
-            printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
-            return false;
-        }
-
-        return true;
-    }
-};
-
-
-
-
-
-
 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
 
 class CAddress
diff --git a/src/protocol.cpp b/src/protocol.cpp
new file mode 100644 (file)
index 0000000..0bb1da9
--- /dev/null
@@ -0,0 +1,61 @@
+// Copyright (c) 2009-2010 Satoshi Nakamoto
+// Copyright (c) 2011 The Bitcoin developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file license.txt or http://www.opensource.org/licenses/mit-license.php.
+
+#include "protocol.h"
+
+CMessageHeader::CMessageHeader()
+{
+    memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
+    memset(pchCommand, 0, sizeof(pchCommand));
+    pchCommand[1] = 1;
+    nMessageSize = -1;
+    nChecksum = 0;
+}
+
+CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
+{
+    memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
+    strncpy(pchCommand, pszCommand, COMMAND_SIZE);
+    nMessageSize = nMessageSizeIn;
+    nChecksum = 0;
+}
+
+std::string CMessageHeader::GetCommand() const
+{
+    if (pchCommand[COMMAND_SIZE-1] == 0)
+        return std::string(pchCommand, pchCommand + strlen(pchCommand));
+    else
+        return std::string(pchCommand, pchCommand + COMMAND_SIZE);
+}
+
+bool CMessageHeader::IsValid() const
+{
+    // Check start string
+    if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
+        return false;
+
+    // Check the command string for errors
+    for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
+    {
+        if (*p1 == 0)
+        {
+            // Must be all zeros after the first zero
+            for (; p1 < pchCommand + COMMAND_SIZE; p1++)
+                if (*p1 != 0)
+                    return false;
+        }
+        else if (*p1 < ' ' || *p1 > 0x7E)
+            return false;
+    }
+
+    // Message size
+    if (nMessageSize > MAX_SIZE)
+    {
+        printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
+        return false;
+    }
+
+    return true;
+}
diff --git a/src/protocol.h b/src/protocol.h
new file mode 100644 (file)
index 0000000..b5baeb2
--- /dev/null
@@ -0,0 +1,52 @@
+// Copyright (c) 2009-2010 Satoshi Nakamoto
+// Copyright (c) 2011 The Bitcoin developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file license.txt or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef __cplusplus
+# error This header can only be compiled as C++.
+#endif
+
+#ifndef __INCLUDED_PROTOCOL_H__
+#define __INCLUDED_PROTOCOL_H__
+
+#include "serialize.h"
+#include <string>
+
+//
+// Message header
+//  (4) message start
+//  (12) command
+//  (4) size
+//  (4) checksum
+
+extern unsigned char pchMessageStart[4];
+
+class CMessageHeader
+{
+    public:
+        CMessageHeader();
+        CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
+
+        std::string GetCommand() const;
+        bool IsValid() const;
+
+        IMPLEMENT_SERIALIZE
+            (
+             READWRITE(FLATDATA(pchMessageStart));
+             READWRITE(FLATDATA(pchCommand));
+             READWRITE(nMessageSize);
+             if (nVersion >= 209)
+             READWRITE(nChecksum);
+            )
+
+    // TODO: make private (improves encapsulation)
+    public:
+        enum { COMMAND_SIZE=12 };
+        char pchMessageStart[sizeof(::pchMessageStart)];
+        char pchCommand[COMMAND_SIZE];
+        unsigned int nMessageSize;
+        unsigned int nChecksum;
+};
+
+#endif // __INCLUDED_PROTOCOL_H__