mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-08 07:06:43 +00:00

* [NOD-1124] Move Router to the router package. * [NOD-1124] Implement SetOnRouteCapacityReachedHandler. * [NOD-1124] Use Routes instead of bare channels. * [NOD-1124] Fix merge errors. * [NOD-1124] Connect the Router to the Connection. * [NOD-1124] Fix merge errors. * [NOD-1124] Move some variables around. * [NOD-1124] Fix unreachable code. * [NOD-1124] Fix a variable name. * [NOD-1124] Rename AddRoute to AddIncomingRoute. * [NOD-1124] Rename SetRouter to Start. * [NOD-1124] Make AddIncomingRoute create a Route by itself. * [NOD-1124] Replace IncomingRoute with EnqueueIncomingMessage. * [NOD-1124] Make Enqueue and Dequeue return isOpen instead of err. * [NOD-1124] Remove writeDuringDisconnectLock. * [NOD-1124] In sendLoop, move outgoingRoute to outside the loop. * [NOD-1124] Start the connection loops only when Start is called. * [NOD-1124] Replace OnIDReceivedHandler with AssociateRouterID. * [NOD-1124] Add isOpen to Enqueue and Dequeue. * [NOD-1124] Protect errChan from writing during disconnect.
93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package grpcserver
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/netadapter/router"
|
|
"github.com/kaspanet/kaspad/netadapter/server/grpcserver/protowire"
|
|
"net"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"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
|
|
|
|
writeToErrChanDuringDisconnectLock sync.Mutex
|
|
errChan chan error
|
|
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,
|
|
errChan: make(chan error),
|
|
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)
|
|
|
|
c.writeToErrChanDuringDisconnectLock.Lock()
|
|
defer c.writeToErrChanDuringDisconnectLock.Unlock()
|
|
close(c.errChan)
|
|
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
|
|
}
|
|
|
|
return c.onDisconnectedHandler()
|
|
}
|
|
|
|
func (c *gRPCConnection) Address() net.Addr {
|
|
return c.address
|
|
}
|