st_asio_wrapper
版權(quán)聲明:本文為 cheng-zhi 原創(chuàng)文章,可以隨意轉(zhuǎn)載习瑰,但必須在明確位置注明出處绪颖!
簡介
·st_asio_wrapper
是基于 Boost.Asio
的異步 C/S
通信框架,因為項目中使用到這個庫甜奄,所以這里記錄下使用的方法柠横。
這里是它的 GitHub 地址:st_asio_wrapper
具體的用法在它的網(wǎng)站上已經(jīng)有了詳細的介紹,這里總結(jié)下自己的使用方法和踩過的坑课兄。
TCP-TCP 連接
TCP 客戶端
為了實現(xiàn) TCP
的連接牍氛,我們在客戶端需要繼承 st_connector
,然后重寫 on_msg_handle
或者 on_msg
函數(shù)來自定義處理消息烟阐,核心代碼如下所示:
#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 服務器端直接發(fā)送指定的消息即可蜒茄,核心代碼如下所示:
class CTcpServer
{
public:
CTcpServer();
~CTcpServer();
private:
typedef std::string msg_type;
public:
// 發(fā)送一條字符串消息
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 發(fā)送
UDP
比 TCP
要簡單些唉擂,因為 UDP
是面向無連接的協(xié)議。下面 是UDP
發(fā)送的核心代碼:
#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;
// 發(fā)送 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 發(fā)送以及接收端屿聋,你要保證你的通信庫是相同的版本空扎,否則可能出現(xiàn)接收不到消息的情況發(fā)送,我之前的遇到過這種情況润讥,因為自己下載過比較新的版本转锈,而項目使用的是老的版本,結(jié)果導致連接失敗象对。
全部代碼
百度云盤地址:
鏈接:http://pan.baidu.com/s/1hs5PxQk 密碼:c4kj