Added Channel::Close()
authorVictor Grishchenko <victor.grishchenko@gmail.com>
Thu, 4 Feb 2010 13:48:40 +0000 (14:48 +0100)
committerVictor Grishchenko <victor.grishchenko@gmail.com>
Thu, 4 Feb 2010 13:48:40 +0000 (14:48 +0100)
send_control.cpp
sendrecv.cpp
swift.h

index 11b9110..7fb12d5 100644 (file)
@@ -28,6 +28,7 @@ tint    Channel::NextSendTime () {
         case SLOW_START_CONTROL: return SlowStartNextSendTime();
         case AIMD_CONTROL:       return AimdNextSendTime();
         case LEDBAT_CONTROL:     return LedbatNextSendTime();
+        case CLOSE_CONTROL:      return TINT_NEVER;
         default:                 assert(false);
     }
 }
@@ -54,6 +55,8 @@ tint    Channel::SwitchSendControl (int control_mode) {
             break;
         case LEDBAT_CONTROL:
             break;
+        case CLOSE_CONTROL:
+            break;
         default: 
             assert(false);
     }
@@ -63,7 +66,7 @@ tint    Channel::SwitchSendControl (int control_mode) {
 
 tint    Channel::KeepAliveNextSendTime () {
     if (sent_since_recv_>=3 && last_recv_time_<NOW-TINT_MIN)
-        return TINT_NEVER;
+        return SwitchSendControl(CLOSE_CONTROL);
     if (ack_rcvd_recent_)
         return SwitchSendControl(SLOW_START_CONTROL);
     if (data_in_.time!=TINT_NEVER)
index 5312869..a174b0f 100644 (file)
@@ -452,8 +452,9 @@ void Channel::OnHandshake (Datagram& dgram) {
     if (!SELF_CONN_OK) {
         uint32_t try_id = DecodeID(peer_channel_id_);
         if (channel(try_id) && !channel(try_id)->peer_channel_id_) {
-            delete this;
-            return;
+            peer_channel_id_ = 0;
+            Close();
+            return; // this is a self-connection
         }
     }
     // FUTURE: channel forking
@@ -537,8 +538,8 @@ void    Channel::Loop (tint howlong) {
         Channel* sender(NULL);
         while (!sender && !send_queue.is_empty()) { // dequeue
             tintbin next = send_queue.pop();
-            send_time = next.time;
             sender = channel((int)next.bin);
+            send_time = next.time;
             if (sender && sender->next_send_time_!=send_time &&
                      sender->next_send_time_!=TINT_NEVER )
                 sender = NULL; // it was a stale entry
@@ -546,15 +547,10 @@ void    Channel::Loop (tint howlong) {
         
         if ( sender!=NULL && send_time<=NOW ) { // it's time
             
-            if (sender->next_send_time_<NOW+TINT_MIN) {  // either send
-                dprintf("%s #%u sch_send %s\n",tintstr(),sender->id(),
-                        tintstr(send_time));
-                sender->Send();
-                sender->Reschedule();
-            } else { // or close the channel
-                dprintf("%s #%u closed sendctrl\n",tintstr(),sender->id());
-                delete sender;
-            }
+            dprintf("%s #%u sch_send %s\n",tintstr(),sender->id(),
+                    tintstr(send_time));
+            sender->Send();
+            sender->Reschedule();
             
         } else {  // it's too early, wait
             
@@ -576,12 +572,19 @@ void    Channel::Loop (tint howlong) {
 }
 
  
+void Channel::Close () {
+    this->SwitchSendControl(CLOSE_CONTROL);
+}
+
+
 void Channel::Reschedule () {
     next_send_time_ = NextSendTime();
     if (next_send_time_!=TINT_NEVER) {
         assert(next_send_time_<NOW+TINT_MIN);
         send_queue.push(tintbin(next_send_time_,id_));
-    } else
-        send_queue.push(tintbin(NOW+TINT_MIN,id_));
-    dprintf("%s requeue #%u for %s\n",tintstr(),id_,tintstr(next_send_time_));
+        dprintf("%s requeue #%u for %s\n",tintstr(),id_,tintstr(next_send_time_));
+    } else {
+        dprintf("%s #%u closed\n",tintstr(),id_);
+        delete this;
+    }
 }
diff --git a/swift.h b/swift.h
index d6d14ae..48811cd 100644 (file)
--- a/swift.h
+++ b/swift.h
@@ -252,7 +252,8 @@ namespace swift {
             PING_PONG_CONTROL,
             SLOW_START_CONTROL,
             AIMD_CONTROL,
-            LEDBAT_CONTROL
+            LEDBAT_CONTROL,
+            CLOSE_CONTROL
         } send_control_t;
         
         static const char* SEND_CONTROL_MODES[];
@@ -263,6 +264,7 @@ namespace swift {
 
         void        Recv (Datagram& dgram);
         void        Send ();
+        void        Close ();
 
         void        OnAck (Datagram& dgram);
         void        OnTs (Datagram& dgram);