psycheqiqi 发表于 2009-8-25 14:42:34

ACE_Message_Block的问题

ACE程序员 第7章P141
int Client::handle_output(ACE_HANDLE)
{

ACE_Message_Block *mb;
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();
}
if(this->msg_queue()->is_empty())
this->reactor()->cancel_wakeup(this, ACE_Event_Handler::WRITE_MASK);
else
this->reactor()->schedule_wakeup(this, ACE_Event_Handler::WRITE_MASK);


return 0;
}
如何把ACE_Message_Block *mb初始化来把数据从客户端发送给服务器
ACE_Message_Block *mb = new ACE_Message_Block (30);
   ACE_OS::sprintf(mb->wr_ptr(),"%s","hello");
或者
char buffer;
   ACE_Message_Block *mb = new ACE_Message_Block (buffer,30);
都不行

psycheqiqi 发表于 2009-8-25 14:55:45

回复 #1 psycheqiqi 的帖子

有人没?........

modern 发表于 2009-8-25 15:03:30

ACE_Message_Block是ACE非常常用的一个类,
建议花一点时间看一下他的头文件声明。
另外C++NPV1有专门的章节讲他的用法,
强烈建议楼主先看书。

psycheqiqi 发表于 2009-8-25 15:29:06

回复 #3 modern 的帖子

看不明白
还是不知道在哪发数据

psycheqiqi 发表于 2009-8-25 15:32:59

回复 #3 modern 的帖子

这是客户端的代码
#include "Client.h"
//#pragma comment(lib,"ws2_32")
int Client::open (void *p)
{
        ACE_Time_Value iter_delay (2);
        if (super::open(p) == -1)
                return -1;
        this->notifier_.reactor (this->reactor());
        this->msg_queue ()->notification_strategy(&this->notifier_);
        return this->reactor()->schedule_timer(this, 0, ACE_Time_Value::zero, iter_delay);
}

int Client::handle_input (ACE_HANDLE)
{
        char buf;
        ssize_t recv_cnt = this->peer().recv (buf,sizeof(buf)-1);
        write(1,buf,recv_cnt);//客户端收到信息的显示
        if(recv_cnt > 0)
        {
                ACE_DEBUG ((LM_DEBUG,
                                ACE_TEXT("%*C"),
                                        ACE_static_cast (int, recv_cnt),
                                        buf));
                return 0;
        }
        if (recv_cnt == 0 || ACE_OS::last_error() != EWOULDBLOCK)
        {
                this->reactor()->end_reactor_event_loop();
                return -1;
        }
        return 0;
}

int Client::handle_timeout(const ACE_Time_Value &, const void *)
{
        if (this->iterations_>=ITERATIONS)
        {
                this->peer().close_writer();
                return 0;
        }
        ACE_Message_Block *mb;
        char msg;
        ACE_OS::sprintf (msg, "Iteration %d\n", this->iterations_);
        ACE_NEW_RETURN (mb, ACE_Message_Block (msg), -1);
        this->putq(mb);
        return 0;
}

int Client::handle_output(ACE_HANDLE)
{


        ACE_Message_Block *mb = new ACE_Message_Block (30);
        ACE_OS::sprintf(mb->wr_ptr(),"%s","hello world");

        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();
        }

        if(this->msg_queue()->is_empty())
                this->reactor()->cancel_wakeup(this, ACE_Event_Handler::WRITE_MASK);
        else
                this->reactor()->schedule_wakeup(this, ACE_Event_Handler::WRITE_MASK);
               
       
        return 0;

}


int ACE_TMAIN(int, ACE_TCHAR *[])
{
        ACE_INET_Addr port_to_connect ("HAStatus",ACE_LOCALHOST);
        ACE_Connector<Client,ACE_SOCK_CONNECTOR> connector;
        Client client;
        Client *pc = &client;
        if(connector.connect(pc,port_to_connect) == -1)
                ACE_ERROR_RETURN((LM_ERROR,
                            ACE_TEXT ("%p\n"),
                                                  ACE_TEXT ("connect")),1);
       
        ACE_Reactor::instance()->run_reactor_event_loop();
        return(0);
}

psycheqiqi 发表于 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;
        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;
        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,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);
}

psycheqiqi 发表于 2009-8-25 15:35:21

回复 #3 modern 的帖子

就是input和output两个函数的message_block不明白
如何写入客户端或者服务器给对方要发送的信息

winston 发表于 2009-8-25 20:15:52

我觉得,你最好先了解一下C++网络编程卷1中,对ACE_Message_Block的描述。
4.2节

winston 发表于 2009-8-25 20:16:00

4.2 The ACE_Message_Block ClassMotivationMany networked applications require a means to manipulate messages efficiently . Standard message management operations include
[*]Storing messages in buffers as they are received from the network or from other processes on the same host
[*]Adding and/or removing headers and trailers from messages as they pass through a user-level protocol stack
[*]Fragmenting and reassembling messages to fit into network maximum transmission units (MTUs)
[*]Storing messages in buffers for transmission or retransmission
[*]Reordering messages that were received out-of-sequence

winston 发表于 2009-8-25 20:17:18

看完了,我相信不用讲,你也明白how to do it 了。
页: [1] 2
查看完整版本: ACE_Message_Block的问题