mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-10 07:30:27 +00:00
send a switch message immediately when peering, and use OS-level TCP keep-alive (shouldn't matter right now, since we have application-level keep-alive that preempts it, but important later)
This commit is contained in:
parent
ce98aac504
commit
abd8b69979
@ -177,6 +177,7 @@ func (p *peer) doSendSwitchMsgs() {
|
|||||||
func (p *peer) linkLoop() {
|
func (p *peer) linkLoop() {
|
||||||
tick := time.NewTicker(time.Second)
|
tick := time.NewTicker(time.Second)
|
||||||
defer tick.Stop()
|
defer tick.Stop()
|
||||||
|
p.doSendSwitchMsgs()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case _, ok := <-p.doSend:
|
case _, ok := <-p.doSend:
|
||||||
@ -185,11 +186,9 @@ func (p *peer) linkLoop() {
|
|||||||
}
|
}
|
||||||
p.sendSwitchMsg()
|
p.sendSwitchMsg()
|
||||||
case _ = <-tick.C:
|
case _ = <-tick.C:
|
||||||
//break // FIXME disabled the below completely to test something
|
dinfo := p.dinfo // FIXME? are pointer reads *always* atomic?
|
||||||
pdinfo := p.dinfo // FIXME this is a bad workarond NPE on the next line
|
if dinfo != nil {
|
||||||
if pdinfo != nil {
|
p.core.dht.peers <- dinfo
|
||||||
dinfo := *pdinfo
|
|
||||||
p.core.dht.peers <- &dinfo
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,14 +31,6 @@ const tcp_msgSize = 2048 + 65535 // TODO figure out what makes sense
|
|||||||
const default_tcp_timeout = 6 * time.Second
|
const default_tcp_timeout = 6 * time.Second
|
||||||
const tcp_ping_interval = (default_tcp_timeout * 2 / 3)
|
const tcp_ping_interval = (default_tcp_timeout * 2 / 3)
|
||||||
|
|
||||||
// Wrapper function for non tcp/ip connections.
|
|
||||||
func setNoDelay(c net.Conn, delay bool) {
|
|
||||||
tcp, ok := c.(*net.TCPConn)
|
|
||||||
if ok {
|
|
||||||
tcp.SetNoDelay(delay)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The TCP listener and information about active TCP connections, to avoid duplication.
|
// The TCP listener and information about active TCP connections, to avoid duplication.
|
||||||
type tcpInterface struct {
|
type tcpInterface struct {
|
||||||
core *Core
|
core *Core
|
||||||
@ -58,6 +50,20 @@ type tcpInfo struct {
|
|||||||
remoteAddr string
|
remoteAddr string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrapper function to set additional options for specific connection types.
|
||||||
|
func (iface *tcpInterface) setExtraOptions(c net.Conn) {
|
||||||
|
switch sock := c.(type) {
|
||||||
|
case *net.TCPConn:
|
||||||
|
sock.SetNoDelay(true)
|
||||||
|
sock.SetKeepAlive(true)
|
||||||
|
sock.SetKeepAlivePeriod(iface.tcp_timeout)
|
||||||
|
panic("DEBUG testing")
|
||||||
|
// TODO something for socks5
|
||||||
|
default:
|
||||||
|
iface.core.log.Println("Unrecognized connection type: %v", sock)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Returns the address of the listener.
|
// Returns the address of the listener.
|
||||||
func (iface *tcpInterface) getAddr() *net.TCPAddr {
|
func (iface *tcpInterface) getAddr() *net.TCPAddr {
|
||||||
return iface.serv.Addr().(*net.TCPAddr)
|
return iface.serv.Addr().(*net.TCPAddr)
|
||||||
@ -205,6 +211,7 @@ func (iface *tcpInterface) call(saddr string, socksaddr *string, sintf string) {
|
|||||||
// It defers a bunch of cleanup stuff to tear down all of these things when the reader exists (e.g. due to a closed connection or a timeout).
|
// It defers a bunch of cleanup stuff to tear down all of these things when the reader exists (e.g. due to a closed connection or a timeout).
|
||||||
func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
|
func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
|
||||||
defer sock.Close()
|
defer sock.Close()
|
||||||
|
iface.setExtraOptions(sock)
|
||||||
// Get our keys
|
// Get our keys
|
||||||
myLinkPub, myLinkPriv := newBoxKeys() // ephemeral link keys
|
myLinkPub, myLinkPriv := newBoxKeys() // ephemeral link keys
|
||||||
meta := version_getBaseMetadata()
|
meta := version_getBaseMetadata()
|
||||||
@ -342,7 +349,6 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
|
|||||||
out <- msg
|
out <- msg
|
||||||
}
|
}
|
||||||
p.close = func() { sock.Close() }
|
p.close = func() { sock.Close() }
|
||||||
setNoDelay(sock, true)
|
|
||||||
go p.linkLoop()
|
go p.linkLoop()
|
||||||
defer func() {
|
defer func() {
|
||||||
// Put all of our cleanup here...
|
// Put all of our cleanup here...
|
||||||
|
Loading…
Reference in New Issue
Block a user