/*
* 并发服务器
* 简介:处理多客户端的请求才是目的
* 作者:陈鲁勇
* 邮箱:727057301@qq.com
* CSDN:http://blog.csdn.net/csnd_ayo
* 码云:https://git.oschina.net/Mr_ChenLuYong
* github:http://github.com/chenluyong
* 创建时间:2017年3月12日 04:32:07
* VS版本:VS2013
* Boost版本:boost_1_61_0
*/
#include <boost/bind.hpp>
#include "Session.h"
Session::Session(boost::asio::io_service& _ioService)
:socket_(_ioService) {
memset(msg_, 0, sizeof(msg_));
}
Session::~Session(void)
{
}
void Session::start(void) {
// 告诉链接成功的客户端,你想要的信息。
char msg[256] = "001:Connect Succeed! Please tell me with 10 bytes, the total data and the size of each package, example:128 1024";
boost::asio::async_write(socket_, boost::asio::buffer(msg, strlen(msg)),
boost::bind(&Session::init_handler, shared_from_this(),
boost::asio::placeholders::error));
}
boost::asio::ip::tcp::socket& Session::socket(void) {
return socket_;
}
// 第一个协议包
void Session::init_handler(const boost::system::error_code& _error) {
if (_error) {
return;
}
// 读取客户端发来的 10 bytes,确定单个包的大小以及数据总大小
boost::asio::async_read(socket_, boost::asio::buffer(msg_, 10),
boost::bind(&Session::analyse_handler, shared_from_this(),
boost::asio::placeholders::error));
}
void Session::analyse_handler(const boost::system::error_code& _error) {
if (_error) {
return;
}
// 分析协议包格式
bool bflag = true;
// 正则分析格式
// do something.
if (!bflag) {
start();
return;
}
// 格式化保存协议包数据
std::stringstream io(msg_);
io >> maxSize_;
io >> sumSize_;
// 发送接收请求信息
char msg[REPLY_SIZE];
sprintf_s(msg, "001:is ok, data remaining %d.", sumSize_);
boost::asio::async_write(socket_, boost::asio::buffer(msg, REPLY_SIZE),
boost::bind(&Session::write_handler, shared_from_this(),
boost::asio::placeholders::error));
}
// 完成数据传输
void Session::done_handler(const boost::system::error_code& _error) {
if (_error) {
return;
}
currentMsg_ += msg_;
// 发送信息到回调
if (!currentMsg_.empty() && callback_ != nullptr) {
callback_(socket_.remote_endpoint().address().to_string(), currentMsg_);
currentMsg_.clear();
}
memset(msg_, 0, sizeof(msg_));
char msg[32] = "001:will done.";
boost::asio::async_write(socket_, boost::asio::buffer(msg, REPLY_SIZE),
boost::bind(&Session::init_handler, shared_from_this(),
boost::asio::placeholders::error));
}
void Session::read_handler(const boost::system::error_code& _error, size_t _readSize) {
if (_error) {
return;
}
// 数据处理
currentMsg_ += msg_;
if (currentMsg_.size() > 1024 * 512) {
// 发送信息到回调
if (callback_ != nullptr) {
callback_(socket_.remote_endpoint().address().to_string(), currentMsg_);
currentMsg_.clear();
}
}
memset(msg_, 0, sizeof(msg_));
// 计算当前剩余数据数量
sumSize_ -= _readSize;
// 接收完成
if (0 > sumSize_) {
done_handler(_error);
}
// 继续接收
else {
char msg[REPLY_SIZE];
sprintf_s(msg, "001:%d.", sumSize_);
boost::asio::async_write(socket_, boost::asio::buffer(msg, REPLY_SIZE),
boost::bind(&Session::write_handler, shared_from_this(),
boost::asio::placeholders::error));
std::cout << "send client recv succeed: " << msg << std::endl;
}
}
void Session::write_handler(const boost::system::error_code& _error) {
if (_error) {
return;
}
boost::asio::async_read(socket_, boost::asio::buffer(msg_, maxSize_),
boost::bind(&Session::read_handler, shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
没有合适的资源?快使用搜索试试~ 我知道了~
利用Asio网络库建立自己的Socket服务器
共20个文件
cpp:7个
filters:5个
vcxproj:5个
5星 · 超过95%的资源 需积分: 46 252 下载量 157 浏览量
2017-03-21
08:40:40
上传
评论 11
收藏 21KB RAR 举报
温馨提示
http://blog.csdn.net/csnd_ayo/article/details/61577634 代码介绍
资源推荐
资源详情
资源评论
收起资源包目录
利用Asio网络库建立自己的Socket服务器.rar (20个子文件)
利用Asio网络库建立自己的Socket服务器
CSND_Ayo服务器
建造
CSDN_Ayo.cpp 1KB
CSND_Ayo.vcxproj 4KB
CSND_Ayo.vcxproj.filters 949B
异步
异步.vcxproj 4KB
并发.vcxproj.filters 949B
CSND_Ayo.cpp 2KB
并发
Server.cpp 1KB
Session.h 2KB
并发.vcxproj.filters 1KB
Server.h 1KB
CSDN_Ayo.cpp 881B
并发.vcxproj 4KB
Session.cpp 4KB
处理
处理.vcxproj 4KB
CSDN_Ayo.cpp 2KB
处理.vcxproj.filters 949B
聆听
聆听.vcxproj 4KB
CSDN_Ayo.cpp 2KB
聆听.vcxproj.filters 949B
CSND_Ayo服务器.sln 3KB
共 20 条
- 1
资源评论
- thomas_blog2019-06-16非常好,谢谢分享
- xiaoliang12012021-08-05能够正常编译运行,不同的功能使用不同的工程来承载。运行正常。
- 普通网友2017-11-14非常好,谢谢分享
- kshmilyhui2018-09-02非常好,谢谢分享
louie_chen
- 粉丝: 260
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功