server

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2021 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const DEFAULT_LOGLEVEL int = 0
View Source
const DEFAULT_PORT int = 6837
View Source
const LOG_CHANNEL int = 2
View Source
const LOG_CONNECTION int = 1
View Source
const LOG_DEBUG int = 3
View Source
const LOG_INFO int = 0
View Source
const LOG_PROTOCOL int = 4

Variables

View Source
var Cert string
View Source
var ClientConnected = func(c *Connection) {
	AddClient(c)
}
View Source
var ClientDisconnected = func(c *Connection) {
	client := FindClient(c)
	if client != nil {
		cc := client.GetChannel()
		if cc != nil {
			cc.Remove(client)
		}
	}
	RemoveClient(c)
}
View Source
var EndMessage byte = '\n'
View Source
var MessageReceived = func(c *Connection, pmsg []byte) {
	var err error
	client := FindClient(c)
	if client == nil {
		Log_error("A client object was not found from the connection receiving a message, number " + strconv.Itoa(c.GetID()) + ". Unexpected behavior encountered. Closing connection.")
		c.Close()
		return
	}
	id := client.GetID()
	cc := client.GetChannel()
	if cc != nil {
		pmsg, err = JsonAdd(pmsg, "origin", id)
		if err != nil {
			Log("Error adding origin to message from client "+strconv.Itoa(id)+".\r\n"+err.Error()+"\r\nSending to all clients without origin field.", LOG_DEBUG)
		}
		cc.SendOthers(pmsg, client)
		return
	}
	authErr := Authorize(client, pmsg)
	if authErr != nil {
		Log("Authorization failure for client "+strconv.Itoa(id)+".\r\n"+authErr.Error(), LOG_DEBUG)
		c.Close()
		return
	}
}
View Source
var StopServers context.CancelFunc

Functions

func AddChannel

func AddChannel(name string, c *Client)

func AddClient

func AddClient(c *Connection)

func Authorize

func Authorize(c *Client, data []byte) error

func Configure

func Configure() error

func Encode

func Encode(data Data) ([]byte, error)

func JsonAdd

func JsonAdd(data []byte, key string, value interface{}) ([]byte, error)

func Log

func Log(msg string, level int)

func Log_error

func Log_error(msg string)

func RemoveChannel

func RemoveChannel(name string)

func RemoveClient

func RemoveClient(c *Connection)

func Start

func Start() int

Types

type Client

type Client struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func FindClient

func FindClient(c *Connection) *Client

func (*Client) ClearChannel

func (c *Client) ClearChannel()

func (*Client) Close

func (c *Client) Close()

func (*Client) CloseForce

func (c *Client) CloseForce()

func (*Client) GetChannel

func (c *Client) GetChannel() *ClientChannel

func (*Client) GetConnectionType

func (c *Client) GetConnectionType() string

func (*Client) GetID

func (c *Client) GetID() int

func (*Client) GetVersion

func (c *Client) GetVersion() int

func (*Client) Send

func (c *Client) Send(msg []byte)

func (*Client) SetChannel

func (c *Client) SetChannel(clientChannel *ClientChannel)

func (*Client) SetConnectionType

func (c *Client) SetConnectionType(ctype string)

func (*Client) SetVersion

func (c *Client) SetVersion(version int)

type ClientChannel

type ClientChannel struct {
	sync.Mutex

	ClientsAll    map[int]*Client
	ClientsMaster map[int]*Client
	ClientsSlave  map[int]*Client
	// contains filtered or unexported fields
}

func FindChannel

func FindChannel(name string) *ClientChannel

func NewClientChannel

func NewClientChannel(name string, client *Client) *ClientChannel

func (*ClientChannel) Add

func (c *ClientChannel) Add(client *Client)

func (*ClientChannel) EndIfEmpty

func (c *ClientChannel) EndIfEmpty() bool

func (*ClientChannel) Name

func (c *ClientChannel) Name() string

func (*ClientChannel) Quit

func (c *ClientChannel) Quit()

func (*ClientChannel) Remove

func (c *ClientChannel) Remove(client *Client)

func (*ClientChannel) SendAll

func (c *ClientChannel) SendAll(msg []byte, client *Client)

func (*ClientChannel) SendOthers

func (c *ClientChannel) SendOthers(msg []byte, client *Client)

type ClientData

type ClientData struct {
	ID             int    `json:"id"`
	ConnectionType string `json:"connection_type"`
}

type Connection

type Connection struct {
	sync.Mutex

	Server *Server

	Close context.CancelFunc
	// contains filtered or unexported fields
}

Connection holds info about server connection

func (*Connection) GetID

func (c *Connection) GetID() int

Get client ID

func (*Connection) GetIP

func (c *Connection) GetIP() string

Get client IP

func (*Connection) Send

func (c *Connection) Send(b []byte) error

Send bytes to client

func (*Connection) SetID

func (c *Connection) SetID(id int)

Set client ID

type Data

type Data struct {
	Type             string       `json:"type"`
	Channel          string       `json:"channel,omitempty"`
	ConnectionType   string       `json:"connection_type,omitempty"`
	Version          int          `json:"version,omitempty"`
	Origin           int          `json:"origin,omitempty"`
	Key              string       `json:"key,omitempty"`
	ID               int          `json:"user_id,omitempty"`
	UserIds          []int        `json:"user_ids,omitempty"`
	Clients          []ClientData `json:"clients,omitempty"`
	Client           *ClientData  `json:"client,omitempty"`
	Error            string       `json:"error,omitempty"`
	Motd             string       `json:"motd,omitempty"`
	MotdForceDisplay bool         `json:"force_display,omitempty"`
}

func Decode

func Decode(data []byte) (Data, error)

type Server

type Server struct {
	sync.Mutex
	sync.WaitGroup

	Stop context.CancelFunc
	// contains filtered or unexported fields
}

TCP server

var S4 *Server
var S6 *Server
var SAll *Server

func New

func New(address string) *Server

Creates new tcp server instance

func NewWithTLS

func NewWithTLS(address, certFile, keyFile string) (*Server, error)

func NewWithTLSConfig

func NewWithTLSConfig(address string, config *tls.Config) *Server

func (*Server) Listen

func (s *Server) Listen() error

Listen starts network server

func (*Server) MessageTerminator

func (s *Server) MessageTerminator(terminator byte)

Set message terminator

func (*Server) OnClientClosed

func (s *Server) OnClientClosed(callback func(c *Connection))

Called right after connection closed

func (*Server) OnNewConnection

func (s *Server) OnNewConnection(callback func(c *Connection))

Called right after server starts listening new client

func (*Server) OnNewMessage

func (s *Server) OnNewMessage(callback func(c *Connection, message []byte))

Called when Connection receives new message

Jump to

Keyboard shortcuts

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