Browse Source
Move CMessageHeader from net.h to protocol.[ch]pp, with the implementation in the .cpp compilation unit (compiling once is enough). This commit does *not* and should not modify *any* code, it only moves it from net.h and splits it across protocol.cpp and protocol.hpp. Indentation changes aside the closest thing to a modification of code is the addition of the 'TODO' comment (the execution of which requires code modifications and thus doesn't belong in this commit). Signed-off-by: Giel van Schijndel <me@mortis.eu>miguelfreitas
Giel van Schijndel
14 years ago
9 changed files with 125 additions and 97 deletions
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2011 The Bitcoin developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "protocol.h" |
||||
|
||||
CMessageHeader::CMessageHeader() |
||||
{ |
||||
memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)); |
||||
memset(pchCommand, 0, sizeof(pchCommand)); |
||||
pchCommand[1] = 1; |
||||
nMessageSize = -1; |
||||
nChecksum = 0; |
||||
} |
||||
|
||||
CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) |
||||
{ |
||||
memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)); |
||||
strncpy(pchCommand, pszCommand, COMMAND_SIZE); |
||||
nMessageSize = nMessageSizeIn; |
||||
nChecksum = 0; |
||||
} |
||||
|
||||
std::string CMessageHeader::GetCommand() const |
||||
{ |
||||
if (pchCommand[COMMAND_SIZE-1] == 0) |
||||
return std::string(pchCommand, pchCommand + strlen(pchCommand)); |
||||
else |
||||
return std::string(pchCommand, pchCommand + COMMAND_SIZE); |
||||
} |
||||
|
||||
bool CMessageHeader::IsValid() const |
||||
{ |
||||
// Check start string
|
||||
if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0) |
||||
return false; |
||||
|
||||
// Check the command string for errors
|
||||
for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++) |
||||
{ |
||||
if (*p1 == 0) |
||||
{ |
||||
// Must be all zeros after the first zero
|
||||
for (; p1 < pchCommand + COMMAND_SIZE; p1++) |
||||
if (*p1 != 0) |
||||
return false; |
||||
} |
||||
else if (*p1 < ' ' || *p1 > 0x7E) |
||||
return false; |
||||
} |
||||
|
||||
// Message size
|
||||
if (nMessageSize > MAX_SIZE) |
||||
{ |
||||
printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize); |
||||
return false; |
||||
} |
||||
|
||||
return true; |
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2011 The Bitcoin developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef __cplusplus |
||||
# error This header can only be compiled as C++. |
||||
#endif |
||||
|
||||
#ifndef __INCLUDED_PROTOCOL_H__ |
||||
#define __INCLUDED_PROTOCOL_H__ |
||||
|
||||
#include "serialize.h" |
||||
#include <string> |
||||
|
||||
//
|
||||
// Message header
|
||||
// (4) message start
|
||||
// (12) command
|
||||
// (4) size
|
||||
// (4) checksum
|
||||
|
||||
extern unsigned char pchMessageStart[4]; |
||||
|
||||
class CMessageHeader |
||||
{ |
||||
public: |
||||
CMessageHeader(); |
||||
CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn); |
||||
|
||||
std::string GetCommand() const; |
||||
bool IsValid() const; |
||||
|
||||
IMPLEMENT_SERIALIZE |
||||
( |
||||
READWRITE(FLATDATA(pchMessageStart)); |
||||
READWRITE(FLATDATA(pchCommand)); |
||||
READWRITE(nMessageSize); |
||||
if (nVersion >= 209) |
||||
READWRITE(nChecksum); |
||||
) |
||||
|
||||
// TODO: make private (improves encapsulation)
|
||||
public: |
||||
enum { COMMAND_SIZE=12 }; |
||||
char pchMessageStart[sizeof(::pchMessageStart)]; |
||||
char pchCommand[COMMAND_SIZE]; |
||||
unsigned int nMessageSize; |
||||
unsigned int nChecksum; |
||||
}; |
||||
|
||||
#endif // __INCLUDED_PROTOCOL_H__
|
Loading…
Reference in new issue