Browse Source

[node] clearer sendPiece error handling in sender node

main
Unbewohnte 3 years ago
parent
commit
442af0205e
  1. 56
      src/node/node.go

56
src/node/node.go

@ -223,42 +223,44 @@ func (node *Node) Start() {
// and wait for it to be ready again // and wait for it to be ready again
if node.State.AllowedToTransfer && node.TransferInfo.Ready { if node.State.AllowedToTransfer && node.TransferInfo.Ready {
err = sendPiece(file, node.Net.Conn, node.Net.EncryptionKey) err = sendPiece(file, node.Net.Conn, node.Net.EncryptionKey)
if err != nil { switch err {
if err == ErrorSentAll { case ErrorSentAll:
// the file has been sent fully // the file has been sent fully
fileIDBuff := new(bytes.Buffer) fileIDBuff := new(bytes.Buffer)
err = binary.Write(fileIDBuff, binary.BigEndian, file.ID) err = binary.Write(fileIDBuff, binary.BigEndian, file.ID)
if err != nil {
panic(err)
}
endFilePacket := protocol.Packet{
Header: protocol.HeaderEndfile,
Body: fileIDBuff.Bytes(),
}
if node.Net.EncryptionKey != nil {
err = endFilePacket.EncryptBody(node.Net.EncryptionKey)
if err != nil { if err != nil {
panic(err) panic(err)
} }
}
endFilePacket := protocol.Packet{ protocol.SendPacket(node.Net.Conn, endFilePacket)
Header: protocol.HeaderEndfile,
Body: fileIDBuff.Bytes(),
}
if node.Net.EncryptionKey != nil { // because there`s still no handling for directories - send
err = endFilePacket.EncryptBody(node.Net.EncryptionKey) // done packet
if err != nil { protocol.SendPacket(node.Net.Conn, protocol.Packet{
panic(err) Header: protocol.HeaderDone,
} })
}
protocol.SendPacket(node.Net.Conn, endFilePacket) node.State.Stopped = true
// because there`s still no handling for directories - send case nil:
// done packet
protocol.SendPacket(node.Net.Conn, protocol.Packet{
Header: protocol.HeaderDone,
})
node.State.Stopped = true default:
} else { node.State.Stopped = true
node.State.Stopped = true
fmt.Printf("An error occured when sending a piece of \"%s\": %s\n", file.Name, err) fmt.Printf("An error occured while sending a piece of \"%s\": %s\n", file.Name, err)
panic(err) panic(err)
}
} }
node.TransferInfo.Ready = false node.TransferInfo.Ready = false

Loading…
Cancel
Save