- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先我问了这个 Running a function on the main thread from a boost thread and passing parameters to that function
所以现在我正在尝试这个:
下面是一个控制台c++项目,我完美模拟了我的大项目
TestServicePost.cpp
#include "stdafx.h"
#include "SomeClass.h"
int _tmain(int argc, _TCHAR* argv[])
{
SomeClass* s = new SomeClass();
while(true)
{
s->update();
}
return 0;
}
#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <queue>
class ServiceNote
{
public:
std::string getType()
{
std::stringstream typeSS;
typeSS << "LamasaTech.MultiWall.PostNote." << (NoteType.compare("Normal") == 0 ? "Node" : "Header") << "." << Shape << "." << Colour;
return typeSS.str();
}
int Action;
int CNoteId;
std::string Colour;
int NoteId;
std::string NoteType;
int SessionId;
std::string Shape;
std::string Style;
std::string Text;
int X;
int Y;
};
class SomeClass
{
public:
SomeClass();
~SomeClass();
void update();
private:
std::queue<ServiceNote> pendingNotes;
void addToQueue(ServiceNote sn);
void pollService(boost::asio::io_service* svc);
int getMessage(boost::asio::io_service* svc, std::string sessionId, int messageId);
boost::thread servicePoller;
};
#include "stdafx.h"
#include "SomeClass.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/asio/signal_set.hpp>
#define POLL_SERVICE = 0;
#define POLLING_WAIT_TIME 1000
#define SAVE_SESSION_EVERY 1800000
SomeClass::SomeClass()
{
boost::asio::io_service io_servicePoller;
io_servicePoller.run();
servicePoller = boost::thread(boost::bind(&SomeClass::pollService, this, &io_servicePoller));
/*boost::asio::io_service io_sessionSaver;
boost::asio::signal_set signalsSaver(io_sessionSaver, SIGINT, SIGTERM);
signalsSaver.async_wait( boost::bind(&boost::asio::io_service::stop, &io_sessionSaver));
sessionSaver = boost::thread(&SomeClass::saveSessionEvery, io_sessionSaver);*/
}
SomeClass::~SomeClass()
{
}
void SomeClass::update()
{
while(!pendingNotes.empty())
{
ServiceNote sn = pendingNotes.front();
pendingNotes.pop();
}
}
void SomeClass::addToQueue(ServiceNote sn)
{
pendingNotes.push(sn);
}
void SomeClass::pollService(boost::asio::io_service* svc)
{
int messageId = 1;
while(true)
{
if(boost::this_thread::interruption_enabled() && boost::this_thread::interruption_requested())
return;
int currentId = messageId;
messageId = getMessage(svc, "49", messageId);
if(currentId == messageId)
boost::this_thread::sleep(boost::posix_time::milliseconds(POLLING_WAIT_TIME));
}
}
int SomeClass::getMessage(boost::asio::io_service* svc, std::string sessionId, int messageId)
{
try
{
boost::asio::io_service io_service;
// Get a list of endpoints corresponding to the server name.
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query("mw.rombus.com", "http");
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
// Try each endpoint until we successfully establish a connection.
boost::asio::ip::tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
// Form the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << "GET " "/Service.svc/message/" << sessionId << "/" << messageId << " HTTP/1.0\r\n";
request_stream << "Host: " << "mw.rombus.com" << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
// Send the request.
boost::asio::write(socket, request);
// Read the response status line. The response streambuf will automatically
// grow to accommodate the entire line. The growth may be limited by passing
// a maximum size to the streambuf constructor.
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");
// Check that response is OK.
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
//std::cout << "Invalid response\n";
return messageId;
}
if (status_code != 200)
{
//std::cout << "Response returned with status code " << status_code << "\n";
return messageId;
}
// Read the response headers, which are terminated by a blank line.
boost::asio::read_until(socket, response, "\r\n\r\n");
// Process the response headers.
std::string header;
std::string fullHeader = "";
while (std::getline(response_stream, header) && header != "\r")
fullHeader.append(header).append("\n");
// Write whatever content we already have to output.
std::string fullResponse = "";
if (response.size() > 0)
{
std::stringstream ss;
ss << &response;
fullResponse = ss.str();
try
{
boost::property_tree::ptree pt;
boost::property_tree::read_json(ss, pt);
ServiceNote sn;
sn.Action = pt.get<int>("Action");
sn.CNoteId = pt.get<int>("CNoteId");
sn.Colour = pt.get<std::string>("Colour");
sn.NoteId = pt.get<int>("NoteId");
sn.NoteType = pt.get<std::string>("NoteType");
sn.SessionId = pt.get<int>("SessionId");
sn.Shape = pt.get<std::string>("Shape");
sn.Style = pt.get<std::string>("Style");
sn.Text = pt.get<std::string>("Text");
sn.X = pt.get<int>("X");
sn.Y = pt.get<int>("Y");
svc->post(boost::bind(&SomeClass::addToQueue, this, sn));
//pendingNotes.push(sn);
}
catch (std::exception const& e)
{
std::string test = e.what();
//std::cerr << e.what() << std::endl;
}
messageId++;
}
// Read until EOF, writing data to output as we go.
std::string fullSth = "";
boost::system::error_code error;
while (boost::asio::read(socket, response,
boost::asio::transfer_at_least(1), error))
{
std::ostringstream ss;
ss << &response;
fullSth = ss.str();
}
if (error != boost::asio::error::eof)
throw boost::system::system_error(error);
}
catch (std::exception& e)
{
std::string test = e.what();
std::cout << "Exception: " << e.what() << "\n";
}
return messageId;
}
Unhandled exception at 0x771215de in TestServicePost.exe: 0xC0000005: Access violation writing location 0xcccccce4.
, 在此行执行之后:
svc->post(boost::bind(&SomeClass::addToQueue, this, sn));
~SomeClass()
, 也很感激这方面的帮助
最佳答案
在 SomeClass
构造函数,您实际上执行以下操作:
io_service
实例。 run()
成员函数,which returns immediately ,因为 io_service 没有工作。 io_service::run()
是一种“消息循环”,所以它应该阻塞调用线程。不要在对象构造函数中调用它。
关于boost - 使用 boost::asio::io_service::post(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13785640/
我有一个 ip::udp::socket用 io_service 构建.只有一个 boost::thread调用 io_service::run()方法,以及 io_service::work 的一个
我为异步 io_service 工作运行 aync 作业线程。 我想销毁用于异步作业的资源。 boost::asio::io_service boost::asio::io_service::work
我有一个 boost::asio 线程池,它有一个 asio::io_service和 asio::work : boost::asio::io_service m_service; boost::a
我正在编写多线程 TCP 服务器,基于应用程序设计,我需要有多个线程,每个线程都有 io_service。 通过该设计,我需要接受来自一个Thread/io_service 的连接,进行身份验证过程(
如果我io_service::run()只在一个线程中运行,那么io_service::post()调用是在我要求它们执行的顺序相同,或者它们可以按任意顺序执行,我仍然需要使用 strand 来强制序
boost::asio::io_service m_io_service; boost::asio::io_service::work m_work(m_io_service); m_io_servi
长话短说,我的代码: #include #include #include #include #include #include namespace ba = boost::asio; n
我的简化问题 我阅读了this thread,并试图删除io_service对象。我做这个 m_IO.stop(); m_IO.~io_service(); m_IO是boost::asio::io_
我使用 boost::asio::io_service 来处理 Foo 中的工作。现在它工作正常,直到我退出 ~Foo 并卡在 io_service 析构函数(在 service_registry 的
我想在里面的类中周期性地制作一个10秒的计时器,但是它不起作用。它会在 10 秒内第一次打印计数。但在那之后,它不会再等待 10 秒。具体代码。 #include #include #includ
我正在尝试运行 udp 服务器。问题是阻塞了 io_service 上的 run() 调用。所以我决定使用 boost bind 在其他线程上运行这个方法。结果主线程执行超出了 DictionaryI
我一直在阅读 boost asio 文档,特别是关于 Proactor 设计的。 当我看到这个标题的时候 前摄器设计模式:无线程的并发 我的理解是,asio 的用户不需要创建额外的线程来执行异步 IO
我想让程序等到它完成所有正在运行的线程,这与 ioService.stop(); 不同,后者无需等待即可停止 ioService。我尝试了以下代码,它工作正常,但没有等待线程完成就停止了 ioServ
为什么在这个简单的类中,如果我直接使用 io.run() 函数将被调用,否则如果要求运行到其他线程,打印将不会被调用? #include #include #include using name
正在尝试学习 asio,我正在关注网站上的示例。 为什么需要 io_service 以及它的具体作用是什么?为什么我在执行异步操作时需要将它发送到几乎所有其他函数,为什么它不能在第一次“绑定(bind
我正在使用boost::io_service构建并行执行计算作业的线程池。有些作业不允许同时运行,我认为这是boost::io_service::strand的理想应用。作为,执行顺序作业的顺序与无关
我正在编写一个网关服务,它监听网络套接字并将接收到的数据包路由到单独的守护进程。我打算使用 boost asio,但我遇到了几个问题。这是我计划实现的服务器的设计: 网关将使用 boost asio
我正在尝试实现一个包含两个处理循环的 C++ 应用程序。当前,第一个处理循环(boost 的 io_service::run)阻止了第二个处理循环的执行。 利用线程或 std::async 方法的方法
我正在尝试学习 io_service 并使用共享指针。我希望代码无限地工作,直到我像这样调用 stop 方法或某事。不幸的是,在屏幕上看到 workHandler 的输出后,程序关闭了。任何人都可以解
我更喜欢使用 io_service 的方式是在应用程序启动时创建一个线程来执行 io_service的运行方法。问题是,如果 io_service 没有任务,它的 run 方法立即返回并且线程终止。
我是一名优秀的程序员,十分优秀!