Documentation ¶
Overview ¶
Package server is used to wrap the p2p services to define multiple req-res messages under one protocol.
Index ¶
- Variables
- func SerializeResponse(data []byte, err error) []byte
- type Message
- type MessageServer
- func (p *MessageServer) Close()
- func (p *MessageServer) RegisterBytesMsgHandler(msgType MessageType, reqHandler func(context.Context, []byte) ([]byte, error))
- func (p *MessageServer) RegisterMsgHandler(msgType MessageType, reqHandler func(context.Context, Message) ([]byte, error))
- func (p *MessageServer) SendRequest(ctx context.Context, msgType MessageType, payload []byte, ...) error
- type MessageType
- type Service
Constants ¶
This section is empty.
Variables ¶
var ErrBadRequest = errors.New("unable to parse request")
ErrBadRequest is returned to the peer upon failure to parse the request
var ErrRequestTimeout = errors.New("request timed out")
ErrRequestTimeout is returned to the caller when the request times out
var ErrShuttingDown = errors.New("node is shutting down")
ErrShuttingDown is returned to the peer when the node is shutting down
Functions ¶
func SerializeResponse ¶
SerializeResponse serializes the response data returned by SendRequest
Types ¶
type Message ¶
type Message interface { service.DirectMessage Data() service.Data }
Message is helper type for `MessegeServer` messages.
type MessageServer ¶
type MessageServer struct { ReqID uint64 //request id (must be declared first to ensure 8 byte alignment on 32-bit systems, required by atomic operations) log.Log // contains filtered or unexported fields }
MessageServer is a request-response multiplexer on top of the p2p layer. it provides a way to register message types on top of a protocol and declare request and response handlers. it matches incoming responses to requests.
func NewMsgServer ¶
func NewMsgServer(ctx context.Context, network Service, name string, requestLifetime time.Duration, c chan service.DirectMessage, logger log.Log) *MessageServer
NewMsgServer registers a protocol and returns a new server to declare request and response handlers on.
func (*MessageServer) RegisterBytesMsgHandler ¶
func (p *MessageServer) RegisterBytesMsgHandler(msgType MessageType, reqHandler func(context.Context, []byte) ([]byte, error))
RegisterBytesMsgHandler sets the handler to act on a specific message request.
func (*MessageServer) RegisterMsgHandler ¶
func (p *MessageServer) RegisterMsgHandler(msgType MessageType, reqHandler func(context.Context, Message) ([]byte, error))
RegisterMsgHandler sets the handler to act on a specific message request.
func (*MessageServer) SendRequest ¶
func (p *MessageServer) SendRequest(ctx context.Context, msgType MessageType, payload []byte, address p2pcrypto.PublicKey, resHandler func(msg []byte), errorHandler func(err error)) error
SendRequest sends a request of a specific message.
type MessageType ¶
type MessageType uint32
MessageType is a uint32 used to distinguish between server messages inside a single protocol.
const ( // PingPong is the ping protocol ID PingPong MessageType = iota // GetAddresses is the findnode protocol ID GetAddresses // LayerBlocksMsg is used to fetch block IDs for a given layer hash LayerBlocksMsg // AtxIDsMsg is used to fetch ATXs for a given epoch AtxIDsMsg // TortoiseBeaconMsg is used to fetch tortoise beacon messages for a given epoch TortoiseBeaconMsg // Fetch is used to fetch data for a given hash Fetch // RequestTimeSync is used for time synchronization with peers. RequestTimeSync )
type Service ¶
type Service interface { RegisterDirectProtocolWithChannel(protocol string, ingressChannel chan service.DirectMessage) chan service.DirectMessage SendWrappedMessage(ctx context.Context, nodeID p2pcrypto.PublicKey, protocol string, payload *service.DataMsgWrapper) error }
Service is the subset of method used by MessageServer for p2p communications.