|
|
@ -21,7 +21,7 @@ n - 2 bytes - actual message payload |
|
|
|
---- content end ----- |
|
|
|
---- content end ----- |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
// The message category enum
|
|
|
|
// MessageCategory defines the message category enum
|
|
|
|
type MessageCategory byte |
|
|
|
type MessageCategory byte |
|
|
|
|
|
|
|
|
|
|
|
//Consensus and other message categories
|
|
|
|
//Consensus and other message categories
|
|
|
@ -39,26 +39,26 @@ const MessageCategoryBytes = 1 |
|
|
|
// MessageTypeBytes is the number of bytes message type takes
|
|
|
|
// MessageTypeBytes is the number of bytes message type takes
|
|
|
|
const MessageTypeBytes = 1 |
|
|
|
const MessageTypeBytes = 1 |
|
|
|
|
|
|
|
|
|
|
|
// Get the message category from the p2p message content
|
|
|
|
// GetMessageCategory gets the message category from the p2p message content
|
|
|
|
func GetMessageCategory(message []byte) (MessageCategory, error) { |
|
|
|
func GetMessageCategory(message []byte) (MessageCategory, error) { |
|
|
|
if len(message) < MessageCategoryBytes { |
|
|
|
if len(message) < MessageCategoryBytes { |
|
|
|
return 0, errors.New("Failed to get message category: no data available.") |
|
|
|
return 0, errors.New("failed to get message category: no data available") |
|
|
|
} |
|
|
|
} |
|
|
|
return MessageCategory(message[MessageCategoryBytes-1]), nil |
|
|
|
return MessageCategory(message[MessageCategoryBytes-1]), nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get the message type from the p2p message content
|
|
|
|
// GetMessageType gets the message type from the p2p message content
|
|
|
|
func GetMessageType(message []byte) (byte, error) { |
|
|
|
func GetMessageType(message []byte) (byte, error) { |
|
|
|
if len(message) < MessageCategoryBytes+MessageTypeBytes { |
|
|
|
if len(message) < MessageCategoryBytes+MessageTypeBytes { |
|
|
|
return 0, errors.New("Failed to get message type: no data available.") |
|
|
|
return 0, errors.New("failed to get message type: no data available") |
|
|
|
} |
|
|
|
} |
|
|
|
return byte(message[MessageCategoryBytes+MessageTypeBytes-1]), nil |
|
|
|
return byte(message[MessageCategoryBytes+MessageTypeBytes-1]), nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get the node message payload from the p2p message content
|
|
|
|
// GetMessagePayload gets the node message payload from the p2p message content
|
|
|
|
func GetMessagePayload(message []byte) ([]byte, error) { |
|
|
|
func GetMessagePayload(message []byte) ([]byte, error) { |
|
|
|
if len(message) < MessageCategoryBytes+MessageTypeBytes { |
|
|
|
if len(message) < MessageCategoryBytes+MessageTypeBytes { |
|
|
|
return []byte{}, errors.New("Failed to get message payload: no data available.") |
|
|
|
return []byte{}, errors.New("failed to get message payload: no data available") |
|
|
|
} |
|
|
|
} |
|
|
|
return message[MessageCategoryBytes+MessageTypeBytes:], nil |
|
|
|
return message[MessageCategoryBytes+MessageTypeBytes:], nil |
|
|
|
} |
|
|
|
} |
|
|
|