Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Add TCPServer documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaralo committed Oct 27, 2023
1 parent e6fcdfa commit 2893df6
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pkg/transport/network/tcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import (
"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
)

type Address string
// Address is an alias for string encoded network addresses (e.g. "127.0.0.1:4040")
type Address = string

// serverTargets are the addresses are expected to connect and their respective target name
type serverTargets = map[Address]abstraction.TransportTarget

// Assertion to check the TCPServer is a TCPSource
var _ TCPSource = &TCPServer{}

// TCPServer is a TCPSource that gets connections from clients
//
// TCPServer must be used as any other TCPSource.
type TCPServer struct {
name abstraction.TransportTarget
targets serverTargets
Expand All @@ -23,6 +28,8 @@ type TCPServer struct {
onError errorCallback
}

// NewServer creates a new TCPServer with the given name, local address and target connections.
// It returns a non nil error if it fails to resolve the local address or when the listener creation fails.
func NewServer(name abstraction.TransportTarget, laddr string, targets serverTargets) (*TCPServer, error) {
localAddr, err := net.ResolveTCPAddr("tcp", laddr)
if err != nil {
Expand All @@ -42,23 +49,35 @@ func NewServer(name abstraction.TransportTarget, laddr string, targets serverTar
}, nil
}

// SetKeepalive sets the keepalive that will be applied to the connections established
func (server *TCPServer) SetKeepalive(keepalive time.Duration) {
server.keepalive = keepalive
}

// SetOnConnection registers the callback that will be used when a connection is made
func (server *TCPServer) SetOnConnection(callback connectionCallback) {
server.onConnection = callback
}

// SetOnError registers the callback that will be used when an error making the connection occurs.
func (server *TCPServer) SetOnError(callback errorCallback) {
server.onError = callback
}

// accept result is an auxiliary struct to pass the results of listener.Accept through a channel
type acceptResult struct {
conn *net.TCPConn
err error
}

// Run starts the TCPServer.
//
// When a conneciton is established, it first checks if it's valid, then configures it and lastly
// notifies of the connection with the OnConnection callback.
//
// Callers should make sure the OnConnection and the OnError callbacks are provided before calling Run.
//
// The server execution can be stopped by sending a message to the cancel channel.
func (server *TCPServer) Run(cancel <-chan struct{}) {
defer server.listener.Close()
for {
Expand Down Expand Up @@ -101,6 +120,7 @@ func (server *TCPServer) Run(cancel <-chan struct{}) {
}
}

// configureConn is a helper to apply all configuration to a connection.
func (server *TCPServer) configureConn(conn *net.TCPConn) error {
err := conn.SetKeepAlive(server.keepalive > 0)
if err != nil {
Expand Down

0 comments on commit 2893df6

Please sign in to comment.