server

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

Unity Matchmaker Server Library

A Golang game server can be integrated with the platform quickly by using this library. Use this library if your game server integrates with Unity Matchmaker.

A fully-formed usage example can be found here.

Short Demonstration

A basic server can be set up by using the mserver.New() function with the Start(), Stop() and message channels:

package main

import (
	mserver "github.com/Unity-Technologies/unity-gaming-services-go-sdk/matchmaker/server"
	"github.com/Unity-Technologies/unity-gaming-services-go-sdk/game-server-hosting/server"
)

func main() {
	s, err := mserver.New(server.TypeAllocation)
	if err != nil {
		// ...
	}
	
	// Handle server events (i.e. allocation, deallocation, errors)
	done := make(chan struct{})
	go handleEvents(s, done)
	
	if err = s.Start(); err != nil {
		// ...
	}

	if err = s.WaitUntilTerminated(); err != nil {
		close(done)
		// ...
	}
}

func handleEvents(s *mserver.Server, done chan struct{}) {
	for {
		select {
		case <-s.OnAllocate():
			// handle allocation

		case <-s.OnDeallocate():
			// handle deallocation

		case <-s.OnError():
			// handle error

		case <-s.OnConfigurationChanged():
			// handle configuration change

		case <-done:
			return
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBackfillApprove is an error which denotes that backfill approval process has failed.
	ErrBackfillApprove = errors.New("failed to approve backfill ticket")

	// ErrNotAllocated is an error which denotes the action cannot be performed as the server is not yet allocated.
	ErrNotAllocated = errors.New("server is not allocated")
)

Functions

This section is empty.

Types

type BackfillTicket

type BackfillTicket struct {
	// ID represents the backfill ticket ID.
	ID string `json:"id"`

	// Connection represents the IP address and port of the server that creates the backfill.
	// The IP address format is ip:port.
	Connection string `json:"connection"`

	// Attributes represents an object that holds a dictionary of attributes (number or string),
	// indexed by the attribute name. The attributes are compared against the corresponding filters
	// defined in the matchmaking config and used to segment the ticket population into pools.
	// The default pool is used if a pool isn't provided.
	Attributes map[string]float64 `json:"attributes"`
}

BackfillTicket represents a backfill ticket. Documentation: https://services.docs.unity.com/matchmaker/v2/index.html#tag/Backfill/operation/approveBackfillTicket

type Server

type Server struct {
	*gsh.Server
	// contains filtered or unexported fields
}

Server represents an instance of a game server which also handles matchmaker backfill requests.

func New

func New(serverType gsh.Type, opts ...gsh.Option) (*Server, error)

New creates a new instance of Server, denoting which type of server to use.

func (*Server) Start

func (s *Server) Start() error

Start starts the server, opening the configured query port which responds with the configured protocol. The event loop will also listen for changes to the `server.json` configuration file, publishing any changes in the form of allocation or de-allocation messages. The server will also keep any backfill state alive on the allocation ID the server is using. As the server can start in an allocated state, make sure that another goroutine is consuming messages from at least the `OnAllocated()` channel before calling this method.

func (*Server) Stop

func (s *Server) Stop() error

Stop stops the game, pushing a de-allocation message and closing the query port.

func (*Server) WaitUntilTerminated

func (s *Server) WaitUntilTerminated() error

WaitUntilTerminated waits until the server receives a termination signal from the platform. The Unity Gaming Services process management daemon will signal the game server to stop. A graceful stop signal (SIGTERM) will be sent if the game server fleet has been configured to support it.

Jump to

Keyboard shortcuts

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