summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/sample/wg_tunnel/test/main.go
blob: 5373ca8401524b45252818a4c1bc751a8e15949e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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()
}