Documentation ¶
Overview ¶
Package packet provides network messaging protocol and serialization layer.
Packet can be created with shortcut:
senderAddr, _ := host.NewAddress("127.0.0.1:1337") receiverAddr, _ := host.NewAddress("127.0.0.1:1338") sender := host.NewHost(senderAddr) receiver := host.NewHost(receiverAddr) msg := packet.NewPingPacket(sender, receiver) // do something with packet
Or with builder:
builder := packet.NewBuilder() senderAddr, _ := host.NewAddress("127.0.0.1:1337") receiverAddr, _ := host.NewAddress("127.0.0.1:1338") sender := host.NewHost(senderAddr) receiver := host.NewHost(receiverAddr) msg := builder. Sender(sender). Receiver(receiver). Type(packet.TypeFindHost). Request(&packet.RequestDataFindHost{}). Build() // do something with packet
Packet may be serialized:
msg := &packet.Packet{} serialized, err := packet.SerializePacket(msg) if err != nil { panic(err.Error()) } fmt.Println(serialized)
And deserialized therefore:
var buffer bytes.Buffer // Fill buffer somewhere msg, err := packet.DeserializePacket(buffer) if err != nil { panic(err.Error()) } // do something with packet
Index ¶
- Constants
- func SerializePacket(q *Packet) ([]byte, error)
- type Builder
- func (cb Builder) Build() (packet *Packet)
- func (cb Builder) Error(err error) Builder
- func (cb Builder) Receiver(host *host.Host) Builder
- func (cb Builder) Request(request interface{}) Builder
- func (cb Builder) Response(response interface{}) Builder
- func (cb Builder) Sender(host *host.Host) Builder
- func (cb Builder) Type(packetType packetType) Builder
- type CheckNodePrivState
- type CommandType
- type Packet
- type RequestAuth
- type RequestCascadeSend
- type RequestCheckNodePriv
- type RequestCheckOrigin
- type RequestDataFindHost
- type RequestDataFindValue
- type RequestDataRPC
- type RequestDataStore
- type RequestGetRandomHosts
- type RequestID
- type RequestKnownOuterHosts
- type RequestObtainIP
- type RequestPulse
- type RequestRelay
- type RequestRelayOwnership
- type ResponseAuth
- type ResponseCascadeSend
- type ResponseCheckNodePriv
- type ResponseCheckOrigin
- type ResponseDataFindHost
- type ResponseDataFindValue
- type ResponseDataRPC
- type ResponseDataStore
- type ResponseGetRandomHosts
- type ResponseKnownOuterHosts
- type ResponseObtainIP
- type ResponsePulse
- type ResponseRelay
- type ResponseRelayOwnership
Constants ¶
const ( // TypePing is packet type for ping method. TypePing packetType = iota + 1 // TypeStore is packet type for store method. TypeStore // TypeFindHost is packet type for FindHost method. TypeFindHost // TypeFindValue is packet type for FindValue method. TypeFindValue // TypeRPC is packet type for RPC method. TypeRPC // TypeRelay is packet type for request target to be a relay. TypeRelay // TypeAuth is packet type for authentication between hosts. TypeAuth // TypeCheckOrigin is packet to check originality of some host. TypeCheckOrigin // TypeObtainIP is packet to get itself IP from another host. TypeObtainIP // TypeRelayOwnership is packet to say all other hosts that current host have a static IP. TypeRelayOwnership // TypeKnownOuterHosts is packet to say how much outer hosts current host know. TypeKnownOuterHosts // TypeCheckNodePriv is packet to check preset node privileges. TypeCheckNodePriv // TypeCascadeSend is the packet type for the cascade send message feature TypeCascadeSend // TypePulse is packet type for the messages received from pulsars TypePulse // TypeGetRandomHosts is packet type for the call to get random hosts of the DHT network TypeGetRandomHosts )
const ( // Unknown - unknown command. Unknown = CommandType(iota + 1) // StartRelay - command start relay. StartRelay // StopRelay - command stop relay. StopRelay // BeginAuth - begin authentication. BeginAuth // RevokeAuth - revoke authentication. RevokeAuth )
const ( // Error - some error, see error string. Error = CheckNodePrivState(iota + 1) // Confirmed - state confirmed. Confirmed // Declined - state declined. Declined )
Variables ¶
This section is empty.
Functions ¶
func SerializePacket ¶
SerializePacket converts packet to byte slice.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder allows lazy building of packets. Each operation returns new copy of a builder.
type CheckNodePrivState ¶ added in v0.0.5
type CheckNodePrivState int
CheckNodePrivState - state of check node privileges request.
type Packet ¶
type Packet struct { Sender *host.Host Receiver *host.Host Type packetType RequestID RequestID RemoteAddress string Data interface{} Error error IsResponse bool }
Packet is DHT packet object.
func DeserializePacket ¶
DeserializePacket reads packet from io.Reader.
func NewPingPacket ¶
NewPingPacket can be used as a shortcut for creating ping packets instead of packet Builder.
type RequestAuth ¶
type RequestAuth struct {
Command CommandType
}
RequestAuth is data for authentication.
type RequestCascadeSend ¶ added in v0.2.0
type RequestCascadeSend struct { RPC RequestDataRPC Data core.Cascade }
RequestCascadeSend is data for cascade sending feature
type RequestCheckNodePriv ¶ added in v0.0.5
type RequestCheckNodePriv struct {
RoleKey string
}
RequestCheckNodePriv is data for check node privileges.
type RequestCheckOrigin ¶
type RequestCheckOrigin struct { }
RequestCheckOrigin is data to check originality.
type RequestDataFindHost ¶
type RequestDataFindHost struct {
Target []byte
}
RequestDataFindHost is data for FindHost request.
type RequestDataFindValue ¶
type RequestDataFindValue struct {
Target []byte
}
RequestDataFindValue is data for FindValue request.
type RequestDataRPC ¶
RequestDataRPC is data for RPC request.
type RequestDataStore ¶
type RequestDataStore struct { Data []byte Publishing bool // Whether or not we are the original publisher. }
RequestDataStore is data for Store request.
type RequestGetRandomHosts ¶ added in v0.3.0
type RequestGetRandomHosts struct {
HostsNumber int
}
RequestGetRandomHosts is data for the call that returns random hosts of the DHT network
type RequestKnownOuterHosts ¶
type RequestKnownOuterHosts struct { ID string // origin ID OuterHosts int // number of known outer hosts }
RequestKnownOuterHosts is data to notify home subnet about known outer hosts.
type RequestPulse ¶ added in v0.2.0
RequestPulse is data received from a pulsar
type RequestRelay ¶
type RequestRelay struct {
Command CommandType
}
RequestRelay is data for relay request (commands: start/stop relay).
type RequestRelayOwnership ¶
type RequestRelayOwnership struct {
Ready bool
}
RequestRelayOwnership is data to notify that current host can be a relay.
type ResponseAuth ¶
ResponseAuth is data for authentication request response.
type ResponseCascadeSend ¶ added in v0.2.0
ResponseCascadeSend is the response data of a cascade sending call
type ResponseCheckNodePriv ¶ added in v0.0.5
type ResponseCheckNodePriv struct { State CheckNodePrivState Error string }
ResponseCheckNodePriv is data for check node privileges response.
type ResponseCheckOrigin ¶
type ResponseCheckOrigin struct {
AuthUniqueKey []byte
}
ResponseCheckOrigin is data for check originality request response.
type ResponseDataFindHost ¶
ResponseDataFindHost is data for FindHost response.
type ResponseDataFindValue ¶
ResponseDataFindValue is data for FindValue response.
type ResponseDataRPC ¶
ResponseDataRPC is data for RPC response.
type ResponseDataStore ¶
type ResponseDataStore struct {
Success bool
}
ResponseDataStore is data for Store response.
type ResponseGetRandomHosts ¶ added in v0.3.0
ResponseGetRandomHosts is the response containing random hosts of the DHT network
type ResponseKnownOuterHosts ¶
type ResponseKnownOuterHosts struct { ID string // id of host in which more known outer hosts OuterHosts int // number of known outer hosts }
ResponseKnownOuterHosts is data to answer if origin host know more outer hosts.
type ResponseObtainIP ¶
type ResponseObtainIP struct {
IP string
}
ResponseObtainIP is data for get a IP of requesting host.
type ResponsePulse ¶ added in v0.2.0
ResponsePulse is the response for a new pulse from a pulsar
type ResponseRelay ¶
ResponseRelay is data for relay request response.
type ResponseRelayOwnership ¶
type ResponseRelayOwnership struct {
Accepted bool
}
ResponseRelayOwnership is data to response to relay ownership request.