本例子采用自定义的协议,完成了一个聊天室的功能。客户上线后能够发言,也能看见最近的消息记录,并能看到其他客户的消息。
1.首先自定义消息格式。
chat_message.hpp
// // chat_message.hpp // ~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2013 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) // #ifndef CHAT_MESSAGE_HPP #define CHAT_MESSAGE_HPP #include <cstdio> #include <cstdlib> #include <cstring> class chat_message { public: enum { header_length = 4 }; //最大消息体长度为512 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_; } //返回body指针 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 new_length) { body_length_ = new_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
2.客户端chat_client.cpp
// // chat_client.cpp // ~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2013 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/thread.hpp> #include "chat_message.hpp" using boost::asio::ip::tcp; typedef std::deque<chat_message> chat_message_queue; class chat_client { public: chat_client(boost::asio::io_service& io_service, tcp::resolver::iterator endpoint_iterator) : io_service_(io_service), socket_(io_service) { boost::asio::async_connect(socket_, endpoint_iterator, boost::bind(&chat_client::handle_connect, this, boost::asio::placeholders::error)); } //io_service::post 请求io_service去调用给定handle并立刻返回。 //io_service 保证handler只能在与run run_one() poll() poll_one()成员对象中的同一个线程执行。估计最大用途是保证线程安全 //http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/io_service/post.html void write(const chat_message& msg) { io_service_.post(boost::bind(&chat_client::do_write, this, msg)); } void close() { io_service_.post(boost::bind(&chat_client::do_close, this)); } private: //连接后执行的handle void handle_connect(const boost::system::error_code& error) { if (!error) { //先接收头部信息 boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.data(), chat_message::header_length), boost::bind(&chat_client::handle_read_header, this, boost::asio::placeholders::error)); } } void handle_read_header(const boost::system::error_code& error) { //对头部消息进行解码 if (!error && read_msg_.decode_header()) { boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.body(), read_msg_.body_length()), boost::bind(&chat_client::handle_read_body, this, boost::asio::placeholders::error)); } else { do_close(); } } void handle_read_body(const boost::system::error_code& error) { //再接受body消息 if (!error) { std::cout.write(read_msg_.body(), read_msg_.body_length()); 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, boost::asio::placeholders::error)); } else { do_close(); } } void do_write(chat_message msg) { 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()), boost::bind(&chat_client::handle_write, this, boost::asio::placeholders::error)); } } void handle_write(const boost::system::error_code& error) { 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, boost::asio::placeholders::error)); } } else { do_close(); } } void do_close() { socket_.close(); } private: boost::asio::io_service& io_service_; tcp::socket socket_; chat_message read_msg_; chat_message_queue write_msgs_; }; int main(int argc, char* argv[]) { try { if (argc != 3) { std::cerr << "Usage: chat_client <host> <port>\n"; return 1; } boost::asio::io_service io_service; tcp::resolver resolver(io_service); tcp::resolver::query query(argv[1], argv[2]); tcp::resolver::iterator iterator = resolver.resolve(query); chat_client c(io_service, iterator); //开启新线程跑io_service::run 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)) { using namespace std; // For strlen and memcpy. chat_message msg; msg.body_length(strlen(line)); memcpy(msg.body(), line, msg.body_length()); msg.encode_header(); //线程安全嘛?或者说io_sevice.post保证是以t线程执行,类似于托管给io_service执行。 c.write(msg); } c.close(); t.join(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } return 0; }
3.服务器端代码chat_server.cpp
// // chat_server.cpp // ~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2013 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; //---------------------------------------------------------------------- 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) { participants_.insert(participant); //std的for_each函数。以此以迭代器的参数调用函数对象。 //将最近的消息发送给该用户。因为对于成员函数的绑定是需要传递一个对象或者 //指针,不能直接调用operator。对于_1,占位符,因为要传递一个for_each中迭代器所指的元素。而该bind所绑定的参数是message而不是对象。 std::for_each(recent_msgs_.begin(), recent_msgs_.end(), boost::bind(&chat_participant::deliver, participant, _1)); } //用户离开 void leave(chat_participant_ptr participant) { participants_.erase(participant); } //把消息传送给各用户 void deliver(const chat_message& msg) { recent_msgs_.push_back(msg); //清除最早的数据 while (recent_msgs_.size() > max_recent_msgs) recent_msgs_.pop_front(); //对于foreach如果要调用迭代器所指向的对象的成员函数,要用到_1占位 //function对象是拷贝参数来实现的,如果参数数据很大,拷贝消耗资源。因此可以使用ref库传递引用参数。 std::for_each(participants_.begin(), participants_.end(), boost::bind(&chat_participant::deliver, _1, boost::ref(msg))); } private: //在线客户列表 std::set<chat_participant_ptr> participants_; enum { max_recent_msgs = 100 }; chat_message_queue recent_msgs_; }; //一个客户的session class chat_session : public chat_participant, public boost::enable_shared_from_this<chat_session> { public: chat_session(boost::asio::io_service& io_service, chat_room& room) : socket_(io_service), room_(room) { } tcp::socket& socket() { return socket_; } void start() { //加入聊天室 room_.join(shared_from_this()); //读取客户端数据 boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.data(), chat_message::header_length), boost::bind( &chat_session::handle_read_header, shared_from_this(), boost::asio::placeholders::error)); } void deliver(const chat_message& msg) { 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()), boost::bind(&chat_session::handle_write, shared_from_this(), boost::asio::placeholders::error)); } } void handle_read_header(const boost::system::error_code& error) { if (!error && read_msg_.decode_header()) { //读消息体 boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.body(), read_msg_.body_length()), boost::bind(&chat_session::handle_read_body, shared_from_this(), boost::asio::placeholders::error)); } else { room_.leave(shared_from_this()); } } void handle_read_body(const boost::system::error_code& error) { if (!error) { room_.deliver(read_msg_); boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.data(), chat_message::header_length), boost::bind(&chat_session::handle_read_header, shared_from_this(), boost::asio::placeholders::error)); } else { room_.leave(shared_from_this()); } } void handle_write(const boost::system::error_code& error) { 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_session::handle_write, shared_from_this(), boost::asio::placeholders::error)); } } else { room_.leave(shared_from_this()); } } private: tcp::socket socket_; chat_room& room_; chat_message read_msg_; chat_message_queue write_msgs_; }; typedef boost::shared_ptr<chat_session> chat_session_ptr; //聊天服务 class chat_server { public: chat_server(boost::asio::io_service& io_service, const tcp::endpoint& endpoint) : io_service_(io_service), acceptor_(io_service, endpoint) { start_accept(); } void start_accept() { chat_session_ptr new_session(new chat_session(io_service_, room_)); acceptor_.async_accept(new_session->socket(), boost::bind(&chat_server::handle_accept, this, new_session, boost::asio::placeholders::error)); } void handle_accept(chat_session_ptr session, const boost::system::error_code& error) { if (!error) { session->start(); } start_accept(); } 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 main(int argc, char* argv[]) { try { if (argc < 2) { std::cerr << "Usage: chat_server <port> [<port> ...]\n"; return 1; } boost::asio::io_service io_service; //可以开启多个端口作为服务。 chat_server_list servers; for (int i = 1; i < argc; ++i) { using namespace std; // For atoi. tcp::endpoint endpoint(tcp::v4(), atoi(argv[i])); chat_server_ptr server(new chat_server(io_service, endpoint));//实例化后即启动服务 servers.push_back(server); } io_service.run(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } return 0; }