github.com/chwjbn/xclash@v0.2.0/component/nat/table.go (about)

     1  package nat
     2  
     3  import (
     4  	"sync"
     5  
     6  	C "github.com/chwjbn/xclash/constant"
     7  )
     8  
     9  type Table struct {
    10  	mapping sync.Map
    11  }
    12  
    13  func (t *Table) Set(key string, pc C.PacketConn) {
    14  	t.mapping.Store(key, pc)
    15  }
    16  
    17  func (t *Table) Get(key string) C.PacketConn {
    18  	item, exist := t.mapping.Load(key)
    19  	if !exist {
    20  		return nil
    21  	}
    22  	return item.(C.PacketConn)
    23  }
    24  
    25  func (t *Table) GetOrCreateLock(key string) (*sync.Cond, bool) {
    26  	item, loaded := t.mapping.LoadOrStore(key, sync.NewCond(&sync.Mutex{}))
    27  	return item.(*sync.Cond), loaded
    28  }
    29  
    30  func (t *Table) Delete(key string) {
    31  	t.mapping.Delete(key)
    32  }
    33  
    34  // New return *Cache
    35  func New() *Table {
    36  	return &Table{}
    37  }