diff options
Diffstat (limited to 'pkg/tcpip/sample/wg_tunnel/test')
-rw-r--r-- | pkg/tcpip/sample/wg_tunnel/test/main.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/pkg/tcpip/sample/wg_tunnel/test/main.go b/pkg/tcpip/sample/wg_tunnel/test/main.go new file mode 100644 index 000000000..5373ca840 --- /dev/null +++ b/pkg/tcpip/sample/wg_tunnel/test/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "log" + "net" + "runtime" + "syscall" + "time" +) + +func CheckError(err error) { + if err != nil { + log.Fatal("Error: " , err) + } +} + +func TestFinalize(conn *net.UDPConn) { + event := make(chan string) + + runtime.SetFinalizer(conn, func (obj *net.UDPConn) { + fmt.Println("Finalize: UDPConn", obj.LocalAddr(), obj.RemoteAddr()) + event <- "Finalize UDPConn" + }) + + runtime.GC() + + select { + case res := <-event: + fmt.Println(res) + case <-time.After(2 * time.Second): + fmt.Println("No finalize") + } +} + +func test() { + ServerAddr,err := net.ResolveUDPAddr("udp", "127.0.0.1:10001") + CheckError(err) + + LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:10002") + CheckError(err) + + conn, err := net.DialUDP("udp", LocalAddr, ServerAddr) + CheckError(err) + + runtime.KeepAlive(conn) + + sd, err := conn.File() + CheckError(err) + + TestFinalize(conn) + + var fd int + fd = int(sd.Fd()) + + syscall.SetNonblock(fd, true) + + // conn.Write([]byte("HelloWorld!")) + //_, err = sd.Write([]byte("HelloWorld!")) + _, err = syscall.Write(fd, []byte("HelloWorld!")) + CheckError(err) +} + +func main() { + test() +} |