mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-21 04:17:50 +01:00
Message encoding of announcement and response messages.
More changes coming. Rename of organization to Peernet s.r.o. InformationRequest proper termination signal.
This commit is contained in:
54
Commands.go
54
Commands.go
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
File Name: Commands.go
|
||||
Copyright: 2021 Peernet Foundation s.r.o.
|
||||
Copyright: 2021 Peernet s.r.o.
|
||||
Author: Peter Kleissner
|
||||
*/
|
||||
|
||||
@@ -9,36 +9,10 @@ package core
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
)
|
||||
|
||||
// Commands between peers
|
||||
const (
|
||||
// Peer List Management
|
||||
CommandAnnouncement = 0 // Announcement
|
||||
CommandResponse = 1 // Response
|
||||
CommandPing = 2 // Keep-alive message (no payload).
|
||||
CommandPong = 3 // Response to ping (no payload).
|
||||
|
||||
// Blockchain
|
||||
CommandGet = 4 // Request blocks for specified peer.
|
||||
|
||||
// File Discovery
|
||||
|
||||
// Debug
|
||||
CommandChat = 10 // Chat message [debug]
|
||||
)
|
||||
|
||||
// packet2 is a high-level message between peers
|
||||
type packet2 struct {
|
||||
PacketRaw
|
||||
SenderPublicKey *btcec.PublicKey // Sender Public Key, ECDSA (secp256k1) 257-bit
|
||||
connection *Connection // Connection that received the packet
|
||||
}
|
||||
|
||||
// cmdAnouncement handles an incoming announcement
|
||||
func (peer *PeerInfo) cmdAnouncement(msg *packet2) {
|
||||
func (peer *PeerInfo) cmdAnouncement(msg *MessageAnnouncement) {
|
||||
if peer == nil {
|
||||
peer, added := PeerlistAdd(msg.SenderPublicKey, msg.connection)
|
||||
fmt.Printf("Incoming initial announcement from %s\n", msg.connection.Address.String())
|
||||
@@ -57,7 +31,7 @@ func (peer *PeerInfo) cmdAnouncement(msg *packet2) {
|
||||
}
|
||||
|
||||
// cmdResponse handles the response to the announcement
|
||||
func (peer *PeerInfo) cmdResponse(msg *packet2) {
|
||||
func (peer *PeerInfo) cmdResponse(msg *MessageResponse) {
|
||||
if peer == nil {
|
||||
peer, _ = PeerlistAdd(msg.SenderPublicKey, msg.connection)
|
||||
fmt.Printf("Incoming initial response from %s\n", msg.connection.Address.String())
|
||||
@@ -69,7 +43,7 @@ func (peer *PeerInfo) cmdResponse(msg *packet2) {
|
||||
}
|
||||
|
||||
// cmdPing handles an incoming ping message
|
||||
func (peer *PeerInfo) cmdPing(msg *packet2) {
|
||||
func (peer *PeerInfo) cmdPing(msg *MessageRaw) {
|
||||
if peer == nil {
|
||||
// Unexpected incoming ping, reply with announce message
|
||||
// TODO
|
||||
@@ -80,12 +54,12 @@ func (peer *PeerInfo) cmdPing(msg *packet2) {
|
||||
}
|
||||
|
||||
// cmdPong handles an incoming pong message
|
||||
func (peer *PeerInfo) cmdPong(msg *packet2) {
|
||||
func (peer *PeerInfo) cmdPong(msg *MessageRaw) {
|
||||
//fmt.Printf("Incoming pong from %s on %s\n", msg.connection.Address.String(), msg.connection.Address.String())
|
||||
}
|
||||
|
||||
// cmdChat handles a chat message [debug]
|
||||
func (peer *PeerInfo) cmdChat(msg *packet2) {
|
||||
func (peer *PeerInfo) cmdChat(msg *MessageRaw) {
|
||||
fmt.Printf("Chat from '%s': %s\n", msg.connection.Address.String(), string(msg.PacketRaw.Payload))
|
||||
}
|
||||
|
||||
@@ -124,7 +98,7 @@ func autoPingAll() {
|
||||
}
|
||||
|
||||
if connection.LastPacketIn.Before(thresholdPing) && connection.LastPingOut.Before(thresholdPing) {
|
||||
peer.sendPing(connection)
|
||||
peer.pingConnection(connection)
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -139,26 +113,16 @@ func autoPingAll() {
|
||||
|
||||
// if no ping was sent recently, send one now
|
||||
if connection.LastPingOut.Before(thresholdPingOut1) {
|
||||
peer.sendPing(connection)
|
||||
peer.pingConnection(connection)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sendPing sends a ping to the target peer
|
||||
func (peer *PeerInfo) sendPing(connection *Connection) {
|
||||
err := peer.sendConnection(&PacketRaw{Command: CommandPing}, connection)
|
||||
connection.LastPingOut = time.Now()
|
||||
|
||||
if (connection.Status == ConnectionActive || connection.Status == ConnectionRedundant) && IsNetworkErrorFatal(err) {
|
||||
peer.invalidateActiveConnection(connection)
|
||||
}
|
||||
}
|
||||
|
||||
// SendChatAll sends a text message to all peers
|
||||
func SendChatAll(text string) {
|
||||
for _, peer := range PeerlistGet() {
|
||||
peer.send(&PacketRaw{Command: CommandChat, Payload: []byte(text)})
|
||||
peer.Chat(text)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user