Improve incapsulation. Yep, finally.
[novacoin.git] / src / protocol.h
index ed2ca59..352a247 100644 (file)
@@ -3,38 +3,28 @@
 // Distributed under the MIT/X11 software license, see the accompanying
 // file COPYING 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 "netbase.h"
 #include <string>
+#include <limits>
+#include <vector>
 #include "uint256.h"
 
-#define PPCOIN_PORT  7777
-#define RPC_PORT     8344
-#define TESTNET_PORT 17777
-
+extern uint32_t nNetworkID;
 extern bool fTestNet;
-
-void GetMessageStart(unsigned char pchMessageStart[], bool fPersistent = false);
-
-static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
+inline uint16_t GetDefaultPort()
 {
-    return testnet ? TESTNET_PORT : PPCOIN_PORT;
+    return static_cast<uint16_t>(fTestNet ? 17777 : 7777);
 }
 
-
-/** Message header.
- * (4) message start.
- * (12) command.
- * (4) size.
- * (4) checksum.
- */
+// Message header.
+// (4) network identifier.
+// (12) command.
+// (4) size.
+// (4) checksum.
 class CMessageHeader
 {
     public:
@@ -46,7 +36,7 @@ class CMessageHeader
 
         IMPLEMENT_SERIALIZE
             (
-             READWRITE(FLATDATA(pchMessageStart));
+             READWRITE(nNetworkID);
              READWRITE(FLATDATA(pchCommand));
              READWRITE(nMessageSize);
              READWRITE(nChecksum);
@@ -54,17 +44,25 @@ class CMessageHeader
 
     // TODO: make private (improves encapsulation)
     public:
-        enum { COMMAND_SIZE=12 };
-        unsigned char pchMessageStart[4];
+        enum {
+            MESSAGE_START_SIZE=4,
+            COMMAND_SIZE=12,
+            MESSAGE_SIZE_SIZE=4,
+            CHECKSUM_SIZE=4,
+
+            MESSAGE_SIZE_OFFSET=MESSAGE_START_SIZE+COMMAND_SIZE,
+            CHECKSUM_OFFSET=MESSAGE_SIZE_OFFSET+MESSAGE_SIZE_SIZE
+        };
+        uint32_t nNetworkID;
         char pchCommand[COMMAND_SIZE];
-        unsigned int nMessageSize;
-        unsigned int nChecksum;
+        uint32_t nMessageSize;
+        uint32_t nChecksum;
 };
 
 /** nServices flags */
 enum
 {
-    NODE_NETWORK = (1 << 0),
+    NODE_NETWORK = (1 << 0)
 };
 
 /** A CService with information about it as peer */
@@ -72,9 +70,7 @@ class CAddress : public CService
 {
     public:
         CAddress();
-        explicit CAddress(CService ipIn, uint64 nServicesIn=NODE_NETWORK);
-
-        void Init();
+        explicit CAddress(CService ipIn, uint64_t nServicesIn=NODE_NETWORK);
 
         IMPLEMENT_SERIALIZE
             (
@@ -91,17 +87,15 @@ class CAddress : public CService
              READWRITE(*pip);
             )
 
-        void print() const;
-
     // TODO: make private (improves encapsulation)
     public:
-        uint64 nServices;
+        uint64_t nServices;
 
         // disk and network only
         unsigned int nTime;
 
         // memory only
-        int64 nLastTry;
+        int64_t nLastTry;
 };
 
 /** inv message data */
@@ -123,12 +117,14 @@ class CInv
         bool IsKnownType() const;
         const char* GetCommand() const;
         std::string ToString() const;
-        void print() const;
+
+        int GetType() const { return type; }
+        uint256 GetHash() const { return hash; }
 
     // TODO: make private (improves encapsulation)
-    public:
-        int type;
-        uint256 hash;
+    private:
+        int type = 0;
+        uint256 hash = 0;
 };
 
 #endif // __INCLUDED_PROTOCOL_H__