Zaphoyd的broadcast_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
s
#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
的替代品,请发布。
答案 0 :(得分: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版本。
答案 1 :(得分:2)
阅读documentation后,我认为您需要在构建时定义BOOST_ROOT_CPP11
。
与BOOST_ROOT类似,但允许链接到单独的版本 针对C ++ 11 STL编译的Boost
我基于以下header
#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
。不会使用排序,但是我可以告诉我这个例子中不需要的。