Remove server

This commit is contained in:
Owen
2025-04-04 11:35:33 -04:00
parent 8fb9468d08
commit 31e5d4e3bd

View File

@@ -23,117 +23,6 @@ const (
packetSize = 13
)
// Server handles listening for connection check requests
type Server struct {
conn *net.UDPConn
listenAddr string
shutdownCh chan struct{}
isRunning bool
runningLock sync.Mutex
}
// NewServer creates a new connection test server
func NewServer(listenAddr string) *Server {
return &Server{
listenAddr: listenAddr,
shutdownCh: make(chan struct{}),
}
}
// Start begins listening for connection test packets
func (s *Server) Start() error {
s.runningLock.Lock()
defer s.runningLock.Unlock()
if s.isRunning {
return nil
}
addr, err := net.ResolveUDPAddr("udp", s.listenAddr)
if err != nil {
return err
}
s.conn, err = net.ListenUDP("udp", addr)
if err != nil {
return err
}
s.isRunning = true
go s.handleConnections()
log.Printf("Server listening on %s", s.listenAddr)
return nil
}
// Stop shuts down the server
func (s *Server) Stop() {
s.runningLock.Lock()
defer s.runningLock.Unlock()
if !s.isRunning {
return
}
close(s.shutdownCh)
if s.conn != nil {
s.conn.Close()
}
s.isRunning = false
log.Println("Server stopped")
}
// handleConnections processes incoming packets
func (s *Server) handleConnections() {
buffer := make([]byte, packetSize)
for {
select {
case <-s.shutdownCh:
return
default:
// Set read deadline to avoid blocking forever
s.conn.SetReadDeadline(time.Now().Add(1 * time.Second))
n, addr, err := s.conn.ReadFromUDP(buffer)
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
// Just a timeout, keep going
continue
}
log.Printf("Error reading from UDP: %v", err)
continue
}
if n != packetSize {
continue // Ignore malformed packets
}
// Check magic header
magic := binary.BigEndian.Uint32(buffer[0:4])
if magic != magicHeader {
continue // Not our packet
}
// Check packet type
packetType := buffer[4]
if packetType != packetTypeRequest {
continue // Not a request packet
}
// Keep the timestamp the same (for RTT calculation)
// Just change the packet type to response
buffer[4] = packetTypeResponse
// Send response
_, err = s.conn.WriteToUDP(buffer, addr)
if err != nil {
log.Printf("Error sending response: %v", err)
}
}
}
}
// Client handles checking connectivity to a server
type Client struct {
conn *net.UDPConn