C++ AMQP-CPP-RabbitMQ事件循环不工作

C++ AMQP-CPP-RabbitMQ事件循环不工作,c++,events,event-handling,rabbitmq,amqp,C++,Events,Event Handling,Rabbitmq,Amqp,我无法使RabbitMQ CPP(哥白尼营销软件/AMQP-CPP)库的事件循环正常运行。我已经设置了一个rabbitmq服务器,它运行良好 我的代码如下所示: #include <iostream> #include <sys/select.h> #include <cop_amqpcpp.h> #include <cop_amqpcpp/linux_tcp.h> using namespace std; fd_set rfds; cla

我无法使RabbitMQ CPP(哥白尼营销软件/AMQP-CPP)库的事件循环正常运行。我已经设置了一个rabbitmq服务器,它运行良好

我的代码如下所示:

#include <iostream>
#include <sys/select.h>
#include <cop_amqpcpp.h>
#include <cop_amqpcpp/linux_tcp.h>


using namespace std;

fd_set rfds;

class MyTcpHandler : public AMQP::TcpHandler
{
private:
    /**
    * Method that is called when the connection succeeded
    * @param socket Pointer to the socket
    */
    virtual void onConnected(AMQP::TcpConnection* connection)
    {
        std::cout << "connected" << std::endl;
    }

    /**
     *  When the connection ends up in an error state this method is called.
     *  This happens when data comes in that does not match the AMQP protocol
     *
     *  After this method is called, the connection no longer is in a valid
     *  state and can be used. In normal circumstances this method is not called.
     *
     *  @param  connection      The connection that entered the error state
     *  @param  message         Error message
     */
    virtual void onError(AMQP::TcpConnection* connection, const char* message)
    {
        // report error
        std::cout << "AMQP TCPConnection error: " << message << std::endl;
    }

    /**
     *  Method that is called when the connection was closed.
     *  @param  connection      The connection that was closed and that is now unusable
     */
    virtual void onClosed(AMQP::TcpConnection* connection)
    {
        std::cout << "closed" << std::endl;
    }


    /**
     *  Method that is called by AMQP-CPP to register a filedescriptor for readability or writability
     *  @param  connection  The TCP connection object that is reporting
     *  @param  fd          The filedescriptor to be monitored
     *  @param  flags       Should the object be monitored for readability or writability?
     */
    virtual void monitor(AMQP::TcpConnection* connection, int fd, int flags)
    {
        // we did not yet have this watcher - but that is ok if no filedescriptor was registered
        if (flags == 0) return;
        cout << "Fd " << fd << " Flags " << flags << endl;
        if (flags & AMQP::readable)
        {
            FD_SET(fd, &rfds);
            m_fd = fd;
            m_flags = flags;
        }
    }

public:
    int m_fd = -1;
    int m_flags = 0;
};


int main(int argc, char* argv[])
{
    MyTcpHandler myHandler;
    int maxfd = 1;
    int result = 0;

    struct timeval tv
    {
        1, 0
    };


    // address of the server
    AMQP::Address address(AMQP::Address("amqp://test:test@localhost/"));

    // create a AMQP connection object
    AMQP::TcpConnection connection(&myHandler, address);

    // and create a channel
    AMQP::TcpChannel channel(&connection);

    channel.onError([](const char* message)
    {
        cout << "channel error: " << message << endl;
    });
    channel.onReady([]()
    {
        cout << "channel ready " << endl;
    });

    // use the channel object to call the AMQP method you like

    channel.declareExchange("my-exchange", AMQP::fanout);
    channel.declareQueue("my-queue");
    channel.bindQueue("my-exchange", "my-queue", "my-routing-key");

    do
    {
        FD_ZERO(&rfds);
        FD_SET(0, &rfds);
        cout << myHandler.m_fd << endl;
        if (myHandler.m_fd != -1)
        {
            maxfd = myHandler.m_fd + 1;
        }


        result = select(maxfd, &rfds, NULL, NULL, &tv);
        if ((result == -1) && errno == EINTR)
        {
            cout << "Error in socket" << endl;
        }
        else if (result > 0)
        {
            if (myHandler.m_flags & AMQP::readable)

                cout << "Got something" << endl;
            if (FD_ISSET(myHandler.m_fd, &rfds))
            {
                connection.process(maxfd, myHandler.m_flags);
            }
        }
    }
    while (1);
}
在示例中,我使用了libuv和libevent等就绪事件循环,但我想了解没有这些循环它是如何工作的

如何让它工作?

下面一行

FD_SET(myHandler.m_fd, &rfds);

需要添加到事件循环中。

您告诉
选择
以监视
stdout
。可以尝试
FDSET(myHandler.m\u fd,&rdfds)?检查手册页。你能解释一下我应该如何在Windows中使用它吗?在我的例子中,
TCPHandler
没有
m_fd
memeber。
FD_SET(myHandler.m_fd, &rfds);