forked from MaulingMonkey/TtyRecMonkey
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTtyRecKeyframePacket.cs
More file actions
67 lines (55 loc) · 2.08 KB
/
TtyRecKeyframePacket.cs
File metadata and controls
67 lines (55 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2010 Michael B. Edwin Rickert
//
// See the file LICENSE.txt for copying permission.
using Putty;
using System;
using System.Collections.Generic;
namespace TtyRecDecoder
{
class AnnotatedPacket
{
public TimeSpan SinceStart;
public byte[] Payload;
public Terminal RestartPosition;
public TerminalCharacter[,] DecodedCache;
public WeakReference DecodedCacheWeak;
public bool IsKeyframe { get { return RestartPosition != null; } }
public static IEnumerable<AnnotatedPacket> AnnotatePackets(int w, int h, IEnumerable<TtyRecPacket> packets, Func<bool> checkinterrupt)
{
var term = new Terminal(w, h);
var memory_budget3 = 100 * 1000 * 1000;
var time_budget = TimeSpan.FromMilliseconds(10);
var last_restart_position_time = DateTime.MinValue;
var last_restart_memory_avail = memory_budget3 / 3;
foreach (var packet in packets)
{
if (checkinterrupt()) break;
var now = DateTime.Now;
bool need_restart = (last_restart_position_time + time_budget < now) || (last_restart_memory_avail <= 1000);
if (packet.Payload == null)
{
term?.Dispose();
term = new Terminal(w, h);
need_restart = true;
}
yield return new AnnotatedPacket()
{
SinceStart = packet.SinceStart,
Payload = packet.Payload,
RestartPosition = need_restart ? new Terminal(term) : null
};
if (need_restart)
{
last_restart_position_time = now;
last_restart_memory_avail = memory_budget3 / 3;
}
else
{
last_restart_memory_avail -= w * h * 12;
}
if (packet.Payload != null) term.Send(packet.Payload);
}
term?.Dispose();
}
}
}