[boost.asio] echo server example

boost.asio async server example

#include <iostream>
#include <thread>
#include <functional>
#include <atomic>
#include <mutex>
#include <functional>
#include <vector>

#include <boost/asio.hpp>

const short PORT_NUM = 7707;
const short BLOG_SIZE = 5;
const short MAX_MSG_LEN = 256;

std::vector<std::thread> workers;
std::mutex mutexG;

using boost::asio::ip::tcp;

class session : public std::enable_shared_from_this<session>
{
public:
	session(tcp::socket socket) : socket_(std::move(socket))
	{

	}
	void start()
	{
		do_read();
	}
private:
	void do_read() {
		auto self(shared_from_this());
		socket_.async_read_some(boost::asio::buffer(data_, MAX_MSG_LEN),
			[this, self](boost::system::error_code ec, std::size_t length)
			{
				if (!ec) {
					{
						std::lock_guard<std::mutex> lock_guard(mutexG);
						std::stringstream ss;
						ss << std::this_thread::get_id();
						printf("[%d] recv: %s \n", ss.str().c_str(), data_);
					}
					do_write(length);
				}
			}
		);
	}
	void do_write(std::size_t length) {
		auto self(shared_from_this());
		boost::asio::async_write(socket_, boost::asio::buffer(data_, MAX_MSG_LEN),
			[this, self](boost::system::error_code ec, std::size_t /*length*/)
			{
				if (!ec) {
					do_read();
				}
			}
		);
	}
	tcp::socket socket_;
	char data_[MAX_MSG_LEN];
};
class server
{
public:
	server(boost::asio::io_context& io_context, short port) : acceptor_(io_context, tcp::endpoint(tcp::v4(), port)), socket_(io_context)
	{
		do_accept();
	}
private:
	void do_accept()
	{
		acceptor_.async_accept(socket_,
			[this](boost::system::error_code ec) {
				if (!ec) {
					{
						std::lock_guard<std::mutex> lock_guard(mutexG);
						std::stringstream ss;
						ss << std::this_thread::get_id();
						printf("[%d] accept: \n", ss.str().c_str());
					}
					std::make_shared<session>(std::move(socket_))->start();
				}
				do_accept();
			});
	}
	tcp::acceptor acceptor_;
	tcp::socket socket_;
};

void worker(boost::asio::io_context& io_context)
{
	io_context.run();
}
int main()
{
	std::stringstream ss;
	ss << std::this_thread::get_id();
	printf("[%s] main : \n", ss.str().c_str());

	boost::asio::io_context io_context_S;
	std::vector<std::thread> workers;
	server s(io_context_S, PORT_NUM);

	for (DWORD i = 0; i < 1; ++i) {
		workers.emplace_back(std::thread(&worker, std::ref(io_context_S)));
	}

	while (1) {

	}


	return 0;
}

소스 코드는 boost 기본 샘플을 기반해서 작성하였음


댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다