Skip to content

Can you provide an interface that calls back on the main thread? #90

Open
@brinkqiang

Description

@brinkqiang

Hello, I am a game server developer. When we use cpp_redis, there will be a need to call back on the main thread.

At present, I have sealed it with asio to temporarily meet the needs. Would like to ask if the official can provide such an interface. Or there are others with similar needs.
My own example: https://github.com/brinkqiang/dmredispp

#include <cpp_redis/cpp_redis>

#include <iostream>

#ifdef _WIN32
#include <Winsock2.h>
#endif /* _WIN32 */
#include "dmevent/dmevent_module.h"


int
main(void) {
#ifdef _WIN32
    //! Windows netword DLL init
    WORD version = MAKEWORD(2, 2);
    WSADATA data;

    if (WSAStartup(version, &data) != 0) {
        std::cerr << "WSAStartup() failure" << std::endl;
        return -1;
    }
#endif /* _WIN32 */

    DMEVENT_INIT();
    DMEVENT_BEGIN
    {
        fmt::print("-------------------------------------------- -------------------\n");
        fmt::print("{} dmevent loop {} ...\n", DMGetExeName(), "running");
        fmt::print("-------------------------------------------- -------------------\n");
    }
    DMEVENT_END;


    //! Enable logging
    cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);

    cpp_redis::client client;

    client.connect("127.0.0.1", 6379, [&](const std::string& host, std::size_t port, cpp_redis::client::connect_state status) {
        DMEVENT_BEGIN
        {
            if (status == cpp_redis::client::connect_state::dropped) {
                fmt::print("client disconnected from {}:{} isMain:{}\n", host, port, isMain());
            }
        }
        DMEVENT_END;
    });

    int roleid = 0x1234;
    // same as client. send({ "SET", "hello", "42" }, ...)
    client.set("hello", "42", [&](cpp_redis::reply& reply) {
        DMEVENT_BEGIN
        {
            fmt::print("set hello 42: {} roleid: {} isMain:{}\n", reply, roleid, isMain());
        }
        DMEVENT_END;
    });

    // same as client. send({ "DECRBY", "hello", 12 }, ...)
    client.decrby("hello", 12, [&](cpp_redis::reply& reply) {
        DMEVENT_BEGIN
        {
            fmt::print("decrby hello 12: {} roleid: {} isMain:{}\n", reply, roleid, isMain());
        }
        DMEVENT_END;
    });

    // same as client. send({ "GET", "hello" }, ...)
    client.get("hello", [&](cpp_redis::reply& reply) {
        DMEVENT_BEGIN
        {
            fmt::print("get hello: {} roleid: {} isMain:{}\n", reply, roleid, isMain());
        }
        DMEVENT_END;
    });

    // commands are pipelined and only sent when client.commit() is called
    client.commit();

    DMEVENT_RUN_UNTIL();
    // synchronous commit, no timeout
    //client.sync_commit();

    // synchronous commit, timeout
    // client.sync_commit(std::chrono::milliseconds(100));

#ifdef _WIN32
    WSACleanup();
#endif /* _WIN32 */

    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions