ece61840d587cb05c9258182cce18c781bb2257f
[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         void print() const;
97
98     // TODO: make private (improves encapsulation)
99     public:
100         uint64_t nServices;
101
102         // disk and network only
103         unsigned int nTime;
104
105         // memory only
106         int64_t nLastTry;
107 };
108
109 /** inv message data */
110 class CInv
111 {
112     public:
113         CInv();
114         CInv(int typeIn, const uint256& hashIn);
115         CInv(const std::string& strType, const uint256& hashIn);
116
117         IMPLEMENT_SERIALIZE
118         (
119             READWRITE(type);
120             READWRITE(hash);
121         )
122
123         friend bool operator<(const CInv& a, const CInv& b);
124
125         bool IsKnownType() const;
126         const char* GetCommand() const;
127         std::string ToString() const;
128         void print() const;
129
130     // TODO: make private (improves encapsulation)
131     public:
132         int type;
133         uint256 hash;
134 };
135
136 #endif // __INCLUDED_PROTOCOL_H__