udps

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package udps provides functionality to create a UDP Server

Index

Constants

View Source
const (
	KEY_CLIENT_ID = "__CLIENT_ID"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {

	// Metadata
	Meta *model.ClientData

	// ID
	Id string
	// Remote Address
	Addr *net.UDPAddr
	// Tags
	Tags map[string]string

	// Closed Status
	Closed bool
	// contains filtered or unexported fields
}

Client

func (*Client) Close

func (c *Client) Close(code int, reason string)

Close Client

func (*Client) CloseAllStreams

func (c *Client) CloseAllStreams()

Close all Streams Used when closing the Server

func (*Client) CloseStream

func (c *Client) CloseStream(id string)

Close the Stream associated with the ID

func (*Client) GetStream

func (c *Client) GetStream(id string) (stream *udp.Stream, err error)

Return the Stream associated with the ID

func (*Client) ID

func (c *Client) ID() string

Client ID

func (*Client) OpenStream

func (c *Client) OpenStream(metadata map[string]string, data map[string]string) (cstream *udp.Stream, err error)

Open a new Stream to the Client

func (*Client) RegisterHandler

func (c *Client) RegisterHandler(mtype network.MessageType, handler MessageHandler) (err error)

Register Client level MessageHandler

func (*Client) Send

func (c *Client) Send(msg *network.Message) (rmsg *network.Message, err error)

Send a Message to Client

func (*Client) String

func (c *Client) String() string

Stringify

type ClientConnectedHandler

type ClientConnectedHandler func(*Client)

Called on initial client connection

type ClientDisconnectedHandler

type ClientDisconnectedHandler func(*Client)

Called when a client disconnects

type ClientValidateHandler

type ClientValidateHandler func(*net.UDPAddr, *model.ClientValidateData) (cd *model.ClientData, err error)

Called when a new client connects to the server

type Config

type Config struct {
	// Log Tag
	Tag string
	// Listen Address
	Addr *net.UDPAddr
	// Protobuf Unmarshaler
	Unmarshaler network.ChannelUnmarshaler
	// Use QUIC Datagrams
	Datagrams bool

	// TLS Config
	TLS *tls.Config
	// QUIC Config
	Quic *quic.Config
	// contains filtered or unexported fields
}

UDP Server Config

func (*Config) MaxStreams

func (c *Config) MaxStreams() int16

Maximum number of streams per connection

func (*Config) Unmarshalers

func (c *Config) Unmarshalers() (unmarshalers []network.ChannelUnmarshaler)

Protobuf Unmarshalers

func (*Config) UseDatagram

func (c *Config) UseDatagram() bool

Whether to use Datagram mode for QUIC

type MessageHandler

type MessageHandler func(*Client, *network.Message)

Called when a client sends a message

type Server

type Server struct {
	// Config
	Cfg Config

	// Client Validation Handler
	ClientValidateHandler ClientValidateHandler

	// New Stream Handler
	StreamHandler udp.StreamHandler

	// UDP Connection
	UDPConn *net.UDPConn

	// Exit Channel
	Exit chan bool
	// Server Closed Status
	Closed bool
	// contains filtered or unexported fields
}

Server

func New

func New(
	log *logrus.Logger,
	cfg Config,
	clientValidateHandler ClientValidateHandler,
) (s *Server, err error)

Creates a Server

func NewWithConnection

func NewWithConnection(
	log *logrus.Logger,
	cfg Config,
	udpConn *net.UDPConn,
	clientValidateHandler ClientValidateHandler,
) (s *Server, err error)

Creates a Server using an existing net.UDPConn connection

func (*Server) Close

func (s *Server) Close(code int, reason string) (err error)

Close Server

func (*Server) GetClient

func (s *Server) GetClient(id string) (c *Client, err error)

Get connected Client by ID

func (*Server) GetClientsWithTag

func (s *Server) GetClientsWithTag(tag string) (clients []*Client)

Get Clients by Tag

func (*Server) Listen

func (s *Server) Listen() (err error)

Start listening for connections

func (*Server) RegisterHandler

func (s *Server) RegisterHandler(mtype network.MessageType, handler MessageHandler) (err error)

Register a MessageType Handler

func (*Server) SetClientConnectedHandler

func (s *Server) SetClientConnectedHandler(handler ClientConnectedHandler)

Set Client Connected handler

func (*Server) SetClientDisconnectedHandler

func (s *Server) SetClientDisconnectedHandler(handler ClientDisconnectedHandler)

Set Client Disconnect Handler

func (*Server) SetStreamHandler

func (s *Server) SetStreamHandler(handler udp.StreamHandler)

Set New Stream Handler

Jump to

Keyboard shortcuts

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