提升异步服务器缓冲区错误

时间:2015-02-13 15:04:52

标签: c++ boost boost-asio

我正在尝试制作服务器&促进asio的客户。目前我收到此错误。你能指出我做错了吗?

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>


// Forward declaration
class ASyncConnectionMT;
class ASyncEchoServerMT;

// Typedef for the buffer type (shared_ptr)
typedef boost::array < char , 65536 > Buffer;
typedef boost::shared_ptr < Buffer > BufferPtr;

// Typedef for the ASyncConnectionMT shared_ptr
// Typedef for the ASyncEchoServerMT shared_ptr
// Derived from "enable_shared_from_this" so the 'this' object can
// be passed as shared_ptr to the callback function
typedef boost::shared_ptr < ASyncConnectionMT > ASyncConnectionMTPtr;
typedef boost::shared_ptr < ASyncEchoServerMT > ASyncEchoServerMTPtr;


// Class the handles the client
class ASyncConnectionMT : public::boost::enable_shared_from_this<ASyncConnectionMT>
{

private:    // The socket class for communication.
            boost::asio::ip::tcp::socket m_socket;

            // Strand object to synchronise calling handlers. Multiple threads might acces the socked
            // at the same time since send and recieve are started asynchronously at the same time
            boost::asio::strand m_strand;

public:     // Constructor with the IO service to use
            ASyncConnectionMT ( boost::asio::io_service& ios) : m_socket(ios), m_strand (ios)
            {
            }

            // Retrieve the socked used by this connection
            // Need to be passed to acceptor.accept() function
            boost::asio::ip::tcp::socket& Socket()
            {
                return m_socket;
            }

            // Start handling the connection
            void Start()
            {
                std:: cout << m_socket.remote_endpoint() << ": Connection accepted" << std:: endl ;
                StartReceiving() ;
            }

            // Start receiving data
            void StartReceiving()
            {
                // Create receive buffer
                BufferPtr receiveBuffer ( new Buffer ) ;

                // Start async read, must pass 'this' as shared_ptr, else the 
                // 'this' object will be destroyed after leaving this function
                async_write ( m_socket,  boost::asio::buffer ( *receiveBuffer ) , m_strand.wrap ( boost::bind ( &ASyncConnectionMT::HandleReceived, shared_from_this() ,
                    receiveBuffer , boost::asio::placeholders::error , 
                    boost::asio::placeholders::bytes_transferred ) ) ) ;
            }


            // Handle received data
            void HandleReceived ( BufferPtr receiveBuffer , const boost::system::error_code& ec , size_t size)
            {

                if (!ec)
                {
                    // Print received message
                    std:: cout << m_socket.remote_endpoint() << ": Message received: " << std:: string (receiveBuffer -> data() , size ) << std:: endl ; 

                    // Convert to uppercare. We can't use the same buffer because that could be
                    // overwritten by another recieve

                    // UPD -> boost shared_ptr<TBuffer> sendBuffer(new TBuffer());
                    BufferPtr sendBuffer ( new Buffer ) ;
                    for ( size_t i=0 ; i!=size ; i++ )
                    {
                        (( &sendBuffer )[i]) = toupper (( &receiveBuffer )[i]) ; 
                    }

                    // Start sending reply, must pass 'this' as shared_ptr, else the 'this' object will be
                    // destroyed after leaving this function. We pass the buffer as shared_ptr to the handler
                    // so the buffer is still in memory after sending is complete. Without it, the buffer could
                    // be deleted before the send operation is complete. The Handle Set is now synchronised via the strand.
                    async_write ( m_socket,  boost::asio::buffer ( *sendBuffer , size ) , m_strand.wrap ( boost::bind ( &ASyncConnectionMT::HandleSent ,
                                                                                            shared_from_this() , sendBuffer , 
                                                                                            boost::asio::placeholders::error ,
                                                                                            boost::asio::placeholders::bytes_transferred ) ) ) ;

                    // Start receiving next bit

                    StartReceiving() ;

                }

                else if ( ec == boost::asio::error::eof)
                {
                    // Client disconnected. Close the socket.
                    std:: cout << m_socket.remote_endpoint() << ": Connection closed ( handle received )" << std:: endl;
                    m_socket.close();
                }

            }

            // Handle for when the data si sent
            void HandleSent ( BufferPtr sendBuffer , const boost::system::error_code& ec , size_t size) 
            {

                if (!ec)
                {
                    // Start receiving again
                    StartReceiving() ;

                }

                else if ( ec == boost::asio::error::eof)
                {
                    // Client disconnected. Close the socket.
                    std:: cout << m_socket.remote_endpoint() << ": Connection closed ( handle received )" << std:: endl;
                    m_socket.close();
                }

                else
                {
                    std:: cout << "Error: " << ec.message << std:: endl ; 
                }
            }

};

我收到以下错误。

  • C3967 - boost :: system :: error_code :: message':函数调用缺少参数列表;使用'&amp;'boost :: system :: error_code :: message'创建指向成员的指针
  • C2664 - 'toupper':无法将参数1从'BufferPtr'转换为'int'
  • C2512 - 'boost :: array':没有合适的默认构造函数
  • C2039 - 'data':不是'boost :: shared_prt'
  • 的成员
  • C2027 - 使用未定义类型'boost :: array'

  • 不允许指向不完整类类型的指针

  • 从“BufferPtr”到“int”存在合适的转换函数
  • 不允许使用不完整类型

提前致谢。

1 个答案:

答案 0 :(得分:1)

  1. 你错过了包含

    #include <boost/array.hpp>
    
  2. toupper循环错误

    ((&sendBuffer)[i]) = toupper((&receiveBuffer)[i]);
    

    应该更像

    ((*sendBuffer)[i]) = toupper((*receiveBuffer)[i]);
    

    甚至更喜欢

    std::transform(receiveBuffer->begin(), receiveBuffer->end(), sendBuffer->begin(), static_cast<int(&)(int)>(std::toupper));
    
      

    专家提示: 考虑在缓冲区中使用unsigned char,以避免在传递int to_upper

    时出现不需要的符号扩展
    ((*sendBuffer)[i]) = toupper(static_cast<unsigned char>((*receiveBuffer)[i]));
    
  3. 就像注释(和编译器消息)所说:

            std::cout << "Error: " << ec.message << std::endl;
    

    应该是

            std::cout << "Error: " << ec.message() << std::endl;
    
  4. 然后编译。不能保证它能达到您的预期(我还没有读过代码)