packet

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2018 License: Apache-2.0 Imports: 9 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) Response

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

Response adds response data to packet

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     RequestID
	RemoteAddress 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 RequestID

type RequestID uint64

RequestID is 64 bit unsigned int request id.

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