Fix MinGW compilation issues.
[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 <limits>
17 #include "uint256.h"
18
19 extern bool fTestNet;
20 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
21 {
22     return testnet ? 17777 : 7777;
23 }
24
25
26 extern unsigned char pchMessageStart[4];
27
28 /** Message header.
29  * (4) message start.
30  * (12) command.
31  * (4) size.
32  * (4) checksum.
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              READWRITE(nChecksum);
49             )
50
51     // TODO: make private (improves encapsulation)
52     public:
53         enum {
54             MESSAGE_START_SIZE=sizeof(::pchMessageStart),
55             COMMAND_SIZE=12,
56             MESSAGE_SIZE_SIZE=sizeof(int),
57             CHECKSUM_SIZE=sizeof(int),
58
59             MESSAGE_SIZE_OFFSET=MESSAGE_START_SIZE+COMMAND_SIZE,
60             CHECKSUM_OFFSET=MESSAGE_SIZE_OFFSET+MESSAGE_SIZE_SIZE
61         };
62         char pchMessageStart[MESSAGE_START_SIZE];
63         char pchCommand[COMMAND_SIZE];
64         unsigned int nMessageSize;
65         unsigned int nChecksum;
66 };
67
68 /** nServices flags */
69 enum
70 {
71     NODE_NETWORK = (1 << 0),
72 };
73
74 /** A CService with information about it as peer */
75 class CAddress : public CService
76 {
77     public:
78         CAddress();
79         explicit CAddress(CService ipIn, uint64_t nServicesIn=NODE_NETWORK);
80
81         void Init();
82
83         IMPLEMENT_SERIALIZE
84             (
85              CAddress* pthis = const_cast<CAddress*>(this);
86              CService* pip = (CService*)pthis;
87              if (fRead)
88                  pthis->Init();
89              if (nType & SER_DISK)
90                  READWRITE(nVersion);
91              if ((nType & SER_DISK) ||
92                  (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
93                  READWRITE(nTime);
94              READWRITE(nServices);
95              READWRITE(*pip);
96             )
97
98         void print() const;
99
100     // TODO: make private (improves encapsulation)
101     public:
102         uint64_t nServices;
103
104         // disk and network only
105         unsigned int nTime;
106
107         // memory only
108         int64_t nLastTry;
109 };
110
111 /** inv message data */
112 class CInv
113 {
114     public:
115         CInv();
116         CInv(int typeIn, const uint256& hashIn);
117         CInv(const std::string& strType, const uint256& hashIn);
118
119         IMPLEMENT_SERIALIZE
120         (
121             READWRITE(type);
122             READWRITE(hash);
123         )
124
125         friend bool operator<(const CInv& a, const CInv& b);
126
127         bool IsKnownType() const;
128         const char* GetCommand() const;
129         std::string ToString() const;
130         void print() const;
131
132     // TODO: make private (improves encapsulation)
133     public:
134         int type;
135         uint256 hash;
136 };
137
138 #endif // __INCLUDED_PROTOCOL_H__