示例#1
0
void EchoClientHandler::messageReceived(ChannelHandlerContext& ctx, const MessageEvent& e) {
    // Send back the received message to the remote peer.
    //transferredBytes.addAndGet(((ChannelBuffer) e.getMessage()).readableBytes());
    
    const ChannelBufferPtr& buffer = e.getMessage().value<ChannelBufferPtr>();
    if (buffer) {
        int readableBytes = buffer->readableBytes();

        ChannelBufferPtr tmp = buffer->readBytes();
        Channel& channel = e.getChannel();
        
        if (intervalTime == 0) {
            channel.write(tmp);
        }
        else {
            timer->newTimeout(boost::bind(&EchoClientHandler::delaySendMessage,
                                          this,
                                          _1,
                                          boost::ref(channel),
                                          tmp),
                              intervalTime);
        }

        printf("received message from %d at %s.\n", channel.getId().intValue(), boost::posix_time::to_simple_string(boost::get_system_time()).c_str());
    }
}