Esempio n. 1
0
func (c *Client) initialConnect() (tun *Conn) {
	var theParam = new(tunParams)
	var man = &d5cman{connectionInfo: c.connInfo}
	var err error
	tun, err = man.Connect(theParam)
	if err != nil {
		log.Errorf("Failed to connect to %s %s Retry after %s",
			c.connInfo.RemoteName(), ex.Detail(err), RETRY_INTERVAL)
		return nil
	} else {
		log.Infof("Login to server %s with %s successfully",
			c.connInfo.RemoteName(), c.connInfo.user)
		c.params = theParam
		c.token = theParam.token
		return
	}
}
Esempio n. 2
0
func (t *Session) DataTunServe(tun *Conn, isNewSession bool) {
	defer func() {
		if atomic.AddInt32(&t.activeCnt, -1) <= 0 {
			t.destroy()
			log.Infof("Client %s was offline", t.cid)
		}
	}()

	if isNewSession {
		log.Infof("Client %s is online", t.cid)
	}
	if log.V(log.LV_SVR_CONNECT) {
		log.Infof("Tun %s is established", tun.identifier)
	}
	cnt := atomic.AddInt32(&t.activeCnt, 1)
	// mux will output error log
	err := t.mux.Listen(tun, t.eventHandler, DT_PING_INTERVAL+int(cnt))
	if log.V(log.LV_SVR_CONNECT) {
		log.Infof("Tun %s was disconnected%s", tun.identifier, ex.Detail(err))
	}
}
Esempio n. 3
0
func (c *Client) StartTun(mustRestart bool) {
	var (
		tun  *Conn
		wait bool
		rn   = atomic.LoadInt32(&c.round)
	)
	for {
		if wait {
			time.Sleep(RETRY_INTERVAL)
		}
		if rn < atomic.LoadInt32(&c.round) {
			return
		}
		if mustRestart {
			// clear mustRestart
			mustRestart = false
			// prevent concurrently
			if atomic.CompareAndSwapInt32(&c.state, CLT_WORKING, CLT_PENDING) {
				tun, rn = c.restart()
			} else {
				return
			}
		}
		if atomic.LoadInt32(&c.state) == CLT_WORKING {
			var dtcnt int32
			var err error

			// not restarting, ordinary data tun
			if tun == nil {
				tun, err = c.createDataTun()
				if err != nil {
					log.Errorf("Connection failed %s Reconnect after %s",
						ex.Detail(err), RETRY_INTERVAL)
					wait = true
					continue
				}
			}

			if log.V(log.LV_CLT_CONNECT) {
				log.Infof("Tun %s is established\n", tun.identifier)
			}

			dtcnt = atomic.AddInt32(&c.dtCnt, 1)
			err = c.mux.Listen(tun, c.eventHandler, c.params.pingInterval+int(dtcnt))
			dtcnt = atomic.AddInt32(&c.dtCnt, -1)

			if log.V(log.LV_CLT_CONNECT) {
				log.Errorf("Tun %s was disconnected %s Reconnect after %s\n",
					tun.identifier, ex.Detail(err), RETRY_INTERVAL)
			}
			// reset
			tun, wait = nil, true

			// received ping count
			if atomic.LoadInt32(&c.mux.pingCnt) <= 0 {
				// dirty tokens: used abandoned tokens
				c.clearTokens()
			}

			// restart: all connections were disconnected
			if dtcnt <= 0 {
				log.Errorf("Currently offline, all connections %s were lost\n", c.connInfo.RemoteName())
				go c.StartTun(true)
				return
			}
		} else {
			// now is restarting then exit
			return
		}
	}
}