server

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: Apache-2.0, BSD-2-Clause Imports: 15 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// MaxPacketSize specifies the maximum allowed size of packets.
	// Packets larger than this will be cut and thus treated as invalid.
	MaxPacketSize = 1280
)
View Source
const (
	// ResponseTimeout specifies the time limit after which a response must have been received.
	ResponseTimeout = 500 * time.Millisecond
)

Variables

View Source
var (
	// ErrTimeout is returned when an expected response was not received in time.
	ErrTimeout = errors.New("response timeout")

	// ErrClosed means that the server was shut down before a response could be received.
	ErrClosed = errors.New("socket closed")

	// ErrNoMessage is returned when the package did not contain any data.
	ErrNoMessage = errors.New("packet does not contain a message")

	// ErrInvalidMessage means that no handler could process the received message.
	ErrInvalidMessage = errors.New("invalid message")
)

Functions

func PacketHash

func PacketHash(data []byte) []byte

PacketHash returns the hash of a packet.

Types

type Handler

type Handler interface {
	// HandleMessage is called for each incoming message.
	// It returns true, if that particular message type can be processed by the current Handler.
	HandleMessage(s *Server, fromAddr *net.UDPAddr, from *identity.Identity, data []byte) (bool, error)
}

A Handler reacts to an incoming message.

type HandlerFunc

type HandlerFunc func(*Server, *net.UDPAddr, *identity.Identity, []byte) (bool, error)

The HandlerFunc type is an adapter to allow the use of ordinary functions as Server handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (HandlerFunc) HandleMessage

func (f HandlerFunc) HandleMessage(s *Server, fromAddr *net.UDPAddr, from *identity.Identity, data []byte) (bool, error)

HandleMessage returns f(s, from, data).

type MType

type MType uint32

MType is the type of message type enum.

type Message

type Message interface {
	proto.Message

	// Type returns the type of the corresponding message as an enum.
	Type() MType
}

Message extends the proto.testMessage interface with additional type.

type NetConn

type NetConn interface {
	// Close closes the connection.
	Close() error

	// LocalAddr returns the local network address.
	LocalAddr() net.Addr

	// ReadFromUDP acts like ReadFrom but returns a UDPAddr.
	ReadFromUDP([]byte) (int, *net.UDPAddr, error)
	// WriteToUDP acts like WriteTo but takes a UDPAddr.
	WriteToUDP([]byte, *net.UDPAddr) (int, error)
}

NetConn defines the interface required for a connection.

type Protocol

type Protocol struct {
	Sender Sender // interface to send own requests
}

Protocol provides a basis for server protocols handling incoming messages.

func (*Protocol) IsExpired

func (p *Protocol) IsExpired(ts int64) bool

IsExpired checks whether the given UNIX time stamp is too far in the past.

func (*Protocol) Send

func (p *Protocol) Send(to *peer.Peer, data []byte)

Send sends the data to the given peer.

func (*Protocol) SendExpectingReply

func (p *Protocol) SendExpectingReply(dstAddr *net.UDPAddr, toID identity.ID, data []byte, replyType MType, callback func(Message) bool) <-chan error

SendExpectingReply sends request data to a peer and expects a response of the given type. On an incoming matching request the callback is executed to perform additional verification steps.

type Sender

type Sender interface {
	Send(toAddr *net.UDPAddr, data []byte)
	SendExpectingReply(toAddr *net.UDPAddr, toID identity.ID, data []byte, replyType MType, callback func(Message) bool) <-chan error
}

The Sender interface specifies common method required to send requests.

type Server

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

Server offers the functionality to start a server that handles requests and responses from peers.

func Serve

func Serve(local *peer.Local, conn NetConn, log *logger.Logger, h ...Handler) *Server

Serve starts a new peer server using the given transport layer for communication. Sent data is signed using the identity of the local peer, received data with a valid peer signature is handled according to the provided Handler.

func (*Server) Close

func (s *Server) Close()

Close shuts down the server.

func (*Server) IsExpectedReply

func (s *Server) IsExpectedReply(fromIP net.IP, fromID identity.ID, msg Message) bool

IsExpectedReply checks whether the given testMessage matches an expected reply added with SendExpectingReply.

func (*Server) Local

func (s *Server) Local() *peer.Local

Local returns the the local peer.

func (*Server) LocalAddr

func (s *Server) LocalAddr() *net.UDPAddr

LocalAddr returns the address of the local peer in string form.

func (*Server) Send

func (s *Server) Send(toAddr *net.UDPAddr, data []byte)

Send sends a message to the given address.

func (*Server) SendExpectingReply

func (s *Server) SendExpectingReply(toAddr *net.UDPAddr, toID identity.ID, data []byte, replyType MType, callback func(Message) bool) <-chan error

SendExpectingReply sends a message to the given address and tells the Server to expect a reply message with the given specifications. If eventually nil is returned, a matching message was received.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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