// ACE_Reactor_Client.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "ace/Reactor.h"
#include "ace/SOCK_Connector.h"
#include "ace/OS.h"
#include "ace/Log_Msg.h"
#include <string>
#include <iostream>
using namespace std;
class MyClient:public ACE_Event_Handler
{
public:
bool do_connect(string ip, int port, int local_port)
{
ACE_SOCK_Connector connector;
ACE_INET_Addr local_addr(local_port,"0.0.0.0");
ACE_INET_Addr addr(port,ip.c_str());
ACE_Time_Value timeout(5,0);
if(connector.connect(peer_sock,addr,&timeout, local_addr) != 0)
{
cout<<"connect fail."<<endl;
return false;
}
int ret = ACE_Reactor::instance()->register_handler(this,ACE_Event_Handler::READ_MASK);
if (ret != 0)
{
cout<<"local_port:"<<local_port<<" register_handler fail."<<endl;
return false;
}
sprintf(buf,"%d",local_port);
peer_sock.send(buf,strlen(buf)+1);
return true;
}
ACE_HANDLE get_handle(void) const
{
return peer_sock.get_handle();
}
int handle_input (ACE_HANDLE fd)
{
int rev=0;
ACE_Time_Value timeout(5,0);
if((rev=peer_sock.recv(buf,sizeof(buf),&timeout))>0)
{
buf[rev]='\0';
cout<<"recv: "<<buf<<endl;
}
ACE_INET_Addr raddr;
peer_sock.get_local_addr(raddr);
//ACE_DEBUG ((LM_DEBUG,ACE_TEXT ( " (%P|%t) close:%s %d\n " ),raddr.get_host_addr(),raddr.get_port_number()));
sprintf(buf,"%d",raddr.get_port_number());
peer_sock.send(buf,strlen(buf)+1);
return 0;
}
private:
ACE_SOCK_Stream peer_sock;
char buf[100];
};
#include <ace/OS.h>
#include <ace/Task.h>
class TTcpNetThread : public ACE_Task_Base
{
public:
/// 运行
int open();
/// 停止运行
int close();
protected:
/// 线程函数
virtual int svc();
};
int TTcpNetThread::open() { return this->activate(); }
int TTcpNetThread::close()
{
ACE_Reactor::instance()->end_reactor_event_loop(); // 终止ACE_Proactor循环
this->wait(); // 等待清理现场
return 0;
}
int TTcpNetThread::svc()
{
// Proactor的事件循环开始
while(!ACE_Reactor
ACE之Reactor模式使用实例
最新推荐文章于 2024-05-25 22:52:42 发布