开发者

Creating static library that uses Boost ASIO and does not expose it

I'm trying to create a library that uses Boost ASIO (UDP multicast, asynchronous) and does not expose it. Basically I am following the async udp example, except I have made the io_service object a static private member of the library.

The code works fine if direct开发者_如何学运维ly compiled into an executable. If I try and make a static library and then use it inside an application, the code throws exceptions while constructing my class.

If anyone has examples or has created a library that uses Boost ASIO and does not expose it and could comment I'd greatly appreciate the help.

I've also tried making io_service a private member of the class and also tried passing it to the constructor. Everything I've tried so far has thrown exceptions.

Here is the example code for the library:

#ifdef _LIB
static boost::asio::io_service asio_service;
#endif

class udpframereader
{
public:
    udpframereader() : m_socket(asio_service)
    {
        m_packetCount = 0;
        ...
    }

    unsigned long long asio_error_count();
    ...

#ifdef _LIB
private:  
    void handle_receive(const boost::system::error_code& error, std::size_t bytes_transferred);

    boost::asio::ip::udp::socket   m_socket;
    boost::asio::ip::udp::endpoint m_remote_endpoint;
    boost::array<char, 4096>       m_buffer;
    boost::crc_ccitt_type          m_crc;

    unsigned long long m_packetCount;
    ...
#endif
};


You cannot change the class's definition between the library and the application like you show; this would result in (among other things) the application and library disagreeing about the size of the object, causing buffer overflows when you try to initialize these objects.

If you want to hide the private implementation of the object, use the pImpl idiom, and do not expose the implementation class to the application at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜