boost::asio 异步主动连接多个服务器

本文档展示了如何使用Boost.Asio库实现一个客户端程序,该程序能异步地主动连接到多个服务器,并进行心跳维持、消息接收和发送。客户端会定期尝试连接到每个服务器,若连接成功则创建Session实例进行数据交互。Session类负责读取和发送数据,包括心跳消息和配置消息。同时,客户端还维护了一个SessionPool来管理已连接的服务器,便于消息的分发。
摘要由CSDN通过智能技术生成
ATC.hpp
  1. #include <boost/asio/deadline_timer.hpp>
  2. #include <boost/asio/io_service.hpp>
  3. #include <boost/asio/ip/tcp.hpp>
  4. #include <boost/asio/read_until.hpp>
  5. #include <boost/asio/streambuf.hpp>
  6. #include <boost/asio/write.hpp>
  7. #include <boost/asio.hpp>
  8. #include <boost/bind.hpp>
  9. #include <boost/shared_ptr.hpp>
  10. #include <boost/enable_shared_from_this.hpp>
  11. #include <iostream>
  12. #include <set>
  13. #include <map>
  14. #include "Message.hpp"
  15. #include "SQLite.hpp"

  16. using boost::asio::deadline_timer;
  17. using boost::asio::ip::tcp;
  18. using boost::asio::ip::udp;

  19. using namespace std;
  20. using namespace PWRD;
  21. using namespace PWRD::SQL;
  22. using namespace PWRD::Protocol;

  23. typedef map<string, int> Peers;
  24. typedef map<string, int> Stats;
  25. typedef map<string, string> Files;
  26. typedef map<string, tcp::socket* > Sockets;
  27. #define DEADLINE 10
  28. #define MAXLEN 1024
  29. #define SECONDS 5

  30. namespace PWRD{
  31.     namespace Network{
  32.         class Session{
  33.             public:
  34.                 Session(boost::asio::ip::tcp::socket &_socket, boost::asio::io_service &io_service, const char *path);
  35.                 ~Session();
  36.                 void start();

  37.                 void stop();
  38.                 void delivery(string &ip);

  39.             private:
  40.                 void handle_connect(const boost::system::error_code& ec,
  41.                         tcp::resolver::iterator endpoint_iter);

  42.                 void start_read();

  43.                 void handle_read_header(const boost::system::error_code& ec);

  44.                 void start_delivery();

  45.                 void handle_read_body(const boost::system::error_code& ec);

  46.                 void start_write_heartbeat();

  47.                 void handle_write_heartbeat(const boost::system::error_code& ec);
  48.                 void handle_wait(const boost::system::error_code& ec);
  49.                 void handle_write_config(const boost::system::error_code& ec, Configure &conf);

  50.             private:
  51.                 bool stopped_;
  52.                 tcp::socket &socket_;
  53.                 Header header_;
  54.                 HeartbeatMessage heartbeat_message_;
  55.                 DataMessage packet_message_;
  56.                 ConfigMessage conf_message_;
  57.                 deadline_timer heartbeat_timer_;
  58.                 Callback *callback_;
  59.                 char *input_stream_;

  60.         };

  61.         class SessionPool{
  62.             public:
  63.                 SessionPool();
  64.                 ~SessionPool();
  65.                 void join(string&, Session *);
  66.                 void delivery(string&);
  67.                 void leave(string &ip);

  68.             private:
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值