Documentation
      ¶
    
    
  
    
  
    Index ¶
- Constants
 - Variables
 - func PacketHash(data []byte) []byte
 - type Handler
 - type HandlerFunc
 - type MType
 - type Message
 - type NetConn
 - type Protocol
 - type Sender
 - type Server
 - func (s *Server) Close()
 - func (s *Server) IsExpectedReply(fromIP net.IP, fromID identity.ID, msg Message) bool
 - func (s *Server) Local() *peer.Local
 - func (s *Server) LocalAddr() *net.UDPAddr
 - func (s *Server) Send(toAddr *net.UDPAddr, data []byte)
 - func (s *Server) SendExpectingReply(toAddr *net.UDPAddr, toID identity.ID, data []byte, replyType MType, ...) <-chan error
 
Constants ¶
const ( // MaxPacketSize specifies the maximum allowed size of packets. // Packets larger than this will be cut and thus treated as invalid. MaxPacketSize = 1280 )
const ( // ResponseTimeout specifies the time limit after which a response must have been received. ResponseTimeout = 500 * time.Millisecond )
Variables ¶
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 ¶
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 ¶
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.
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 ¶
IsExpired checks whether the given UNIX time stamp is too far in the past.
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 ¶
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) IsExpectedReply ¶
IsExpectedReply checks whether the given testMessage matches an expected reply added with SendExpectingReply.
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.