bool TickLoaderImpl::processResponse()

in src/dxapi/native/tickdb/http/tickloader_http.cpp [749:846]


bool TickLoaderImpl::processResponse()
{
    auto ioStream = ioStream_;
    if (interruption_ > Interruption::STOPPING || NULL == ioStream) {
        return true;
    }

    dbgProcessingServerResponse_ = true;
    try {
        byte buf[0x80];
        int result;
        while (0 != ioStream->tryRead(buf, 1)) {
            unsigned command = buf[0];
            switch (command) {
            case ERROR_BLOCK_ID:
                {
                    string s;
                    result = read<int32_t>();
                    s = readUTF8();
                    DBGLOG(LOGHDR ".processResponse(): Loading error: %d (%s)", ID, (int)result, s.c_str());
                    dispatchError(result, s);
                    break;
                }
            case LOADER_KEEPALIVE_ID:
                //DBGLOG_VERBOSE(LOGHDR ".processResponse(): Keepalive marker received", ID);
                break;

            case LOADER_RESPONSE_BLOCK_ID:
                result = read<int32_t>();
                TickLoaderNs::Interruption::Enum interruptionCode;
                if (RESP_ERROR == result) {
                    error_.code = Error::ERROR;
                    readErrorResponse();
                    DBGLOG(LOGHDR ".processResponse(): Loading finished with error, error recorded", ID);
                    interruptionCode = Interruption::SERVER_FINISHED_ERROR;
                }
                else {
                    DBGLOG_VERBOSE(LOGHDR ".processResponse(): Loading succesful, result = %d", ID, (int)result);
                    interruptionCode = Interruption::SERVER_FINISHED_OK;
                }

                interruption_ = interruptionCode;
                dbgProcessingServerResponse_ = false;
                return true;

            // Subscription change listener
            case TYPE_BLOCK_ID:
                readTypesSubscriptionChange();
                break;

            case INSTRUMENT_BLOCK_ID:
                readEntitiesSubscriptionChange();
                break;

            default:
                error_.className.clear();
                format_string(&error_.msg, "Unexpected token received from the server: %02X", command);
                DBGLOG(LOGHDR ".processResponse(): unrecognized token %02X: %s", ID, command, serverErrorText().c_str());
                error_.code = Error::IO_ERROR;
                interruption_ = Interruption::SERVER_FINISHED_ERROR; // TODO: new error signatures
                ioStream_->closeInput();
            }
        }
    }

    catch (const IOStreamDisconnectException &) {
        error_.code = Error::IO_ERROR;
        interruption_ = Interruption::SERVER_DISCONNECTED;
        DBGLOG(LOGHDR ".processResponse(): WRN: disconnected from the server side", ID);
        format_string(&error_.msg, "disconnected by server");
        ioStream_->close();
    }

    catch (const IOStreamException &e) {
        error_.code = Error::IO_ERROR;
        interruption_ = Interruption::SERVER_DISCONNECTED;
        DBGLOG(LOGHDR ".processResponse(): WRN: Socket exception: %s", ID, e.what());
        format_string(&error_.msg, "IOStream exception: %s", e.what());
        ioStream_->close();
    }

    catch (const std::exception &e) {
        error_.code = Error::ERROR;
        interruption_ = Interruption::ERROR;
        DBGLOG(LOGHDR ".processResponse(): ERR: Exception: %s", ID, e.what());
        format_string(&error_.msg, "std::exception: %s", e.what());
    }

    catch (...) {
        error_.code = Error::ERROR;
        interruption_ = Interruption::ERROR;
        DBGLOG(LOGHDR ".processResponse(): ERR: Unknown exception", ID);
        format_string(&error_.msg, LOGHDR ".processResponse(): Unknown exception", ID);
    }

    dbgProcessingServerResponse_ = false;
    return false;
}