network

package
v0.0.0-...-32a2d26 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2018 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

clients must make send request eventually or be considered disconnected package implements disconnect notification for all other clients in the same room

Index

Constants

View Source
const (
	GET  = http.MethodGet
	POST = http.MethodPost

	ClientDefaultTimeout    = time.Second / 30
	ClientLargeTimeout      = time.Second * 5
	ClientDefaultPingPeriod = time.Second / 10
	ClientLostPingsNumber   = 3

	ServerDefaultRoomUpdatePeriod = ClientDefaultPingPeriod
	ServerDefaultLastSeenTimeout  = 3 * ServerDefaultRoomUpdatePeriod
)
View Source
const (
	COMMAND_ROOMBROADCAST = "B"
	COMMAND_CLIENTREQUEST = "C"
	COMMAND_REQUESTSTATE  = "S"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(opts ClientOpts) (*Client, error)

func (*Client) PauseReason

func (c *Client) PauseReason() PauseReason

func (*Client) RequestNewState

func (c *Client) RequestNewState(wanted string, dropCommands bool)

func (*Client) SendRequest

func (c *Client) SendRequest(command string)

func (*Client) SendRoomBroadcast

func (c *Client) SendRoomBroadcast(command string)

func (*Client) Start

func (c *Client) Start()

type ClientOpts

type ClientOpts struct {
	//default ClientDefaultTimeout
	Timeout time.Duration

	//default ClientDefaultPingPeriod
	PingPeriod time.Duration

	Addr string

	Room, Role string

	//Specific self disconnect (server lost). may be needed later , but in general use Pause
	OnReconnect  func()
	OnDisconnect func()

	//Any reason's pause of game process (disconnect self, disconnect other, loading new state self or other
	//Specific reason may be getted by PauseReason()
	OnPause   func()
	OnUnpause func()

	OnCommonSend func() []byte
	OnCommonRecv func(data []byte, readOwnPart bool)

	OnStateChanged func(wanted string)

	//async, must close result chan then done
	OnGetStateData func([]byte)

	OnCommand func(command string)
}

type CommonReq

type CommonReq struct {
	DataSent bool
	Data     string

	//Client -> Server
	Message wrnt.Message

	//for Server->Client confirmation
	ClientConfirmN int
}

type CommonResp

type CommonResp struct {
	Data string

	//Server->Client
	Message wrnt.Message
}

type MetricResp

type MetricResp struct {
	RoomCount   int
	RoomNames   []string
	OnlineTotal int
	RoomOnline  map[string]int
	States      map[string]string

	Second ServerMetricMsg
	Total  ServerMetricMsg
}

func (MetricResp) String

func (m MetricResp) String() string

type PauseReason

type PauseReason struct {
	PingLost   bool
	IsFull     bool
	IsCoherent bool
	CurState   string
	WantState  string
}

func (PauseReason) String

func (pr PauseReason) String() string

type PingResp

type PingResp struct {
	//for Client -> Server confirmation
	ServerConfirmN int
	Room           RoomState
}

type RoomCheckGetSetter

type RoomCheckGetSetter interface {
	//mb role separation needed, but now Common and State data get full and common
	GetRoomCommon(room string) ([]byte, error)
	SetRoomCommon(room string, data []byte) error
	IsValidState(room string, state string) bool
	RdyStateData(room string, state string)
	GetStateData(room string) []byte

	OnCommand(room string, role string, command string)
	OnKillRoom(roomname string)
}

Interface all-in-one for server implementation

type RoomState

type RoomState struct {
	//room specific
	IsFull      bool
	IsCoherent  bool
	RdyServData bool
	Wanted      string
}

network.Server - network.Client ping response IsFull - all needed roles are online IsCoherent - state of room is confirmed by all roles RdyServData - Server downloaded data for new state (needed while not coherent) Wanted - Wanted state

func (RoomState) String

func (s RoomState) String() string

type Server

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

func NewServer

func NewServer(opts ServerOpts) *Server

NewServer creates a server listening

func (*Server) AddCommand

func (s *Server) AddCommand(roomName string, command string)

sends command to all clients in room

func (*Server) Close

func (s *Server) Close() error

func (*Server) KillRoom

func (s *Server) KillRoom(roomName string) error

func (*Server) Metric

func (s *Server) Metric() MetricResp

func (*Server) SetNewState

func (srv *Server) SetNewState(roomName, newState string, dropCommands bool) bool

type ServerMetricMsg

type ServerMetricMsg struct {
	Ping, State, Common ServerReqMetric
}

func (*ServerMetricMsg) Add

func (a *ServerMetricMsg) Add(b ServerMetricMsg)

func (ServerMetricMsg) String

func (m ServerMetricMsg) String() string

type ServerOpts

type ServerOpts struct {
	Addr string

	//hooks for server implementation
	RoomServ   RoomCheckGetSetter
	StartState string

	NeededRoles []string

	RoomUpdatePeriod time.Duration
	LastSeenTimeout  time.Duration

	ConsoleHandler func(w http.ResponseWriter, r *http.Request)
}

Options to create and start server

type ServerReqMetric

type ServerReqMetric struct {
	//requests per second
	RPS int
	//request.body bytes per second
	ReqBPS int
	//responce.body bytes per second
	RespBPS int
}

func (*ServerReqMetric) Add

func (a *ServerReqMetric) Add(b ServerReqMetric)

func (ServerReqMetric) String

func (m ServerReqMetric) String() string

type StateDataResp

type StateDataResp struct {
	StateData   []byte
	StartCommon []byte
}

Jump to

Keyboard shortcuts

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