ACE 전면 카메라 Proactor 모드 예제 프로그램

5107 단어
ACE 전면 카메라 Proactor 모드 예제 프로그램은 ACE 6.0.0 버전에서 테스트를 통과했습니다.
1. 서버측 코드(ProactorServer.cpp)
#include "ace/Message_Queue.h"
#include "ace/Asynch_IO.h"
#include "ace/Proactor.h"
#include "ace/Asynch_Acceptor.h"


class HA_Proactive_Service : public ACE_Service_Handler
{
public:
~HA_Proactive_Service ()
{
if (this->handle () != ACE_INVALID_HANDLE)
ACE_OS::closesocket (this->handle ());
}

virtual void open (ACE_HANDLE h, ACE_Message_Block&)
{
     this->handle (h);
     if (this->reader_.open (*this) != 0 )
     {
         ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p
"), ACE_TEXT ("HA_Proactive_Service open"))); delete this; return; } ACE_Message_Block *mb = new ACE_Message_Block(buffer,1024); if (this->reader_.read (*mb, mb->space ()) != 0) { ACE_OS::printf("Begin read fail
"); delete this; return; } return; } // virtual void handle_read_stream (const ACE_Asynch_Read_Stream::Result &result) { ACE_Message_Block &mb = result.message_block (); if (!result.success () || result.bytes_transferred () == 0) { mb.release (); delete this; return; } mb.copy(""); // '\0' ACE_OS::printf("rev:\t%s",mb.rd_ptr()); mb.release(); ACE_Message_Block *nmb = new ACE_Message_Block(buffer,1024); if (this->reader_.read (*nmb, nmb->space ()) != 0) return; } private: ACE_Asynch_Read_Stream reader_; char buffer[1024]; }; int main(int argc, char *argv[]) { int port=3000; ACE_Asynch_Acceptor<HA_Proactive_Service> acceptor; if (acceptor.open (ACE_INET_Addr (port)) == -1) return -1; while(true) ACE_Proactor::instance ()->handle_events (); return 0; }

2. 클라이언트 코드(ProactorClient.cpp)
#include "ace/Message_Queue.h"
#include "ace/Asynch_IO.h"
#include "ace/Proactor.h"
#include "ace/Asynch_Connector.h"
#include "ace/OS_NS_unistd.h"


class HA_Proactive_Service : public ACE_Service_Handler
{
public:
~HA_Proactive_Service ()
{
if (this->handle () != ACE_INVALID_HANDLE)
ACE_OS::closesocket (this->handle ());
}

virtual void open (ACE_HANDLE h, ACE_Message_Block&)
{
	char  time[100];

     this->handle (h);
     if (this->writer_.open (*this) != 0 )
     {
         ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p
"), ACE_TEXT ("HA_Proactive_Service open"))); delete this; return; } ACE_OS::printf("connceted"); for(int i=0;i<10;i++) // { ACE_OS::sleep(1); time_t now = ACE_OS::gettimeofday().sec(); ctime_s(time, sizeof(time),&now); // ACE_Message_Block *mb = new ACE_Message_Block(100); mb->copy(time); if (this->writer_.write(*mb,mb->length()) !=0) { ACE_OS::printf("Begin read fail
"); delete this; return; } } return; } // virtual void handle_write_dgram (const ACE_Asynch_Write_Stream::Result &result) { ACE_Message_Block &mb = result.message_block (); mb.release(); return; } private: ACE_Asynch_Write_Stream writer_; }; int main(int argc, char *argv[]) { ACE_INET_Addr addr(3000,"localhost"); ACE_Asynch_Connector<HA_Proactive_Service> connector; connector.open(); if (connector.connect(addr) == -1) return -1; while(true) ACE_Proactor::instance ()->handle_events (); return 0; }

3. 실행 결과
서버측 실행 후 클라이언트측 실행 클라이언트측에서 클라이언트측 시간 정보를 받습니다.
rev:    Fri Jun 13 17:19:36 2014 rev:    Fri Jun 13 17:19:37 2014 rev:    Fri Jun 13 17:19:38 2014 rev:    Fri Jun 13 17:19:39 2014 rev:    Fri Jun 13 17:19:40 2014 rev:    Fri Jun 13 17:19:41 2014 rev:    Fri Jun 13 17:19:42 2014 rev:    Fri Jun 13 17:19:43 2014 rev:    Fri Jun 13 17:19:44 2014 rev:    Fri Jun 13 17:19:45 2014
참조:http://www.cnblogs.com/TianFang/archive/2006/12/31/608952.html

좋은 웹페이지 즐겨찾기