st_asio_wrapper
版權聲明:本文為 cheng-zhi 原創文章,可以隨意轉載,但必須在明確位置注明出處!
簡介
·st_asio_wrapper
是基于 Boost.Asio
的異步 C/S
通信框架,因為項目中使用到這個庫,所以這里記錄下使用的方法。
這里是它的 GitHub 地址:st_asio_wrapper
具體的用法在它的網站上已經有了詳細的介紹,這里總結下自己的使用方法和踩過的坑。
TCP-TCP 連接
TCP 客戶端
為了實現 TCP
的連接,我們在客戶端需要繼承 st_connector
,然后重寫 on_msg_handle
或者 on_msg
函數來自定義處理消息,核心代碼如下所示:
#pragma once
//configuration
#undef MAX_MSG_NUM
#define MAX_MSG_NUM 1500
//#define FORCE_TO_USE_MSG_RECV_BUFFER //force to use the msg recv buffer
#include "stdafx.h"
#include "communication/st_asio_wrapper_base.h"
#include "communication/st_asio_wrapper_tcp_client.h"
#include <string>
using namespace st_asio_wrapper;
class CTcpClient
{
public:
CTcpClient();
~CTcpClient();
private:
class MyConnector : public st_connector
{
public:
MyConnector(boost::asio::io_service& io_service_) : st_connector(io_service_){}
protected:
typedef std::string MsgType;
// 自定義處理消息
virtual void on_msg_handle(MsgType& msg)
{
AfxMessageBox(_T("on_msg_handle"));
}
virtual bool on_msg(MsgType& msg)
{
AfxMessageBox(_T("on_msg"));
return true;
}
};
private:
st_service_pump m_pump;
st_sclient<MyConnector> m_client;
};
這里只是部分的核心代碼,全部的代碼最后會給出鏈接。
TCP 服務器端
我們在 TCP 服務器端直接發送指定的消息即可,核心代碼如下所示:
class CTcpServer
{
public:
CTcpServer();
~CTcpServer();
private:
typedef std::string msg_type;
public:
// 發送一條字符串消息
void send_msg(const msg_type& p_msg)
{
m_server.broadcast_msg(p_msg);
}
private:
class My_st_server_socket : public st_server_socket_base<>
{
public:
typedef std::string msg_type;
public:
My_st_server_socket(i_server& server_): st_server_socket_base(server_){}
};
private:
st_service_pump m_pump;
st_server_base<My_st_server_socket> m_server;
};
UDP - UDP 鏈接
UDP 發送
UDP
比 TCP
要簡單些,因為 UDP
是面向無連接的協議。下面 是UDP
發送的核心代碼:
#pragma once
#include "communication/st_asio_wrapper_base.h"
#include "communication/st_asio_wrapper_udp_client.h"
#include "communication/st_asio_wrapper_udp_socket.h"
#include "communication/st_asio_wrapper_client.h"
using namespace st_asio_wrapper;
class CUdpServer
{
public:
CUdpServer();
~CUdpServer();
public:
typedef std::string MsgType;
// 發送 UDP 消息
void SendMsg(const MsgType& msg)
{
m_UdpServer.safe_send_native_msg(m_PeerAddr, msg);
}
private:
boost::asio::ip::udp::endpoint m_PeerAddr;
st_service_pump m_UdpPump;
st_udp_sclient m_UdpServer;
};
UDP 接收
我們在接收 UDP
消息的時候,需要繼承 st_udp_socket
類,重寫 on_msg_handle
或者 on_msg
來自定義消息處理。下面是 UDP
接收的核心代碼:
#pragma once
#include "communication/st_asio_wrapper_base.h"
#include "communication/st_asio_wrapper_udp_socket.h"
#include "communication/st_asio_wrapper_udp_client.h"
#include "communication/st_asio_wrapper_client.h"
using namespace st_asio_wrapper;
class CUdpClient
{
public:
CUdpClient();
~CUdpClient();
private:
class MyUdpConnector : public st_udp_socket
{
typedef std::string msg_type;
public:
MyUdpConnector(boost::asio::io_service& io_service_): st_udp_socket(io_service_) {}
protected:
// 重寫 on_msg
virtual bool on_msg(msg_type& msg)
{
//自定義處理消息
AfxMessageBox(_T("Test"));
return true;
}
// 重寫 on_msg_handle
virtual bool on_msg_handle(msg_type& msg)
{
return true;
}
};
private:
st_service_pump m_Pump;
st_sclient<MyUdpConnector> m_client;
boost::asio::ip::udp::endpoint m_PeerAddr;
};
注意
在 TCP 的客戶端和服務器端和 UDP 發送以及接收端,你要保證你的通信庫是相同的版本,否則可能出現接收不到消息的情況發送,我之前的遇到過這種情況,因為自己下載過比較新的版本,而項目使用的是老的版本,結果導致連接失敗。
全部代碼
百度云盤地址:
鏈接:http://pan.baidu.com/s/1hs5PxQk 密碼:c4kj