dht_pb

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLengthDht        = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowDht          = fmt.Errorf("proto: integer overflow")
	ErrUnexpectedEndOfGroupDht = fmt.Errorf("proto: unexpected end of group")
)
View Source
var Message_ConnectionType_name = map[int32]string{
	0: "NOT_CONNECTED",
	1: "CONNECTED",
	2: "CAN_CONNECT",
	3: "CANNOT_CONNECT",
}
View Source
var Message_ConnectionType_value = map[string]int32{
	"NOT_CONNECTED":  0,
	"CONNECTED":      1,
	"CAN_CONNECT":    2,
	"CANNOT_CONNECT": 3,
}
View Source
var Message_MessageType_name = map[int32]string{
	0: "PUT_VALUE",
	1: "GET_VALUE",
	2: "ADD_PROVIDER",
	3: "GET_PROVIDERS",
	4: "FIND_NODE",
	5: "PING",
}
View Source
var Message_MessageType_value = map[string]int32{
	"PUT_VALUE":     0,
	"GET_VALUE":     1,
	"ADD_PROVIDER":  2,
	"GET_PROVIDERS": 3,
	"FIND_NODE":     4,
	"PING":          5,
}

Functions

func Connectedness

Connectedness 返回与Message_ConnectionType关联的network.Connectedness 参数:

  • c: Message_ConnectionType 消息连接类型

返回值:

  • network.Connectedness 连接状态

func PBPeerToPeerInfo

func PBPeerToPeerInfo(pbp Message_Peer) peer.AddrInfo

PBPeerToPeerInfo 将Message_Peer转换为peer.AddrInfo 参数:

  • pbp: Message_Peer 消息对等节点

返回值:

  • peer.AddrInfo 对等节点地址信息

func PBPeersToPeerInfos

func PBPeersToPeerInfos(pbps []Message_Peer) []*peer.AddrInfo

PBPeersToPeerInfos 将Message_Peer切片转换为peer.AddrInfo切片 参数:

  • pbps: []Message_Peer 消息对等节点切片

返回值:

  • []*peer.AddrInfo 对等节点地址信息切片,无效地址将被静默忽略

Types

type Message

type Message struct {
	// defines what type of message it is.
	Type Message_MessageType `protobuf:"varint,1,opt,name=type,proto3,enum=dht.pb.Message_MessageType" json:"type,omitempty"`
	// defines what coral cluster level this query/response belongs to.
	// in case we want to implement coral's cluster rings in the future.
	ClusterLevelRaw int32 `protobuf:"varint,10,opt,name=clusterLevelRaw,proto3" json:"clusterLevelRaw,omitempty"`
	// Used to specify the key associated with this message.
	// PUT_VALUE, GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
	Key []byte `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"`
	// Used to return a value
	// PUT_VALUE, GET_VALUE
	Record *pb.Record `protobuf:"bytes,3,opt,name=record,proto3" json:"record,omitempty"`
	// Used to return peers closer to a key in a query
	// GET_VALUE, GET_PROVIDERS, FIND_NODE
	CloserPeers []Message_Peer `protobuf:"bytes,8,rep,name=closerPeers,proto3" json:"closerPeers"`
	// Used to return Providers
	// GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
	ProviderPeers        []Message_Peer `protobuf:"bytes,9,rep,name=providerPeers,proto3" json:"providerPeers"`
	XXX_NoUnkeyedLiteral struct{}       `json:"-"`
	XXX_unrecognized     []byte         `json:"-"`
	XXX_sizecache        int32          `json:"-"`
}

func NewMessage

func NewMessage(typ Message_MessageType, key []byte, level int) *Message

NewMessage 构造一个新的DHT消息 参数:

  • typ: Message_MessageType 消息类型
  • key: []byte 键值
  • level: int 集群层级

返回值:

  • *Message DHT消息对象

func (*Message) Descriptor

func (*Message) Descriptor() ([]byte, []int)

func (*Message) GetCloserPeers

func (m *Message) GetCloserPeers() []Message_Peer

func (*Message) GetClusterLevel

func (m *Message) GetClusterLevel() int

GetClusterLevel 获取并调整消息的集群层级 需要+/-1调整以区分有效的第一层级(1)和protobuf默认的"无值"行为(0) 参数:

  • m: *Message 消息对象

返回值:

  • int 集群层级

func (*Message) GetClusterLevelRaw

func (m *Message) GetClusterLevelRaw() int32

func (*Message) GetKey

func (m *Message) GetKey() []byte

func (*Message) GetProviderPeers

func (m *Message) GetProviderPeers() []Message_Peer

func (*Message) GetRecord

func (m *Message) GetRecord() *pb.Record

func (*Message) GetType

func (m *Message) GetType() Message_MessageType

func (*Message) Marshal

func (m *Message) Marshal() (dAtA []byte, err error)

func (*Message) MarshalTo

func (m *Message) MarshalTo(dAtA []byte) (int, error)

func (*Message) MarshalToSizedBuffer

func (m *Message) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*Message) ProtoMessage

func (*Message) ProtoMessage()

func (*Message) Reset

func (m *Message) Reset()

func (*Message) SetClusterLevel

func (m *Message) SetClusterLevel(level int)

SetClusterLevel 调整并设置消息的集群层级 需要+/-1调整以区分有效的第一层级(1)和protobuf默认的"无值"行为(0) 参数:

  • m: *Message 消息对象
  • level: int 集群层级

func (*Message) Size

func (m *Message) Size() (n int)

func (*Message) String

func (m *Message) String() string

func (*Message) Unmarshal

func (m *Message) Unmarshal(dAtA []byte) error

func (*Message) XXX_DiscardUnknown

func (m *Message) XXX_DiscardUnknown()

func (*Message) XXX_Marshal

func (m *Message) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Message) XXX_Merge

func (m *Message) XXX_Merge(src proto.Message)

func (*Message) XXX_Size

func (m *Message) XXX_Size() int

func (*Message) XXX_Unmarshal

func (m *Message) XXX_Unmarshal(b []byte) error

type MessageSender

type MessageSender interface {
	// SendRequest 向对等节点发送消息并等待响应
	SendRequest(ctx context.Context, p peer.ID, pmes *Message) (*Message, error)
	// SendMessage 向对等节点发送消息但不等待响应
	SendMessage(ctx context.Context, p peer.ID, pmes *Message) error
}

MessageSender 处理向指定对等节点发送协议消息的接口

type MessageSenderWithDisconnect

type MessageSenderWithDisconnect interface {
	MessageSender

	OnDisconnect(context.Context, peer.ID)
}

MessageSenderWithDisconnect 带断开连接处理的消息发送器接口

type Message_ConnectionType

type Message_ConnectionType int32
const (
	// sender does not have a connection to peer, and no extra information (default)
	Message_NOT_CONNECTED Message_ConnectionType = 0
	// sender has a live connection to peer
	Message_CONNECTED Message_ConnectionType = 1
	// sender recently connected to peer
	Message_CAN_CONNECT Message_ConnectionType = 2
	// sender recently tried to connect to peer repeatedly but failed to connect
	// ("try" here is loose, but this should signal "made strong effort, failed")
	Message_CANNOT_CONNECT Message_ConnectionType = 3
)

func ConnectionType

ConnectionType 返回与network.Connectedness关联的Message_ConnectionType 参数:

  • c: network.Connectedness 连接状态

返回值:

  • Message_ConnectionType 消息连接类型

func (Message_ConnectionType) EnumDescriptor

func (Message_ConnectionType) EnumDescriptor() ([]byte, []int)

func (Message_ConnectionType) String

func (x Message_ConnectionType) String() string

type Message_MessageType

type Message_MessageType int32
const (
	Message_PUT_VALUE     Message_MessageType = 0
	Message_GET_VALUE     Message_MessageType = 1
	Message_ADD_PROVIDER  Message_MessageType = 2
	Message_GET_PROVIDERS Message_MessageType = 3
	Message_FIND_NODE     Message_MessageType = 4
	Message_PING          Message_MessageType = 5
)

func (Message_MessageType) EnumDescriptor

func (Message_MessageType) EnumDescriptor() ([]byte, []int)

func (Message_MessageType) String

func (x Message_MessageType) String() string

type Message_Peer

type Message_Peer struct {
	// ID of a given peer.
	Id byteString `protobuf:"bytes,1,opt,name=id,proto3,customtype=byteString" json:"id"`
	// multiaddrs for a given peer
	Addrs [][]byte `protobuf:"bytes,2,rep,name=addrs,proto3" json:"addrs,omitempty"`
	// used to signal the sender's connection capabilities to the peer
	Connection           Message_ConnectionType `protobuf:"varint,3,opt,name=connection,proto3,enum=dht.pb.Message_ConnectionType" json:"connection,omitempty"`
	XXX_NoUnkeyedLiteral struct{}               `json:"-"`
	XXX_unrecognized     []byte                 `json:"-"`
	XXX_sizecache        int32                  `json:"-"`
}

func PeerInfosToPBPeers

func PeerInfosToPBPeers(n network.Network, peers []peer.AddrInfo) []Message_Peer

PeerInfosToPBPeers 将对等节点切片转换为Message_Peer切片,并设置连接状态 参数:

  • n: network.Network 网络对象
  • peers: []peer.AddrInfo 对等节点地址信息切片

返回值:

  • []Message_Peer 转换后的消息对等节点切片

func PeerRoutingInfosToPBPeers

func PeerRoutingInfosToPBPeers(peers []PeerRoutingInfo) []Message_Peer

PeerRoutingInfosToPBPeers 将PeerRoutingInfo切片转换为Message_Peer切片 参数:

  • peers: []PeerRoutingInfo 对等节点路由信息切片

返回值:

  • []Message_Peer 转换后的消息对等节点切片

func RawPeerInfosToPBPeers

func RawPeerInfosToPBPeers(peers []peer.AddrInfo) []Message_Peer

RawPeerInfosToPBPeers 将对等节点切片转换为Message_Peer切片 参数:

  • peers: []peer.AddrInfo 对等节点地址信息切片

返回值:

  • []Message_Peer 转换后的消息对等节点切片

func (*Message_Peer) Addresses

func (m *Message_Peer) Addresses() []ma.Multiaddr

Addresses 返回与Message_Peer条目关联的多地址 参数:

  • m: *Message_Peer 消息对等节点

返回值:

  • []ma.Multiaddr 多地址切片

func (*Message_Peer) Descriptor

func (*Message_Peer) Descriptor() ([]byte, []int)

func (*Message_Peer) GetAddrs

func (m *Message_Peer) GetAddrs() [][]byte

func (*Message_Peer) GetConnection

func (m *Message_Peer) GetConnection() Message_ConnectionType

func (*Message_Peer) Marshal

func (m *Message_Peer) Marshal() (dAtA []byte, err error)

func (*Message_Peer) MarshalTo

func (m *Message_Peer) MarshalTo(dAtA []byte) (int, error)

func (*Message_Peer) MarshalToSizedBuffer

func (m *Message_Peer) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*Message_Peer) ProtoMessage

func (*Message_Peer) ProtoMessage()

func (*Message_Peer) Reset

func (m *Message_Peer) Reset()

func (*Message_Peer) Size

func (m *Message_Peer) Size() (n int)

func (*Message_Peer) String

func (m *Message_Peer) String() string

func (*Message_Peer) Unmarshal

func (m *Message_Peer) Unmarshal(dAtA []byte) error

func (*Message_Peer) XXX_DiscardUnknown

func (m *Message_Peer) XXX_DiscardUnknown()

func (*Message_Peer) XXX_Marshal

func (m *Message_Peer) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Message_Peer) XXX_Merge

func (m *Message_Peer) XXX_Merge(src proto.Message)

func (*Message_Peer) XXX_Size

func (m *Message_Peer) XXX_Size() int

func (*Message_Peer) XXX_Unmarshal

func (m *Message_Peer) XXX_Unmarshal(b []byte) error

type PeerRoutingInfo

type PeerRoutingInfo struct {
	peer.AddrInfo
	network.Connectedness
}

PeerRoutingInfo 对等节点路由信息

type ProtocolMessenger

type ProtocolMessenger struct {
	// contains filtered or unexported fields
}

ProtocolMessenger 用于向对等节点发送DHT消息并处理其响应。 这将协议格式与DHT协议实现和路由接口实现解耦。

注意: ProtocolMessenger的MessageSender仍需处理一些协议细节,如使用varint分隔的protobuf

func NewProtocolMessenger

func NewProtocolMessenger(msgSender MessageSender, opts ...ProtocolMessengerOption) (*ProtocolMessenger, error)

NewProtocolMessenger 创建一个新的ProtocolMessenger用于向对等节点发送DHT消息并处理其响应 参数:

  • msgSender: MessageSender 消息发送器
  • opts: ...ProtocolMessengerOption 选项函数

返回值:

  • *ProtocolMessenger 协议消息器
  • error 错误信息

func (*ProtocolMessenger) GetClosestPeers

func (pm *ProtocolMessenger) GetClosestPeers(ctx context.Context, p peer.ID, id peer.ID) (closerPeers []*peer.AddrInfo, err error)

GetClosestPeers 请求对等节点返回在XOR空间中离id最近的K个DHT服务器节点(K是DHT范围的参数) 注意:如果对等节点恰好知道一个peerID完全匹配给定id的对等节点,即使该节点不是DHT服务器节点也会返回 参数:

  • ctx: context.Context 上下文
  • p: peer.ID 对等节点ID
  • id: peer.ID 目标ID

返回值:

  • []*peer.AddrInfo 更近的对等节点
  • error 错误信息

func (*ProtocolMessenger) GetProviders

func (pm *ProtocolMessenger) GetProviders(ctx context.Context, p peer.ID, key multihash.Multihash) (provs []*peer.AddrInfo, closerPeers []*peer.AddrInfo, err error)

GetProviders 向对等节点请求它所知道的给定键的提供者。同时返回离键最近的K个对等节点,如GetClosestPeers所述 参数:

  • ctx: context.Context 上下文
  • p: peer.ID 对等节点ID
  • key: multihash.Multihash 键的多重哈希

返回值:

  • []*peer.AddrInfo 提供者列表
  • []*peer.AddrInfo 更近的对等节点
  • error 错误信息

func (*ProtocolMessenger) GetValue

func (pm *ProtocolMessenger) GetValue(ctx context.Context, p peer.ID, key string) (record *recpb.Record, closerPeers []*peer.AddrInfo, err error)

GetValue 向对等节点请求给定键对应的值。同时返回离键最近的K个对等节点,如GetClosestPeers所述 参数:

  • ctx: context.Context 上下文
  • p: peer.ID 对等节点ID
  • key: string 键

返回值:

  • *recpb.Record 记录
  • []*peer.AddrInfo 更近的对等节点
  • error 错误信息

func (*ProtocolMessenger) Ping

func (pm *ProtocolMessenger) Ping(ctx context.Context, p peer.ID) (err error)

Ping 向指定对等节点发送ping消息并等待响应 参数:

  • ctx: context.Context 上下文
  • p: peer.ID 对等节点ID

返回值:

  • error 错误信息

func (*ProtocolMessenger) PutProvider

func (pm *ProtocolMessenger) PutProvider(ctx context.Context, p peer.ID, key multihash.Multihash, h host.Host) error

PutProvider 已弃用:请使用[ProtocolMessenger.PutProviderAddrs]

func (*ProtocolMessenger) PutProviderAddrs

func (pm *ProtocolMessenger) PutProviderAddrs(ctx context.Context, p peer.ID, key multihash.Multihash, self peer.AddrInfo) (err error)

PutProviderAddrs 请求对等节点存储我们是给定键的提供者 参数:

  • ctx: context.Context 上下文
  • p: peer.ID 对等节点ID
  • key: multihash.Multihash 键的多重哈希
  • self: peer.AddrInfo 本节点地址信息

返回值:

  • error 错误信息

func (*ProtocolMessenger) PutValue

func (pm *ProtocolMessenger) PutValue(ctx context.Context, p peer.ID, rec *recpb.Record) (err error)

PutValue 请求对等节点存储给定的键值对 参数:

  • ctx: context.Context 上下文
  • p: peer.ID 对等节点ID
  • rec: *recpb.Record 记录

返回值:

  • error 错误信息

type ProtocolMessengerOption

type ProtocolMessengerOption func(*ProtocolMessenger) error

ProtocolMessengerOption ProtocolMessenger选项函数类型

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL