CopernicaMarketingSoftware / AMQP-CPP

C++ library for asynchronous non-blocking communication with RabbitMQ
Apache License 2.0
884 stars 340 forks source link

Running in the Windows C++ environment #494

Closed gunhak01 closed 1 year ago

gunhak01 commented 1 year ago

Hi, I was given this library to try and utilize rabbitmq in c++.

I use visual studio and received the library via nuget. But when I tried to run it through tcpchannel, I got a lot of LNK2019, LNK2001 errors.

My question is twofold.

  1. is there anything I need to do besides getting the package from nuget?

  2. do I need to implement the tcp connection part myself in the windows c++ environment? My English is weak and I don't understand much from the readme.

Thanks.

lukebakken commented 1 year ago

https://groups.google.com/g/rabbitmq-users/c/lWcWxbQE4Fc

Please see this example:

https://github.com/CopernicaMarketingSoftware/AMQP-CPP/blob/master/examples/libuv.cpp

LibUV is supported on Windows.

Note: see this related issue https://github.com/CopernicaMarketingSoftware/AMQP-CPP/issues/491

gunhak01 commented 1 year ago

@lukebakken I'm trying to create a class that inherits from ConnectionHandler and do tcp communication via libuv, as suggested as a solution in #491. However, I'm not sure about the part where I put the IP address in the connection, do you have any ideas on that? In onReady, I have the code to queue, exchange and publish the channel.

MyConnectionHandler handler;

AMQP::Login login("admin", "nimda"); AMQP::Connection connection(&handler, login, "10.10.50.242");

handler.onReady(&connection);

lukebakken commented 1 year ago

@gunhak01 the best way to help me (and other people) assist you is to provide a git repository with all of your code that I can clone, compile, and run to see EXACTLY what you are seeing. Please do that. Using GitHub would be ideal. We can then take this discussion to your repository and close this issue.

gunhak01 commented 1 year ago

@lukebakken Thank you for your interest. Right now I can't upload it to GitHub. But since my code is not very long, I will post it here. My code is below and I installed amqpcpp and libuv via vcpkg with the latest 64-bit versions (4.3.19 and 1.44.2, respectively). This is all I have implemented and I would like to try sending at least one message to rabbitmq first via onReady().

#define NOMINMAX
#include <amqpcpp.h>
#include <uv.h>

class MyConnectionHandler : public AMQP::ConnectionHandler
{
private:
  uv_loop_t* loop;
  uv_tcp_t* socket;
  uv_write_t write_req;
  uv_buf_t buffer;

public:
  MyConnectionHandler()
  {
    loop = uv_default_loop();
    socket = new uv_tcp_t();
    uv_tcp_init(loop, socket);
  }

  virtual void onData(AMQP::Connection* connection, const char* data, size_t size)
  {
    buffer = uv_buf_init(const_cast<char*>(data), size);
    uv_write(&write_req, reinterpret_cast<uv_stream_t*>(socket), &buffer, 1,
             [](uv_write_t* req, int status) {
      // Check the status and handle any errors
      // ...
    });
  }

  void onReady(AMQP::Connection* connection) override
  {
    std::cout << "Connected to RabbitMQ!" << std::endl;

    // create a channel
    AMQP::Channel channel(connection);

    // declare an exchange
    channel.declareExchange("my-exchange", AMQP::fanout);

    // declare a queue
    channel.declareQueue("my-queue");

    // bind the queue to the exchange
    channel.bindQueue("my-exchange", "my-queue", "my-routing-key");

    // publish a message
    std::string message = "Hello, RabbitMQ!";

    channel.publish("my-exchange", "my-routing-key", message);
  }

  void onError(AMQP::Connection* connection, const char* message) override
  {
    std::cerr << "AMQP error: " << message << std::endl;
  }

  virtual void onClosed(AMQP::Connection* connection)
  {

  }
};

int main()
{
  MyConnectionHandler handler;

  // create a AMQP connection object
  //AMQP::Connection connection(&handler, AMQP::Address("amqp://admin:nimda@10.10.50.242/"));

  AMQP::Address address("amqp://admin:nimda@10.10.50.242:5672/");
  AMQP::Connection connection(&handler, address);
  //AMQP::Login login("admin", "nimda");
  //AMQP::Connection connection(&handler, login, "10.10.50.242");

  handler.onReady(&connection);

  return 0;
}
lukebakken commented 1 year ago

@gunhak01. Please create a repository on GitHub - https://github.com/gunhak01?tab=repositories

Commit ALL of your code, your Visual Studio project, Makefiles, dependencies ... EVERYTHING. I should only have to clone your project and either open it in Visual Studo or run make or run nmake.

I'm happy to assist but right now you're asking me to set up a build environment when presumably you have already done so.

gunhak01 commented 1 year ago

@lukebakken https://github.com/gunhak01/amqpcpp_pilot Yes, thank you. I've uploaded it to GitHub, but it doesn't seem to upload the libraries I received via vcpkg?

vcpkg install amqpcpp:x64-windows
vcpkg install libuv:x64-windows
vcpkg integrate install

vcpkglist

I got the libraries as above and am using them in visual studio and that code and vcpkg is all I ran. I didn't make any other environment modifications.

lukebakken commented 1 year ago

Thank you. I will find time to test this out next week. Please be patient. I suggest closing this issue (https://github.com/CopernicaMarketingSoftware/AMQP-CPP/issues/494) and we can continue discussion in your repository.

gunhak01 commented 1 year ago

@lukebakken Okay, I really appreciate your help.