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