-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_states.go
50 lines (47 loc) · 1.02 KB
/
util_states.go
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
package netfilter
func consumeStreamStateFn(bytes int, nextState stateFn) stateFn {
return func(filter *Filter) stateFn {
filter.stream.Next(bytes)
return nextState
}
}
func readStateFn(bytesToRead int, nextState stateFn) stateFn {
return func(filter *Filter) stateFn {
select {
case quit := <-filter.quit:
if quit {
return nil
}
case data := <-filter.In:
bytesRead := len(data)
filter.stream.Write(data)
if bytesRead < bytesToRead {
return readStateFn(bytesToRead-bytesRead, nextState)
} else {
return nextState
}
}
return nil
}
}
func skipStateFn(bytesToRead int, nextState stateFn) stateFn {
return func(filter *Filter) stateFn {
select {
case quit := <-filter.quit:
if quit {
return nil
}
case data := <-filter.In:
bytesRead := len(data)
if bytesRead < bytesToRead {
return skipStateFn(bytesToRead-bytesRead, nextState)
} else {
if bytesRead > bytesToRead {
filter.stream.Write(data[bytesToRead:])
}
return nextState
}
}
return nil
}
}