protocol.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef __cplusplus
  3. # error This header can only be compiled as C++.
  4. #endif
  5. #ifndef __INCLUDED_PROTOCOL_H__
  6. #define __INCLUDED_PROTOCOL_H__
  7. #include "serialize.h"
  8. #include "netbase.h"
  9. #include <string>
  10. #include "uint256.h"
  11. extern bool fTestNet;
  12. static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
  13. {
  14. return testnet ? 17408 : 7408;
  15. }
  16. extern unsigned char pchMessageStart[4];
  17. class CMessageHeader
  18. {
  19. public:
  20. CMessageHeader();
  21. CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
  22. std::string GetCommand() const;
  23. bool IsValid() const;
  24. IMPLEMENT_SERIALIZE
  25. (
  26. READWRITE(FLATDATA(pchMessageStart));
  27. READWRITE(FLATDATA(pchCommand));
  28. READWRITE(nMessageSize);
  29. READWRITE(nChecksum);
  30. )
  31. public:
  32. enum {
  33. MESSAGE_START_SIZE=sizeof(::pchMessageStart),
  34. COMMAND_SIZE=12,
  35. MESSAGE_SIZE_SIZE=sizeof(int),
  36. CHECKSUM_SIZE=sizeof(int),
  37. MESSAGE_SIZE_OFFSET=MESSAGE_START_SIZE+COMMAND_SIZE,
  38. CHECKSUM_OFFSET=MESSAGE_SIZE_OFFSET+MESSAGE_SIZE_SIZE
  39. };
  40. char pchMessageStart[MESSAGE_START_SIZE];
  41. char pchCommand[COMMAND_SIZE];
  42. unsigned int nMessageSize;
  43. unsigned int nChecksum;
  44. };
  45. enum
  46. {
  47. NODE_NETWORK = (1 << 0),
  48. };
  49. class CAddress : public CService
  50. {
  51. public:
  52. CAddress();
  53. explicit CAddress(CService ipIn, uint64 nServicesIn=NODE_NETWORK);
  54. void Init();
  55. IMPLEMENT_SERIALIZE
  56. (
  57. CAddress* pthis = const_cast<CAddress*>(this);
  58. CService* pip = (CService*)pthis;
  59. if (fRead)
  60. pthis->Init();
  61. if (nType & SER_DISK)
  62. READWRITE(nVersion);
  63. if ((nType & SER_DISK) ||
  64. (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
  65. READWRITE(nTime);
  66. READWRITE(nServices);
  67. READWRITE(*pip);
  68. )
  69. void print() const;
  70. public:
  71. uint64 nServices;
  72. // disk and network only
  73. unsigned int nTime;
  74. // memory only
  75. int64 nLastTry;
  76. };
  77. class CInv
  78. {
  79. public:
  80. CInv();
  81. CInv(int typeIn, const uint256& hashIn);
  82. CInv(const std::string& strType, const uint256& hashIn);
  83. IMPLEMENT_SERIALIZE
  84. (
  85. READWRITE(type);
  86. READWRITE(hash);
  87. )
  88. friend bool operator<(const CInv& a, const CInv& b);
  89. bool IsKnownType() const;
  90. const char* GetCommand() const;
  91. std::string ToString() const;
  92. void print() const;
  93. public:
  94. int type;
  95. uint256 hash;
  96. };
  97. #endif // __INCLUDED_PROTOCOL_H__