This commit is contained in:
esrrhs 2019-10-30 18:57:26 +08:00
parent 225992d9eb
commit c348cfb2e9

View File

@ -43,6 +43,8 @@ func NewClient(addr string, server string, target string, timeout int, key int,
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
return &Client{ return &Client{
exit: false,
rtt: 0,
id: r.Intn(math.MaxInt16), id: r.Intn(math.MaxInt16),
ipaddr: ipaddr, ipaddr: ipaddr,
tcpaddr: tcpaddr, tcpaddr: tcpaddr,
@ -63,6 +65,9 @@ func NewClient(addr string, server string, target string, timeout int, key int,
} }
type Client struct { type Client struct {
exit bool
rtt time.Duration
id int id int
sequence int sequence int
@ -131,6 +136,10 @@ func (p *Client) ServerAddr() string {
return p.addrServer return p.addrServer
} }
func (p *Client) RTT() time.Duration {
return p.rtt
}
func (p *Client) Run() { func (p *Client) Run() {
conn, err := icmp.ListenPacket("ip4:icmp", "") conn, err := icmp.ListenPacket("ip4:icmp", "")
@ -174,7 +183,7 @@ func (p *Client) Run() {
interval := time.NewTicker(time.Second) interval := time.NewTicker(time.Second)
defer interval.Stop() defer interval.Stop()
for { for !p.exit {
select { select {
case <-interval.C: case <-interval.C:
p.checkTimeoutConn() p.checkTimeoutConn()
@ -186,11 +195,15 @@ func (p *Client) Run() {
} }
} }
func (p *Client) Stop() {
p.exit = true
}
func (p *Client) AcceptTcp() error { func (p *Client) AcceptTcp() error {
loggo.Info("client waiting local accept tcp") loggo.Info("client waiting local accept tcp")
for { for !p.exit {
p.tcplistenConn.SetDeadline(time.Now().Add(time.Millisecond * 1000)) p.tcplistenConn.SetDeadline(time.Now().Add(time.Millisecond * 1000))
conn, err := p.tcplistenConn.AcceptTCP() conn, err := p.tcplistenConn.AcceptTCP()
@ -210,7 +223,7 @@ func (p *Client) AcceptTcp() error {
} }
} }
} }
return nil
} }
func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) { func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) {
@ -230,7 +243,7 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) {
loggo.Info("start connect remote tcp %s %s", uuid, tcpsrcaddr.String()) loggo.Info("start connect remote tcp %s %s", uuid, tcpsrcaddr.String())
clientConn.fm.Connect() clientConn.fm.Connect()
startConnectTime := time.Now() startConnectTime := time.Now()
for { for !p.exit {
if clientConn.fm.IsConnected() { if clientConn.fm.IsConnected() {
break break
} }
@ -263,7 +276,7 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) {
tcpActiveRecvTime := time.Now() tcpActiveRecvTime := time.Now()
tcpActiveSendTime := time.Now() tcpActiveSendTime := time.Now()
for { for !p.exit {
now := time.Now() now := time.Now()
sleep := true sleep := true
@ -351,7 +364,7 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) {
} }
startCloseTime := time.Now() startCloseTime := time.Now()
for { for !p.exit {
now := time.Now() now := time.Now()
clientConn.fm.Update() clientConn.fm.Update()
@ -408,7 +421,7 @@ func (p *Client) Accept() error {
bytes := make([]byte, 10240) bytes := make([]byte, 10240)
for { for !p.exit {
p.listenConn.SetReadDeadline(time.Now().Add(time.Millisecond * 100)) p.listenConn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
n, srcaddr, err := p.listenConn.ReadFromUDP(bytes) n, srcaddr, err := p.listenConn.ReadFromUDP(bytes)
if err != nil { if err != nil {
@ -443,6 +456,7 @@ func (p *Client) Accept() error {
p.sendPacket++ p.sendPacket++
p.sendPacketSize += (uint64)(n) p.sendPacketSize += (uint64)(n)
} }
return nil
} }
func (p *Client) processPacket(packet *Packet) { func (p *Client) processPacket(packet *Packet) {
@ -464,6 +478,7 @@ func (p *Client) processPacket(packet *Packet) {
t.UnmarshalBinary(packet.my.Data) t.UnmarshalBinary(packet.my.Data)
d := time.Now().Sub(t) d := time.Now().Sub(t)
loggo.Info("pong from %s %s", packet.src.String(), d.String()) loggo.Info("pong from %s %s", packet.src.String(), d.String())
p.rtt = d
return return
} }