MCPcopy Create free account
hub / github.com/LagrangeDev/LagrangeGo / RunTCPPingLoop

Function RunTCPPingLoop

utils/tcping.go:17–47  ·  view source on GitHub ↗

RunTCPPingLoop 使用 tcp 进行 ping

(ipport string, count int)

Source from the content-addressed store, hash-verified

15
16// RunTCPPingLoop 使用 tcp 进行 ping
17func RunTCPPingLoop(ipport string, count int) (r ICMPPingResult) {
18 r = ICMPPingResult{
19 PacketsSent: count,
20 PacketsLoss: count,
21 AvgTimeMill: 9999,
22 }
23 if count <= 0 {
24 return
25 }
26 durs := make([]int64, 0, count)
27 for i := 0; i < count; i++ {
28 d, err := tcping(ipport)
29 if err == nil {
30 r.PacketsLoss--
31 durs = append(durs, d)
32 }
33 time.Sleep(time.Millisecond * 100)
34 }
35
36 if len(durs) > 0 {
37 r.AvgTimeMill = 0
38 for _, d := range durs {
39 r.AvgTimeMill += d
40 }
41 if len(durs) > 1 {
42 r.AvgTimeMill /= int64(len(durs))
43 }
44 }
45
46 return
47}
48
49func tcping(ipport string) (int64, error) {
50 t := time.Now().UnixMilli()

Callers 1

ConnectionQualityTestMethod · 0.92

Calls 1

tcpingFunction · 0.85

Tested by

no test coverage detected