3

我正在使用 C++ 开发一个负载测试应用程序,该应用程序旨在测试 - 除其他外 - 我们的音频服务器的流媒体功能。对于这个客户,我使用 websocketpp 并遵循这个例子

我总是在行中得到一个 sigsegv

con = endpoint.get_connection(connection_string);

我的 Workthread::Work 方法。

主应用程序在伪代码中看起来像这样

namespace blah
{
    TestmanagerClass::run()
    {
        start (additional) threads until limit n is reached;
        call workthread::work() method of each thread;
        handle finished threads;
        goto 1;
    }
}

这是 WorkThread::work(),我用伪代码替换了身份验证代码......

namespace blah {
void WorkThread::work()
{
    AudioStreamHandlerPtr handler(new AudioStreamHandler());
    websocketpp::client::connection_ptr con;
    websocketpp::client endpoint(handler);

    try
    {
        // authenticate
        // get login/access token from webservice

        if( !worksOutFine )
        {
            logError();
        }
        else
        { // got access token :D

            // now obtain session token from session service

            if( !worksOutFine )
            {
                logError();
            }
            else
            { // process this session's calls

                std::string connection_string = "ws://localhost";//boost::str(boost::format("ws://%1%/data?user_id=%2%&session_token=%3%") % serverAddressNoPort % session_.userId % sessionToken);
                std::cout << "CONNECTIONSTRING " << connection_string << std::endl;
                con = endpoint.get_connection(connection_string);
                std::cout << "1" << std::endl;
                endpoint.connect(con);
                std::cout << "2" << std::endl;
                boost::thread t(boost::bind(& websocketpp::client::run, &endpoint, false));
                more code....
}
}

连接字符串的输出是最后一个起作用的东西。“1”没有被输出,但出现了 sigsegv。

这是传递给端点构造函数的处理程序代码:

标题:

#ifndef __AUDIOSTREAMHANDLER_H__
#define __AUDIOSTREAMHANDLER_H__

#include <boost/shared_ptr.hpp>

#include <websocketpp/roles/client.hpp>
#include <websocketpp/websocketpp.hpp>

namespace blah
{
class AudioStreamHandler : public websocketpp::client::handler
{
    private:
        connection_ptr con_;

    public:
        AudioStreamHandler();
        virtual ~AudioStreamHandler();

        void on_fail(connection_ptr con);
        void on_open(connection_ptr con);
        void on_close(connection_ptr con);
        void on_message(connection_ptr con, message_ptr msg);
        void send(const std::string &msg);
        void close();
};

typedef boost::shared_ptr<AudioStreamHandler> AudioStreamHandlerPtr;
}

#endif  //__AUDIOSTREAMHANDLER_H__

执行:

#include "AudioStreamHandler.h"

namespace blah
{
AudioStreamHandler::AudioStreamHandler()
{
}

AudioStreamHandler::~AudioStreamHandler()
{
}

void AudioStreamHandler::on_fail(connection_ptr con)
{
    std::cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>> WebSocket connection failed <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << std::endl;
}

void AudioStreamHandler::on_open(connection_ptr con)
{
    con_ = con;
    std::cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>> WebSocket connection established <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << std::endl;
}

void AudioStreamHandler::on_close(connection_ptr con)
{
    con_ = websocketpp::client::connection_ptr();
    std::cout << "WebSocket connection terminated" << std::endl;
}

void AudioStreamHandler::on_message(connection_ptr con, message_ptr msg)
{
    std::cout << "Got WebSocket message: " << msg->get_payload() << std::endl;
}

void AudioStreamHandler::send(const std::string &msg)
{
    if( con_ ) {
        std::cerr << "Error: no connected session" << std::endl;
        return;
    }

    if( msg == "/close" ) {
        close();
    } else {
        con_->send(msg);
    }
}

void AudioStreamHandler::close() {
    if( !con_ ) {
        std::cerr << "Error: no connected session" << std::endl;
        return;
    }
    con_->close(websocketpp::close::status::GOING_AWAY,"");
}
}

最后是我从 gdb 获得的堆栈跟踪:

(gdb) where
#0  0x00000000004aeabd in boost::re_detail::perl_matcher<char const*,         std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char,   boost::cpp_regex_traits<char> > >::match_all_states() ()
#1  0x00000000004af293 in boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_imp() ()
#2  0x00000000004afeb4 in bool boost::regex_match<char const*, std::allocator<boost::sub_match<char const*> >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(char const*, char const*, boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags) ()
#3  0x00000000004a9c47 in websocketpp::uri::uri(std::string const&) ()
#4  0x0000000000441254 in websocketpp::role::client<websocketpp::endpoint<websocketpp::role::client, websocketpp::socket::plain, websocketpp::log::logger> >::get_connection(std::string const&) (this=0x7ffff2501450)
at /usr/include/c++/4.4/bits/stl_vector.h:313
#5  0x0000000000426130 in blah::WorkThread::work (this=0x7c4250) at workthread.cpp:93
#6  0x000000000049658d in boost::_mfi::mf0<void, blah::WorkThread>::operator()(blah::WorkThread*) const (this=0x789cc8) at /usr/include/c++/4.4/ext/new_allocator.h:66
#7  0x0000000000495ffc in void boost::_bi::list1<boost::_bi::value<blah::WorkThread*> >::operator()<boost::_mfi::mf0<void, blah::WorkThread>, boost::_bi::list0>(boost::_bi::type<void>, boost::_mfi::mf0<void, blah::WorkThread>&, boost::_bi::list0&, int) (this=0x789cd8) at /usr/include/c++/4.4/ext/new_allocator.h:66
#8  0x00000000004957eb in boost::_bi::bind_t<void, boost::_mfi::mf0<void,   blah::WorkThread>, boost::_bi::list1<boost::_bi::value<blah::WorkThread*> > >::operator()() (this=0x800000) at /usr/include/c++/4.4/ext/new_allocator.h:66
#9  0x0000000000493a26 in boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, blah::WorkThread>, boost::_bi::list1<boost::_bi::value<blah::WorkThread*> > > >::run() (this=0x789b40)
at /usr/include/c++/4.4/ext/new_allocator.h:66
#10 0x00007ffff79b1693 in thread_proxy () from /usr/lib/libboost_thread.so.1.49.0
#11 0x00007ffff5ade9ca in start_thread (arg=<value optimized out>) at pthread_create.c:300
#12 0x00007ffff5ddbcdd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
#13 0x0000000000000000 in ?? ()

我已经尝试过使用 boost 的调试版本,但没有得到更多的错误信息。我是简单地使用“ws://localhost”还是真正的连接字符串/uri也没有关系。

有没有人遇到过同样的问题或者有人知道我在这里做错了什么?非常感谢任何帮助。感谢您花时间阅读这篇长文。

4

0 回答 0