mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-07 14:46:44 +00:00

* [NOD-1120] Removed closure in NetAdapter.onConnectedHanlder * [NOD-1120] Implement all connection manager methods * [NOD-1120] Integrated connmanager into kaspad + added call for dnsseeder * [NOD-1120] Allow buffer to not be bytes.Buffer * [NOD-1120] Added timeout to connect * [NOD-1120] Don't enter connections to add loop if none needed * [NOD-1120] Add call for addressManager.Good * [NOD-1120] Minor bug fixes * [NOD-1120] Remove errChan from grpcConnection * [NOD-1120] Add comments to exported methods * [NOD-1120] cancel the context for DialContext in gRPCServer.Connect * [NOD-1120] Don't try to remove from connSet a connection that doesn't exist * [NOD-1120] add ok bool to connectionSet.get * [NOD-1120] Remove overuse of if-else in checkConnectionRequests * [NOD-1120] Made some order in ConnectionManager * [NOD-1120] Moved checkIncomingConnections to it's own file * [NOD-1120] cleanup in checkOutgoingConnections * [NOD-1120] Cleanup in SeedDNS, and move call outside of connection manager * [NOD-1120] Add check that both --connect and --addpeer aren't used * [NOD-1120] Move dial timeout to constant * [NOD-1120] Enhance comment * [NOD-1120] Log connection failure out of initiateConnection * [NOD-1148] Reshuffle checkRequestedConnections to make more sense * [NOD-1120] Move continue to correct place + reshuffle logging code * [NOD-1120] Don't expose server.Connection outside netAdapter - expose a wrapper instead * [NOD-1120] Add comments * [NOD-1120] Don't return the connection from netAdapter.Connect() * [NOD-1120] Use .Address as key for connectionSet * [NOD-1120] Fix minRetryDuration usage * [NOD-1120] Remove the correct number of incoming connections * [NOD-1120] Add comment * [NOD-1120] Rename connSet -> incomingConnectionSet * [NOD-1120] fix grammar
89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
package grpcserver
|
|
|
|
import (
|
|
"net"
|
|
"sync/atomic"
|
|
|
|
"github.com/kaspanet/kaspad/netadapter/router"
|
|
"github.com/kaspanet/kaspad/netadapter/server/grpcserver/protowire"
|
|
|
|
"github.com/kaspanet/kaspad/netadapter/server"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type gRPCConnection struct {
|
|
server *gRPCServer
|
|
address net.Addr
|
|
isOutbound bool
|
|
stream grpcStream
|
|
router *router.Router
|
|
|
|
stopChan chan struct{}
|
|
clientConn grpc.ClientConn
|
|
onDisconnectedHandler server.OnDisconnectedHandler
|
|
|
|
isConnected uint32
|
|
}
|
|
|
|
func newConnection(server *gRPCServer, address net.Addr, isOutbound bool, stream grpcStream) *gRPCConnection {
|
|
connection := &gRPCConnection{
|
|
server: server,
|
|
address: address,
|
|
isOutbound: isOutbound,
|
|
stream: stream,
|
|
stopChan: make(chan struct{}),
|
|
isConnected: 1,
|
|
}
|
|
|
|
return connection
|
|
}
|
|
|
|
func (c *gRPCConnection) Start(router *router.Router) {
|
|
c.router = router
|
|
|
|
spawn(func() {
|
|
err := c.connectionLoops()
|
|
if err != nil {
|
|
log.Errorf("error from connectionLoops for %s: %+v", c.address, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (c *gRPCConnection) String() string {
|
|
return c.Address().String()
|
|
}
|
|
|
|
func (c *gRPCConnection) IsConnected() bool {
|
|
return atomic.LoadUint32(&c.isConnected) != 0
|
|
}
|
|
|
|
func (c *gRPCConnection) SetOnDisconnectedHandler(onDisconnectedHandler server.OnDisconnectedHandler) {
|
|
c.onDisconnectedHandler = onDisconnectedHandler
|
|
}
|
|
|
|
// Disconnect disconnects the connection
|
|
// Calling this function a second time doesn't do anything
|
|
//
|
|
// This is part of the Connection interface
|
|
func (c *gRPCConnection) Disconnect() error {
|
|
if !c.IsConnected() {
|
|
return nil
|
|
}
|
|
atomic.StoreUint32(&c.isConnected, 0)
|
|
|
|
close(c.stopChan)
|
|
|
|
if c.isOutbound {
|
|
clientStream := c.stream.(protowire.P2P_MessageStreamClient)
|
|
_ = clientStream.CloseSend() // ignore error because we don't really know what's the status of the connection
|
|
}
|
|
|
|
log.Debugf("Disconnected from %s", c)
|
|
|
|
return c.onDisconnectedHandler()
|
|
}
|
|
|
|
func (c *gRPCConnection) Address() net.Addr {
|
|
return c.address
|
|
}
|