mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-1223] Delete unused files/packages. * [NOD-1223] Move signal and limits to the os package. * [NOD-1223] Put database and dbaccess into the db package. * [NOD-1223] Fold the logs package into the logger package. * [NOD-1223] Rename domainmessage to appmessage. * [NOD-1223] Rename to/from DomainMessage to AppMessage. * [NOD-1223] Move appmessage to the app packge. * [NOD-1223] Move protocol to the app packge. * [NOD-1223] Move the network package to the infrastructure packge. * [NOD-1223] Rename cmd to executables. * [NOD-1223] Fix go.doc in the logger package.
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package standalone
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kaspanet/kaspad/app/appmessage"
|
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/router"
|
|
)
|
|
|
|
// Routes holds the incoming and outgoing routes of a connection created by MinimalNetAdapter
|
|
type Routes struct {
|
|
netConnection *netadapter.NetConnection
|
|
IncomingRoute, OutgoingRoute *router.Route
|
|
handshakeRoute *router.Route
|
|
pingRoute *router.Route
|
|
}
|
|
|
|
// WaitForMessageOfType waits for a message of requested type up to `timeout`, skipping all messages of any other type
|
|
// received while waiting
|
|
func (r *Routes) WaitForMessageOfType(command appmessage.MessageCommand, timeout time.Duration) (appmessage.Message, error) {
|
|
timeoutTime := time.Now().Add(timeout)
|
|
for {
|
|
message, err := r.IncomingRoute.DequeueWithTimeout(timeoutTime.Sub(time.Now()))
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "error waiting for message of type %s", command)
|
|
}
|
|
if message.Command() == command {
|
|
return message, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// WaitForDisconnect waits for a disconnect up to `timeout`, skipping all messages received while waiting
|
|
func (r *Routes) WaitForDisconnect(timeout time.Duration) error {
|
|
timeoutTime := time.Now().Add(timeout)
|
|
for {
|
|
_, err := r.IncomingRoute.DequeueWithTimeout(timeoutTime.Sub(time.Now()))
|
|
if errors.Is(err, router.ErrRouteClosed) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return errors.Wrap(err, "error waiting for disconnect")
|
|
}
|
|
}
|
|
}
|
|
|
|
// Disconnect closes the connection behind the routes, thus closing all routes
|
|
func (r *Routes) Disconnect() {
|
|
r.netConnection.Disconnect()
|
|
}
|