ACE I/O事件多路分離在VS.net 2005中調試排錯

 //測試ACE I/O多路分離器
//原文中有幾處誤比,具體請參考註釋
//運行:
//1.運行該程序
//2.打開命令行,輸入:telnet 127.0.0.1 19998
//3.隨便輸入,然後回車,關閉命令窗口,察看該程序窗口。
#ifdef _DEBUG
#pragma  comment (lib,"ACEd.lib")
#else
#pragma  comment (lib,"ACE.lib")
#endif

#include 
"ace/OS.h"
#include 
"ace/Log_Msg.h"

#include 
"ace/Reactor.h"
#include 
"ace/SOCK_Acceptor.h"


#define PORT_NO 19998
typedef ACE_SOCK_Acceptor Acceptor;

//forward declaration
//class My_Accept_Handler;
class My_Input_Handler: public ACE_Event_Handler
{
public:
    
//Constructor
    My_Input_Handler(void)
    
{
        
//原代碼少一個右括號,如下:
        
//ACE_DEBUG((LM_DEBUG,"Constructor ");
        ACE_DEBUG((LM_DEBUG,"Constructor "));
    }

    
//Called back to handle any input received
    int handle_input(ACE_HANDLE)
    
{
        
//receive the data
        
//原文將peer_i()誤寫爲peer()
        
//如下:
        
//peer().recv_n(data,12);
        peer_i().recv_n(data,12);
        ACE_DEBUG((LM_DEBUG,
"%s ",data));
        
// do something with the input received.
        
// ...
        
//keep yourself registered with the reactor
        return 0;
    }

    
//Used by the reactor to determine the underlying handle
    ACE_HANDLE get_handle(voidconst
    
{
        
//原文調用this->peer_i()編譯錯誤原因
        
//該調用爲非const的
        
//可改爲直接調用
        return this->peer_.get_handle();
    }

    
//Returns a reference to the underlying stream.
    ACE_SOCK_Stream &peer_i()
    
{
        
return this->peer_;
    }

private:
    ACE_SOCK_Stream peer_;
    
char data [12];
}
;
class My_Accept_Handler: public ACE_Event_Handler
{
public:
    
//Constructor
    My_Accept_Handler(ACE_Addr &addr)
    
{
        
this->open(addr);
    }

    
//Open the peer_acceptor so it starts to "listen"
    
//for incoming clients.
    int open(ACE_Addr &addr)
    
{
        peer_acceptor.open(addr);
        
return 0;
    }

    
//Overload the handle input method
    int handle_input(ACE_HANDLE handle)
    
{
        
//Client has requested connection to server.
        
//Create a handler to handle the connection
        My_Input_Handler *eh= new My_Input_Handler();
        
//Accept the connection "into" the Event Handler
        
//以下原文將peer_i()誤寫爲peer()
        if (this->peer_acceptor.accept(eh->peer_i(), // stream
            0// remote address
            0// timeout
            1==-1//restart if interrupted
            ACE_DEBUG((LM_ERROR,"Error in connection "));
        ACE_DEBUG((LM_DEBUG,
"Connection established "));
        
//Register the input event handler for reading
        ACE_Reactor::instance()->
            register_handler(eh,ACE_Event_Handler::READ_MASK);
        
//Unregister as the acceptor is not expecting new clients
        return -1;
    }

    
//Used by the reactor to determine the underlying handle
    ACE_HANDLE get_handle(voidconst
    
{
        
return this->peer_acceptor.get_handle();
    }

private:
    Acceptor peer_acceptor;
}
;


int run_main (int argc, ACE_TCHAR *argv[]);

int
ACE_TMAIN (
int argc, ACE_TCHAR *argv[])
{
    
return run_main (argc, argv);
}

int run_main (int argc, ACE_TCHAR *argv[])
{
    
//Create an address on which to receive connections
    ACE_INET_Addr addr(PORT_NO);
    
//Create the Accept Handler which automatically begins to “listen"
    
//for client requests for connections
    My_Accept_Handler *eh=new My_Accept_Handler(addr);
    
//Register the reactor to call back when incoming client connects
    ACE_Reactor::instance()->register_handler(eh,
        ACE_Event_Handler::ACCEPT_MASK);
    
//Start the event loop
    while(1)
    
{
        ACE_Reactor::instance()
->handle_events();
    }

    
return 0;
}
發佈了13 篇原創文章 · 獲贊 4 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章