mirror of
https://github.com/datarhei/core.git
synced 2025-10-06 16:37:04 +08:00

Commits (Ingo Oppermann): - Add experimental SRT connection stats and logs - Hide /config/reload endpoint in reade-only mode - Add SRT server - Create v16 in go.mod - Fix data races, tests, lint, and update dependencies - Add trailing slash for routed directories (datarhei/restreamer#340) - Allow relative URLs in content in static routes Co-Authored-By: Ingo Oppermann <57445+ioppermann@users.noreply.github.com>
99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
package srt
|
|
|
|
import (
|
|
"errors"
|
|
)
|
|
|
|
// Server is a framework for a SRT server
|
|
type Server struct {
|
|
// The address the SRT server should listen on, e.g. ":6001".
|
|
Addr string
|
|
|
|
// Config is the configuration for a SRT listener.
|
|
Config *Config
|
|
|
|
// HandleConnect will be called for each incoming connection. This
|
|
// allows you to implement your own interpretation of the streamid
|
|
// and authorization. If this is nil, all connections will be
|
|
// rejected.
|
|
HandleConnect AcceptFunc
|
|
|
|
// HandlePublish will be called for a publishing connection.
|
|
HandlePublish func(conn Conn)
|
|
|
|
// HandlePublish will be called for a subscribing connection.
|
|
HandleSubscribe func(conn Conn)
|
|
|
|
ln Listener
|
|
}
|
|
|
|
// ErrServerClosed is returned when the server is about to shutdown.
|
|
var ErrServerClosed = errors.New("srt: server closed")
|
|
|
|
// ListenAndServe starts the SRT server. It blocks until an error happens.
|
|
// If the error is ErrServerClosed the server has shutdown normally.
|
|
func (s *Server) ListenAndServe() error {
|
|
// Set some defaults if required.
|
|
if s.HandlePublish == nil {
|
|
s.HandlePublish = s.defaultHandler
|
|
}
|
|
|
|
if s.HandleSubscribe == nil {
|
|
s.HandleSubscribe = s.defaultHandler
|
|
}
|
|
|
|
if s.Config == nil {
|
|
config := DefaultConfig()
|
|
s.Config = &config
|
|
}
|
|
|
|
// Start listening for incoming connections.
|
|
ln, err := Listen("srt", s.Addr, *s.Config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
s.ln = ln
|
|
|
|
for {
|
|
// Wait for connections.
|
|
conn, mode, err := ln.Accept(s.HandleConnect)
|
|
if err != nil {
|
|
if err == ErrListenerClosed {
|
|
return ErrServerClosed
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
if conn == nil {
|
|
// rejected connection, ignore
|
|
continue
|
|
}
|
|
|
|
if mode == PUBLISH {
|
|
go s.HandlePublish(conn)
|
|
} else {
|
|
go s.HandleSubscribe(conn)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Shutdown will shutdown the server. ListenAndServe will return a ErrServerClosed
|
|
func (s *Server) Shutdown() {
|
|
if s.ln == nil {
|
|
return
|
|
}
|
|
|
|
// Close the listener
|
|
s.ln.Close()
|
|
s.ln = nil
|
|
}
|
|
|
|
func (s *Server) defaultHandler(conn Conn) {
|
|
// Close the incoming connection
|
|
conn.Close()
|
|
}
|