Ejemplo n.º 1
0
func (c *GsiScanClient) streamResponse(
	conn net.Conn,
	pkt *transport.TransportPacket,
	callb ResponseHandler) (cont bool, healthy bool, err error) {

	var resp interface{}
	var finish bool

	laddr := conn.LocalAddr()
	timeoutMs := c.readDeadline * time.Millisecond
	conn.SetReadDeadline(time.Now().Add(timeoutMs))
	if resp, err = pkt.Receive(conn); err != nil {
		//resp := &protobuf.ResponseStream{
		//    Err: &protobuf.Error{Error: proto.String(err.Error())},
		//}
		//callb(resp) // callback with error
		cont, healthy = false, false
		if err == io.EOF {
			err = fmt.Errorf("server closed connection (EOF)")
		} else {
			fmsg := "%v connection %q response transport failed `%v`\n"
			logging.Errorf(fmsg, c.logPrefix, laddr, err)
		}

	} else if resp == nil {
		finish = true
		fmsg := "%v connection %q received StreamEndResponse"
		logging.Tracef(fmsg, c.logPrefix, laddr)
		callb(&protobuf.StreamEndResponse{}) // callback most likely return true
		cont, healthy = false, true

	} else {
		streamResp := resp.(*protobuf.ResponseStream)
		if err = streamResp.Error(); err == nil {
			cont = callb(streamResp)
		}
		healthy = true
	}

	var closeErr error
	if cont == false && healthy == true && finish == false {
		if closeErr, healthy = c.closeStream(conn, pkt); err == nil {
			err = closeErr
		}
	}
	return
}
Ejemplo n.º 2
0
func (c *GsiScanClient) closeStream(
	conn net.Conn, pkt *transport.TransportPacket) (err error, healthy bool) {

	var resp interface{}
	laddr := conn.LocalAddr()
	healthy = true
	// request server to end the stream.
	err = c.sendRequest(conn, pkt, &protobuf.EndStreamRequest{})
	if err != nil {
		fmsg := "%v closeStream() request transport failed `%v`\n"
		logging.Errorf(fmsg, c.logPrefix, err)
		healthy = false
		return
	}
	fmsg := "%v connection %q transmitted protobuf.EndStreamRequest"
	logging.Tracef(fmsg, c.logPrefix, laddr)

	timeoutMs := c.readDeadline * time.Millisecond
	// flush the connection until stream has ended.
	for true {
		conn.SetReadDeadline(time.Now().Add(timeoutMs))
		resp, err = pkt.Receive(conn)
		if err != nil {
			healthy = false
			if err == io.EOF {
				logging.Errorf("%v connection %q closed \n", c.logPrefix, laddr)
				return
			}
			fmsg := "%v connection %q response transport failed `%v`\n"
			logging.Errorf(fmsg, c.logPrefix, laddr, err)
			return

		} else if resp == nil { // End of stream marker
			return
		}
	}
	return
}