d771406b7cfdf4b582ed0d9fbf18b1fd0dea5dc7
[novacoin.git] / src / protocol.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #ifndef __cplusplus
7 # error This header can only be compiled as C++.
8 #endif
9
10 #ifndef __INCLUDED_PROTOCOL_H__
11 #define __INCLUDED_PROTOCOL_H__
12
13 #include "serialize.h"
14 #include "netbase.h"
15 #include <string>
16 #include "uint256.h"
17
18 extern bool fTestNet;
19 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
20 {
21     return testnet ? 17777 : 7777;
22 }
23
24
25 extern unsigned char pchMessageStart[4];
26
27 /** Message header.
28  * (4) message start.
29  * (12) command.
30  * (4) size.
31  * (4) checksum.
32  */
33 class CMessageHeader
34 {
35     public:
36         CMessageHeader();
37         CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
38
39         std::string GetCommand() const;
40         bool IsValid() const;
41
42         IMPLEMENT_SERIALIZE
43             (
44              READWRITE(FLATDATA(pchMessageStart));
45              READWRITE(FLATDATA(pchCommand));
46              READWRITE(nMessageSize);
47              READWRITE(nChecksum);
48             )
49
50     // TODO: make private (improves encapsulation)
51     public:
52         enum {
53             MESSAGE_START_SIZE=sizeof(::pchMessageStart),
54             COMMAND_SIZE=12,
55             MESSAGE_SIZE_SIZE=sizeof(int),
56             CHECKSUM_SIZE=sizeof(int),
57
58             MESSAGE_SIZE_OFFSET=MESSAGE_START_SIZE+COMMAND_SIZE,
59             CHECKSUM_OFFSET=MESSAGE_SIZE_OFFSET+MESSAGE_SIZE_SIZE
60         };
61         char pchMessageStart[MESSAGE_START_SIZE];
62         char pchCommand[COMMAND_SIZE];
63         unsigned int nMessageSize;
64         unsigned int nChecksum;
65 };
66
67 /** nServices flags */
68 enum
69 {
70     NODE_NETWORK = (1 << 0),
71 };
72
73 /** A CService with information about it as peer */
74 class CAddress : public CService
75 {
76     public:
77         CAddress();
78         explicit CAddress(CService ipIn, uint64 nServicesIn=NODE_NETWORK);
79
80         void Init();
81
82         IMPLEMENT_SERIALIZE
83             (
84              CAddress* pthis = const_cast<CAddress*>(this);
85              CService* pip = (CService*)pthis;
86              if (fRead)
87                  pthis->Init();
88              if (nType & SER_DISK)
89                  READWRITE(nVersion);
90              if ((nType & SER_DISK) ||
91                  (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
92                  READWRITE(nTime);
93              READWRITE(nServices);
94              READWRITE(*pip);
95             )
96
97         void print() const;
98
99     // TODO: make private (improves encapsulation)
100     public:
101         uint64 nServices;
102
103         // disk and network only
104         unsigned int nTime;
105
106         // memory only
107         int64 nLastTry;
108 };
109
110 /** inv message data */
111 class CInv
112 {
113     public:
114         CInv();
115         CInv(int typeIn, const uint256& hashIn);
116         CInv(const std::string& strType, const uint256& hashIn);
117
118         IMPLEMENT_SERIALIZE
119         (
120             READWRITE(type);
121             READWRITE(hash);
122         )
123
124         friend bool operator<(const CInv& a, const CInv& b);
125
126         bool IsKnownType() const;
127         const char* GetCommand() const;
128         std::string ToString() const;
129         void print() const;
130
131     // TODO: make private (improves encapsulation)
132     public:
133         int type;
134         uint256 hash;
135 };
136
137 #endif // __INCLUDED_PROTOCOL_H__