packet

package
v0.7.1-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2019 License: Apache-2.0, BSD-3-Clause-Clear Imports: 10 Imported by: 0

Documentation

Overview

Package packet provides network messaging protocol and serialization layer.

Packet can be created with shortcut:

senderAddr, _ := host.NewAddress("127.0.0.1:1337")
receiverAddr, _ := host.NewAddress("127.0.0.1:1338")

sender := host.NewHost(senderAddr)
receiver := host.NewHost(receiverAddr)

msg := packet.NewPingPacket(sender, receiver)

// do something with packet

Or with builder:

builder := packet.NewBuilder()

senderAddr, _ := host.NewAddress("127.0.0.1:1337")
receiverAddr, _ := host.NewAddress("127.0.0.1:1338")

sender := host.NewHost(senderAddr)
receiver := host.NewHost(receiverAddr)

msg := builder.
	Sender(sender).
	Receiver(receiver).
	Type(packet.TypeFindHost).
	Request(&packet.RequestDataFindHost{}).
	Build()

// do something with packet

Packet may be serialized:

msg := &packet.Packet{}
serialized, err := packet.SerializePacket(msg)

if err != nil {
	panic(err.Error())
}

fmt.Println(serialized)

And deserialized therefore:

var buffer bytes.Buffer

// Fill buffer somewhere

msg, err := packet.DeserializePacket(buffer)

if err != nil {
	panic(err.Error())
}

// do something with packet

Index

Constants

View Source
const (
	TestPacket = types.PacketType(1337)
)

Variables

This section is empty.

Functions

func SerializePacket

func SerializePacket(q *Packet) ([]byte, error)

SerializePacket converts packet to byte slice.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder allows lazy building of packets. Each operation returns new copy of a builder.

func NewBuilder

func NewBuilder(sender *host.Host) Builder

NewBuilder returns empty packet builder.

func (Builder) Build

func (cb Builder) Build() (packet *Packet)

Build returns configured packet.

func (Builder) Error

func (cb Builder) Error(err error) Builder

Error adds error description to packet.

func (Builder) Receiver

func (cb Builder) Receiver(host *host.Host) Builder

Receiver sets packet receiver.

func (Builder) Request

func (cb Builder) Request(request interface{}) Builder

Request adds request data to packet.

func (Builder) RequestID added in v0.7.5

func (cb Builder) RequestID(id network.RequestID) Builder

func (Builder) Response

func (cb Builder) Response(response interface{}) Builder

Response adds response data to packet

func (Builder) TraceID added in v0.7.5

func (cb Builder) TraceID(traceID string) Builder

func (Builder) Type

func (cb Builder) Type(packetType types.PacketType) Builder

Type sets packet type.

type Packet

type Packet struct {
	Sender        *host.Host
	Receiver      *host.Host
	Type          types.PacketType
	RequestID     network.RequestID
	RemoteAddress string

	TraceID    string
	Data       interface{}
	Error      error
	IsResponse bool
}

Packet is DHT packet object.

func DeserializePacket

func DeserializePacket(conn io.Reader) (*Packet, error)

DeserializePacket reads packet from io.Reader.

type RequestGetRandomHosts

type RequestGetRandomHosts struct {
	HostsNumber int
}

RequestGetRandomHosts is data for the call that returns random hosts of the Insolar network.

type RequestPulse

type RequestPulse struct {
	Pulse core.Pulse
}

RequestPulse is data received from a pulsar.

type RequestTest added in v0.6.3

type RequestTest struct {
	Data []byte
}

type ResponseGetRandomHosts

type ResponseGetRandomHosts struct {
	Hosts []host.Host
	Error string
}

ResponseGetRandomHosts is the response containing random hosts of the Insolar network.

type ResponsePulse

type ResponsePulse struct {
	Success bool
	Error   string
}

ResponsePulse is the response for a new pulse from a pulsar.

type ResponseTest added in v0.6.3

type ResponseTest struct {
	Number int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL