下面是代码:- CTestA::CTestA()
- {
- pSoc = 0;
- }
- CTestA::~CTestA()
- {
- if (pSoc)
- {
- delete pSoc;
- pSoc = 0;
- }
- }
- bool CTestA::Connect()
- {
- ACE_Asynch_Connector<CTestB> vConnector;
- vConnector.open();
- ACE_INET_Addr peer_addr (8000, 192.168.1.10);
- if (vConnector.connect(peer_addr, (const ACE_INET_Addr &)ACE_Addr::sap_any, 1, this) == -1)
- {
- return false;
- }
- return true;
- }
- bool CTestA:isConnect()
- {
- if (!pSoc)
- return false;
- if (pSoc->handle() != ACE_INVALID_HANDLE)
- ACE_OS::closesocket (pSoc->handle());
- if (pSoc)
- {
- pSoc->m_pTestA = 0;
- delete pSoc;
- pSoc = 0;
- }
- return true;
- }
- void CTestA::MethodA(void* pSocket)
- {
- if (!pSocket) return;
- pSoc = (CTestB*) pSocket;
- ACE_OS::memset(cBuffer, 0, 1024);//cBuffer成员变量
- }
- void CTestA::MethodB()
- {
- if (!pSoc) return;
- ACE_CString str = "ddddddddddddddddddd";
- pSoc->ostWrite((char*)str.fast_rep(), str.length());
- }
- void CTestA::MethodC(char *pContent, int nSize)
- {
- //
- }
- void CTestA::CloseA()
- {
- pSoc = 0;
- }
- CTestB继承ACE_Service_Handler
- CTestB::CTestB()
- {
- m_pTestA = 0;
- m_pReaderMB = 0;
- }
- CTestB::~CTestB()
- {
- if (m_pTestA)
- {
- m_pTestA->CloseA();
- m_pTestA = 0;
- }
- if (this->handle() != ACE_INVALID_HANDLE)
- ACE_OS::closesocket (this->handle());
- if (m_pReaderMB)
- {
- m_pReaderMB->release();
- m_pReaderMB = 0;
- }
- }
- void CTestB::act (const void *pTest)
- {
- m_pTestA = (CTestA*)pTest;
- if (m_pTestA)
- m_pTestA->MethodA(this);
- }
- void CTestB:pen (ACE_HANDLE h, ACE_Message_Block&)
- {
- if (!m_pTestA)
- {
- delete this;
- return;
- }
- this->handle (h);
- if (this->m_Reader.open (*this) != 0 || this->m_Writer.open (*this) != 0)
- {
- delete this;
- return;
- }
- ACE_NEW_NORETURN (m_pReaderMB, ACE_Message_Block (PACKET_BUFFERSIZE));
- if (m_pTestA)
- m_pTestA->MethodB();
- PostRead();
- }
- void CTestB::handle_read_stream(const ACE_Asynch_Read_Stream::Result &result)
- {
- if (!result.success () || result.bytes_transferred () == 0)
- {
- delete this;
- return;
- }
- ACE_Message_Block &mb = result.message_block();
- if (m_pTestA)
- m_pTestA->MethodC(mb.rd_ptr(), mb.length());
- PostRead();
- }
- void CTestB::handle_write_stream(const ACE_Asynch_Write_Stream::Result &result)
- {
- result.message_block().release();
- if (!result.success () || result.bytes_transferred () == 0)
- {
- delete this;
- return;
- }
- return;
- }
- void CTestB:ostRead()
- {
- if (!m_pReaderMB) return;
- if (this->handle() == ACE_INVALID_HANDLE) return;
- m_pReaderMB->reset();
- if (this->m_Reader.read (*m_pReaderMB, m_pReaderMB->space()) != 0);
- }
- bool CTestB:ostWrite(char* pBuffer, int nSize)
- {
- if (this->handle() == ACE_INVALID_HANDLE) return false;
- ACE_Message_Block* pWriterMB = 0;
- ACE_NEW_RETURN (pWriterMB, ACE_Message_Block (1024), false);
- pWriterMB->copy(pBuffer, nSize);
- if (this->m_Writer.write (*pWriterMB, pWriterMB->length ()) != 0);
- return true;
- }
复制代码
[ 本帖最后由 winston 于 2007-12-25 15:16 编辑 ] |