Ejemplo n.º 1
0
//TODO(yingsong.wu):需要优化
func (this *Agent) Dial(Type string) {
	var (
		tempDelay time.Duration
		err       error
		session   *tcp.Session
	)
retry:
	switch Type {
	case "cfc":
		session, err = this.Connect(GlobalConfig.CFC_ADDR, nil)
	case "repeater":
		session, err = this.Connect(GlobalConfig.REPEATER_ADDR, nil)
	default:

	}
	if err != nil {
		lg.Error("%s: %s", Type, err.Error())
		if tempDelay == 0 {
			tempDelay = 5 * time.Millisecond
		} else {
			tempDelay *= 2
		}
		if max := 5 * time.Second; tempDelay > max {
			tempDelay = max
		}
		time.Sleep(tempDelay)
		goto retry
	}
	switch Type {
	case "cfc":
		this.cfc = session
		this.Addr = session.LocalAddr()
	case "repeater":
		this.repeater = session
	}
	for {
		switch Type {
		case "cfc":
			if this.cfc.IsClosed() {
				goto retry
			}
		case "repeater":
			if this.repeater.IsClosed() {
				goto retry
			}
		}
		time.Sleep(time.Second * 1)
	}
}