|
|
|
@ -33,32 +33,32 @@ const ( |
|
|
|
|
// TODO: add more types
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// MESSAGE_CATEGORY_BYTES is the number of bytes message category takes
|
|
|
|
|
const MESSAGE_CATEGORY_BYTES = 1 |
|
|
|
|
// MessageCategoryBytes is the number of bytes message category takes
|
|
|
|
|
const MessageCategoryBytes = 1 |
|
|
|
|
|
|
|
|
|
// MESSAGE_TYPE_BYTES is the number of bytes message type takes
|
|
|
|
|
const MESSAGE_TYPE_BYTES = 1 |
|
|
|
|
// MessageTypeBytes is the number of bytes message type takes
|
|
|
|
|
const MessageTypeBytes = 1 |
|
|
|
|
|
|
|
|
|
// Get the message category from the p2p message content
|
|
|
|
|
func GetMessageCategory(message []byte) (MessageCategory, error) { |
|
|
|
|
if len(message) < MESSAGE_CATEGORY_BYTES { |
|
|
|
|
if len(message) < MessageCategoryBytes { |
|
|
|
|
return 0, errors.New("Failed to get message category: no data available.") |
|
|
|
|
} |
|
|
|
|
return MessageCategory(message[MESSAGE_CATEGORY_BYTES-1]), nil |
|
|
|
|
return MessageCategory(message[MessageCategoryBytes-1]), nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Get the message type from the p2p message content
|
|
|
|
|
func GetMessageType(message []byte) (byte, error) { |
|
|
|
|
if len(message) < MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES { |
|
|
|
|
if len(message) < MessageCategoryBytes+MessageTypeBytes { |
|
|
|
|
return 0, errors.New("Failed to get message type: no data available.") |
|
|
|
|
} |
|
|
|
|
return byte(message[MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES-1]), nil |
|
|
|
|
return byte(message[MessageCategoryBytes+MessageTypeBytes-1]), nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Get the node message payload from the p2p message content
|
|
|
|
|
func GetMessagePayload(message []byte) ([]byte, error) { |
|
|
|
|
if len(message) < MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES { |
|
|
|
|
if len(message) < MessageCategoryBytes+MessageTypeBytes { |
|
|
|
|
return []byte{}, errors.New("Failed to get message payload: no data available.") |
|
|
|
|
} |
|
|
|
|
return message[MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES:], nil |
|
|
|
|
return message[MessageCategoryBytes+MessageTypeBytes:], nil |
|
|
|
|
} |
|
|
|
|