Documentation ¶
Index ¶
- Constants
- Variables
- func Checksum(msg *pb.XuperMessage) uint32
- func Compress(msg *pb.XuperMessage) *pb.XuperMessage
- func Decompress(msg *pb.XuperMessage) ([]byte, error)
- func GenerateKeyPairWithPath(path string) error
- func GenerateNetKeyFromPemKey(path string) error
- func GeneratePemKeyFromNetKey(path string) error
- func GenerateUniqueRandList(size int, max int) []int
- func GetKeyPairFromPath(path string) (crypto.PrivKey, error)
- func GetPeerIDByAddress(peerAddr string) (peer.ID, error)
- func GetPeerIDFromPath(path string) (string, error)
- func GetPemKeyPairFromPath(path string) (crypto.PrivKey, error)
- func GetRespMessageType(msgType pb.XuperMessage_MessageType) pb.XuperMessage_MessageType
- func MessageKey(msg *pb.XuperMessage) string
- func NewMessage(typ pb.XuperMessage_MessageType, message proto.Message, opts ...MessageOption) *pb.XuperMessage
- func NewTLS(path, serviceName string) (credentials.TransportCredentials, error)
- func Unmarshal(msg *pb.XuperMessage, message proto.Message) error
- func VerifyChecksum(msg *pb.XuperMessage) bool
- func VerifyMessageType(request *pb.XuperMessage, response *pb.XuperMessage, peerID string) bool
- type Dispatcher
- type FilterStrategy
- type HandleFunc
- type MessageOption
- type Option
- type OptionFunc
- type Server
- type Stream
- type Subscriber
- type SubscriberOption
Constants ¶
const ( DefaultStrategy FilterStrategy = "DefaultStrategy" BucketsStrategy = "BucketsStrategy" NearestBucketStrategy = "NearestBucketStrategy" BucketsWithFactorStrategy = "BucketsWithFactorStrategy" CorePeersStrategy = "CorePeersStrategy" )
supported filter strategies
const ( MessageVersion1 = "1.0.0" MessageVersion2 = "2.0.0" MessageVersion3 = "3.0.0" )
define message versions
Variables ¶
var ( ErrSubscriber = errors.New("subscribe error") ErrRegistered = errors.New("subscriber already registered") ErrMessageEmpty = errors.New("message empty") ErrMessageHandled = errors.New("message handled") ErrStreamNil = errors.New("stream is nil") ErrNotRegister = errors.New("message not register") )
var ( ErrMessageChecksum = errors.New("verify checksum error") ErrMessageDecompress = errors.New("decompress error") ErrMessageUnmarshal = errors.New("message unmarshal error") )
var ( ErrHandlerError = errors.New("handler error") ErrResponseNil = errors.New("handler response is nil") ErrStreamSendError = errors.New("send response error") ErrChannelBlock = errors.New("channel block") )
var ( // Metrics is the default instance of metrics. It is intended // to be used in conjunction the default Prometheus metrics // registry. Metrics = newMetrics() )
Functions ¶
func GenerateKeyPairWithPath ¶
GenerateKeyPairWithPath generate xuper net key pair
func GenerateNetKeyFromPemKey ¶
GenerateNetKeyFromPemKey get net private key from pem format private key
func GeneratePemKeyFromNetKey ¶
GeneratePemKeyFromNetKey get pem format private key from net private key
func GenerateUniqueRandList ¶
GenerateUniqueRandList get a random unique number list
func GetKeyPairFromPath ¶
GetKeyPairFromPath get xuper net key from file path
func GetPeerIDByAddress ¶
GetPeerIDByAddress return peer ID corresponding to peerAddr
func GetPeerIDFromPath ¶
GetPeerIDFromPath return peer id of given private key path
func GetPemKeyPairFromPath ¶
GetPemKeyPairFromPath get xuper pem private key from file path
func GetRespMessageType ¶
func GetRespMessageType(msgType pb.XuperMessage_MessageType) pb.XuperMessage_MessageType
GetRespMessageType get the message type
func MessageKey ¶
func MessageKey(msg *pb.XuperMessage) string
func NewMessage ¶
func NewMessage(typ pb.XuperMessage_MessageType, message proto.Message, opts ...MessageOption) *pb.XuperMessage
NewMessage create P2P message instance with given params
func NewTLS ¶
func NewTLS(path, serviceName string) (credentials.TransportCredentials, error)
func Unmarshal ¶
func Unmarshal(msg *pb.XuperMessage, message proto.Message) error
Unmarshal unmarshal msgInfo
func VerifyChecksum ¶
func VerifyChecksum(msg *pb.XuperMessage) bool
VerifyChecksum verify the checksum of message
func VerifyMessageType ¶
func VerifyMessageType(request *pb.XuperMessage, response *pb.XuperMessage, peerID string) bool
VerifyMessageType 用于带返回的请求场景下验证收到的消息是否为预期的消息
Types ¶
type Dispatcher ¶
type Dispatcher interface { Register(sub Subscriber) error UnRegister(sub Subscriber) error // Dispatch dispatch message to registered subscriber Dispatch(*pb.XuperMessage, Stream) error }
Dispatcher
func NewDispatcher ¶
func NewDispatcher(ctx *nctx.NetCtx) Dispatcher
type FilterStrategy ¶
type FilterStrategy string
FilterStrategy defines the supported filter strategies
type HandleFunc ¶
type HandleFunc func(xctx.XContext, *pb.XuperMessage) (*pb.XuperMessage, error)
type MessageOption ¶
type MessageOption func(*pb.XuperMessage)
func WithBCName ¶
func WithBCName(bcname string) MessageOption
func WithErrorType ¶
func WithErrorType(errorType pb.XuperMessage_ErrorType) MessageOption
func WithVersion ¶
func WithVersion(version string) MessageOption
type Option ¶
type OptionFunc ¶
type OptionFunc func(*Option)
OptionFunc define single Option function for send message
func WithAccounts ¶
func WithAccounts(accounts []string) OptionFunc
func WithAddresses ¶
func WithAddresses(addresses []string) OptionFunc
WithAddresses add target peer addresses to message Option
func WithFilter ¶
func WithFilter(filters []FilterStrategy) OptionFunc
WithFilter add filter strategies to message Option
func WithPeerIDs ¶
func WithPeerIDs(peerIDs []string) OptionFunc
WithPeerIDs add target peer IDs to message Option
func WithPercent ¶
func WithPercent(percent float32) OptionFunc
WithPercent add percentage to message Option
func WithWhiteList ¶
func WithWhiteList(whiteList map[string]bool) OptionFunc
WithWhiteList add whiteList
type Server ¶
type Server interface { Init(*nctx.NetCtx) error Start() Stop() NewSubscriber(pb.XuperMessage_MessageType, interface{}, ...SubscriberOption) Subscriber Register(Subscriber) error UnRegister(Subscriber) error SendMessage(xctx.XContext, *pb.XuperMessage, ...OptionFunc) error SendMessageWithResponse(xctx.XContext, *pb.XuperMessage, ...OptionFunc) ([]*pb.XuperMessage, error) Context() *nctx.NetCtx PeerInfo() pb.PeerInfo }
P2P is the p2p server interface
type Stream ¶
type Stream interface {
Send(*pb.XuperMessage) error
}
Stream send p2p response message
type Subscriber ¶
type Subscriber interface { GetMessageType() pb.XuperMessage_MessageType Match(*pb.XuperMessage) bool HandleMessage(xctx.XContext, *pb.XuperMessage, Stream) error }
Subscriber is the interface for p2p message subscriber
func NewSubscriber ¶
func NewSubscriber(ctx *nctx.NetCtx, typ pb.XuperMessage_MessageType, v interface{}, opts ...SubscriberOption) Subscriber
type SubscriberOption ¶
type SubscriberOption func(*subscriber)
func WithFilterBCName ¶
func WithFilterBCName(bcName string) SubscriberOption
func WithFilterFrom ¶
func WithFilterFrom(from string) SubscriberOption