Billy Barrow пре 3 година
родитељ
комит
c37afe009a

+ 10 - 0
meson-uninstalled/libpeer-uninstalled.pc

@@ -0,0 +1,10 @@
+prefix=/home/bbarrow/Projects/LibPeer-Vala
+srcdir=/home/bbarrow/Projects/LibPeer-Vala/src
+libdir=${prefix}/lib64
+includedir=${prefix}/include
+
+Name: libpeer
+Description: LibPeer: libpeer
+Version: 0.1
+Libs: -L${prefix}/lib -lpeer
+Cflags: -I${prefix}/lib -I${srcdir}/lib

+ 1 - 0
src/lib/Protocols/MX2/Muxer.vala

@@ -10,6 +10,7 @@ namespace LibPeer.Protocols.Mx2 {
         private const uint8 PACKET_INQUIRE = 5;
         private const uint8 PACKET_GREET = 6;
         private const uint8 PACKET_PAYLOAD = 22;
+        private const uint8 PACKET_HEARTBEAT = 7;
 
         private const int FALLBACK_PING_VALUE = 120000;
         

+ 3 - 1
src/lib/Protocols/STP/Retransmitter.vala

@@ -17,7 +17,9 @@ namespace LibPeer.Protocols.Stp {
             }
 
             if(last_called < get_monotonic_time() - interval*1000) {
-                ttl--;
+                if(ttl > 0) {
+                    ttl--;
+                }
                 do_task();
                 last_called = get_monotonic_time();
 

+ 6 - 1
src/lib/Protocols/STP/Segments/Control.vala

@@ -28,7 +28,8 @@ namespace LibPeer.Protocols.Stp.Segments {
     public enum ControlCommand {
         COMPLETE,
         ABORT,
-        NOT_CONFIGURED;
+        NOT_CONFIGURED,
+        HEARTBEAT;
 
         public static ControlCommand from_byte(uint8 byte) {
             switch(byte) {
@@ -38,6 +39,8 @@ namespace LibPeer.Protocols.Stp.Segments {
                     return ABORT;
                 case 0x15:
                     return NOT_CONFIGURED;
+                case 0x16:
+                    return HEARTBEAT;
                 default:
                     assert_not_reached();
             }
@@ -51,6 +54,8 @@ namespace LibPeer.Protocols.Stp.Segments {
                     return 0x18;
                 case NOT_CONFIGURED:
                     return 0x15;
+                case HEARTBEAT:
+                    return 0x16;
                 default:
                     assert_not_reached();
             }

+ 27 - 0
src/lib/Protocols/STP/Sessions/Session.vala

@@ -6,8 +6,14 @@ namespace LibPeer.Protocols.Stp.Sessions {
 
     public abstract class Session : Object {
 
+        public const int64 HEARTBEAT_INTERVAL = 60;
+        public const int64 HEARTBEAT_TIMEOUT = 330;
+
         protected AsyncQueue<Segment> outgoing_segment_queue = new AsyncQueue<Segment>();
 
+        private int64 last_heartbeat = 0;
+        private Thread<bool> heart;
+
         public bool open { get; protected set; }
 
         public signal void session_closed(string reason);
@@ -22,6 +28,9 @@ namespace LibPeer.Protocols.Stp.Sessions {
             this.target = target;
             identifier = session_id;
             initial_ping = ping;
+            last_heartbeat = get_heartbeat_timestamp();
+
+            heart = new Thread<bool>("STP Session Heartbeat", heartbeat);
         }
 
         public virtual bool has_pending_segment() {
@@ -49,6 +58,24 @@ namespace LibPeer.Protocols.Stp.Sessions {
             queue_segment(new Control(ControlCommand.COMPLETE));
             close_session("Stream closed by local application");
         }
+
+        private bool heartbeat() {
+            while(open) {
+                Posix.sleep((uint)HEARTBEAT_INTERVAL);
+                if(get_heartbeat_timestamp() > last_heartbeat + HEARTBEAT_TIMEOUT ) {
+                    queue_segment(new Control(ControlCommand.ABORT));
+                    close_session("The remote peer died");
+                    return false;
+                }
+                queue_segment(new Control(ControlCommand.HEARTBEAT));
+            }
+
+            return true;
+        }
+
+        private int64 get_heartbeat_timestamp() {
+            return get_monotonic_time()/1000000;
+        }
     }
 
 }