gosocsvr

package module
v0.0.0-...-3a03188 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2019 License: GPL-3.0 Imports: 8 Imported by: 0

README

gosocsvr

GoLang package for creating elegant socket servers.

Available Options

gosocsvr.DefaultPortRangeMin - this is the start range port

gosocsvr.DefaultPortRangeMax - this is the end range port

gosocsvr.ConfigPort - this is the port the configuration handler uses

gosocsvr.ServicePool - this is a collection of services in use

Handlers

Handlers are functions that accept an input/sender (string, string) and outputs a byte array.

Example

AuthenticationHandler
func AuthenticationHandler(input string, sender string) []byte {
	var inConv = []byte(input)
	var a AuthenticationPayload
	json.Unmarshal(inConv, &a)

	//do some authentication
	
	resp := AuthenticationResponse{
		SessionID: "",
		Message:   "Authentication failed",
		Code:      999,
	}
	return resp.ToJSON()
}
MessageHandler
func MessageHandler(input string, sender string) []byte {
    var inConv = []byte(input)
    var m MessagePayload
    json.Unmarshal(inConv, &m)

	//do some message handling
    
    resp := MessageResponse{Code: 99}
return resp.ToJSON()
}

Implementation

The only public function of the gosocsvr is the Instatiate function.

func InstantiateServerPool(handlerFunctions []func(string, string) []byte)

Parameters

handlerFunctions []func(string, string) []byte - a collection of functions

handlers are used as the server entry points allowing connection via tcp, see above examples of AuthenticationHandler or MessageHandler for an example on use.

Example

func initServerPool() {
	var serverPool = []func(string, string) []byte{
		AuthenticationHandler,
		MessageHandler,
	}
	gosocsvr.InstantiateServerPool(serverPool)
}

Working Examples

You can visit https://github.com/Rakeki/gosocsvr-example to see example code.

Configuration Server

The server will automatically listen on a specified port and output a JSON representation of the handler configuration.

[{ServiceName: "Example1", ServicePort: 2020 },{ServiceName: "Example2", ServicePort: 2022 }]

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//DefaultPortRangeMin - Default min port range
	DefaultPortRangeMin = 8000
	//DefaultPortRangeMax - Default max port range
	DefaultPortRangeMax = 9000
	//ServicePool - ServerPoolEntry slice
	ServicePool []*ServerPoolEntry
	//ConfigPort - Default configuration handler port
	ConfigPort = 50660
)

Functions

func CheckIfPortFree

func CheckIfPortFree(port *int) bool

CheckIfPortFree - Checks a given port to see if anything is currently using it.

func ConfigHandler

func ConfigHandler(input string, sender string) []byte

ConfigHandler - function to output server pool config

func GetAvailablePort

func GetAvailablePort() int

GetAvailablePort - Gets the next available port from a range

func InstantiateServerPool

func InstantiateServerPool(handlerFunctions []func(string, string) []byte)

InstantiateServerPool - This will configure and start the services passed via the functions parameter.

func Listen

func Listen(n *ServerPoolEntry)

Listen - initializes a handler service, this function also starts a given listener.

Types

type ConfigResponse

type ConfigResponse struct {
	ServicePool []Service
}

ConfigResponse -

func (*ConfigResponse) ToJSON

func (c *ConfigResponse) ToJSON() []byte

ToJSON - Converts object to JSON

type ServerPoolEntry

type ServerPoolEntry struct {
	Name          string
	PortAllocated int
	Handler       func(string, string) []byte
	IsActive      bool
	HasError      error
}

ServerPoolEntry - contains server pool entry information.

func (*ServerPoolEntry) ListenAsync

func (n *ServerPoolEntry) ListenAsync()

ListenAsync - initializes a handler service, this function also starts a given listener as a go func.

type Service

type Service struct {
	ServiceName string
	ServicePort int
}

Service -

Jump to

Keyboard shortcuts

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