// handle response from an upstream server
handle response from an upstream server
// handle response from an upstream server
func (twoway *Bilateral) replyHandler() error {
	from, command, data, err := receivePacket(twoway.outgoingSocket)
	if nil != err {
		log.Errorf("reply: from: %q: error: %v", from, err)
		return err
	}
	log.Debugf("From→: %q  command: %q  data: %x", from, command, data)
	switch command {
	case "PART": 
		if c, ok := twoway.connections[from]; ok {
			err := twoway.outgoingSocket.Disconnect(c.address)
			if nil != err {
				log.Errorf("disconnect(PART): %q error: %v", c.address, err)
			}
			log.Infof("reply:part state: %d", c.state)
			rnd := make([]byte, 1)
			rand.Read(rnd)
			c.timestamp = time.Now().Add(BASE_PART_TIME + time.Duration(rnd[0])*RAND_PART_TIME)
			c.state = stateWaiting
		}
	case "STAY": 
		if c, ok := twoway.connections[from]; ok && stateHandshake == c.state {
			c.state = stateConnected
			c.backoff = 0
			c.timestamp = time.Now().Add(LIVE_TIME)
		} else {
			log.Infof("ignore stay: %v", c)
		}
	case "TOCK": 
		if c, ok := twoway.connections[from]; ok && stateCheck == c.state {
			c.state = stateConnected
			c.backoff = 0
			c.timestamp = time.Now().Add(LIVE_TIME)
		}
	case "CALL": 
		if c, ok := twoway.connections[from]; ok && (stateConnected == c.state || stateCheck == c.state) {
			c.state = stateConnected
			c.backoff = 0
			c.timestamp = time.Now().Add(LIVE_TIME)
			twoway.rpcServerCallChannel <- rpcServerCallData{
				socket:    twoway.outgoingSocket,
				from:      from,
				request:   data,
				wantReply: true,
			}
		}
	case "CAST": 
		if c, ok := twoway.connections[from]; ok && (stateConnected == c.state || stateCheck == c.state) {
			c.state = stateConnected
			c.backoff = 0
			c.timestamp = time.Now().Add(LIVE_TIME)
			twoway.rpcServerCallChannel <- rpcServerCallData{
				socket:    twoway.outgoingSocket,
				from:      from,
				request:   data,
				wantReply: false,
			}
		}
	case "BACK": 
		if c, ok := twoway.connections[from]; ok && (stateConnected == c.state || stateCheck == c.state) {
			c.state = stateConnected
			c.backoff = 0
			c.timestamp = time.Now().Add(LIVE_TIME)
			twoway.rpcClientResponseChannel <- rpcClientResponseData{
				from:     from,
				response: data,
			}
		}
	default: 
		log.Errorf("replyHandler unknown command: %q", command)
	}
	if nil != err {
		log.Errorf("replyHandler error: %v", err)
	}
	return err
}
func ( twoway * Bilateral ) replyHandler ( ) error { from , command , data , err := receivePacket ( twoway . outgoingSocket ) if nil != err { log . Errorf ( " " , from , err ) return err } log . Debugf ( " f om,  c mmand,  d ta) switch command { case " " : if c , ok := twoway . connections [ from ] ; ok { err := twoway . outgoingSocket . Disconnect ( c . address ) if nil != err { log . Errorf ( " " , c . address , err ) } log . Infof ( " " , c . state ) rnd := make ( [ ] byte , 1 ) rand . Read ( rnd ) c . timestamp = time . Now ( ) . Add ( BASE_PART_TIME + time . Duration ( rnd [ 0 ] ) * RAND_PART_TIME ) c . state = stateWaiting } case " " : if c , ok := twoway . connections [ from ] ; ok && stateHandshake == c . state { c . state = stateConnected c . backoff = 0 c . timestamp = time . Now ( ) . Add ( LIVE_TIME ) } else { log . Infof ( " " , c ) } case " " : if c , ok := twoway . connections [ from ] ; ok && stateCheck == c . state { c . state = stateConnected c . backoff = 0 c . timestamp = time . Now ( ) . Add ( LIVE_TIME ) } case " " : if c , ok := twoway . connections [ from ] ; ok && ( stateConnected == c . state || stateCheck == c . state ) { c . state = stateConnected c . backoff = 0 c . timestamp = time . Now ( ) . Add ( LIVE_TIME ) twoway . rpcServerCallChannel <- rpcServerCallData { socket : twoway . outgoingSocket , from : from , request : data , wantReply : true , } } case " " : if c , ok := twoway . connections [ from ] ; ok && ( stateConnected == c . state || stateCheck == c . state ) { c . state = stateConnected c . backoff = 0 c . timestamp = time . Now ( ) . Add ( LIVE_TIME ) twoway . rpcServerCallChannel <- rpcServerCallData { socket : twoway . outgoingSocket , from : from , request : data , wantReply : false , } } case " " : if c , ok := twoway . connections [ from ] ; ok && ( stateConnected == c . state || stateCheck == c . state ) { c . state = stateConnected c . backoff = 0 c . timestamp = time . Now ( ) . Add ( LIVE_TIME ) twoway . rpcClientResponseChannel <- rpcClientResponseData { from : from , response : data , } } default : log . Errorf ( " " , command ) } if nil != err { log . Errorf ( " " , err ) } return err }
handle response from an upstream server
handle response from an upstream server
// server will disconnect this connection // server allows this connection // keep the connection alive // server's "client" submits a request // server's "client" submits a request // server replied to our request // ignore others