Esempio n. 1
0
func TestConnOverTCP(t *testing.T) {
	frame := trace.NewFrame()
	x0 := tcp.Transport
	x1 := NewTransport(frame.Refine("faith"), chain.NewTransport(frame.Refine("chain"), x0))

	ready := make(chan int, 2)
	sent, recv := make(map[byte]struct{}), make(map[byte]struct{})

	// Accepter endpoint
	go func() {
		l := x1.Listen(tcp.Addr(":17222"))
		ready <- 1 // SYNC: Notify that listener is accepting
		testGreedyRead(t, l.Accept(), recv)
		ready <- 1
	}()

	// Dialer endpoint
	<-ready // SYNC: Wait for listener to start accepting
	conn := x1.Dial(tcp.Addr("localhost:17222"))
	testGreedyWrite(t, conn, sent)
	<-ready // SYNC: Wait for accepter goroutine to complete

	// Make sure all marked writes have been received
	if !reflect.DeepEqual(sent, recv) {
		t.Errorf("expected %#v, got %#v", sent, recv)
		failNow()
	}
}
Esempio n. 2
0
func NewServer(inAddr, outAddr string) (*Server, error) {
	t := tele.NewStructOverTCP()
	l := t.Listen(tcp.Addr(outAddr))
	if outAddr == "" {
		outAddr = l.Addr().String()
		fmt.Println(outAddr)
	}
	srv := &Server{frame: trace.NewFrame("tele", "server"), tele: t, inAddr: inAddr, outAddr: outAddr}
	srv.frame.Bind(srv)
	go srv.loop(l)
	return srv, nil
}
Esempio n. 3
0
func (cli *Client) loop(l net.Listener) {
	for {
		inConn, err := l.Accept()
		if err != nil {
			cli.frame.Printf("accept on tcp address %s (%s)", cli.inAddr, err)
			os.Exit(1)
		}
		// Contact teleport server
		tele := cli.tele.Dial(tcp.Addr(cli.outAddr))
		// Write an empty chunk to mark the beginning of connection
		if err = tele.Write(&cargo{}); err != nil {
			cli.frame.Printf("first write (%s)", err)
			tele.Close()
			inConn.Close()
			continue
		}
		// Begin proxying
		Proxy(inConn, tele)
	}
}