Update all copyrights to 2012
[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 license.txt 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 "util.h"
16 #include <string>
17 #include "uint256.h"
18
19 extern bool fTestNet;
20 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
21 {
22     return testnet ? 18333 : 8333;
23 }
24
25 //
26 // Message header
27 //  (4) message start
28 //  (12) command
29 //  (4) size
30 //  (4) checksum
31
32 extern unsigned char pchMessageStart[4];
33
34 class CMessageHeader
35 {
36     public:
37         CMessageHeader();
38         CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
39
40         std::string GetCommand() const;
41         bool IsValid() const;
42
43         IMPLEMENT_SERIALIZE
44             (
45              READWRITE(FLATDATA(pchMessageStart));
46              READWRITE(FLATDATA(pchCommand));
47              READWRITE(nMessageSize);
48              if (nVersion >= 209)
49              READWRITE(nChecksum);
50             )
51
52     // TODO: make private (improves encapsulation)
53     public:
54         enum { COMMAND_SIZE=12 };
55         char pchMessageStart[sizeof(::pchMessageStart)];
56         char pchCommand[COMMAND_SIZE];
57         unsigned int nMessageSize;
58         unsigned int nChecksum;
59 };
60
61 enum
62 {
63     NODE_NETWORK = (1 << 0),
64 };
65
66 class CAddress : public CService
67 {
68     public:
69         CAddress();
70         explicit CAddress(CService ipIn, uint64 nServicesIn=NODE_NETWORK);
71
72         void Init();
73
74         IMPLEMENT_SERIALIZE
75             (
76              CAddress* pthis = const_cast<CAddress*>(this);
77              CService* pip = (CService*)pthis;
78              if (fRead)
79                  pthis->Init();
80              if (nType & SER_DISK)
81              READWRITE(nVersion);
82              if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
83              READWRITE(nTime);
84              READWRITE(nServices);
85              READWRITE(*pip);
86             )
87
88         void print() const;
89
90     // TODO: make private (improves encapsulation)
91     public:
92         uint64 nServices;
93
94         // disk and network only
95         unsigned int nTime;
96
97         // memory only
98         unsigned int nLastTry;
99 };
100
101 class CInv
102 {
103     public:
104         CInv();
105         CInv(int typeIn, const uint256& hashIn);
106         CInv(const std::string& strType, const uint256& hashIn);
107
108         IMPLEMENT_SERIALIZE
109         (
110             READWRITE(type);
111             READWRITE(hash);
112         )
113
114         friend bool operator<(const CInv& a, const CInv& b);
115
116         bool IsKnownType() const;
117         const char* GetCommand() const;
118         std::string ToString() const;
119         void print() const;
120
121     // TODO: make private (improves encapsulation)
122     public:
123         int type;
124         uint256 hash;
125 };
126
127 #endif // __INCLUDED_PROTOCOL_H__