Commit ba189e24 authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Add network protocol and address config options. (#216)

parent 109fe069
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
### Added
- Config option to adjust authoritative match data input queue size. 
- Config option to adjust authoritative match call queue size.
- Config options to allow listening on IPv4/6 and a particular network interface.
- Authoritative match modules now allow a `match_join` callback that triggers when users have completed their join process.

### Changed
+8 −4
Original line number Diff line number Diff line
@@ -185,7 +185,6 @@ func StartApiServer(logger *zap.Logger, startupLogger *zap.Logger, db *sql.DB, j

	// Set up and start GRPC Gateway server.
	s.grpcGatewayServer = &http.Server{
		Addr:           fmt.Sprintf(":%d", config.GetSocket().Port),
		ReadTimeout:    time.Millisecond * time.Duration(int64(config.GetSocket().ReadTimeoutMs)),
		WriteTimeout:   time.Millisecond * time.Duration(int64(config.GetSocket().WriteTimeoutMs)),
		IdleTimeout:    time.Millisecond * time.Duration(int64(config.GetSocket().IdleTimeoutMs)),
@@ -198,7 +197,12 @@ func StartApiServer(logger *zap.Logger, startupLogger *zap.Logger, db *sql.DB, j

	startupLogger.Info("Starting API server gateway for HTTP requests", zap.Int("port", config.GetSocket().Port))
	go func() {
		if err := s.grpcGatewayServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
		listener, err := net.Listen(config.GetSocket().Protocol, fmt.Sprintf("%v:%d", config.GetSocket().Address, config.GetSocket().Port))
		if err != nil {
			startupLogger.Fatal("API server gateway listener failed to start", zap.Error(err))
		}

		if err := s.grpcGatewayServer.Serve(listener); err != nil && err != http.ErrServerClosed {
			startupLogger.Fatal("API server gateway listener failed", zap.Error(err))
		}
	}()
@@ -207,11 +211,11 @@ func StartApiServer(logger *zap.Logger, startupLogger *zap.Logger, db *sql.DB, j
}

func (s *ApiServer) Stop() {
	// 1. Stop GRPC Gateway server first as it sits above GRPC server.
	// 1. Stop GRPC Gateway server first as it sits above GRPC server. This also closes the underlying listener.
	if err := s.grpcGatewayServer.Shutdown(context.Background()); err != nil {
		s.logger.Error("API server gateway listener shutdown failed", zap.Error(err))
	}
	// 2. Stop GRPC server.
	// 2. Stop GRPC server. This also closes the underlying listener.
	s.grpcServer.GracefulStop()
}

+8 −1
Original line number Diff line number Diff line
@@ -94,6 +94,9 @@ func ParseArgs(logger *zap.Logger, args []string) Config {
	if l := len(mainConfig.Name); l < 1 || l > 16 {
		logger.Fatal("Name must be 1-16 characters", zap.String("param", "name"))
	}
	if p := mainConfig.GetSocket().Protocol; p != "tcp" && p != "tcp4" && p != "tcp6" {
		logger.Fatal("Socket protocol must be one of: tcp, tcp4, tcp6", zap.String("socket.protocol", mainConfig.GetSocket().Protocol))
	}
	if mainConfig.GetSocket().PingPeriodMs >= mainConfig.GetSocket().PongWaitMs {
		logger.Fatal("Ping period value must be less than pong wait value", zap.Int("socket.ping_period_ms", mainConfig.GetSocket().PingPeriodMs), zap.Int("socket.pong_wait_ms", mainConfig.GetSocket().PongWaitMs))
	}
@@ -315,7 +318,9 @@ func NewSessionConfig() *SessionConfig {
// SocketConfig is configuration relevant to the transport socket and protocol.
type SocketConfig struct {
	ServerKey           string            `yaml:"server_key" json:"server_key" usage:"Server key to use to establish a connection to the server."`
	Port                int               `yaml:"port" json:"port" usage:"The port for accepting connections from the client, listening on all interfaces."`
	Port                int               `yaml:"port" json:"port" usage:"The port for accepting connections from the client for the given interface(s), address(es), and protocol(s). Default 7350."`
	Address             string            `yaml:"address" json:"address" usage:"The IP address of the interface to listen for client traffic on. Default listen on all available addresses/interfaces."`
	Protocol            string            `yaml:"protocol" json:"protocol" usage:"The network protocol to listen for traffic on. Possible values are 'tcp' for both IPv4 and IPv6, 'tcp4' for IPv4 only, or 'tcp6' for IPv6 only. Default 'tcp'."`
	MaxMessageSizeBytes int64             `yaml:"max_message_size_bytes" json:"max_message_size_bytes" usage:"Maximum amount of data in bytes allowed to be read from the client socket per message. Used for real-time, gRPC and HTTP connections."`
	ReadTimeoutMs       int               `yaml:"read_timeout_ms" json:"read_timeout_ms" usage:"Maximum duration in milliseconds for reading the entire request. Used for HTTP connections."`
	WriteTimeoutMs      int               `yaml:"write_timeout_ms" json:"write_timeout_ms" usage:"Maximum duration in milliseconds before timing out writes of the response. Used for HTTP connections."`
@@ -334,6 +339,8 @@ func NewSocketConfig() *SocketConfig {
	return &SocketConfig{
		ServerKey:           "defaultkey",
		Port:                7350,
		Address:             "",
		Protocol:            "tcp",
		MaxMessageSizeBytes: 4096,
		ReadTimeoutMs:       10 * 1000,
		WriteTimeoutMs:      10 * 1000,
+3 −0
Original line number Diff line number Diff line
@@ -1856,6 +1856,8 @@ func (n *NakamaModule) streamUserJoin(l *lua.LState) int {
	hidden := l.OptBool(4, false)
	// By default persistence is enabled, if the stream supports it.
	persistence := l.OptBool(5, true)
	// By default no status is set.
	status := l.OptString(6, "")

	// Look up the session.
	session := n.sessionRegistry.Get(sessionID)
@@ -1869,6 +1871,7 @@ func (n *NakamaModule) streamUserJoin(l *lua.LState) int {
		Hidden:      hidden,
		Persistence: persistence,
		Username:    session.Username(),
		Status:      status,
	}, false)
	if !success {
		l.RaiseError("tracker rejected new presence, session is closing")