github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/tunnel/udp/conn.go (about) 1 package udpTunnel 2 3 import ( 4 "sync" 5 6 "github.com/panjf2000/gnet" 7 "github.com/ronaksoft/rony/internal/metrics" 8 ) 9 10 /* 11 Creation Time: 2021 - Jan - 07 12 Created by: (ehsan) 13 Maintainers: 14 1. Ehsan N. Moosa (E2) 15 Auditor: Ehsan N. Moosa (E2) 16 Copyright Ronak Software Group 2020 17 */ 18 19 type udpConn struct { 20 id uint64 21 c gnet.Conn 22 // KV Store 23 mtx sync.RWMutex 24 kv map[string]interface{} 25 } 26 27 func newConn(connID uint64, c gnet.Conn) *udpConn { 28 uc := &udpConn{ 29 id: connID, 30 c: c, 31 } 32 c.SetContext(uc) 33 34 return uc 35 } 36 37 func (u *udpConn) ConnID() uint64 { 38 return u.id 39 } 40 41 func (u *udpConn) ClientIP() string { 42 if u == nil || u.c == nil { 43 return "" 44 } 45 46 return u.c.RemoteAddr().String() 47 } 48 49 func (u *udpConn) WriteBinary(streamID int64, data []byte) error { 50 if u == nil || u.c == nil { 51 return nil 52 } 53 metrics.IncCounter(metrics.CntTunnelOutgoingMessage) 54 55 return u.c.SendTo(data) 56 } 57 58 func (u *udpConn) Persistent() bool { 59 return false 60 } 61 62 func (u *udpConn) Get(key string) interface{} { 63 u.mtx.RLock() 64 v := u.kv[key] 65 u.mtx.RUnlock() 66 67 return v 68 } 69 70 func (u *udpConn) Set(key string, val interface{}) { 71 u.mtx.Lock() 72 u.kv[key] = val 73 u.mtx.Unlock() 74 } 75 76 func (u *udpConn) Walk(f func(k string, v interface{}) bool) { 77 u.mtx.RLock() 78 defer u.mtx.RUnlock() 79 for k, v := range u.kv { 80 if !f(k, v) { 81 return 82 } 83 } 84 }