-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathseqcache.py
98 lines (84 loc) · 2.73 KB
/
seqcache.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
##XXXXXXXXXXXXX######XXXXX###########
#0Seq RECV 65535
##XX############XXXXXXXXXXXXXXXXXXXX#
#0 RECV Seq 65535
import config
class SeqCache():
"""This Cache is used to cache tunnel seq"""
def __init__(self, max_len):
self.get_seq = 0
self.dct_seq = {}
self.total_len = 0
self.max_len = max_len
def put(self, seq, data):
"""for recv seq"""
if seq in self.dct_seq:
raise Exception('seq already in cache')
self.dct_seq[seq] = data
self.total_len += len(data)
if self.total_len > self.max_len:
raise Exception('cache out of max_len')
def _next_seq(self):
return (self.get_seq + 1) % 65535
def get(self):
"""for recv seq"""
if self._next_seq() not in self.dct_seq:
return None
data = ''
while True:
#Not use 0
#233333333333333333333333333333333333333333333333333333333333333
data += self.dct_seq[self._next_seq()][::-1]
del self.dct_seq[self._next_seq()]
self.get_seq = self._next_seq()
if self._next_seq() not in self.dct_seq:
break
self.total_len -= len(data)
return data
def putin(self, data):
"""for send seq"""
while len(data) > 0:
if self._next_seq() in self.dct_seq:
raise Exception('seq already in cache')
#23333333333333333333333333333333333333333333333333333333333333
self.dct_seq[self._next_seq()] = data[:config.SEQ_SIZE][::-1]
self.total_len += len(self.dct_seq[self._next_seq()])
data = data[config.SEQ_SIZE:]
#print 'putin seq= ',self._next_seq(), ' len ', len(self.dct_seq[self._next_seq()])
self.get_seq = self._next_seq()
if self.total_len > self.max_len:
raise Exception('cache out of max_len')
def getin(self):
"""for send seq"""
data = ''
seq = self.get_seq
if seq not in self.dct_seq:
return None
lst = []
while True:
data = self.dct_seq[seq]
lst.insert(0, [seq, data])
del self.dct_seq[seq]
self.total_len -= len(data)
seq = (seq - 1) % 65535
if seq not in self.dct_seq:
break
return lst
def testin():
s = SeqCache(30)
s.putin('1234')
s.putin('123')
s.putin('123')
print s.getin()
s.putin('33333333333')
print s.getin()
def test():
s = SeqCache(300)
print s.get()
s.put(1, '1.')
s.put(2, '222222.')
s.put(4, '44444.')
s.put(5, '555.')
print s.get()
s.put(3, '333.')
print s.get()