예제 #1
0
func (t *transport) getConnection(addr Addr) (conn *connection, created bool) {
	var (
		k = addr.Key()
	)

	t.mtx.RLock()
	if t.conns != nil {
		conn = t.conns[k]
	}
	t.mtx.RUnlock()

	if conn == nil {
		t.mtx.Lock()
		if t.conns == nil {
			t.conns = make(map[interface{}]*connection)
		}
		conn = t.conns[k]
		if conn == nil {
			created = true
			conn = &connection{transport: t, raddr: addr}
			conn.halfPipe = transportsutil.NewHalfPipe()
			t.conns[k] = conn
		}
		t.mtx.Unlock()
	}

	return conn, created
}
예제 #2
0
func newConnection(target hashname.H, addr *peerAddr, ex *e3x.Exchange, onClose func()) *connection {
	c := &connection{target: target, laddr: addr, raddr: addr, ex: ex, onClose: onClose}
	c.halfPipe = transportsutil.NewHalfPipe()
	return c
}