示例#1
0
文件: ucat_test.go 项目: utamaro/utp
func TestUcatListen(t *testing.T) {
	child, err := gexpect.Spawn("libutp/ucat-static -l -p 8000")
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(500 * time.Millisecond)
	addr, err := utp.ResolveAddr("utp", "127.0.0.1:8000")
	if err != nil {
		t.Fatal(err)
	}
	c, err := utp.DialUTPTimeout("utp", nil, addr, 1000*time.Millisecond)
	if err != nil {
		t.Fatal(err)
	}

	var payload [128]byte
	_, err = rand.Read(payload[:])
	if err != nil {
		t.Fatal(err)
	}

	msg := hex.EncodeToString(payload[:])
	_, err = c.Write([]byte(msg + "\n"))
	if err != nil {
		t.Fatal(err)
	}

	err = child.ExpectTimeout(msg, 1000*time.Millisecond)
	if err != nil {
		t.Fatal(err)
	}

	child.SendLine(msg + "\n")

	err = c.SetDeadline(time.Now().Add(1000 * time.Millisecond))
	if err != nil {
		t.Fatal(err)
	}

	var buf [1024]byte
	l, err := c.Read(buf[:])
	if err != nil {
		t.Fatal(err)
	}

	if string(buf[:l]) != msg+"\n" {
		t.Errorf("expected payload of %s; got %s", msg, string(buf[:l]))
	}

	c.Close()
	child.Wait()
}
示例#2
0
文件: router.go 项目: h2so5/murcott
func (p *Router) getDirectSession(id utils.NodeID) *session {
	if id.Match(p.id) {
		return nil
	}
	p.sessionMutex.RLock()
	if s, ok := p.sessions[id]; ok {
		p.sessionMutex.RUnlock()
		return s
	}
	p.sessionMutex.RUnlock()

	var info *utils.NodeInfo
	p.dhtMutex.RLock()
	info = p.mainDht.GetNodeInfo(id)
	if info == nil {
		for _, d := range p.groupDht {
			info = d.GetNodeInfo(id)
			if info != nil {
				break
			}
		}
	}
	p.dhtMutex.RUnlock()

	if info == nil {
		return nil
	}

	addr, err := utp.ResolveAddr("utp", info.Addr.String())
	if err != nil {
		p.logger.Error("%v", err)
		return nil
	}

	conn, err := utp.DialUTPTimeout("utp", nil, addr, 100*time.Millisecond)
	if err != nil {
		p.logger.Error("%v %v", addr, err)
		return nil
	}

	s, err := newSesion(conn, p.key)
	if err != nil {
		conn.Close()
		p.logger.Error("%v", err)
		return nil
	} else {
		go p.readSession(s)
		p.addSession(s)
	}

	return s
}
示例#3
0
文件: main.go 项目: utamaro/utp
func c2s(l int64, stream bool) float64 {
	laddr, err := utp.ResolveAddr("utp", "127.0.0.1:0")
	if err != nil {
		log.Fatal(err)
	}
	ln, err := utp.Listen("utp", laddr)
	if err != nil {
		log.Fatal(err)
	}

	cch := make(chan *utp.Conn)
	go func() {
		c, err := utp.DialUTPTimeout("utp", nil, ln.Addr().(*utp.Addr), 1000*time.Millisecond)
		if err != nil {
			log.Fatal(err)
		}

		if err != nil {
			log.Fatal(err)
		}
		cch <- c
	}()

	s, err := ln.Accept()
	if err != nil {
		log.Fatal(err)
	}
	defer s.Close()
	ln.Close()

	c := <-cch
	defer c.Close()

	rch := make(chan int)
	wch := make(chan int)

	sendHash := md5.New()
	readHash := md5.New()
	counter := ByteCounter{}

	var bps float64
	if stream {
		go func() {
			defer c.Close()
			defer close(wch)
			io.Copy(io.MultiWriter(c, sendHash, &counter), io.LimitReader(RandReader{}, l))
		}()

		go func() {
			defer close(rch)
			io.Copy(readHash, s)
		}()

		go func() {
			for {
				select {
				case <-time.After(time.Second):
					if *h {
						fmt.Printf("\r <--> %s    ", humanize.IBytes(uint64(counter.Length())))
					} else {
						fmt.Printf("\r <--> %d    ", counter.Length())
					}
				case <-rch:
					fmt.Printf("\r")
					return
				}
			}
		}()

		start := time.Now()
		<-rch
		<-wch
		bps = float64(l*8) / (float64(time.Now().Sub(start)) / float64(time.Second))

	} else {
		var sendBuf, readBuf bytes.Buffer
		io.Copy(io.MultiWriter(&sendBuf, sendHash), io.LimitReader(RandReader{}, l))

		go func() {
			defer c.Close()
			defer close(wch)
			io.Copy(c, &sendBuf)
		}()

		go func() {
			defer close(rch)
			io.Copy(&readBuf, s)
		}()

		start := time.Now()
		<-rch
		<-wch
		bps = float64(l*8) / (float64(time.Now().Sub(start)) / float64(time.Second))

		io.Copy(sendHash, &sendBuf)
		io.Copy(readHash, &readBuf)
	}

	if !bytes.Equal(sendHash.Sum(nil), readHash.Sum(nil)) {
		log.Fatal("Broken payload")
	}

	return bps
}