[PD1] Error handling project-wide

This commit is contained in:
Afonso Franco 2024-04-28 22:02:13 +01:00
parent f5b3726673
commit b918211736
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
13 changed files with 364 additions and 245 deletions

View file

@ -2,7 +2,6 @@ package networking
import (
"crypto/tls"
"fmt"
"log"
"net"
)
@ -16,16 +15,16 @@ type Server[T any] struct {
C chan Connection[T]
}
func NewServer[T any](serverTLSConfigProvider ServerTLSConfigProvider, port int) Server[T] {
func NewServer[T any](serverTLSConfigProvider ServerTLSConfigProvider) (Server[T], error) {
listener, err := tls.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port), serverTLSConfigProvider.GetServerTLSConfig())
listener, err := tls.Listen("tcp", "127.0.0.1:8080", serverTLSConfigProvider.GetServerTLSConfig())
if err != nil {
log.Fatalln("Server could not bind to address")
return Server[T]{}, err
}
return Server[T]{
listener: listener,
C: make(chan Connection[T]),
}
}, nil
}
func (s *Server[T]) ListenLoop() {
@ -39,7 +38,9 @@ func (s *Server[T]) ListenLoop() {
if !ok {
log.Fatalln("Connection is not a TLS connection")
}
tlsConn.Handshake()
if err := tlsConn.Handshake(); err != nil {
log.Fatalln(err)
}
state := tlsConn.ConnectionState()
if len(state.PeerCertificates) == 0 {