2019-07-28 10:31:56 +08:00
|
|
|
|
#include "lib_acl.h"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#ifndef WIN32
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <iostream>
|
2016-10-27 21:25:14 +08:00
|
|
|
|
#include "acl_cpp/acl_cpp_init.hpp"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include "acl_cpp/stream/aio_handle.hpp"
|
|
|
|
|
#include "acl_cpp/ipc/ipc_server.hpp"
|
|
|
|
|
#include "acl_cpp/ipc/ipc_client.hpp"
|
|
|
|
|
|
|
|
|
|
#define MSG_REQ 1
|
|
|
|
|
#define MSG_RES 2
|
|
|
|
|
#define MSG_STOP 3
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
class test_client1 : public acl::ipc_client
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2019-06-16 20:10:40 +08:00
|
|
|
|
test_client1(void) {}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
protected:
|
|
|
|
|
~test_client1(void) {}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
// @override
|
|
|
|
|
void on_open(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
this->append_message(MSG_RES);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
this->send_message(MSG_REQ, NULL, 0);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>첽<EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>Ϣ
|
2019-06-16 20:10:40 +08:00
|
|
|
|
this->wait();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
// @override
|
|
|
|
|
void on_close(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
// @override
|
|
|
|
|
void on_message(int nMsg, void* data, int dlen)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
(void) data;
|
|
|
|
|
(void) dlen;
|
|
|
|
|
std::cout << "test_client1 on message:" << nMsg << std::endl;
|
|
|
|
|
this->send_message(MSG_STOP, NULL, 0);
|
|
|
|
|
this->delete_message(MSG_RES);
|
|
|
|
|
this->get_handle().stop();
|
|
|
|
|
this->close();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
static bool client_main(acl::aio_handle* handle, const char* addr)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::ipc_client* ipc = new test_client1();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!ipc->open(handle, addr, 0)) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "open " << addr << " error!" << std::endl;
|
|
|
|
|
delete ipc;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return false;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void* thread_callback(void *ctx)
|
|
|
|
|
{
|
|
|
|
|
const char* addr = (const char*) ctx;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::aio_handle handle;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!client_main(&handle, addr)) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
handle.check();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return NULL;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>Ϣѭ<CFA2><D1AD>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
while (true) {
|
|
|
|
|
if (!handle.check()) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
break;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handle.check();
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return NULL;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
class test_client2 : public acl::ipc_client
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2019-06-16 20:10:40 +08:00
|
|
|
|
test_client2(void) {}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
protected:
|
|
|
|
|
~test_client2(void) {}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
// @override
|
|
|
|
|
void on_close(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
// @override
|
|
|
|
|
void on_message(int nMsg, void* data, int dlen)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
(void) data;
|
|
|
|
|
(void) dlen;
|
|
|
|
|
|
|
|
|
|
std::cout << "test_client2 on message:" << nMsg << std::endl;
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (nMsg == MSG_STOP) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
this->close();
|
|
|
|
|
this->get_handle().stop();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
} else {
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>Ӧ<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
this->send_message(MSG_RES, NULL, 0);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
class test_server : public acl::ipc_server
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2019-06-16 20:10:40 +08:00
|
|
|
|
test_server(void) {}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
~test_server(void) {}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
protected:
|
|
|
|
|
// @override
|
|
|
|
|
void on_accept(acl::aio_socket_stream* client)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::ipc_client* ipc = new test_client2();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>첽IPC<50><43><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
ipc->open(client);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
ipc->append_message(MSG_REQ);
|
|
|
|
|
ipc->append_message(MSG_STOP);
|
|
|
|
|
ipc->wait();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
|
{
|
|
|
|
|
printf("usage: %s -h[help] -t[use thread]\n", procname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
int ch;
|
|
|
|
|
bool use_thread = false;
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
while ((ch = getopt(argc, argv, "ht")) > 0) {
|
|
|
|
|
switch (ch) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
case 'h':
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return (0);
|
|
|
|
|
case 't':
|
|
|
|
|
use_thread = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::acl_cpp_init();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::aio_handle handle;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::ipc_server* server = new test_server();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ʹ<><CAB9>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 127.0.0.1 <20>ĵ<EFBFBD>ַ
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!server->open(&handle, "127.0.0.1:0")) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
delete server;
|
|
|
|
|
std::cout << "open server error!" << std::endl;
|
|
|
|
|
getchar();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return 1;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char addr[256];
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
_snprintf(addr, sizeof(addr), "%s", server->get_addr());
|
|
|
|
|
#else
|
|
|
|
|
snprintf(addr, sizeof(addr), "%s", server->get_addr());
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (use_thread) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl_pthread_t tid;
|
|
|
|
|
acl_pthread_create(&tid, NULL, thread_callback, addr);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
} else {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_main(&handle, addr);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
while (true) {
|
|
|
|
|
if (!handle.check()) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "stop now!" << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete server;
|
|
|
|
|
handle.check();
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
std::cout << "server stopped, enter any key to exit ..." << std::endl;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
getchar();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return 0;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|