CSI-ES-2324/Projs/PD1/internal/utils/networking/server.go

36 lines
625 B
Go
Raw Normal View History

package networking
import (
"fmt"
"net"
)
type Server[T any] struct {
listener net.Listener
C chan Connection[T]
}
func NewServer[T any](port int) Server[T]{
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
if err != nil {
panic("Server could not bind to address")
}
return Server[T]{
listener:listener,
C: make(chan Connection[T]),
}
}
func (s *Server[T]) ListenLoop() {
for {
listenerConn, err := s.listener.Accept()
if err != nil {
panic("Server could not accept connection")
}
conn := NewConnection[T](listenerConn)
s.C <- conn
}
}