Example #1
0
void Client::SetLoginProperty(String key, Urho3D::Variant value)
{
    key = key.Trimmed();
    if (value.IsEmpty())
        properties_.Erase(properties_.Find(key));
    else
        properties_[key] = value;
}
Example #2
0
Variant Material::ParseShaderParameterValue(const String& value)
{
    String valueTrimmed = value.Trimmed();
    if (valueTrimmed.Length() && IsAlpha(valueTrimmed[0]))
        return Variant(ToBool(valueTrimmed));
    else
        return ToVectorVariant(valueTrimmed);
}
Example #3
0
Urho3D::Variant Client::LoginProperty(String key) const
{
    key = key.Trimmed();
    auto i = properties_.Find(key);
    if (i != properties_.End())
        return i.ptr_;
    else
        return Urho3D::Variant();
}
Example #4
0
void FileFilter::AddExtension( const String& ext )
{
   String x = ext.Trimmed();
   if ( !x.StartsWith( '.' ) )
      if ( !x.StartsWith( '*' ) )
         x = '*' + x;
   x.ToLowercase(); // case-insensitive file extensions
   if ( !extensions.Contains( x ) )
      extensions.Add( x );
}
void WebRequest::SetRequestHeader(const String& key, const String& value)
{
    // Trim and only add non-empty header strings.
    String header;
    header += key.Trimmed();
    header += ": ";
    header += value;
    if (header.Length())
    {
        is_->headers = curl_slist_append(is_->headers, header.CString());
    }
}
Example #6
0
void Client::Login(const String& address, unsigned short port, const String& username, const String& password, const String &protocol)
{
    if (IsConnected())
        DoLogout();

    // Set properties that the "lower" overload wont be adding.
    SetLoginProperty("username", username);
    SetLoginProperty("password", password);

    String p = protocol.Trimmed().ToLower();
    kNet::SocketTransportLayer transportLayer = kNet::StringToSocketTransportLayer(p.CString());
    if (transportLayer == kNet::InvalidTransportLayer && !p.Empty())
    {
        LogError("Client::Login: Cannot log to server using unrecognized protocol: " + p);
        return;
    }
    Login(address, port, transportLayer);
}
WebRequest::WebRequest(Context* context, const String& verb, const String& url, double requestContentSize) :
    Object(context),
    is_(new WebRequestInternalState(*this))
{
    is_->url = url.Trimmed();
    is_->verb = verb;
    is_->upload = new BufferQueue(context);
    is_->download = new BufferQueue(context);
    is_->state = HTTP_INITIALIZING;
    is_->curlm = NULL;
    is_->curl = NULL;
    is_->requestContentSize = curl_off_t(std::floor(requestContentSize));
    is_->isAborted = false;
    is_->isAddedToMulti = false;

    Web* web = GetSubsystem<Web>();
    web->setup(*this);
}
void StaticModel::ApplyMaterialList(const String& fileName)
{
    String useFileName = fileName;
    if (useFileName.Trimmed().Empty() && model_)
        useFileName = ReplaceExtension(model_->GetName(), ".txt");

    ResourceCache* cache = GetSubsystem<ResourceCache>();
    SharedPtr<File> file = cache->GetFile(useFileName, false);
    if (!file)
        return;

    unsigned index = 0;
    while (!file->IsEof() && index < batches_.Size())
    {
        Material* material = cache->GetResource<Material>(file->ReadLine());
        if (material)
            SetMaterial(index, material);

        ++index;
    }
}
Example #9
0
HttpRequest::HttpRequest(const String& url, const String& verb, const Vector<String>& headers, const String& postData) :
    url_(url.Trimmed()),
    verb_(!verb.Empty() ? verb : "GET"),
    headers_(headers),
    postData_(postData),
    state_(HTTP_INITIALIZING),
    httpReadBuffer_(new unsigned char[READ_BUFFER_SIZE]),
    readBuffer_(new unsigned char[READ_BUFFER_SIZE]),
    readPosition_(0),
    writePosition_(0)
{
    // Size of response is unknown, so just set maximum value. The position will also be changed
    // to maximum value once the request is done, signaling end for Deserializer::IsEof().
    size_ = M_MAX_UNSIGNED;

    URHO3D_LOGDEBUG("HTTP " + verb_ + " request to URL " + url_);

#ifdef URHO3D_THREADING
    // Start the worker thread to actually create the connection and read the response data.
    Run();
#else
    URHO3D_LOGERROR("HTTP request will not execute as threading is disabled");
#endif
}
void AssetRefListener::HandleAssetRefChange(AssetAPI *assetApi, String assetRef, const String& assetType)
{
    // Disconnect from any previous transfer we might be listening to
    if (!currentTransfer.Expired())
    {
        IAssetTransfer* current = currentTransfer.Get();
        current->Succeeded.Disconnect(this, &AssetRefListener::OnTransferSucceeded);
        current->Failed.Disconnect(this, &AssetRefListener::OnTransferFailed);
        currentTransfer.Reset();
    }
    
    assert(assetApi);

    // Store AssetAPI ptr for later signal hooking.
    if (!myAssetAPI)
        myAssetAPI = assetApi;

    // If the ref is empty, don't go any further as it will just trigger the LogWarning below.
    assetRef = assetRef.Trimmed();
    if (assetRef.Empty())
    {
        asset = AssetPtr();
        return;
    }
    currentWaitingRef = "";

    // Resolve the protocol for generated:// assets. These assets are never meant to be
    // requested from AssetAPI, they cannot be fetched from anywhere. They can only be either
    // loaded or we must wait for something to load/create them.
    String protocolPart = "";
    assetApi->ParseAssetRef(assetRef, &protocolPart);
    if (protocolPart.ToLower() == "generated")
    {
        AssetPtr loadedAsset = assetApi->FindAsset(assetRef);
        if (loadedAsset.Get() && loadedAsset->IsLoaded())
        {
            // Asset is loaded, emit Loaded with 1 msec delay to preserve the logic
            // that HandleAssetRefChange won't emit anything itself as before.
            // Otherwise existing connection can break/be too late after calling this function.
            asset = loadedAsset;
            assetApi->GetFramework()->Frame()->DelayedExecute(0.0f).Connect(this, &AssetRefListener::EmitLoaded);
            return;
        }
        else
        {
            // Wait for it to be created.
            currentWaitingRef = assetRef;
            myAssetAPI->AssetCreated.Connect(this, &AssetRefListener::OnAssetCreated);
        }
    }
    else
    {
        // This is not a generated asset, request normally from asset api.
        AssetTransferPtr transfer = assetApi->RequestAsset(assetRef, assetType);
        if (!transfer)
        {
            LogWarning("AssetRefListener::HandleAssetRefChange: Asset request for asset \"" + assetRef + "\" failed.");
            return;
        }
        currentWaitingRef = assetRef;

        transfer->Succeeded.Connect(this, &AssetRefListener::OnTransferSucceeded);
        transfer->Failed.Connect(this, &AssetRefListener::OnTransferFailed);

        currentTransfer = transfer;
    }
    
    // Disconnect from the old asset's load signal
    if (asset)
        asset->Loaded.Disconnect(this, &AssetRefListener::OnAssetLoaded);
    asset = AssetPtr();
}
DbResult DbConnection::Execute(const String& sql, bool useCursorEvent)
{
    DbResult result;
    const char* zLeftover = 0;
    sqlite3_stmt* pStmt = 0;
    assert(connectionImpl_);
    int rc = sqlite3_prepare_v2(connectionImpl_, sql.Trimmed().CString(), -1, &pStmt, &zLeftover);
    if (rc != SQLITE_OK)
    {
        LOGERRORF("Could not execute: %s", sqlite3_errmsg(connectionImpl_));
        assert(!pStmt);
        return result;
    }
    if (*zLeftover)
    {
        LOGERROR("Could not execute: only one SQL statement is allowed");
        sqlite3_finalize(pStmt);
        return result;
    }

    unsigned numCols = (unsigned)sqlite3_column_count(pStmt);
    result.columns_.Resize(numCols);
    for (unsigned i = 0; i < numCols; ++i)
        result.columns_[i] = sqlite3_column_name(pStmt, i);

    bool filtered = false;
    bool aborted = false;

    while (1)
    {
        rc = sqlite3_step(pStmt);
        if (rc == SQLITE_ROW)
        {
            VariantVector colValues(numCols);
            for (unsigned i = 0; i < numCols; ++i)
            {
                int type = sqlite3_column_type(pStmt, i);
                if (type != SQLITE_NULL)
                {
                    // We can only bind primitive data type that our Variant class supports
                    switch (type)
                    {
                    case SQLITE_INTEGER:
                        colValues[i] = sqlite3_column_int(pStmt, i);
                        if (String(sqlite3_column_decltype(pStmt, i)).Compare("BOOLEAN", false) == 0)
                            colValues[i] = colValues[i] != 0;
                        break;

                    case SQLITE_FLOAT:
                        colValues[i] = sqlite3_column_double(pStmt, i);
                        break;

                    default:
                        // All other types are stored using their string representation in the Variant
                        colValues[i] = (const char*)sqlite3_column_text(pStmt, i);
                        break;
                    }
                }
            }

            if (useCursorEvent)
            {
                using namespace DbCursor;

                VariantMap& eventData = GetEventDataMap();
                eventData[P_DBCONNECTION] = this;
                eventData[P_RESULTIMPL] = pStmt;
                eventData[P_SQL] = sql;
                eventData[P_NUMCOLS] = numCols;
                eventData[P_COLVALUES] = colValues;
                eventData[P_COLHEADERS] = result.columns_;
                eventData[P_FILTER] = false;
                eventData[P_ABORT] = false;

                SendEvent(E_DBCURSOR, eventData);

                filtered = eventData[P_FILTER].GetBool();
                aborted = eventData[P_ABORT].GetBool();
            }

            if (!filtered)
                result.rows_.Push(colValues);
            if (aborted)
            {
                sqlite3_finalize(pStmt);
                break;
            }
        }
        else if (rc != SQLITE_DONE)
            LOGERRORF("Could not execute: %s", sqlite3_errmsg(connectionImpl_));
        if (rc != SQLITE_ROW)
        {
            sqlite3_finalize(pStmt);
            break;
        }
    }

    result.numAffectedRows_ = numCols ? -1 : sqlite3_changes(connectionImpl_);
    return result;
}