refactor: store incoming message as a child property vs overriding itself via pointer magic

This commit is contained in:
Derrick Hammer 2024-01-07 22:55:57 -05:00
parent 0d083e8567
commit 2f5a853ff8
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 8 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import (
type IncomingMessage interface { type IncomingMessage interface {
HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error
SetIncomingMessage(msg IncomingMessage) SetIncomingMessage(msg IncomingMessage)
IncomingMessage() IncomingMessage
Self() IncomingMessage Self() IncomingMessage
SetSelf(self IncomingMessage) SetSelf(self IncomingMessage)
msgpack.CustomDecoder msgpack.CustomDecoder

View File

@ -22,6 +22,7 @@ type IncomingMessageImpl struct {
original []byte original []byte
known bool known bool
self IncomingMessage self IncomingMessage
incoming IncomingMessage
} }
func (i *IncomingMessageImpl) Self() IncomingMessage { func (i *IncomingMessageImpl) Self() IncomingMessage {
@ -53,13 +54,12 @@ func (i *IncomingMessageImpl) Original() []byte {
} }
func (i *IncomingMessageImpl) SetIncomingMessage(msg IncomingMessage) { func (i *IncomingMessageImpl) SetIncomingMessage(msg IncomingMessage) {
if msgImpl, ok := msg.(*IncomingMessageImpl); ok { i.incoming = msg
*i = *msgImpl i.known = true
i.known = true }
} else {
// Handle the error or panic func (i *IncomingMessageImpl) IncomingMessage() IncomingMessage {
panic("msg is not of type *IncomingMessageImpl") return i.incoming
}
} }
func (i *IncomingMessageImpl) GetKind() types.ProtocolMethod { func (i *IncomingMessageImpl) GetKind() types.ProtocolMethod {