github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/p2p/message.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package p2p
    19  
    20  import (
    21  	"github.com/rs/zerolog/log"
    22  	"google.golang.org/protobuf/proto"
    23  )
    24  
    25  const (
    26  	// TopicKeepAlive is keep alive endpoint.
    27  	TopicKeepAlive = "p2p-keepalive"
    28  
    29  	// TopicSessionCreate is a session create endpoint for p2p communication.
    30  	TopicSessionCreate = "p2p-session-create"
    31  	// TopicSessionAcknowledge is a session acknowledge endpoint for p2p communication.
    32  	TopicSessionAcknowledge = "p2p-session-acknowledge"
    33  	// TopicSessionStatus is a session status notification for p2p communication.
    34  	TopicSessionStatus = "p2p-session-connectivity-status"
    35  	// TopicSessionDestroy is a session destroy endpoint for p2p communication.
    36  	TopicSessionDestroy = "p2p-session-destroy"
    37  
    38  	// TopicPaymentMessage is a payment messages endpoint for p2p communication.
    39  	TopicPaymentMessage = "p2p-payment-message"
    40  	// TopicPaymentInvoice is a payment invoices endpoint for p2p communication.
    41  	TopicPaymentInvoice = "p2p-payment-invoice"
    42  )
    43  
    44  // Message represent message with data bytes.
    45  type Message struct {
    46  	Data []byte
    47  }
    48  
    49  // UnmarshalProto is convenient helper to unmarshal message data into strongly typed proto message.
    50  func (m *Message) UnmarshalProto(to proto.Message) error {
    51  	return proto.Unmarshal(m.Data, to)
    52  }
    53  
    54  // ProtoMessage is convenient helper to return message with marshaled proto data bytes.
    55  func ProtoMessage(m proto.Message) *Message {
    56  	pbBytes, err := proto.Marshal(m)
    57  	if err != nil {
    58  		// In general this should never happen as passed input value
    59  		// should implement proto.Message interface.
    60  		log.Err(err).Msg("Failed to marshal proto message")
    61  		return &Message{Data: []byte{}}
    62  	}
    63  	return &Message{Data: pbBytes}
    64  }
    65  
    66  const (
    67  	statusCodeOK                 = 1
    68  	statusCodePublicErr          = 2
    69  	statusCodeInternalErr        = 3
    70  	statusCodeHandlerNotFoundErr = 4
    71  )
    72  
    73  // transportMsg is internal structure for sending and receiving messages.
    74  type transportMsg struct {
    75  	// Header fields.
    76  	id         uint64
    77  	statusCode uint64
    78  	topic      string
    79  	msg        string
    80  
    81  	// Data field.
    82  	data []byte
    83  }
    84  
    85  func (m *transportMsg) readFrom(conn wireReader) error {
    86  	return conn.readMsg(m)
    87  }
    88  
    89  func (m *transportMsg) writeTo(conn wireWriter) error {
    90  	return conn.writeMsg(m)
    91  }