Browse Source

Changed MAXPACKETSIZE, added a time of completion

main
Unbewohnte 3 years ago
parent
commit
ac68ddb395
  1. 2
      protocol/constants.go
  2. 6
      protocol/packet.go
  3. 13
      receiver/receiver.go
  4. 7
      receiver/transferinfo.go
  5. 11
      sender/sender.go
  6. 7
      sender/transferinfo.go

2
protocol/constants.go

@ -4,7 +4,7 @@ package protocol
// MAXPACKETSIZE.
// How many bytes can contain one packet (header + body) at maximum
// (packets with size bigger than MAXPACKETSIZE are invalid and will not be sent)
const MAXPACKETSIZE uint = 512000 // 50 kilobytes
const MAXPACKETSIZE uint = 131072 // 128 KiB
// PACKETSIZEDELIMETER.
// Character that delimits one and the other sides of the next incoming packet.

6
protocol/packet.go

@ -78,7 +78,7 @@ func PacketToBytes(packet Packet) ([]byte, error) {
// for debug purposes (ᗜˬᗜ)
// fmt.Printf("SENDING PACKET: %s%s%s%s%s%s\n",
// []byte(PACKETSIZEDELIMETER), packetSizeBytes, []byte(PACKETSIZEDELIMETER),
// []byte(packetToSend.Header), []byte(HEADERDELIMETER), packetToSend.Body)
// []byte(packet.Header), []byte(HEADERDELIMETER), packet.Body)
return packetBuffer.Bytes(), nil
}
@ -152,13 +152,13 @@ func ReadFromConn(connection net.Conn) ([]byte, error) {
// have a packetsize, now reading the whole packet
packetBuffer := new(bytes.Buffer)
// splitting big-sized packet into chunks and constructing it from pieces
// splitting a big-sized packet into chunks and constructing it from pieces
left := packetSize
for {
if left == 0 {
break
}
buff := make([]byte, 1024)
buff := make([]byte, 8192)
if left < len(buff) {
buff = make([]byte, left)
}

13
receiver/receiver.go

@ -53,8 +53,8 @@ func NewReceiver(downloadsFolder string) *Receiver {
Filesize: 0,
},
TransferInfo: &transferInfo{
ReceivedFileBytesPackets: 0,
ApproximateNumberOfFilePackets: 0,
ReceivedFileBytesPackets: 0,
ApproximateNumOfPackets: 0,
},
}
}
@ -166,7 +166,7 @@ func (r *Receiver) HandleFileOffer() error {
return fmt.Errorf("could not send an acceptance packet: %s", err)
}
r.TransferInfo.ApproximateNumberOfFilePackets = uint64(float32(r.FileToDownload.Filesize) / float32(protocol.MAXPACKETSIZE))
r.TransferInfo.ApproximateNumOfPackets = uint64(float32(r.FileToDownload.Filesize) / float32(protocol.MAXPACKETSIZE))
return nil
}
@ -193,6 +193,7 @@ func (r *Receiver) WritePieceOfFile(filePacket protocol.Packet) error {
// Prints a brief information about the state of the transfer
func (r *Receiver) PrintTransferInfo(pauseDuration time.Duration) {
next := time.Now().UTC()
r.TransferInfo.StartTime = next
for {
if r.TransferInfo.ReceivedFileBytesPackets == 0 {
time.Sleep(time.Second)
@ -210,8 +211,8 @@ func (r *Receiver) PrintTransferInfo(pauseDuration time.Duration) {
| Received packets/Approximate number of packets
| (%d|%d) (%.2f%%/100%%)
`, r.TransferInfo.ReceivedFileBytesPackets,
r.TransferInfo.ApproximateNumberOfFilePackets,
float32(r.TransferInfo.ReceivedFileBytesPackets)/float32(r.TransferInfo.ApproximateNumberOfFilePackets)*100)
r.TransferInfo.ApproximateNumOfPackets,
float32(r.TransferInfo.ReceivedFileBytesPackets)/float32(r.TransferInfo.ApproximateNumOfPackets)*100)
time.Sleep(pauseDuration)
}
@ -331,7 +332,7 @@ func (r *Receiver) MainLoop() {
// the sender has completed its mission,
// checking hashes and exiting
fmt.Println("Got ", r.TransferInfo.ReceivedFileBytesPackets, " file packets in total")
fmt.Printf("Got %d file packets in total. Took %v\n", r.TransferInfo.ReceivedFileBytesPackets, time.Since(r.TransferInfo.StartTime))
fmt.Println("Comparing checksums...")
file, err := os.Open(filepath.Join(r.DownloadsFolder, r.FileToDownload.Filename))

7
receiver/transferinfo.go

@ -1,6 +1,9 @@
package receiver
import "time"
type transferInfo struct {
ReceivedFileBytesPackets uint64
ApproximateNumberOfFilePackets uint64
ReceivedFileBytesPackets uint64
ApproximateNumOfPackets uint64
StartTime time.Time
}

11
sender/sender.go

@ -63,8 +63,8 @@ func NewSender(port int, filepath string) *Sender {
Connection: nil,
IncomingPackets: incomingPacketsChan,
TransferInfo: &transferInfo{
SentFileBytesPackets: 0,
ApproximateNumberOfFilePackets: uint64(float32(fileToTransfer.Filesize) / float32(protocol.MAXPACKETSIZE)),
SentFileBytesPackets: 0,
ApproximateNumOfPackets: uint64(float32(fileToTransfer.Filesize) / float32(protocol.MAXPACKETSIZE)),
},
EncryptionKey: key,
TransferAllowed: false,
@ -181,7 +181,7 @@ func (s *Sender) SendOffer() error {
func (s *Sender) SendPiece() error {
// if no data to send - exit
if s.FileToTransfer.LeftBytes == 0 {
fmt.Printf("Done. Sent %d file packets\n", s.TransferInfo.SentFileBytesPackets)
fmt.Printf("Done. Sent %d file packets\nTook %v\n", s.TransferInfo.SentFileBytesPackets, time.Since(s.TransferInfo.StartTime))
s.Stop()
}
@ -224,6 +224,7 @@ func (s *Sender) SendPiece() error {
// Prints a brief information about the state of the transfer
func (s *Sender) PrintTransferInfo(pauseDuration time.Duration) {
next := time.Now().UTC()
s.TransferInfo.StartTime = next
for {
if !s.TransferAllowed {
time.Sleep(time.Second)
@ -241,8 +242,8 @@ func (s *Sender) PrintTransferInfo(pauseDuration time.Duration) {
| Sent packets/Approximate number of packets
| (%d|%d) (%.2f%%/100%%)
`, s.TransferInfo.SentFileBytesPackets,
s.TransferInfo.ApproximateNumberOfFilePackets,
float32(s.TransferInfo.SentFileBytesPackets)/float32(s.TransferInfo.ApproximateNumberOfFilePackets)*100)
s.TransferInfo.ApproximateNumOfPackets,
float32(s.TransferInfo.SentFileBytesPackets)/float32(s.TransferInfo.ApproximateNumOfPackets)*100)
time.Sleep(pauseDuration)
}

7
sender/transferinfo.go

@ -1,6 +1,9 @@
package sender
import "time"
type transferInfo struct {
SentFileBytesPackets uint64
ApproximateNumberOfFilePackets uint64
SentFileBytesPackets uint64
ApproximateNumOfPackets uint64
StartTime time.Time
}

Loading…
Cancel
Save