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
if node.State.AllowedToTransfer && node.TransferInfo.Ready {
err = sendPiece(file, node.Net.Conn, node.Net.EncryptionKey)
if err != nil {
if err == ErrorSentAll {
// the file has been sent fully
fileIDBuff := new(bytes.Buffer)
err = binary.Write(fileIDBuff, binary.BigEndian, file.ID)
switch err {
case ErrorSentAll:
// the file has been sent fully
fileIDBuff := new(bytes.Buffer)
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 {
panic(err)
}
}
endFilePacket := protocol.Packet{
Header: protocol.HeaderEndfile,
Body: fileIDBuff.Bytes(),
}
protocol.SendPacket(node.Net.Conn, endFilePacket)
if node.Net.EncryptionKey != nil {
err = endFilePacket.EncryptBody(node.Net.EncryptionKey)
if err != nil {
panic(err)
}
}
// because there`s still no handling for directories - send
// done packet
protocol.SendPacket(node.Net.Conn, protocol.Packet{
Header: protocol.HeaderDone,
})
protocol.SendPacket(node.Net.Conn, endFilePacket)
node.State.Stopped = true
// because there`s still no handling for directories - send
// done packet
protocol.SendPacket(node.Net.Conn, protocol.Packet{
Header: protocol.HeaderDone,
})
case nil:
node.State.Stopped = true
} else {
node.State.Stopped = true
default:
node.State.Stopped = true
fmt.Printf("An error occured when sending a piece of \"%s\": %s\n", file.Name, err)
panic(err)
}
fmt.Printf("An error occured while sending a piece of \"%s\": %s\n", file.Name, err)
panic(err)
}
node.TransferInfo.Ready = false

Loading…
Cancel
Save