mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-12 04:43:22 +08:00
fix(tcp): recv on new endpoint
This commit is contained in:
@@ -106,8 +106,13 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if p.endpoint == nil || !p.endpoint.Euqal(addr) {
|
if p.endpoint == nil || !p.endpoint.Euqal(addr) {
|
||||||
logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String())
|
if m.ep.Network() == "udp" {
|
||||||
p.endpoint = addr
|
logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String())
|
||||||
|
p.endpoint = addr
|
||||||
|
} else if !addr.Euqal(p.endpoint) && p.endpoint == nil { // tcp/ws, ep not registered
|
||||||
|
logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String())
|
||||||
|
p.endpoint = addr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
case p.IsToMe(packet.Dst):
|
case p.IsToMe(packet.Dst):
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ func (ep *EndPoint) Network() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ep *EndPoint) Euqal(ep2 p2p.EndPoint) bool {
|
func (ep *EndPoint) Euqal(ep2 p2p.EndPoint) bool {
|
||||||
|
if ep == nil || ep2 == nil {
|
||||||
|
return ep == nil && ep2 == nil
|
||||||
|
}
|
||||||
tcpep2, ok := ep2.(*EndPoint)
|
tcpep2, ok := ep2.(*EndPoint)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
@@ -113,6 +116,10 @@ func (conn *Conn) accept() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Conn) receive(ep *EndPoint) {
|
func (conn *Conn) receive(ep *EndPoint) {
|
||||||
|
dialtimeout := conn.addr.dialtimeout
|
||||||
|
if dialtimeout < time.Second {
|
||||||
|
dialtimeout = time.Second
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
r := &connrecv{addr: ep}
|
r := &connrecv{addr: ep}
|
||||||
if conn.addr == nil || conn.lstn == nil || conn.peers == nil || conn.recv == nil {
|
if conn.addr == nil || conn.lstn == nil || conn.peers == nil || conn.recv == nil {
|
||||||
@@ -123,7 +130,27 @@ func (conn *Conn) receive(ep *EndPoint) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.conn = tcpconn
|
r.conn = tcpconn
|
||||||
_, err := io.Copy(&r.pckt, tcpconn)
|
|
||||||
|
stopch := make(chan struct{})
|
||||||
|
t := time.AfterFunc(dialtimeout, func() {
|
||||||
|
stopch <- struct{}{}
|
||||||
|
})
|
||||||
|
|
||||||
|
var err error
|
||||||
|
copych := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
_, err = io.Copy(&r.pckt, tcpconn)
|
||||||
|
copych <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-stopch:
|
||||||
|
logrus.Debugln("[tcp] recv from", ep, "timeout")
|
||||||
|
return
|
||||||
|
case <-copych:
|
||||||
|
t.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Debugln("[tcp] recv from", ep, "err:", err)
|
logrus.Debugln("[tcp] recv from", ep, "err:", err)
|
||||||
return
|
return
|
||||||
@@ -211,6 +238,7 @@ func (conn *Conn) WriteToPeer(b []byte, ep p2p.EndPoint) (n int, err error) {
|
|||||||
})
|
})
|
||||||
logrus.Debugln("[tcp] dial to", tcpep.addr, "success, local:", tcpconn.LocalAddr())
|
logrus.Debugln("[tcp] dial to", tcpep.addr, "success, local:", tcpconn.LocalAddr())
|
||||||
conn.peers.Set(tcpep.String(), tcpconn)
|
conn.peers.Set(tcpep.String(), tcpconn)
|
||||||
|
go conn.receive(tcpep)
|
||||||
} else {
|
} else {
|
||||||
logrus.Debugln("[tcp] reuse tcpconn from", tcpconn.LocalAddr(), "to", tcpconn.RemoteAddr())
|
logrus.Debugln("[tcp] reuse tcpconn from", tcpconn.LocalAddr(), "to", tcpconn.RemoteAddr())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ func (ep *EndPoint) Network() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ep *EndPoint) Euqal(ep2 p2p.EndPoint) bool {
|
func (ep *EndPoint) Euqal(ep2 p2p.EndPoint) bool {
|
||||||
|
if ep == nil || ep2 == nil {
|
||||||
|
return ep == nil && ep2 == nil
|
||||||
|
}
|
||||||
udpep2, ok := ep2.(*EndPoint)
|
udpep2, ok := ep2.(*EndPoint)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user