|
楼主 |
发表于 2009-8-25 15:33:50
|
显示全部楼层
回复 #3 modern 的帖子
这是服务器的
#include "ClientService.h"
#include "ace/Reactor.h"
int ClientService::open(void *p)
{
if(super::open(p) == -1)
return -1;
const int MAXHOSTNAMELEN=32;
ACE_TCHAR peer_name[MAXHOSTNAMELEN];
ACE_INET_Addr peer_addr;
// if(this->sock_.get_remote_addr(peer_addr)==0 &&
if(this->peer().get_remote_addr(peer_addr)==0 &&
peer_addr.addr_to_string(peer_name,MAXHOSTNAMELEN)==0)
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("Connection from %s\n"),
peer_name));
return 0;
// return this->reactor()->register_handler(this,ACE_Event_Handler::READ_MASK);
}
int ClientService::handle_input(ACE_HANDLE)
{
const size_t INPUT_SIZE =4096;
char buffer[INPUT_SIZE];
SSIZE_T recv_cnt,send_cnt;
// if((recv_cnt = this->sock_.recv(buffer,sizeof(buffer)))<=0)
if((recv_cnt = this->peer().recv(buffer,sizeof(buffer)))<=0)
{
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("Connection closed\n")));
return -1;
}
// write(1,buffer,recv_cnt);//服务器收到信息显示
// send_cnt = this->sock_.send(buffer,ACE_static_cast(size_t,recv_cnt));
send_cnt = this->peer().send(buffer,ACE_static_cast(size_t,recv_cnt));
if(send_cnt == recv_cnt)
return 0;
if(send_cnt==-1 && ACE_OS::last_error()!=EWOULDBLOCK)
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("%p\n"),
ACE_TEXT("send")),
0);
if(send_cnt==-1)
send_cnt=0;
ACE_Message_Block *mb;
size_t remaining = ACE_static_cast(size_t,(recv_cnt - send_cnt));
ACE_NEW_RETURN(mb,ACE_Message_Block(&buffer[send_cnt],remaining),-1);
int output_off=this->msg_queue()->is_empty();
ACE_Time_Value nowait(ACE_OS::gettimeofday());
if(this->putq(mb, &nowait) == -1)
{
ACE_ERROR((LM_ERROR,
ACE_TEXT("%p; discarding data\n"),
ACE_TEXT("enqueue failed")));
mb->release();
return 0;
}
if(output_off)
return this->reactor()->register_handler(this,ACE_Event_Handler::WRITE_MASK);
return 0;
}
int ClientService::handle_output(ACE_HANDLE)
{
ACE_Message_Block *mb = new ACE_Message_Block (30);
ACE_OS::sprintf(mb->wr_ptr(),"%s","hello");
ACE_Time_Value nowait(ACE_OS::gettimeofday());
while (-1 != this->getq (mb, &nowait))
{
ssize_t send_cnt = this->peer().send (mb->rd_ptr(),mb->length());
if(send_cnt == -1)
ACE_ERROR((LM_ERROR,
ACE_TEXT("%p\n"),
ACE_TEXT("send")));
else
mb->rd_ptr(ACE_static_cast (ssize_t, send_cnt));
if (mb->length () > 0)
{
this->ungetq (mb);
break;
}
mb->release();
}
return (this->msg_queue()->is_empty())? -1 : 0;
}
int ClientService::handle_close (ACE_HANDLE h, ACE_Reactor_Mask mask)
{
if (mask == ACE_Event_Handler::WRITE_MASK)
return 0;
/*
mask = ACE_Event_Handler::ALL_EVENTS_MASK |
ACE_Event_Handler::DONT_CALL;
this->reactor()->remove_handler(this,mask);
this->sock_.close();
this->output_queue_.flush();
delete this;
return 0;
*/
return super::handle_close(h, mask);
} |
|