Boost.Asio学习之实现广播ChatRoom

见:http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/examples/cpp11_examples.html
或者:https://github.com/NearXdu/AsioLearn

1.服务端

对于一个广播聊天室来说。
服务端能够接受连接,并且将收到的消息转发到所有已连接的客户端。

因此,在设计服务端代码时,需要考虑如何保存Tcp连接实例。

在Boost.Asio中给出的例子中一共设计了3个类(实际上是4个,其中一个作为虚基类)。
1. Server
2. Room
3. Session

Server类对象异步的接收连接,它包含一个Room类对象引用,将管理所有的连接(Session对象的shared_ptr)当Server收到消息之后,将向所有的Session(连接)转发消息。

#include <cstdlib>
#include <deque>
#include <iostream>
#include <list>
#include <memory>
#include <set>
#include <utility>
#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; //participant should deliver message
};

typedef std::shared_ptr<chat_participant> chat_participant_ptr;//shared ptr

class chat_room
{
    public:
    void join(chat_participant_ptr participant)
    {
        participants_.insert(participant);//add a client
        //将之前的消息写一遍给最新的连接者
        for (auto msg: recent_msgs_)
        participant->deliver(msg);
    }

    void leave(chat_participant_ptr participant)
    {
        participants_.erase(participant);//remove a client
    }

    void deliver(const chat_message& msg)
    {
        //先将发送的消息放入待发送列表。
        recent_msgs_.push_back(msg);
        while (recent_msgs_.size() > max_recent_msgs)
        recent_msgs_.pop_front();//delete expired message

        for (auto participant: participants_)
        participant->deliver(msg);//将消息向所有的对象转发一遍
    }

    private:
    std::set<chat_participant_ptr> participants_;//all client
    enum { max_recent_msgs = 100 };
    chat_message_queue recent_msgs_;
};//chat r
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值