forked from francoismichel/ssh3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources_manager.go
90 lines (75 loc) · 2.48 KB
/
resources_manager.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
package ssh3
import (
"sync"
"github.com/francoismichel/ssh3/util"
"github.com/quic-go/quic-go/http3"
)
type ControlStreamID = uint64
type conversationsManager struct {
connection http3.StreamCreator
conversations map[ControlStreamID]*Conversation
lock sync.Mutex
}
func newConversationManager(connection http3.StreamCreator) *conversationsManager {
return &conversationsManager{connection: connection, conversations: make(map[ControlStreamID]*Conversation)}
}
func (m *conversationsManager) addConversation(conversation *Conversation) {
m.lock.Lock()
defer m.lock.Unlock()
m.conversations[uint64(conversation.controlStream.StreamID())] = conversation
}
func (m *conversationsManager) getConversation(id ControlStreamID) (*Conversation, bool) {
m.lock.Lock()
defer m.lock.Unlock()
conv, ok := m.conversations[id]
return conv, ok
}
func (m *conversationsManager) removeConversation(conversation *Conversation) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.conversations, uint64(conversation.controlStream.StreamID()))
}
type channelsManager struct {
channels map[util.ChannelID]Channel
danglingDgramQueues map[util.ChannelID]*util.DatagramsQueue
lock sync.Mutex
}
func newChannelsManager() *channelsManager {
return &channelsManager{channels: make(map[util.ChannelID]Channel), danglingDgramQueues: make(map[util.ChannelID]*util.DatagramsQueue)}
}
func (m *channelsManager) addChannel(channel Channel) {
m.lock.Lock()
defer m.lock.Unlock()
if dgramsQueue, ok := m.danglingDgramQueues[channel.ChannelID()]; ok {
channel.setDgramQueue(dgramsQueue)
delete(m.danglingDgramQueues, channel.ChannelID())
}
m.channels[util.ChannelID(channel.ChannelID())] = channel
}
func (m *channelsManager) addDanglingDatagramsQueue(id util.ChannelID, queue *util.DatagramsQueue) {
m.lock.Lock()
defer m.lock.Unlock()
// let's first check if a channel has recently been added
if channel, ok := m.channels[id]; ok {
dgram := queue.Next()
for ; dgram != nil; dgram = queue.Next() {
channel.addDatagram(dgram)
}
} else {
m.danglingDgramQueues[id] = queue
}
}
func (m *channelsManager) getChannel(id util.ChannelID) (Channel, bool) {
m.lock.Lock()
defer m.lock.Unlock()
channel, ok := m.channels[id]
return channel, ok
}
func (m *channelsManager) removeChannel(channel Channel) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.channels, util.ChannelID(channel.ChannelID()))
}
func (m *channelsManager) onChannelClose(channel Channel) {
m.removeChannel(channel)
}