2

Zaphoydbroadcast_server.cpp看起来像是 websocket 服务器的完美主干,它可以快速接受和发送消息以及与线程的连接以进行实际操作,因此不会中断通信。 https://github.com/zaphoyd/websocketpp/blob/experimental/examples/broadcast_server/broadcast_server.cpp

他的简单print_server.cpp示例很容易编译;但是,我遇到了一些编译错误broadcast_server.cpp

root@server:~# g++ -O3 broadcast_server.cpp -I ~/websocketpp-experimental/ -lboost_system
broadcast_server.cpp:126:37: error: 'owner_less' is not a member of 'std'
broadcast_server.cpp:126:37: error: 'owner_less' is not a member of 'std'
broadcast_server.cpp:126:70: error: 'con_list' was not declared in this scope
broadcast_server.cpp:126:70: error: template argument 2 is invalid
broadcast_server.cpp:126:18: warning: 'typedef' was ignored in this declaration [enabled by default]
broadcast_server.cpp:129:5: error: 'con_list' does not name a type
broadcast_server.cpp: In member function 'void broadcast_server::process_messages()':
broadcast_server.cpp:109:17: error: 'm_connections' was not declared in this scope
broadcast_server.cpp:112:17: error: 'm_connections' was not declared in this scope
broadcast_server.cpp:116:17: error: 'con_list' has not been declared
broadcast_server.cpp:116:36: error: expected ';' before 'it'
broadcast_server.cpp:117:22: error: 'it' was not declared in this scope
broadcast_server.cpp:117:27: error: 'm_connections' was not declared in this scope

我是 C++ 新手,这些可能是简单的问题,但我找不到解决方法。

'owner_less' is not a member of 'std'? http://en.cppreference.com/w/cpp/memory/owner_less

我猜template argument 2 error当上述问题解决后,它就会消失。

'con_list' was not declared in this scope什么时候在里面private:?与'm_connections'

我猜'con_list' has not been declared当上述错误发生时,它会消失。

我不知道它在说什么,也不知道为什么它前面'it'应该有一个。;

版本

Ubuntu 12.10

gcc --version gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2

提升 1.53.0

额外#include

#include <cstdlib>
#include <iostream>
#include <memory>
#include <set>

依然没有

-std=c++11

In file included from /usr/include/c++/4.7/map:60:0,
                 from /usr/local/include/boost/date_time/gregorian/greg_month.hpp:18,
                 from /usr/local/include/boost/date_time/gregorian/greg_ymd.hpp:16,
                 from /usr/local/include/boost/date_time/gregorian/greg_calendar.hpp:16,
                 from /usr/local/include/boost/date_time/gregorian/gregorian_types.hpp:19,
                 from /usr/local/include/boost/date_time/posix_time/posix_time_config.hpp:18,
                 from /usr/local/include/boost/date_time/posix_time/posix_time_system.hpp:13,
                 from /usr/local/include/boost/date_time/posix_time/ptime.hpp:12,
                 from /usr/local/include/boost/date_time/posix_time/posix_time_types.hpp:12,
                 from /usr/local/include/boost/thread/thread_time.hpp:11,
                 from /usr/local/include/boost/thread/lock_types.hpp:18,
                 from /usr/local/include/boost/thread/pthread/thread_data.hpp:12,
                 from /usr/local/include/boost/thread/thread.hpp:17,
                 from /usr/local/include/boost/thread.hpp:13,
                 from /root/websocketpp-experimental/websocketpp/common/thread.hpp:41,
                 from /root/websocketpp-experimental/websocketpp/concurrency/basic.hpp:31,
                 from /root/websocketpp-experimental/websocketpp/config/core.hpp:35,
                 from /root/websocketpp-experimental/websocketpp/config/asio_no_tls.hpp:31,
                 from broadcast_server.cpp:1:
/usr/include/c++/4.7/bits/stl_tree.h: In instantiation of 'class std::_Rb_tree<boost::weak_ptr<void>, boost::weak_ptr<void>, std::_Identity<boost::weak_ptr<void> >, std::owner_less<boost::weak_ptr<void> >, std::allocator<boost::weak_ptr<void> > >':
/usr/include/c++/4.7/bits/stl_set.h:116:17:   required from 'class std::set<boost::weak_ptr<void>, std::owner_less<boost::weak_ptr<void> > >'
broadcast_server.cpp:132:14:   required from here
/usr/include/c++/4.7/bits/stl_tree.h:471:31: error: invalid use of incomplete type 'struct std::owner_less<boost::weak_ptr<void> >'
In file included from /usr/include/c++/4.7/bits/shared_ptr.h:52:0,
                 from /usr/include/c++/4.7/memory:87,
                 from /usr/local/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/include/boost/smart_ptr/shared_ptr.hpp:27,
                 from /usr/local/include/boost/shared_ptr.hpp:17,
                 from /usr/local/include/boost/date_time/time_clock.hpp:17,
                 from /usr/local/include/boost/thread/thread_time.hpp:9,
                 from /usr/local/include/boost/thread/lock_types.hpp:18,
                 from /usr/local/include/boost/thread/pthread/thread_data.hpp:12,
                 from /usr/local/include/boost/thread/thread.hpp:17,
                 from /usr/local/include/boost/thread.hpp:13,
                 from /root/websocketpp-experimental/websocketpp/common/thread.hpp:41,
                 from /root/websocketpp-experimental/websocketpp/concurrency/basic.hpp:31,
                 from /root/websocketpp-experimental/websocketpp/config/core.hpp:35,
                 from /root/websocketpp-experimental/websocketpp/config/asio_no_tls.hpp:31,
                 from broadcast_server.cpp:1:
/usr/include/c++/4.7/bits/shared_ptr_base.h:270:12: error: declaration of 'struct std::owner_less<boost::weak_ptr<void> >'

我是如何安装的(依赖项、实际程序等)

https://github.com/zaphoyd/websocketpp/wiki/Setup-0.3X-on-Ubuntu-12.10

欢迎替代品

如果有人有替代品std::owner_less,请张贴。

4

2 回答 2

5

std::owner_less 是一个 C++11 STL 功能,它允许您将 std::weak_ptr 放入 set 和 map 等关联容器中。最终,connection_hdl 是一个weak_ptr,这就是它需要自定义比较函数的原因。

WebSocket++ 0.3.x 可以通过预处理器定义配置为使用 C++03 + Boost 或 C++11。默认情况下,它使用 C++03 + Boost。Boost weak_ptr 的工作方式略有不同,不需要自定义比较函数。如果您使用列表、队列或向量,则也没有必要,因为这些容器不使用 connection_hdl 作为键。

0.3.x 手册中的大多数示例以及与库捆绑在一起的示例都使用 C++11,因为它更加简洁明了。要按原样编译和链接这些示例,您将需要一个 C++11 编译器并使用适当的 C++11 编译器标志和 WebSocket++ 定义。Scons 是我用来自动构建和运行所有示例和单元测试的构建系统。无需构建和运行单独的示例程序。任何其他构建系统都应该可以正常工作,在命令行上临时提供编译器参数也是如此。

我在手册中设置了一个页面,描述了启用/禁用 C++11 功能的各种选项。http://www.zaphoyd.com/websocketpp/manual/reference/cpp11-support。该手册页列出了我最常用于测试的操作系统和编译器的推荐编译器标志列表。

对于 Linux 上的 g++ 4.7,我建议:“-std=c++0x -D_WEBSOCKETPP_CPP11_STL_ -D_WEBSOCKETPP_NO_CPP11_REGEX_ -lboost_regex -lboost_system”。这启用了除库(损坏/错误)之外的所有 C++11 功能,并引入了 Boost 版本。

于 2013-03-18T03:26:18.157 回答
2

阅读文档后,我认为您需要BOOST_ROOT_CPP11在构建时定义。

类似于 BOOST_ROOT 但允许链接到针对 C++11 STL 编译的单独版本的 Boost

我基于以下标题

#ifdef _WEBSOCKETPP_CPP11_MEMORY_
    using std::shared_ptr;
    using std::weak_ptr;
    using std::enable_shared_from_this;
    using std::static_pointer_cast;

    typedef std::unique_ptr<unsigned char[]> unique_ptr_uchar_array;
#else
    using boost::shared_ptr;
    using boost::weak_ptr;
    using boost::enable_shared_from_this;
    using boost::static_pointer_cast;

    typedef boost::scoped_array<unsigned char> unique_ptr_uchar_array;
#endif

应该使用哪个std::weak_ptr而不是boost::weak_ptr


如果您想使用替代容器而不是std::set,请修改类似于此伪代码的示例

         if (a.type == SUBSCRIBE) {
                boost::unique_lock<boost::mutex> lock(m_connection_lock);
                m_connections.push_back(a.hdl);
            } else if (a.type == UNSUBSCRIBE) {
                boost::unique_lock<boost::mutex> lock(m_connection_lock);
                m_connections.erase(std::remove(m_connections.begin(), m_connections.end(), a.hdl));
            } else if (a.type == MESSAGE) {
                boost::unique_lock<boost::mutex> lock(m_connection_lock);

                con_list::iterator it;
                for (it = m_connections.begin(); it != m_connections.end(); ++it) {
                    m_server.send(*it,a.msg);
                }
            } else {
                // undefined.
            }
        }
    }
private:
    typedef std::deque<connection_hdl> con_list;

哪个将使用std::deque而不是std::set. 将不会使用排序,但据我所知,在此示例中不需要。

于 2013-03-15T14:35:12.307 回答