Skip to content

Commit

Permalink
Added packet split for DHT client
Browse files Browse the repository at this point in the history
  • Loading branch information
crioto committed Mar 13, 2018
1 parent 333665e commit 4da3722
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions lib/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"time"

proto "github.com/golang/protobuf/proto"
uuid "github.com/wayn3h0/go-uuid"
)

Expand Down Expand Up @@ -134,7 +133,10 @@ func (dht *DHTClient) read() (*DHTPacket, error) {
func (dht *DHTClient) send(packet *DHTPacket) error {
// if dht.OutgoingData != nil && !dht.isShutdown {
if dht.OutgoingData != nil {
dht.OutgoingData <- packet
packets, _ := dht.ProducePacket(packet)
for _, npacket := range packets {
dht.OutgoingData <- npacket
}
} else {
// Log(Debug, "%+v ||| %+v", dht.OutgoingData, dht.isShutdown)
return fmt.Errorf("Trying to send to closed channel")
Expand Down Expand Up @@ -322,16 +324,12 @@ func (dht *DHTClient) ReportLoad(clientsNum int) error {

// ProduceData will return one or more byte slices for a provided packet
// If packet is too big it will be split into multiple packets
func (dht *DHTClient) ProducePacket(packet *DHTPacket) ([][]byte, error) {
func (dht *DHTClient) ProducePacket(packet *DHTPacket) ([]*DHTPacket, error) {

result := [][]byte{}
result := []*DHTPacket{}

if len(packet.Proxies) < 4 && len(packet.Arguments) < 4 {
data, err := proto.Marshal(packet)
if err != nil {
return result, err
}
result = append(result, data)
result = append(result, packet)
} else {
for len(packet.Proxies) != 0 || len(packet.Arguments) != 0 {
npacket := &DHTPacket{
Expand Down Expand Up @@ -359,11 +357,7 @@ func (dht *DHTClient) ProducePacket(packet *DHTPacket) ([][]byte, error) {
packet.Arguments = packet.Arguments[:0]
}

data, err := proto.Marshal(npacket)
if err != nil {
return result, err
}
result = append(result, data)
result = append(result, npacket)
}
}

Expand Down

0 comments on commit 4da3722

Please sign in to comment.