Skip to content
Snippets Groups Projects
Commit 9eaead50 authored by Hans Moog's avatar Hans Moog
Browse files

Fix: made peer thread safe + checking for close conn

parent 324cf785
No related branches found
No related tags found
No related merge requests found
......@@ -17,7 +17,9 @@ func createIncomingResponseProcessor(plugin *node.Plugin) *events.Closure {
func processIncomingResponse(plugin *node.Plugin, peeringResponse *response.Response) {
plugin.LogDebug("received peering response from " + peeringResponse.Issuer.String())
_ = peeringResponse.Issuer.Conn.Close()
if conn := peeringResponse.Issuer.GetConn(); conn != nil {
_ = conn.Close()
}
knownpeers.INSTANCE.AddOrUpdate(peeringResponse.Issuer)
for _, peer := range peeringResponse.Peers {
......
package protocol
import (
"time"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/outgoingrequest"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types"
"github.com/iotaledger/goshimmer/plugins/autopeering/server/tcp"
"time"
"github.com/iotaledger/goshimmer/packages/timeutil"
......@@ -55,7 +56,7 @@ func sendOutgoingRequests(plugin *node.Plugin) {
plugin.LogDebug("sent peering request to " + chosenNeighborCandidate.String())
if dialed {
tcp.HandleConnection(chosenNeighborCandidate.Conn)
tcp.HandleConnection(chosenNeighborCandidate.GetConn())
}
}
......@@ -63,10 +64,10 @@ func sendOutgoingRequests(plugin *node.Plugin) {
}(doneChan)
select {
case <-daemon.ShutdownSignal:
return
case <-doneChan:
continue
case <-daemon.ShutdownSignal:
return
case <-doneChan:
continue
}
}
}
......
......@@ -140,11 +140,11 @@ func processIncomingRequestPacket(connectionState *byte, receiveBuffer *[]byte,
return
} else {
req.Issuer.Conn = conn
req.Issuer.SetConn(conn)
req.Issuer.Address = conn.RemoteAddr().(*net.TCPAddr).IP
req.Issuer.Conn.Events.Close.Attach(events.NewClosure(func() {
req.Issuer.Conn = nil
conn.Events.Close.Attach(events.NewClosure(func() {
req.Issuer.SetConn(nil)
}))
Events.ReceiveRequest.Trigger(req)
......@@ -173,11 +173,11 @@ func processIncomingResponsePacket(connectionState *byte, receiveBuffer *[]byte,
return
} else {
res.Issuer.Conn = conn
res.Issuer.SetConn(conn)
res.Issuer.Address = conn.RemoteAddr().(*net.TCPAddr).IP
res.Issuer.Conn.Events.Close.Attach(events.NewClosure(func() {
res.Issuer.Conn = nil
conn.Events.Close.Attach(events.NewClosure(func() {
res.Issuer.SetConn(nil)
}))
Events.ReceiveResponse.Trigger(res)
......@@ -206,11 +206,11 @@ func processIncomingPingPacket(connectionState *byte, receiveBuffer *[]byte, con
return
} else {
ping.Issuer.Conn = conn
ping.Issuer.SetConn(conn)
ping.Issuer.Address = conn.RemoteAddr().(*net.TCPAddr).IP
ping.Issuer.Conn.Events.Close.Attach(events.NewClosure(func() {
ping.Issuer.Conn = nil
conn.Events.Close.Attach(events.NewClosure(func() {
ping.Issuer.SetConn(nil)
}))
Events.ReceivePing.Trigger(ping)
......
......@@ -21,14 +21,28 @@ type Peer struct {
PeeringPort uint16
GossipPort uint16
Salt *salt.Salt
Conn *network.ManagedConnection
connectMutex sync.Mutex
conn *network.ManagedConnection
connectMutex sync.RWMutex
firstSeen time.Time
firstSeenMutex sync.RWMutex
lastSeen time.Time
lastSeenMutex sync.RWMutex
}
func (peer *Peer) GetConn() (result *network.ManagedConnection) {
peer.connectMutex.RLock()
result = peer.conn
peer.connectMutex.RUnlock()
return
}
func (peer *Peer) SetConn(conn *network.ManagedConnection) {
peer.connectMutex.Lock()
peer.conn = conn
peer.connectMutex.Unlock()
}
func Unmarshal(data []byte) (*Peer, error) {
if len(data) < MARSHALED_TOTAL_SIZE {
return nil, errors.New("size of marshaled peer is too small")
......@@ -76,27 +90,31 @@ func (peer *Peer) Send(data []byte, protocol types.ProtocolType, responseExpecte
}
func (peer *Peer) ConnectTCP() (*network.ManagedConnection, bool, error) {
if peer.Conn == nil {
peer.connectMutex.RLock()
if peer.conn == nil {
peer.connectMutex.RUnlock()
peer.connectMutex.Lock()
defer peer.connectMutex.Unlock()
if peer.Conn == nil {
if peer.conn == nil {
conn, err := net.Dial("tcp", peer.Address.String()+":"+strconv.Itoa(int(peer.PeeringPort)))
if err != nil {
return nil, false, errors.New("error when connecting to " + peer.String() + ": " + err.Error())
} else {
peer.Conn = network.NewManagedConnection(conn)
peer.Conn.Events.Close.Attach(events.NewClosure(func() {
peer.Conn = nil
peer.conn = network.NewManagedConnection(conn)
peer.conn.Events.Close.Attach(events.NewClosure(func() {
peer.SetConn(nil)
}))
return peer.Conn, true, nil
return peer.conn, true, nil
}
}
} else {
peer.connectMutex.RUnlock()
}
return peer.Conn, false, nil
return peer.conn, false, nil
}
func (peer *Peer) ConnectUDP() (*network.ManagedConnection, bool, error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment