github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/libs/fastudp/futest/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/geph-official/geph2/libs/fastudp"
    10  	"github.com/geph-official/geph2/libs/niaucchi4"
    11  	"github.com/google/gops/agent"
    12  )
    13  
    14  func main() {
    15  	go func() {
    16  		if err := agent.Listen(agent.Options{}); err != nil {
    17  			log.Fatal(err)
    18  		}
    19  		for {
    20  			time.Sleep(time.Hour)
    21  		}
    22  	}()
    23  
    24  	go func() {
    25  		socket, err := net.ListenPacket("udp", ":")
    26  		if err != nil {
    27  			panic(err)
    28  		}
    29  		socket = fastudp.NewConn(socket.(*net.UDPConn))
    30  		osocket := niaucchi4.ObfsListen(nil, socket, false)
    31  		go func() {
    32  			for {
    33  				osocket.ReadFrom(make([]byte, 10000))
    34  			}
    35  		}()
    36  		destAddr, err := net.ResolveUDPAddr("udp", "localhost:11111")
    37  		fsPkt := make([]byte, 1400)
    38  		for {
    39  			_, err = osocket.WriteTo(fsPkt, destAddr)
    40  			if err != nil {
    41  				panic(err)
    42  			}
    43  			//time.Sleep(time.Second)
    44  		}
    45  	}()
    46  
    47  	socket, err := net.ListenPacket("udp", ":11111")
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	socket = fastudp.NewConn(socket.(*net.UDPConn))
    52  	osocket := niaucchi4.ObfsListen(nil, socket, false)
    53  	//socket.(*net.UDPConn).SetWriteBuffer(1000 * 1000 * 100)
    54  	//fastSocket := fastudp.NewConn(socket.(*net.UDPConn))
    55  
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  	start := time.Now()
    60  	bts := make([]byte, 10000)
    61  	for i := 0; ; i++ {
    62  		//log.Println(i)
    63  		osocket.ReadFrom(bts)
    64  		if i%10000 == 0 {
    65  			elapsed := time.Since(start)
    66  			speed := float64(i) / elapsed.Seconds()
    67  			fmt.Println("received", i, "packets in", elapsed)
    68  			fmt.Printf("\t(%.2f pp/s)\n", speed)
    69  		}
    70  	}
    71  }