Bump version to 0.5.9
[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 inline unsigned short GetDefaultPort()
21 {
22     return static_cast<unsigned short>(fTestNet ? 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         IMPLEMENT_SERIALIZE
82             (
83              CAddress* pthis = const_cast<CAddress*>(this);
84              CService* pip = (CService*)pthis;
85              if (fRead)
86                  pthis->Init();
87              if (nType & SER_DISK)
88                  READWRITE(nVersion);
89              if ((nType & SER_DISK) ||
90                  (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
91                  READWRITE(nTime);
92              READWRITE(nServices);
93              READWRITE(*pip);
94             )
95
96     // TODO: make private (improves encapsulation)
97     public:
98         uint64_t nServices;
99
100         // disk and network only
101         unsigned int nTime;
102
103         // memory only
104         int64_t nLastTry;
105 };
106
107 /** inv message data */
108 class CInv
109 {
110     public:
111         CInv();
112         CInv(int typeIn, const uint256& hashIn);
113         CInv(const std::string& strType, const uint256& hashIn);
114
115         IMPLEMENT_SERIALIZE
116         (
117             READWRITE(type);
118             READWRITE(hash);
119         )
120
121         friend bool operator<(const CInv& a, const CInv& b);
122
123         bool IsKnownType() const;
124         const char* GetCommand() const;
125         std::string ToString() const;
126
127     // TODO: make private (improves encapsulation)
128     public:
129         int type;
130         uint256 hash;
131 };
132
133 #endif // __INCLUDED_PROTOCOL_H__