浏览 2663 次
锁定老帖子 主题:一个 helloword 程序
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-03-04
#include "ace/Asynch_IO.h" #include "ace/OS.h" #include "ace/Proactor.h" #include "ace/Asynch_Acceptor.h" #include "ace/Addr.cpp" #include "ace/Time_Value.cpp" #include "ace/String_Base_Const.cpp" #include "ace/OS_String.cpp" #include "ace/Synch_Options.cpp" //#include <mstcpip.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\n"), ACE_TEXT ("HA_Proactive_Service reader open"))); delete this; return; } if (this->writer_.open (*this) != 0 ) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("HA_Proactive_Service write 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\n"); 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\n",mb.rd_ptr()); /* if(this->writer_.write(mb,mb.length()) !=0) { ACE_OS::printf("Begin read fail\n"); delete this; return; }*/ mb.release(); //实现一个WEB helloword程序~ ACE_Message_Block *bb = new ACE_Message_Block(1000); bb->copy("HTTP/1.1 200 OK\r\n \ Date: Tue, 03 Mar 2009 09:27:26 GMT\r\n \ Server: BWS/1.0\r\n \ Content-Length: 50\r\n \ Content-Type: text/html\r\n \ Cache-Control: privateExpires: Tue, 03 Mar 2009 09:27:26 GMT\r\n \ Content-Encoding: text\r\n\r\n-hello world-<br> saving people ,saving the word"); if(this->writer_.write(*bb,bb->length()) !=0) { ACE_OS::printf("Begin read fail\n"); delete this; return; } ACE_Message_Block *nmb = new ACE_Message_Block(buffer,1024); if (this->reader_.read (*nmb, nmb->space ()) != 0) return; } virtual void handle_write_stream(const ACE_Asynch_Write_Stream::Result &result) { ACE_Message_Block &mb = result.message_block (); ACE_OS::printf("mb.release{ %s\n",mb.base()); mb.release(); return; } private: ACE_Asynch_Read_Stream reader_; ACE_Asynch_Write_Stream writer_; 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; } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |