debian环境:
apt install make \
gcc \
g++ \
librabbitmq-dev \
rabbitmq-server
service rabbitmq-server start
MyRmq.h
#ifndef my_MyRmq
#define my_MyRmq
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <amqp.h>
#include <amqp_tcp_socket.h>
#include <string>
#include <iostream>
#include "utils.h"
typedef char* (*Callback)(char*);
class MyRmq
{
public:
MyRmq(std::string user,std::string pwd,std::string ip,int port);
~MyRmq();
void receive(std::string exchange,std::string binding_key,std::string queue_name,Callback fun);
char* send(std::string exchange,std::string routing_key,std::string data);
private:
amqp_socket_t * socket;
amqp_connection_state_t conn;
};
#endif
MyRmq.cpp
#include "MyRmq.h"
MyRmq::MyRmq(std::string user,std::string pwd,std::string ip,int port)
{
socket = NULL;
conn = amqp_new_connection();
socket = amqp_tcp_socket_new(conn);
if (!socket)
{
die("creating tcp socket fail\n");
}
if (amqp_socket_open(socket,ip.c_str(),port))
{
die("opening tcp socket fail\n");
}
die_on_amqp_error(
amqp_login(
conn,
"/",
0,
131072,
0,
AMQP_SASL_METHOD_PLAIN,
user.c_str(),
pwd.c_str()
),
"login"
);
amqp_channel_open(conn,1);
die_on_amqp_error(amqp_get_rpc_reply(conn),"opening channel");
}
MyRmq::~MyRmq()
{
}
void MyRmq::receive(std::string exchange,std::string binding_key,std::string queue_name,Callback fun)
{
amqp_bytes_t queue_name_bytes = amqp_cstring_bytes(queue_name.c_str());
amqp_queue_declare_ok_t * r = amqp_queue_declare(
conn,
1,
queue_name_bytes,
0,
0,
0,
1,
amqp_empty_table
);
die_on_amqp_error(amqp_get_rpc_reply(conn),"declaring queue");
amqp_queue_bind(
conn,
1,
queue_name_bytes,
amqp_cstring_bytes(exchange.c_str()),
amqp_cstring_bytes(binding_key.c_str()),
amqp_empty_table
);
die_on_amqp_error(amqp_get_rpc_reply(conn),"binding queue");
amqp_basic_consume(
conn,
1,
queue_name_bytes,
amqp_empty_bytes,
0,
1,
0,
amqp_empty_table
);
die_on_amqp_error(amqp_get_rpc_reply(conn),"consuming");
for (;;)
{
amqp_rpc_reply_t res;
amqp_envelope_t envelope;
amqp_maybe_release_buffers(conn);
res = amqp_consume_message(conn,&envelope,NULL,0);
if (AMQP_RESPONSE_NORMAL != res.reply_type)
{
std::cout << "res.library_error: " << res.library_error << std::endl;
break;
}
char * request = (char*)envelope.message.body.bytes;
*(request + envelope.message.body.len) = '\0';
char * response = (*fun)(request);
std::cout << "request: " << request << std::endl;
std::cout << "response: " << response << std::endl;
amqp_basic_properties_t props;
props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
props.content_type = amqp_cstring_bytes("application/json");
props.delivery_mode = AMQP_DELIVERY_PERSISTENT; /* persistent delivery mode */
int res_code = amqp_basic_publish(
conn,
1,
amqp_cstring_bytes(""),
envelope.message.properties.reply_to,
0,
0,
&props,
amqp_cstring_bytes(response)
);
if (AMQP_STATUS_OK != res_code)
{
std::cout << "response failed, code: " << res_code << std::endl;
}
amqp_destroy_envelope(&envelope);
free(response);
}
die_on_amqp_error(amqp_channel_close(conn,1,AMQP_REPLY_SUCCESS),"closing channel");
die_on_amqp_error(amqp_connection_close(conn,AMQP_REPLY_SUCCESS),"closing connection");
die_on_error(amqp_destroy_connection(conn),"ending connection");
}
char * MyRmq::send(std::string exchange,std::string routing_key,std::string data)
{
amqp_bytes_t reply_to_queue;
amqp_queue_declare_ok_t * r = amqp_queue_declare(
conn,
1,
amqp_empty_bytes,
0,
0,
0,
1,
amqp_empty_table
);
die_on_amqp_error(amqp_get_rpc_reply(conn),"declaring queue");
reply_to_queue = amqp_bytes_malloc_dup(r->queue);
if (reply_to_queue.bytes == NULL)
{
std::cout << "out of memory\n";
exit(1);
}
amqp_basic_properties_t props;
props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG |
AMQP_BASIC_DELIVERY_MODE_FLAG | AMQP_BASIC_REPLY_TO_FLAG |
AMQP_BASIC_CORRELATION_ID_FLAG;
props.content_type = amqp_cstring_bytes("text/plain");
props.delivery_mode = 2;
props.reply_to = amqp_bytes_malloc_dup(reply_to_queue);
if (props.reply_to.bytes == NULL)
{
std::cout << "out of memory\n";
exit(1);
}
std::cout << "publishing data:" << data.c_str() << std::endl;
die_on_error(
amqp_basic_publish(
conn,
1,
amqp_cstring_bytes(exchange.c_str()),
amqp_cstring_bytes(routing_key.c_str()),
0,
0,
&props,
amqp_cstring_bytes(data.c_str())
),
"publishing"
);
amqp_bytes_free(props.reply_to);
amqp_basic_consume(
conn,
1,
reply_to_queue,
amqp_empty_bytes,
0,
1,
0,
amqp_empty_table
);
die_on_amqp_error(amqp_get_rpc_reply(conn),"consuming");
amqp_bytes_free(reply_to_queue);
amqp_frame_t frame;
int result;
amqp_basic_deliver_t *d;
amqp_basic_properties_t *p;
size_t body_target;
size_t body_received;
char * response_str = NULL;
for (;;)
{
amqp_maybe_release_buffers(conn);
result = amqp_simple_wait_frame(conn,&frame);
std::cout << "result: " << result << std::endl;
if (result < 0)
break;
std::cout << "frame type: " << frame.frame_type << std::endl;
std::cout << "channel: " << frame.channel << std::endl;
if (frame.frame_type != AMQP_FRAME_METHOD)
continue;
std::cout << "method: " << amqp_method_name(frame.payload.method.id) << std::endl;
if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
continue;
d = (amqp_basic_deliver_t *)frame.payload.method.decoded;
std::cout << "delivery: " << (unsigned)d->delivery_tag
<< ", exchanges: " << (char*)d->exchange.bytes <<
", routingkey: " << (char*)d->routing_key.bytes << std::endl;
result = amqp_simple_wait_frame(conn,&frame);
if (result < 0)
break;
if (frame.frame_type != AMQP_FRAME_HEADER) {
std::cout << "expected header" << std::endl;
abort();
}
p = (amqp_basic_properties_t *)frame.payload.properties.decoded;
if (p->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG){
std::cout << "content-type: " << (char *)p->content_type.bytes << std::endl;
}
std::cout << "-----" << std::endl;
body_target = (size_t)frame.payload.properties.body_size;
body_received = 0;
std::string response = "";
while( body_received < body_target) {
result = amqp_simple_wait_frame(conn,&frame);
if (result < 0)
break;
if (frame.frame_type != AMQP_FRAME_BODY){
std::cout << "expected body\n";
abort();
}
body_received += frame.payload.body_fragment.len;
assert(body_received <= body_target);
amqp_dump(frame.payload.body_fragment.bytes,frame.payload.body_fragment.len);
response += std::string((char*)frame.payload.body_fragment.bytes);
}
response_str = (char*)malloc(response.size() + 1);
strcpy(response_str, response.c_str());
if (body_received != body_target)
break;
break;
}
die_on_amqp_error(amqp_channel_close(conn,1,AMQP_REPLY_SUCCESS),"closing channel");
die_on_amqp_error(amqp_connection_close(conn,AMQP_REPLY_SUCCESS),"closing connection");
die_on_error(amqp_destroy_connection(conn),"ending connection");
return response_str;
}
utils.h
#ifndef librabbitmq_examples_utils_h
#define librabbitmq_examples_utils_h
/*
* ***** BEGIN LICENSE BLOCK *****
* Version: MIT
*
* Portions created by Alan Antonuk are Copyright (c) 2012-2013
* Alan Antonuk. All Rights Reserved.
*
* Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
* All Rights Reserved.
*
* Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
* VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* ***** END LICENSE BLOCK *****
*/
void die(const char *fmt, ...);
extern void die_on_error(int x, char const *context);
extern void die_on_amqp_error(amqp_rpc_reply_t x, char const *context);
extern void amqp_dump(void const *buffer, size_t len);
extern uint64_t now_microseconds(void);
extern void microsleep(int usec);
#endif
utils.c
/*
* ***** BEGIN LICENSE BLOCK *****
* Version: MIT
*
* Portions created by Alan Antonuk are Copyright (c) 2012-2013
* Alan Antonuk. All Rights Reserved.
*
* Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
* All Rights Reserved.
*
* Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
* VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* ***** END LICENSE BLOCK *****
*/
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <amqp.h>
#include <amqp_framing.h>
#include <stdint.h>
#include "utils.h"
void die(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(1);
}
void die_on_error(int x, char const *context) {
if (x < 0) {
fprintf(stderr, "%s: %s\n", context, amqp_error_string2(x));
exit(1);
}
}
void die_on_amqp_error(amqp_rpc_reply_t x, char const *context) {
switch (x.reply_type) {
case AMQP_RESPONSE_NORMAL:
return;
case AMQP_RESPONSE_NONE:
fprintf(stderr, "%s: missing RPC reply type!\n", context);
break;
case AMQP_RESPONSE_LIBRARY_EXCEPTION:
fprintf(stderr, "%s: %s\n", context, amqp_error_string2(x.library_error));
break;
case AMQP_RESPONSE_SERVER_EXCEPTION:
switch (x.reply.id) {
case AMQP_CONNECTION_CLOSE_METHOD: {
amqp_connection_close_t *m =
(amqp_connection_close_t *)x.reply.decoded;
fprintf(stderr, "%s: server connection error %uh, message: %.*s\n",
context, m->reply_code, (int)m->reply_text.len,
(char *)m->reply_text.bytes);
break;
}
case AMQP_CHANNEL_CLOSE_METHOD: {
amqp_channel_close_t *m = (amqp_channel_close_t *)x.reply.decoded;
fprintf(stderr, "%s: server channel error %uh, message: %.*s\n",
context, m->reply_code, (int)m->reply_text.len,
(char *)m->reply_text.bytes);
break;
}
default:
fprintf(stderr, "%s: unknown server error, method id 0x%08X\n",
context, x.reply.id);
break;
}
break;
}
exit(1);
}
static void dump_row(long count, int numinrow, int *chs) {
int i;
printf("%08lX:", count - numinrow);
if (numinrow > 0) {
for (i = 0; i < numinrow; i++) {
if (i == 8) {
printf(" :");
}
printf(" %02X", chs[i]);
}
for (i = numinrow; i < 16; i++) {
if (i == 8) {
printf(" :");
}
printf(" ");
}
printf(" ");
for (i = 0; i < numinrow; i++) {
if (isprint(chs[i])) {
printf("%c", chs[i]);
} else {
printf(".");
}
}
}
printf("\n");
}
static int rows_eq(int *a, int *b) {
int i;
for (i = 0; i < 16; i++)
if (a[i] != b[i]) {
return 0;
}
return 1;
}
void amqp_dump(void const *buffer, size_t len) {
unsigned char *buf = (unsigned char *)buffer;
long count = 0;
int numinrow = 0;
int chs[16];
int oldchs[16] = {0};
int showed_dots = 0;
size_t i;
for (i = 0; i < len; i++) {
int ch = buf[i];
if (numinrow == 16) {
int j;
if (rows_eq(oldchs, chs)) {
if (!showed_dots) {
showed_dots = 1;
printf(
" .. .. .. .. .. .. .. .. : .. .. .. .. .. .. .. ..\n");
}
} else {
showed_dots = 0;
dump_row(count, numinrow, chs);
}
for (j = 0; j < 16; j++) {
oldchs[j] = chs[j];
}
numinrow = 0;
}
count++;
chs[numinrow++] = ch;
}
dump_row(count, numinrow, chs);
if (numinrow != 0) {
printf("%08lX:\n", count);
}
}
收发:
receiver.cpp
#include "MyRmq.h"
#include <iostream>
char * process(char * request)
{
char * resp_str = "process...response...";
char * response = (char*)malloc((int)strlen(resp_str) + 1);
strcpy(response,resp_str);
std::cout << "request: " << request << std::endl;
std::cout << "response: " << response << std::endl;
return response;
}
int main()
{
Callback p = NULL;
char * resp = NULL;
p = process;
MyRmq MyRmq("guest","guest","0.0.0.0",5672);
MyRmq.receive("amq.direct","my_binding_key","my_queue",p);
return 0;
}
sender.cpp
#include "MyRmq.h"
#include <iostream>
int main()
{
char * resp = NULL;
MyRmq MyRmq("guest","guest","0.0.0.0",5672);
// MyRmq.listen("amq.direct","my_binding_key","my_queue",process);
resp = MyRmq.send("amq.direct","my_binding_key","request...");
if (resp != NULL){
std::cout << "receive response: " << resp << std::endl;
free(resp);
}
return 0;
}
Makefile:
all: receiver.out sender.out
receiver.out: receiver.cpp MyRmq.cpp
g++ -std=c++11 receiver.cpp MyRmq.cpp utils.c -o receiver.out -w -g -lrabbitmq
sender.out: sender.cpp MyRmq.cpp
g++ -std=c++11 sender.cpp MyRmq.cpp utils.c -o sender.out -w -g -lrabbitmq
运行结果如下:
root@debian:~/kvm-esxi/test/MYRMQ# ./receiver.out
request: request...
response: process...response...
request: request...
response: process...response...
root@debian:~/kvm-esxi/test/MYRMQ# ./sender.out
publishing data:request...
result: 0
frame type:
channel: 1
method: AMQP_BASIC_DELIVER_METHOD
delivery: 1, exchanges: amq.gen-VDkHOlJ0QjEwxGRA-ZcumA�, routingkey: amq.gen-VDkHOlJ0QjEwxGRA-ZcumA�
content-type: application/json�
-----
00000000: 70 72 6F 63 65 73 73 2E : 2E 2E 72 65 73 70 6F 6E process...respon
00000010: 73 65 2E 2E 2E : se...
00000015:
receive response: process...response...�
root@debian:~/kvm-esxi/test/MYRMQ#