routine
authorvictor <victor@e16421f0-f15b-0410-abcd-98678b794739>
Tue, 24 Nov 2009 11:58:44 +0000 (11:58 +0000)
committervictor <victor@e16421f0-f15b-0410-abcd-98678b794739>
Tue, 24 Nov 2009 11:58:44 +0000 (11:58 +0000)
git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@626 e16421f0-f15b-0410-abcd-98678b794739

datagram.cpp
ext/send_control.cpp
hashtree.cpp
sendrecv.cpp
transfer.cpp

index ecb72df..4d6a4ef 100644 (file)
@@ -102,7 +102,7 @@ int Datagram::Recv () {
        socklen_t addrlen = sizeof(struct sockaddr_in);
        offset = 0;
        length = recvfrom (sock, (char *)buf, MAXDGRAMSZ, 0,
-                                          (struct sockaddr*)&(addr), &addrlen);
+                                          (struct sockaddr*)&(addr.addr), &addrlen);
        if (length<0) {
         length = 0;
         print_error("error on recv");
index 490ecfa..c2c1cad 100644 (file)
@@ -51,7 +51,8 @@ void    PingPongController::OnAckRcvd(bin64_t ackd) {
 }
 
 
-KeepAliveController::KeepAliveController (Channel* ch) : SendController(ch), delay_(ch->rtt_avg_) {
+KeepAliveController::KeepAliveController (Channel* ch) : 
+SendController(ch), delay_(ch->rtt_avg_) {
 }
 
 
@@ -108,7 +109,7 @@ bool    CwndController::MaySendData() {
 
 
 void    CwndController::OnDataSent(bin64_t b) {
-    if ( (b==bin64_t::ALL || b==bin64_t::NONE) && MaySendData() ) { // no more data (no hints?)
+    if ( (b==bin64_t::ALL || b==bin64_t::NONE) && MaySendData() ) {// no more data (no hints?)
         Schedule(NOW+ch_->rtt_avg_); // soft pause; nothing to send yet
         if (ch_->last_send_data_time_ < NOW-ch_->rtt_avg_)
             Swap(new KeepAliveController(this)); // really, nothing to send
index fd3a713..4f08b11 100644 (file)
@@ -85,6 +85,7 @@ complete_(0), completek_(0)
 {
        fd_ = open(filename,OPENFLAGS,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
        if (fd_<0) {
+        fd_ = 0;
         print_error("cannot open the file");
         return;
     }
@@ -96,6 +97,7 @@ complete_(0), completek_(0)
         strcpy(hfn,hash_filename);
     hash_fd_ = open(hfn,OPENFLAGS,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
     if (hash_fd_<0) {
+        hash_fd_ = 0;
         print_error("cannot open hash file");
         return;
     }
@@ -110,7 +112,7 @@ complete_(0), completek_(0)
 
 void            HashTree::Submit () {
     size_ = file_size(fd_);
-       sizek_ = (size_>>10) + ((size_&1023) ? 1 : 0);
+       sizek_ = (size_ + 1023) >> 10;
     peak_count_ = bin64_t::peaks(sizek_,peaks_);
     int hashes_size = Sha1Hash::SIZE*sizek_*2;
     file_resize(hash_fd_,hashes_size);
@@ -150,7 +152,7 @@ void            HashTree::Submit () {
  for some optimizations. */
 void            HashTree::RecoverProgress () {
     size_t size = file_size(fd_);
-       size_t sizek = (size>>10) + ((size&1023) ? 1 : 0);
+       size_t sizek = (size + 1023) >> 10;
     bin64_t peaks[64];
     int peak_count = bin64_t::peaks(sizek,peaks);
     for(int i=0; i<peak_count; i++) {
@@ -211,7 +213,7 @@ bool            HashTree::OfferPeakHash (bin64_t pos, const Sha1Hash& hash) {
 
     size_ = sizek_<<10;
     completek_ = complete_ = 0;
-       sizek_ = (size_>>10) + ((size_&1023) ? 1 : 0);
+       sizek_ = (size_ + 1023) >> 10;
 
     size_t cur_size = file_size(fd_);
     if ( cur_size<=(sizek_-1)<<10  || cur_size>sizek_<<10 )
@@ -343,5 +345,7 @@ HashTree::~HashTree () {
         memory_unmap(hash_fd_, hashes_, sizek_*2*sizeof(Sha1Hash));
     if (fd_)
         close(fd_);
+    if (hash_fd_)
+        close(hash_fd_);
 }
 
index 6895739..7506751 100644 (file)
@@ -54,6 +54,9 @@ bin64_t               Channel::DequeueHint () { // TODO: resilience
         hint_in_.pop_front();
         if (time < NOW-TINT_SEC*3/2 ) //NOW-8*rtt_avg_)
             continue;
+        // Totally flawed:
+        // a. May empty the queue when you least expect
+        // b. May lose parts of partially ACKd HINTs
         send = file().ack_out().find_filtered(ack_in_,hint,bins::FILLED);
         send = send.left_foot(); // single packet
         if (send!=bin64_t::NONE)
@@ -275,7 +278,7 @@ void    Channel::CleanHintOut (bin64_t pos) {
     while (hi<hint_out_.size() && !pos.within(hint_out_[hi].bin))
         hi++;
     if (hi==hint_out_.size())
-        return;
+        return; // something not hinted or hinted in far past
     while (hi--) { // removing likely snubbed hints
         hint_out_size_ -= hint_out_.front().bin.width();
         hint_out_.pop_front();
index 97979b8..d64cd60 100644 (file)
@@ -44,6 +44,9 @@ FileTransfer::~FileTransfer ()
 {
 
     files[fd()] = NULL;
+    for(int i=0; i<Channel::channels.size(); i++) 
+        if (Channel::channels[i] && Channel::channels[i]->transfer_==this) 
+            delete Channel::channels[i];
 }