From 4f1a17dea1f9a273841ec01cae2f102252f96589 Mon Sep 17 00:00:00 2001 From: Yi-Ting Shih Date: Thu, 16 Oct 2025 08:09:19 +0800 Subject: [PATCH] Fix: fuck udp --- cmd.go | 1 - stages/lobby.go | 7 +------ test.go | 38 -------------------------------------- utils/udp.go | 37 ++++++++++++++++++++++++++----------- 4 files changed, 27 insertions(+), 56 deletions(-) delete mode 100644 test.go diff --git a/cmd.go b/cmd.go index c2a8aaa..f95b04c 100644 --- a/cmd.go +++ b/cmd.go @@ -26,5 +26,4 @@ func init() { RootCmd.AddCommand(authCmd) RootCmd.AddCommand(playerCmd) - RootCmd.AddCommand(testCmd) } diff --git a/stages/lobby.go b/stages/lobby.go index 1304ada..ae42f18 100644 --- a/stages/lobby.go +++ b/stages/lobby.go @@ -116,12 +116,7 @@ type clientScanMsg time.Time func (m *LobbyModel) clientScan() tea.Cmd { return tea.Tick(REFRESH_TIME, func(t time.Time) tea.Msg { - m.endpoints = []string{} - for _, endpoint := range viper.GetStringSlice("udp-endpoints") { - if err := utils.Ping(endpoint); err == nil { - m.endpoints = append(m.endpoints, endpoint) - } - } + m.endpoints, m.err = utils.Ping(viper.GetStringSlice("udp-endpoints")) return clientScanMsg(t) }) } diff --git a/test.go b/test.go deleted file mode 100644 index 54df20c..0000000 --- a/test.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "gitea.konchin.com/ytshih/inp2025/utils" - "github.com/spf13/cobra" -) - -var testCmd = &cobra.Command{ - Use: "test", -} - -var testServerCmd = &cobra.Command{ - Use: "server", - Run: func(cmd *cobra.Command, args []string) { - dataCh := make(chan string) - _, shutdown, err := utils.ListenUDPData(18787, dataCh) - if err != nil { - panic(err) - } - defer shutdown() - <-dataCh - }, -} - -var testClientCmd = &cobra.Command{ - Use: "client", - Run: func(cmd *cobra.Command, args []string) { - err := utils.Ping("localhost:18787") - if err != nil { - panic(err) - } - }, -} - -func init() { - testCmd.AddCommand(testServerCmd) - testCmd.AddCommand(testClientCmd) -} diff --git a/utils/udp.go b/utils/udp.go index e33b37a..214577a 100644 --- a/utils/udp.go +++ b/utils/udp.go @@ -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(