mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-24 15:02:32 +00:00

* [NOD-1118] Added protobufs for the MessageStream * [NOD-1118] Implement some of the basic grpc methods * [NOD-1118] Implemented gRPCConnection send and receive * [NOD-1118] Implemented basic connection loops * [NOD-1118] gRPC server implementation ready * [NOD-1118] Add connection management * [NOD-1118] Sort out the connection loops * [NOD-1118] Add temporary testConnection * [NOD-1118] Send to c.errChan whether error was recieved or not * [NOD-1118] Call OnConnectHandler in time * [NOD-1118] Handle closing connections properly * [NOD-1118] Add comments to exported functions * [NOD-1118] Call server.addConnection on newConnection * [NOD-1118] Add a TODO comment * [NOD-1118] Add a TODO comment * [NOD-1118] Make connection a Stringer * [NOD-1118] Made the connection loops 100% synchronic * [NOD-1118] Make connection.isConnected uint32 * [NOD-1118] Move the Add/Remove connection from grpcConnection to register/unregister connection * [NOD-1118] Convert error messages to lower case * [NOD-1118] Remove protoc inline dependency * [NOD-1118] Fix comment * [NOD-1118] Exit if there was an error starting the protocol manager * [NOD-1118] Fix error message * [NOD-1118] Fixed a few comments * [NOD-1118] Extract listenOn to a method * [NOD-1118] Use !=0 for isConnected * [NOD-1118] Refactor listenOn * [NOD-1118] Add lock on channelWrites in gRPCConnection * [NOD-1118] Rename channelWriteLock -> writeDuringDisconnectLock * [NOD-1118] Reshuffle a comment * [NOD-1118] Add a TODO comment
39 lines
847 B
Go
39 lines
847 B
Go
package protowire
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/kaspanet/kaspad/wire"
|
|
)
|
|
|
|
// ToWireMessage converts a KaspadMessage to its wire.Message representation
|
|
func (x *KaspadMessage) ToWireMessage() (wire.Message, error) {
|
|
message, err := wire.MakeEmptyMessage(x.Command)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
payloadReader := bytes.NewReader(x.Payload)
|
|
err = message.KaspaDecode(payloadReader, wire.ProtocolVersion)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return message, nil
|
|
}
|
|
|
|
// FromWireMessage creates a KaspadMessage from a wire.Message
|
|
func FromWireMessage(message wire.Message) (*KaspadMessage, error) {
|
|
payloadWriter := &bytes.Buffer{}
|
|
|
|
err := message.KaspaEncode(payloadWriter, wire.ProtocolVersion)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &KaspadMessage{
|
|
Command: message.Command(),
|
|
Payload: payloadWriter.Bytes(),
|
|
}, nil
|
|
}
|