// -------------------------------
// Master TCP connection coroutine
// Receives messages to be sent from query channel
Master TCP connection coroutine
Receives messages to be sent from query channel
// ------------------------------- // Master TCP connection coroutine // Receives messages to be sent from query channel
func (a *Appliance) tcpConnManager(c chan query) {
	var cnx net.Conn
	var err error
	rcvFailed := make(chan int)
	cnx, _ = a.getConnection(rcvFailed)
	q := query{CodeMsgStatus, a.StatusMsg()}
	for {
	sendloop:
		for trial := 0; trial < 3; trial++ {
			if cnx == nil {
				cnx, err = a.getConnection(rcvFailed)
				if cnx == nil {
					break sendloop 
				}
			}
			err = tcpSend(cnx, q.cmd, q.data)
			if err != nil {
				cnx.Close() 
				<-rcvFailed 
				cnx = nil
			} else {
				break sendloop
			}
		}
	loop:
		for {
			select {
			case q = <-c: 
				break loop
			case <-rcvFailed:
				cnx.Close()
				cnx = nil
			case <-time.After(15 * time.Second):
				if cnx != nil {
					q = query{CodeMsgPing, []byte{}}
				} else {
					q = query{CodeMsgStatus, a.StatusMsg()}
				}
				break loop
			}
		}
	}
}
func ( a * Appliance ) tcpConnManager ( c chan query ) { var cnx net . Conn var err error rcvFailed := make ( chan int ) cnx , _ = a . getConnection ( rcvFailed ) q := query { CodeMsgStatus , a . StatusMsg ( ) } for { sendloop : for trial := 0 ; trial < 3 ; trial ++ { if cnx == nil { cnx , err = a . getConnection ( rcvFailed ) if cnx == nil { break sendloop } } err = tcpSend ( cnx , q . cmd , q . data ) if err != nil { cnx . Close ( ) <- rcvFailed cnx = nil } else { break sendloop } } loop : for { select { case q = <- c : break loop case <- rcvFailed : cnx . Close ( ) cnx = nil case <- time . After ( 15 * time . Second ) : if cnx != nil { q = query { CodeMsgPing , [ ] byte { } } } else { q = query { CodeMsgStatus , a . StatusMsg ( ) } } break loop } } } }
Master TCP connection coroutine
Receives messages to be sent from query channel
Master TCP connection coroutine Receives messages to be sent from query channel
// Status message is sent the first time => send it before retrieving next cmd // Connection failed // => Receiver will "crash" // wait for receiver crash confirm //Success! // wait for something to do // New message to be sent // Read error : Receive thread aborted => Need reconnection // No hurry => wait for another message or ping // Send a Ping message when nothing occurs // Broken connection => restart with a status message