stasatdaglabs 8a4ece1101
[NOD-1223] Reorganize project (#868)
* [NOD-1223] Move all network stuff into a new network package.

* [NOD-1223] Delete the unused package testutil.

* [NOD-1223] Move infrastructure stuff into a new instrastructure package.

* [NOD-1223] Move domain stuff into a new domain package.
2020-08-13 17:27:25 +03:00

48 lines
1.4 KiB
Go

package server
import (
"fmt"
"net"
"github.com/pkg/errors"
"github.com/kaspanet/kaspad/network/netadapter/router"
)
// OnConnectedHandler is a function that is to be called
// once a new Connection is successfully established.
type OnConnectedHandler func(connection Connection) error
// OnDisconnectedHandler is a function that is to be
// called once a Connection has been disconnected.
type OnDisconnectedHandler func()
// OnInvalidMessageHandler is a function that is to be called when
// an invalid message (cannot be parsed/doesn't have a route)
// was received from a connection.
type OnInvalidMessageHandler func(err error)
// Server represents a p2p server.
type Server interface {
Connect(address string) (Connection, error)
Start() error
Stop() error
SetOnConnectedHandler(onConnectedHandler OnConnectedHandler)
}
// Connection represents a p2p server connection.
type Connection interface {
fmt.Stringer
Start(router *router.Router)
Disconnect()
IsConnected() bool
IsOutbound() bool
SetOnDisconnectedHandler(onDisconnectedHandler OnDisconnectedHandler)
SetOnInvalidMessageHandler(onInvalidMessageHandler OnInvalidMessageHandler)
Address() *net.TCPAddr
}
// ErrNetwork is an error related to the internals of the connection, and not an error that
// came from outside (e.g. from OnDisconnectedHandler).
var ErrNetwork = errors.New("network error")