Fix: fuck udp
This commit is contained in:
1
cmd.go
1
cmd.go
@@ -26,5 +26,4 @@ func init() {
|
|||||||
|
|
||||||
RootCmd.AddCommand(authCmd)
|
RootCmd.AddCommand(authCmd)
|
||||||
RootCmd.AddCommand(playerCmd)
|
RootCmd.AddCommand(playerCmd)
|
||||||
RootCmd.AddCommand(testCmd)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,12 +116,7 @@ type clientScanMsg time.Time
|
|||||||
|
|
||||||
func (m *LobbyModel) clientScan() tea.Cmd {
|
func (m *LobbyModel) clientScan() tea.Cmd {
|
||||||
return tea.Tick(REFRESH_TIME, func(t time.Time) tea.Msg {
|
return tea.Tick(REFRESH_TIME, func(t time.Time) tea.Msg {
|
||||||
m.endpoints = []string{}
|
m.endpoints, m.err = utils.Ping(viper.GetStringSlice("udp-endpoints"))
|
||||||
for _, endpoint := range viper.GetStringSlice("udp-endpoints") {
|
|
||||||
if err := utils.Ping(endpoint); err == nil {
|
|
||||||
m.endpoints = append(m.endpoints, endpoint)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return clientScanMsg(t)
|
return clientScanMsg(t)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
38
test.go
38
test.go
@@ -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)
|
|
||||||
}
|
|
||||||
33
utils/udp.go
33
utils/udp.go
@@ -3,6 +3,7 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.konchin.com/ytshih/inp2025/types"
|
"gitea.konchin.com/ytshih/inp2025/types"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
@@ -11,6 +12,8 @@ import (
|
|||||||
const (
|
const (
|
||||||
BUFFER_SIZE int = 1024
|
BUFFER_SIZE int = 1024
|
||||||
MAGIC_NUMBER int = 114514
|
MAGIC_NUMBER int = 114514
|
||||||
|
|
||||||
|
LISTEN_TIMEOUT = 200 * time.Millisecond
|
||||||
)
|
)
|
||||||
|
|
||||||
type UDPReqType int
|
type UDPReqType int
|
||||||
@@ -36,24 +39,36 @@ func ListenUDPData(
|
|||||||
return ListenUDP(port, dataCh, nil)
|
return ListenUDP(port, dataCh, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Ping(endpoint string) error {
|
func Ping(endpoints []string) ([]string, error) {
|
||||||
pingCh := make(chan string)
|
pingCh := make(chan string)
|
||||||
local, shutdown, err := ListenUDP(0, nil, pingCh)
|
local, shutdown, err := ListenUDP(0, nil, pingCh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return []string{}, err
|
||||||
}
|
}
|
||||||
defer shutdown()
|
defer shutdown()
|
||||||
|
|
||||||
err = SendRawPayload(endpoint, UDPPayload{
|
for _, endpoint := range endpoints {
|
||||||
|
SendRawPayload(endpoint, UDPPayload{
|
||||||
MagicNumber: MAGIC_NUMBER,
|
MagicNumber: MAGIC_NUMBER,
|
||||||
Endpoint: local,
|
Endpoint: local,
|
||||||
Type: UDPReqTypePingRequest,
|
Type: UDPReqTypePingRequest,
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
<-pingCh
|
|
||||||
return nil
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListenUDP(
|
func ListenUDP(
|
||||||
@@ -61,7 +76,7 @@ func ListenUDP(
|
|||||||
dataCh chan string,
|
dataCh chan string,
|
||||||
pingCh chan string,
|
pingCh chan string,
|
||||||
) (string, types.ShutdownFunc, error) {
|
) (string, types.ShutdownFunc, error) {
|
||||||
conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: port})
|
conn, err := net.ListenUDP("udp4", &net.UDPAddr{Port: port})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, fmt.Errorf("failed to listen udp, %w", err)
|
return "", nil, fmt.Errorf("failed to listen udp, %w", err)
|
||||||
}
|
}
|
||||||
@@ -123,7 +138,7 @@ func SendRawPayload(
|
|||||||
endpoint string,
|
endpoint string,
|
||||||
payload UDPPayload,
|
payload UDPPayload,
|
||||||
) error {
|
) error {
|
||||||
conn, err := net.Dial("udp", endpoint)
|
conn, err := net.Dial("udp4", endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to dial endpoint, %w", err)
|
return fmt.Errorf("failed to dial endpoint, %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user