【发布时间】:2015-02-22 05:51:28
【问题描述】:
我目前正在使用一个小型 servlet 通过 TCP 发送模拟数据,使用 boost::asio 作为网络部分。我已经设法在我的机器上的两个进程之间进行通信(简单的客户端是用 Python 编写的)。问题在于相同的数据一直通过套接字发送,而不是被更新。
我正在使用两个线程:一个运行模拟,创建数据,并使用当前数据更新服务器的连接对象。第二个运行服务器,并且每隔一段时间将当前数据写入套接字。我在这里创建了一个与您分享的最小示例(它使用 MSVC++ 12.0 编译,如果您想复制,我正在谈论的问题)。
tcp_server * server;
bool connected = false;
void runServer() {
try
{
boost::asio::io_service io_service;
server = new tcp_server(io_service);
connected = true;
io_service.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
void runSim() {
for (int i = 0; i < 1000; i++) {
if (connected)
server->setData("Current Message: " + std::to_string(i));
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread serverThread(runServer);
boost::thread simThread(runSim);
simThread.join();
serverThread.join();
return 0;
}
这里有两个类,TCP_Connection 和 TCP_Server。这些非常接近于现在在 boost 网站上的 boost::asio 教程中找到的那些。
class tcp_connection
: public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service)
{
return pointer(new tcp_connection(io_service));
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
message_ = make_daytime_string();
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write, shared_from_this()));
}
void setData(std::string msg) {
boost::unique_lock<boost::shared_mutex> msgLock(msgMutex, boost::try_to_lock);
if (msgLock.owns_lock()) {
message_ = msg;
}
}
private:
tcp_connection(boost::asio::io_service& io_service)
: socket_(io_service)
{
timer_ = new boost::asio::deadline_timer(io_service,boost::posix_time::milliseconds(250));
}
void handle_write()
{
boost::shared_lock<boost::shared_mutex> msgLock(msgMutex);
std::cout << "Writing to socket: " << message_ << std::endl;
boost::asio::write(socket_, boost::asio::buffer(message_));
timer_->expires_at(timer_->expires_at() + boost::posix_time::milliseconds(1500));
timer_->async_wait(boost::bind(&tcp_connection::handle_write, shared_from_this()));
}
tcp::socket socket_;
std::string message_;
int counter_;
boost::asio::deadline_timer * timer_;
boost::shared_mutex msgMutex;
};
class tcp_server
{
public:
tcp_server(boost::asio::io_service& io_service)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
{
start_accept();
}
void setData(std::string msg) {
if (current_connection != NULL) {
current_connection->setData(msg);
}
}
private:
void start_accept()
{
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service());
acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
current_connection = new_connection;
}
void handle_accept(tcp_connection::pointer new_connection,
const boost::system::error_code& error)
{
if (!error)
{
new_connection->start();
std::cout << "New Connection on 127.0.0.1" << std::endl;
}
start_accept();
}
tcp::acceptor acceptor_;
tcp_connection::pointer current_connection;
};
通过明智地使用 std::cout,我设法确定服务器线程正在从模拟线程获取数据,并且连接对象也正在传递(因为 setData() 方法正在在它应该调用的时候调用)。无论出于何种原因,连接的成员“message_”似乎没有更新。我也知道连接没有被重置或从“新连接”更新到控制台重新创建。
【问题讨论】:
-
您似乎将同步
boost::asio::write()与异步boost::asio::async_write()操作混合在一起。同样,async_wait()的完成处理程序为什么要在套接字上写入另一个数据流? -
好吧,我这样做是因为这是我可以弄清楚如何在传输之间设置一个固定时间段的唯一方法(async_wait 位于具有指定时间段的截止时间计时器上)。我的想法是让写入重复发生,数据由另一个线程更新,这就是完成处理程序启动另一个写入并等待的原因。
-
我应该补充一点,我只添加了使用阻塞 boost::asio::write() 的代码。 async_write() 来自我用作此应用程序基础的教程,如果需要或有理由仅使用其中一个,可以轻松更改为阻塞写入。
-
我建议你重新考虑你的设计。它可以通过仅使用异步操作来实现。摆脱同步写入和互斥锁,改用链。与
async_write相比,async_wait使用单独的完成处理程序。您需要maintain an outgoing message queue 以避免交错写入。您还应该检查提供给异步完成处理程序的错误参数。 -
是的,您需要确保在给定套接字上最多有一个写操作未完成。队列就是这样发生的。
标签: multithreading sockets boost tcp boost-asio