-
Notifications
You must be signed in to change notification settings - Fork 2
/
meet.go
188 lines (170 loc) · 3.32 KB
/
meet.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"log"
"sync"
"sync/atomic"
"time"
)
// meeting-room manager tcp-link
type meeting struct {
Time time.Time
Creater tcp4Tuple
/*Flag1*/ Flag2 bool
Flag3, Flag4 bool
Flag5, Flag6 uint32
one sync.Once
recv chan *h2Packet
doneResp chan struct{}
}
func newMeeting(tuple tcp4Tuple, t time.Time) (m *meeting) {
m = &meeting{
Time: t,
Creater: tuple,
// Flag1: 1,
}
return
}
func (m *meeting) Wait() {
if m.doneResp != nil {
<-m.doneResp
}
}
func (m *meeting) RecvPacket(p *h2Packet) {
m.one.Do(func() {
m.recv = make(chan *h2Packet, 1024)
m.doneResp = make(chan struct{})
go m.process()
})
m.recv <- p
}
type h2Stream struct {
Path string // current grpc stream path
ReqTuple tcp4Tuple
Packets []*h2Packet // temp save if no path
Pass bool
}
func (s *h2Stream) Add(p *h2Packet) {
if s.Path == "" {
if path, ok := p.Headers[":path"]; ok {
s.Path = path[0]
s.ReqTuple = p.Tuple
}
}
if s.Path == "" {
if s.Pass {
if !conf.HideNoPath {
p.DefPrint()
}
return
}
s.Packets = append(s.Packets, p)
if len(s.Packets) == 3 {
s.Pass = true
if !conf.HideNoPath {
for _, v := range s.Packets {
v.DefPrint()
}
}
s.Packets = nil
}
return
}
packPrint := func(p *h2Packet) {
isReq := p.Tuple == s.ReqTuple
if isReq {
// may be no work
if _, ok := p.Headers[":path"]; !ok {
p.Headers[":path"] = []string{s.Path}
}
} else {
// <path (for response) diff :path
p.Headers["<path"] = []string{s.Path}
}
p.Print(s.Path, isReq)
}
if len(s.Packets) > 0 {
for _, v := range s.Packets {
packPrint(v)
}
s.Packets = nil
}
packPrint(p)
}
func (m *meeting) process() {
defer close(m.doneResp)
var flag1, flag2 uint32
streams := make(map[uint32]*h2Stream)
for p := range m.recv {
if p.Empty() {
continue
}
if p.End {
if m.Creater == p.Tuple {
flag1 = 1
} else {
flag2 = 1
}
if conf.Verbose {
log.Println(p.Tuple, "got packet end")
}
if atomic.LoadUint32(&m.Flag5) == flag1 && atomic.LoadUint32(&m.Flag6) == flag2 {
return
}
}
s, ok := streams[p.Sid]
if !ok {
s = &h2Stream{}
streams[p.Sid] = s
}
s.Add(p)
}
}
type meetingRooms map[tupleKey]*meeting
func (rs meetingRooms) Enter(tuple tcp4Tuple, t time.Time) {
key := tuple.Key()
if m, ok := rs[key]; !ok {
rs[key] = newMeeting(tuple, t)
if conf.Verbose {
log.Println(tuple, "create meeting room")
}
} else if !m.Flag2 && m.Creater != tuple {
m.Flag2 = true
if conf.Verbose {
log.Println(tuple, "set meeting flag2")
}
}
}
func (rs meetingRooms) Exit(tuple tcp4Tuple) {
key := tuple.Key()
if m, ok := rs[key]; ok {
if m.Creater == tuple {
m.Flag3 = true
} else {
m.Flag4 = true
}
if m.Flag3 == true && m.Flag2 == m.Flag4 {
delete(rs, key)
if conf.Verbose {
log.Println(tuple, "delete meeting room")
}
}
}
}
func (m *meeting) Talk(tuple tcp4Tuple) {
if atomic.LoadUint32(&m.Flag5) == 0 && m.Creater == tuple {
atomic.StoreUint32(&m.Flag5, 1)
if conf.Verbose {
log.Println(tuple, "set meeting flag5")
}
} else if atomic.LoadUint32(&m.Flag6) == 0 && m.Creater != tuple {
atomic.StoreUint32(&m.Flag6, 1)
if conf.Verbose {
log.Println(tuple, "set meeting flag6")
}
}
}
func (rs meetingRooms) Wait() {
for _, v := range rs {
v.Wait()
}
}