Fix: fuck udp

This commit is contained in:
2025-10-16 08:09:19 +08:00
parent 0dea850cfa
commit 4f1a17dea1
4 changed files with 27 additions and 56 deletions

View File

@@ -3,6 +3,7 @@ package utils
import (
"fmt"
"net"
"time"
"gitea.konchin.com/ytshih/inp2025/types"
"github.com/vmihailenco/msgpack/v5"
@@ -11,6 +12,8 @@ import (
const (
BUFFER_SIZE int = 1024
MAGIC_NUMBER int = 114514
LISTEN_TIMEOUT = 200 * time.Millisecond
)
type UDPReqType int
@@ -36,24 +39,36 @@ func ListenUDPData(
return ListenUDP(port, dataCh, nil)
}
func Ping(endpoint string) error {
func Ping(endpoints []string) ([]string, error) {
pingCh := make(chan string)
local, shutdown, err := ListenUDP(0, nil, pingCh)
if err != nil {
return err
return []string{}, err
}
defer shutdown()
err = SendRawPayload(endpoint, UDPPayload{
MagicNumber: MAGIC_NUMBER,
Endpoint: local,
Type: UDPReqTypePingRequest,
})
if err != nil {
return err
for _, endpoint := range endpoints {
SendRawPayload(endpoint, UDPPayload{
MagicNumber: MAGIC_NUMBER,
Endpoint: local,
Type: UDPReqTypePingRequest,
})
}
doneCh := make(chan struct{})
go func() {
time.Sleep(LISTEN_TIMEOUT)
doneCh <- struct{}{}
}()
ret := []string{}
for {
select {
case <-doneCh:
return ret, nil
case endpoint := <-pingCh:
ret = append(ret, endpoint)
}
}
<-pingCh
return nil
}
func ListenUDP(