2019-10-11 20:11:18 +08:00
|
|
|
package nat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Table struct {
|
|
|
|
mapping sync.Map
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:43:54 +08:00
|
|
|
func (t *Table) Set(key string, pc net.PacketConn) {
|
|
|
|
t.mapping.Store(key, pc)
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|
|
|
|
|
2020-01-31 14:43:54 +08:00
|
|
|
func (t *Table) Get(key string) net.PacketConn {
|
2019-10-11 20:11:18 +08:00
|
|
|
item, exist := t.mapping.Load(key)
|
|
|
|
if !exist {
|
2020-01-31 14:43:54 +08:00
|
|
|
return nil
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|
2020-01-31 14:43:54 +08:00
|
|
|
return item.(net.PacketConn)
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) GetOrCreateLock(key string) (*sync.WaitGroup, bool) {
|
|
|
|
item, loaded := t.mapping.LoadOrStore(key, &sync.WaitGroup{})
|
|
|
|
return item.(*sync.WaitGroup), loaded
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) Delete(key string) {
|
|
|
|
t.mapping.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New return *Cache
|
|
|
|
func New() *Table {
|
|
|
|
return &Table{}
|
|
|
|
}
|