1. 程式人生 > >boost asio非同步讀寫網路聊天室【官方示例】

boost asio非同步讀寫網路聊天室【官方示例】

//
// chat_message.hpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma warning(disable:4996)
#ifndef CHAT_MESSAGE_HPP
#define CHAT_MESSAGE_HPP

#include <cstdio>
#include <cstdlib>
#include <cstring>

class chat_message
{
public:
	enum { header_length = 4 };
	enum { max_body_length = 512 };

	chat_message()
		: body_length_(0)
	{
	}

	const char* data() const
	{
		return data_;
	}

	char* data()
	{
		return data_;
	}

	size_t length() const
	{
		return header_length + body_length_;
	}

	const char* body() const
	{
		return data_ + header_length;
	}

	char* body()
	{
		return data_ + header_length;
	}

	size_t body_length() const
	{
		return body_length_;
	}

	void body_length(size_t length)
	{
		body_length_ = length;
		if (body_length_ > max_body_length)
			body_length_ = max_body_length;
	}

	bool decode_header()
	{
		using namespace std; // For strncat and atoi.
		char header[header_length + 1] = "";
		strncat(header, data_, header_length);
		body_length_ = atoi(header);
		if (body_length_ > max_body_length)
		{
			body_length_ = 0;
			return false;
		}
		return true;
	}

	void encode_header()
	{
		using namespace std; // For sprintf and memcpy.
		char header[header_length + 1] = "";
		sprintf(header, "%4d", body_length_);
		memcpy(data_, header, header_length);
	}

private:
	char data_[header_length + max_body_length];
	size_t body_length_;
};

#endif // CHAT_MESSAGE_HPP

服務端:server.cpp

// server.cpp : 定義控制檯應用程式的入口點。
// 聊天室程式,支援多個client相互聊天

#include "stdafx.h"

//
// chat_server.cpp
// ~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <algorithm>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <list>
#include <set>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include "chat_message.hpp"

using boost::asio::ip::tcp;
using namespace std;

//----------------------------------------------------------------------

typedef std::deque<chat_message> chat_message_queue;

//----------------------------------------------------------------------

// 一個抽象類,用於提供聊天室成員的介面
class chat_participant //  聊天參與者
{
public:
	virtual ~chat_participant() {}
	virtual void deliver(const chat_message& msg) = 0; // 發言
};

typedef boost::shared_ptr<chat_participant> chat_participant_ptr;

//----------------------------------------------------------------------

class chat_room // 聊天室
{
public:
	void join(chat_participant_ptr participant) // 6
	{
		cout<<__FUNCTION__<<endl;
		participants_.insert(participant);

		// 把聊天室內快取的訊息傳送給新加入的成員,相當於:
// 		chat_message_queue::const_iterator it;
// 		for(it = recent_msgs_.begin(); it!=recent_msgs_.end(); ++it)
// 			participant->deliver(*it); 
		std::for_each(recent_msgs_.begin(), recent_msgs_.end(),
			boost::bind(&chat_participant::deliver, participant, _1)); // 12 動態繫結
	}

	void leave(chat_participant_ptr participant)
	{
		cout<<__FUNCTION__<<endl;
		participants_.erase(participant);
	}

	// 存入msg到緩衝區佇列
	void deliver(const chat_message& msg) // 11 發言
	{
		cout<<__FUNCTION__<<endl;
		recent_msgs_.push_back(msg);
		while (recent_msgs_.size() > max_recent_msgs)
			recent_msgs_.pop_front(); // 將過時發言清出緩衝區

		// 將新訊息發給每個聊天室成員,相當於:
// 		std::set<chat_participant_ptr>::iterator it;
// 		for(it=participants_.begin(); it!=participants_.end(); ++it)
// 			(*it)->deliver(msg);
		std::for_each(participants_.begin(), participants_.end(),
			boost::bind(&chat_participant::deliver, _1, boost::ref(msg))); // 12
	}

private:
	std::set<chat_participant_ptr> participants_; // 當前聊天室的n個參與者:set,不能重複
	enum { max_recent_msgs = 100 }; // 最大最近訊息:緩衝區最多儲存最近100條發言
	chat_message_queue recent_msgs_; // 訊息佇列:deque,先到先出
};

//----------------------------------------------------------------------

// 在聊天室環境下,一個session就是一個成員
class chat_session
	: public chat_participant, // 繼承
	public boost::enable_shared_from_this<chat_session> // 可以使用shared_from_this()(即shared_ptr<chat_session>)
{
public:
	chat_session(boost::asio::io_service& io_service, chat_room& room) // 2 7
		: socket_(io_service),
		room_(room)
	{
		cout<<__FUNCTION__<<endl;
	}

	tcp::socket& socket() // 3 8
	{
		cout<<__FUNCTION__<<endl;
		return socket_;
	}

	void start() // 5
	{
		cout<<__FUNCTION__<<endl;
		room_.join(shared_from_this());  // 6 room_.join(shared_ptr<chat_session>);
		// async_read是事件處理一個機制,使用回撥函式從而實現事件處理器方法
		// 本示例大量採用這個機制,也就是非同步機制
		// 通過回撥函式可以形成一個事件鏈,即在回撥函式中設定一個新的事件與新回撥函式
		boost::asio::async_read(socket_,
			boost::asio::buffer(read_msg_.data(), chat_message::header_length), // 取buffer頭部,正文字元數到read_msg_
			boost::bind(
			&chat_session::handle_read_header, shared_from_this(), // 9 呼叫:shared_from_this()->handle_read_header(boost::asio::placeholders::error);
			boost::asio::placeholders::error));
	}

	// 存buffer中的資料到read_msg_:header部分
	void handle_read_header(const boost::system::error_code& error) // 9
	{
		cout<<__FUNCTION__<<endl;
		if (!error && read_msg_.decode_header())
		{
			boost::asio::async_read(socket_,
				boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),// 取buffer文字部分到read_msg_
				boost::bind(&chat_session::handle_read_body, shared_from_this(), // 10 呼叫:shared_from_this()->handle_read_body(boost::asio::placeholders::error);
				boost::asio::placeholders::error));
		}
		else
		{
			room_.leave(shared_from_this()); // 14
		}
	}

	// 存buffer中的資料到read_msg_:body部分
	void handle_read_body(const boost::system::error_code& error) // 10
	{
		cout<<__FUNCTION__<<endl;
		if (!error)
		{
			room_.deliver(read_msg_); // 11
			boost::asio::async_read(socket_,
				boost::asio::buffer(read_msg_.data(), chat_message::header_length), // 取buffer頭部,正文字元數到read_msg_
				boost::bind(&chat_session::handle_read_header, shared_from_this(),// 呼叫:shared_from_this()->handle_read_header(boost::asio::placeholders::error);
				boost::asio::placeholders::error));
		}
		else
		{
			room_.leave(shared_from_this());
		}
	}

	//存入資料到write_msgs_,送佇列的最開始一條發言到buffer
	void deliver(const chat_message& msg) // 12,有幾個客戶端呼叫幾次
	{
		cout<<__FUNCTION__<<endl;
		bool write_in_progress = !write_msgs_.empty();
		write_msgs_.push_back(msg);
		if (!write_in_progress)
		{
			boost::asio::async_write(socket_,
				boost::asio::buffer(write_msgs_.front().data(),
				write_msgs_.front().length()), // 佇列的最開始一條發言到buffer
				boost::bind(&chat_session::handle_write, shared_from_this(), // 13 shared_from_this()->handle_write(boost::asio::placeholders::error)
				boost::asio::placeholders::error));
		}
	}

	// 把write_msgs_資料送buffer,使客戶端可以得到,遞迴呼叫自身值到write_msgs_為空
	void handle_write(const boost::system::error_code& error) // 13,有幾個客戶端呼叫幾次
	{
		cout<<__FUNCTION__<<endl;
		if (!error)
		{
			write_msgs_.pop_front();
			if (!write_msgs_.empty())
			{
				boost::asio::async_write(socket_,
					boost::asio::buffer(write_msgs_.front().data(),// 佇列的最開始一條發言到buffer
					write_msgs_.front().length()),
					boost::bind(&chat_session::handle_write, shared_from_this(),// 13 shared_from_this()->handle_write(boost::asio::placeholders::error)
					boost::asio::placeholders::error));
			}
		}
		else
		{
			room_.leave(shared_from_this());
		}
	}

private:
	tcp::socket socket_;
	chat_room& room_;
	chat_message read_msg_; // 存從buffer讀出的資料
	chat_message_queue write_msgs_; // 欲寫入buffer的資料佇列,deque
};

typedef boost::shared_ptr<chat_session> chat_session_ptr;

//----------------------------------------------------------------------

class chat_server
{
public:
	chat_server(boost::asio::io_service& io_service, // 1
		const tcp::endpoint& endpoint)
		: io_service_(io_service),
		acceptor_(io_service, endpoint)
	{
		cout<<__FUNCTION__<<endl;
		chat_session_ptr new_session(new chat_session(io_service_, room_)); // 2
		acceptor_.async_accept(new_session->socket(), // 3
			boost::bind(&chat_server::handle_accept, this, new_session, // 4  this->handle_accept(new_session, boost::asio::placeholders::error);
			boost::asio::placeholders::error));
	}
	// 有連線到來時觸發,然後等待下個連線到來
	void handle_accept(chat_session_ptr session, // 4
		const boost::system::error_code& error)
	{
		cout<<__FUNCTION__<<endl;
		if (!error)
		{
			session->start(); // 5 
			chat_session_ptr new_session(new chat_session(io_service_, room_)); // 7
			acceptor_.async_accept(new_session->socket(), // 8
				boost::bind(&chat_server::handle_accept, this, new_session, //this->handle_accept(new_session, boost::asio::placeholders::error);
				boost::asio::placeholders::error));
		}
	}

private:
	boost::asio::io_service& io_service_;
	tcp::acceptor acceptor_;
	chat_room room_;
};

typedef boost::shared_ptr<chat_server> chat_server_ptr;
typedef std::list<chat_server_ptr> chat_server_list;

//----------------------------------------------------------------------

int _tmain(int argc, _TCHAR* argv[])
{
	// 呼叫棧:
	// 開啟服務端並等待連線:1-3,acceptor_.async_accept()
	// 一個客戶端進入並連線:4-8,然後等待下個客戶端:acceptor_.async_accept();又加入一個客戶端:重複呼叫4-8
	// 任意客戶端發言:9-13,12\13呼叫n次;任意客戶發言,重複9-13
	// 聊天中途又新加入一個客戶端:456 12(n) 78 13(n)
	// 一個客戶端斷開連線:9 14
	// 非同步機制:立刻返回,boost提供的非同步函式字首async_要求一個回撥函式,回撥函式只在某一事件觸發時才被呼叫
	// 回撥函式:void your_completion_handler(const boost::system::error_code& ec);
	try
	{
		boost::asio::io_service io_service;

		chat_server_list servers; // server列表:一個server就是一個聊天室
		
		tcp::endpoint endpoint(/*boost::asio::ip::address_v4::from_string("127.0.0.1")*/tcp::v4(), 1000);
		chat_server_ptr server(new chat_server(io_service, endpoint)); // 1
		servers.push_back(server);

		io_service.run();
	}
	catch (std::exception& e)
	{
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 0;
}
客戶端:client.cpp
// client.cpp : 定義控制檯應用程式的入口點。
//

#include "stdafx.h"

//
// chat_client.cpp
// ~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <cstdlib>
#include <deque>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include "chat_message.hpp"

using boost::asio::ip::tcp;
using namespace std;
typedef std::deque<chat_message> chat_message_queue;

class chat_client
{
public:
	chat_client(boost::asio::io_service& io_service, // 1
		tcp::resolver::iterator endpoint_iterator)
		: io_service_(io_service),
		socket_(io_service)
	{
		cout<<__FUNCTION__<<endl;
		tcp::endpoint endpoint = *endpoint_iterator;
		socket_.async_connect(endpoint,
			boost::bind(&chat_client::handle_connect, this, // 2
			boost::asio::placeholders::error, ++endpoint_iterator));
	}

	void write(const chat_message& msg) // 5
	{
		cout<<__FUNCTION__<<endl;
		io_service_.post(boost::bind(&chat_client::do_write, this, msg));
	}

	void close()
	{
		cout<<__FUNCTION__<<endl;
		io_service_.post(boost::bind(&chat_client::do_close, this));
	}

private:

	void handle_connect(const boost::system::error_code& error, // 2
		tcp::resolver::iterator endpoint_iterator)
	{
		cout<<__FUNCTION__<<endl;
		if (!error)
		{
			boost::asio::async_read(socket_,
				boost::asio::buffer(read_msg_.data(), chat_message::header_length),  //copy buffer to read_msg_'s header
				boost::bind(&chat_client::handle_read_header, this, // 3
				boost::asio::placeholders::error));
		}
		else if (endpoint_iterator != tcp::resolver::iterator())
		{
			socket_.close();
			tcp::endpoint endpoint = *endpoint_iterator;
			socket_.async_connect(endpoint,
				boost::bind(&chat_client::handle_connect, this, // 2
				boost::asio::placeholders::error, ++endpoint_iterator));
		}
	}

	void handle_read_header(const boost::system::error_code& error) // 3
	{
		cout<<__FUNCTION__<<endl;
		if (!error && read_msg_.decode_header())
		{
			boost::asio::async_read(socket_,
				boost::asio::buffer(read_msg_.body(), read_msg_.body_length()), //copy buffer to read_msg_'s body
				boost::bind(&chat_client::handle_read_body, this, // 4
				boost::asio::placeholders::error));
		}
		else
		{
			do_close();
		}
	}

	void handle_read_body(const boost::system::error_code& error) // 4
	{
		cout<<__FUNCTION__<<endl;
		if (!error)
		{
			std::cout.write(read_msg_.body(), read_msg_.body_length()); // print read_msg_'s body
			std::cout << "\n";
			boost::asio::async_read(socket_,
				boost::asio::buffer(read_msg_.data(), chat_message::header_length),
				boost::bind(&chat_client::handle_read_header, this, // 4
				boost::asio::placeholders::error));
		}
		else
		{
			do_close();
		}
	}

	void do_write(chat_message msg) // 6
	{
		cout<<__FUNCTION__<<endl;
		bool write_in_progress = !write_msgs_.empty();
		write_msgs_.push_back(msg);
		if (!write_in_progress)
		{
			boost::asio::async_write(socket_,
				boost::asio::buffer(write_msgs_.front().data(),
				write_msgs_.front().length()), // copy write_msgs_.front() to buffer
				boost::bind(&chat_client::handle_write, this, // 7 send message
				boost::asio::placeholders::error));
		}
	}

	void handle_write(const boost::system::error_code& error) // 7
	{
		cout<<__FUNCTION__<<endl;
		if (!error)
		{
			write_msgs_.pop_front();
			if (!write_msgs_.empty())
			{
				boost::asio::async_write(socket_,
					boost::asio::buffer(write_msgs_.front().data(),
					write_msgs_.front().length()),
					boost::bind(&chat_client::handle_write, this, // 7
					boost::asio::placeholders::error));
			}
		}
		else
		{
			do_close();
		}
	}

	void do_close()
	{
		cout<<__FUNCTION__<<endl;
		socket_.close();
	}

private:
	boost::asio::io_service& io_service_;
	tcp::socket socket_;
	chat_message read_msg_;  // 存從buffer讀出的資料
	chat_message_queue write_msgs_; // 欲寫入buffer的資料佇列,deque
};

int _tmain(int argc, _TCHAR* argv[])
{
	// 呼叫棧:
	// 開啟客戶端並連線(聊天室沒有對話時):1-2
	// 開啟客戶端並連線(聊天室有對話時):1-4
	// 自己發言:56734
	// 聊天室內其他成員發言:34
	try
	{
		boost::asio::io_service io_service;

		tcp::resolver resolver(io_service);
		tcp::resolver::query query("127.0.0.1", "1000"); // ip port:本機
		tcp::resolver::iterator iterator = resolver.resolve(query);

		chat_client c(io_service, iterator); // 初始化、連線

		boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service)); // 執行緒

		char line[chat_message::max_body_length + 1];
		while (std::cin.getline(line, chat_message::max_body_length + 1))
		{
			chat_message msg;
			msg.body_length(strlen(line));
			memcpy(msg.body(), line, msg.body_length());// line to msg
			msg.encode_header();
			c.write(msg);
		}

		c.close();
		t.join(); // 執行執行緒
	}
	catch (std::exception& e)
	{
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 0;
}

在這裡,為了檢視程式的呼叫過程,在每個成員函式中加了一條:
cout<<__FUNCTION__<<endl;

同時,為了測試方便,固定ip與埠為本機,而不是從main引數中傳遞進來

參考資料:http://blog.csdn.net/cyg0810/article/details/36179195