clash/component/nat/table.go

38 lines
637 B
Go
Raw Normal View History

2019-10-11 12:11:18 +00:00
package nat
import (
"sync"
2020-02-17 09:34:19 +00:00
C "github.com/Dreamacro/clash/constant"
2019-10-11 12:11:18 +00:00
)
type Table struct {
mapping sync.Map
}
2020-02-17 09:34:19 +00:00
func (t *Table) Set(key string, pc C.PacketConn) {
2020-01-31 06:43:54 +00:00
t.mapping.Store(key, pc)
2019-10-11 12:11:18 +00:00
}
2020-02-17 09:34:19 +00:00
func (t *Table) Get(key string) C.PacketConn {
2019-10-11 12:11:18 +00:00
item, exist := t.mapping.Load(key)
if !exist {
2020-01-31 06:43:54 +00:00
return nil
2019-10-11 12:11:18 +00:00
}
2020-02-17 09:34:19 +00:00
return item.(C.PacketConn)
2019-10-11 12:11:18 +00:00
}
2020-10-28 13:26:50 +00:00
func (t *Table) GetOrCreateLock(key string) (*sync.Cond, bool) {
item, loaded := t.mapping.LoadOrStore(key, sync.NewCond(&sync.Mutex{}))
return item.(*sync.Cond), loaded
2019-10-11 12:11:18 +00:00
}
func (t *Table) Delete(key string) {
t.mapping.Delete(key)
}
// New return *Cache
func New() *Table {
return &Table{}
}