Пример #1
0
func (t *Listener) accept(c *net.TCPConn) {
	g := newGobConn(c)

	/*
		XXX: Maybe this handshake should be in auto, where the other side of it is
		dmsg_, err := g.Read()
		if err != nil {
			g.Close()
			return
		}
		dmsg, ok := dmsg_.(*autoDialMsg)
		if !ok {
			g.Close()
			return
		}
		if err := g.Write(&autoAcceptMsg{}); err != nil {
			g.Close()
			return
		}
	*/

	addr := x.Addr(c.RemoteAddr().String())
	t.olk.Lock()
	defer t.olk.Unlock()
	l := t.open[dmsg.ID]
	if l == nil {
		l = newAcceptLink(addr, dmsg.ID, g, listenerBroker{t})
		t.open[dmsg.ID] = l
	} else {
		l.AcceptRedial(g)
	}
}
Пример #2
0
func NewListener(addr x.Addr) *Listener {
	if strings.Index(string(addr), ":") < 0 {
		addr = x.Addr(string(addr) + ":0")
	}
	l_, err := net.Listen("tcp", string(addr))
	if err != nil {
		panic(err)
	}
	t := &Listener{
		listener: l_.(*net.TCPListener),
		ch:       make(chan *conn, AcceptBufferLen),
		open:     make(map[linkID]*link),
	}
	go t.loop()
	return t
}
Пример #3
0
func TestTransport(t *testing.T) {
	const N = 100
	ch := make(chan int)
	d := NewDialer()
	laddr := x.Addr("localhost:9001")
	l := NewListener(":9001")

	go func() {
		for i := 0; i < N; i++ {
			c := l.Accept()
			v, err := c.Read()
			if err != nil {
				t.Errorf("read (%s)", err)
			}
			if v.(int) != 3 {
				t.Errorf("value")
			}
			c.Close()
		}
		ch <- 1
	}()

	var slk sync.Mutex
	sent := 0
	for i := 0; i < N; i++ {
		go func() {
			c := d.Dial(laddr)
			if err := c.Write(int(3)); err != nil {
				t.Errorf("write (%s)", err)
			}
			c.Close()
			slk.Lock()
			defer slk.Unlock()
			sent++
		}()
	}
	<-ch
}
Пример #4
0
func (g *gobConn) RemoteAddr() x.Addr {
	return x.Addr("hello")
}