tun2: move pingloop into a function

This commit is contained in:
Cadey Ratio 2017-03-26 22:19:43 -07:00
parent 1c221cdf6d
commit 03f9570151
1 changed files with 45 additions and 41 deletions

View File

@ -156,8 +156,16 @@ func (s *Server) ListenAndServe() error {
go func() {
for {
time.Sleep(5 * time.Second)
s.pingLoopInner()
}
}()
return nil
}
func (s *Server) pingLoopInner() {
s.connlock.Lock()
defer s.connlock.Unlock()
for _, c := range s.conns {
c.session.SetDeadline(time.Now().Add(time.Second))
@ -171,35 +179,31 @@ func (s *Server) ListenAndServe() error {
if err != nil {
ln.Error(err, c.F())
c.cancel()
continue
return
}
err = req.Write(stream)
if err != nil {
ln.Error(err, c.F())
c.cancel()
continue
return
}
_, err = stream.Read(make([]byte, 30))
if err != nil {
ln.Error(err, c.F())
c.cancel()
continue
return
}
stream.Close()
/*
ln.Log(ln.F{
"action": "ping_health_is_good",
}, c.F())
*/
}
s.connlock.Unlock()
}
}()
return nil
}
func (s *Server) HandleConn(c net.Conn, isKCP bool) {
@ -381,7 +385,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
Request: req,
}
return resp, nil
return resp, errors.New("no backend connected")
}
}