This commit is contained in:
esrrhs 2018-12-22 18:42:41 +08:00
parent e1cdec8c00
commit 8efea89357
2 changed files with 10 additions and 18 deletions

View File

@ -9,7 +9,7 @@ import (
"time" "time"
) )
func NewClient(addr string, server string, target string, timeout int, sproto int, rproto int, pingSeq int) (*Client, error) { func NewClient(addr string, server string, target string, timeout int, sproto int, rproto int) (*Client, error) {
ipaddr, err := net.ResolveUDPAddr("udp", addr) ipaddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil { if err != nil {
@ -32,7 +32,6 @@ func NewClient(addr string, server string, target string, timeout int, sproto in
timeout: timeout, timeout: timeout,
sproto: sproto, sproto: sproto,
rproto: rproto, rproto: rproto,
pingSeq: pingSeq,
}, nil }, nil
} }
@ -43,7 +42,6 @@ type Client struct {
timeout int timeout int
sproto int sproto int
rproto int rproto int
pingSeq int
ipaddr *net.UDPAddr ipaddr *net.UDPAddr
addr string addr string
@ -121,16 +119,12 @@ func (p *Client) Run() {
interval := time.NewTicker(time.Second) interval := time.NewTicker(time.Second)
defer interval.Stop() defer interval.Stop()
intervalPing := time.NewTicker(time.Millisecond * (time.Duration)(p.pingSeq))
defer intervalPing.Stop()
for { for {
select { select {
case <-interval.C: case <-interval.C:
p.checkTimeoutConn() p.checkTimeoutConn()
p.showNet()
case <-intervalPing.C:
p.ping() p.ping()
p.showNet()
case r := <-recv: case r := <-recv:
p.processPacket(r) p.processPacket(r)
} }
@ -241,11 +235,13 @@ func (p *Client) checkTimeoutConn() {
} }
func (p *Client) ping() { func (p *Client) ping() {
now := time.Now() if p.sendPacket == 0 {
b, _ := now.MarshalBinary() now := time.Now()
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto) b, _ := now.MarshalBinary()
fmt.Printf("ping %s %s %d %d %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence) sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto)
p.sequence++ fmt.Printf("ping %s %s %d %d %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence)
p.sequence++
}
} }
func (p *Client) showNet() { func (p *Client) showNet() {

View File

@ -36,9 +36,6 @@ Usage:
-rproto 客户端接收ping协议的协议默认是0 -rproto 客户端接收ping协议的协议默认是0
The protocol that the client receives the ping. The default is 0. The protocol that the client receives the ping. The default is 0.
-ping 客户端发送ping协议的间隔
The protocol that the client receives the ping. The default is 0.
` `
func main() { func main() {
@ -50,7 +47,6 @@ func main() {
timeout := flag.Int("timeout", 60, "conn timeout") timeout := flag.Int("timeout", 60, "conn timeout")
sproto := flag.Int("sproto", 8, "send ping proto") sproto := flag.Int("sproto", 8, "send ping proto")
rproto := flag.Int("rproto", 0, "recv ping proto") rproto := flag.Int("rproto", 0, "recv ping proto")
ping := flag.Int("ping", 1000, "recv ping proto")
flag.Usage = func() { flag.Usage = func() {
fmt.Printf(usage) fmt.Printf(usage)
} }
@ -80,7 +76,7 @@ func main() {
fmt.Printf("server %s\n", *server) fmt.Printf("server %s\n", *server)
fmt.Printf("target %s\n", *target) fmt.Printf("target %s\n", *target)
c, err := pingtunnel.NewClient(*listen, *server, *target, *timeout, *sproto, *rproto, *ping) c, err := pingtunnel.NewClient(*listen, *server, *target, *timeout, *sproto, *rproto)
if err != nil { if err != nil {
fmt.Printf("ERROR: %s\n", err.Error()) fmt.Printf("ERROR: %s\n", err.Error())
return return