高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)
高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)
高性能TcpServer(C#) - 3.命令通道(处理:掉包,粘包,垃圾包)
高性能TcpServer(C#) - 4.文件通道(处理:文件分包,支持断点续传)
代码解析
SocketAsyncEventArgs对象管理 -- 用于CheckOut/CheckIn SocketAsyncEventArgs对象
SocketArgsPool socketArgsPool = new SocketArgsPool(MAX_CLIENTCOUNT);
this.m_EventArgs = this.m_socketArgsPool.CheckOut();// 初始化对象
this.m_bufferPool.CheckIn(m_EventArgs);// 回收对象
SocketArgsBufferPool对象管理 -- 用于CheckOut/CheckIn SocketAsyncEventArgs的Buffer
SocketArgsBufferPool bufferPool = new SocketArgsBufferPool(MAX_CLIENTCOUNT, MAX_CLIENTBUFFERSIZE);
this.m_bufferPool.CheckOut(this.m_EventArgs);// 设置setBuffer
this.m_bufferPool.CheckIn(m_EventArgs);// 回收对象
SocketEntityPool对象管理 -- 用于CheckOut/CheckIn SocketEntity
SocketEntityPool socketEntityPool = new SocketEntityPool(MAX_CLIENTCOUNT, MAX_CLIENTBUFFERSIZE);// 初始化
m_socketEntity = this.m_socketEntityPool.CheckOut();
m_socketEntity.SocketClient = socket;
m_bufferRecv = m_socketEntity.BufferRecv; m_bufferRecv.Clear();// 每个client的接收缓冲区
m_handle = m_socketEntity.ProtocolHandle;// 每个client的处理类
m_analysis = m_socketEntity.ProtocolAnalysis;// 每个client的解析类
this.m_socketEntityPool.CheckIn(socketEntity);// 回收对象
部分代码
服务器监听和接收客户端连接
public void Start(int port)
{
IPEndPoint ipEP = new IPEndPoint(IPAddress.Any, port);
this.m_listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.m_listenerSocket.Bind(ipEP);
this.m_listenerSocket.Listen(100);
ListenForConnection(m_listenerArgs);
}
void ListenForConnection(SocketAsyncEventArgs args)
{
lock (this)
{
args.AcceptSocket = null;
m_listenerSocket.InvokeAsyncMethod(new SocketAsyncMethod(m_listenerSocket.AcceptAsync), AcceptAsyncCompleted, args);
}
}
void AcceptAsyncCompleted(object sender, SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.OperationAborted)
{
CLogHelp.AppendLog("[Error] AcceptAsyncCompleted:SocketError.OperationAborted");
return; //Server was stopped
}
if (e.SocketError == SocketError.Success)
{
Socket acceptSocket = e.AcceptSocket;
if (acceptSocket != null)
{
if (connections + 1 <= MAX_CLIENTCOUNT)
{
IPEndPoint clientEP = (IPEndPoint)acceptSocket.RemoteEndPoint;
sn = String.Format("{0}:{1}", clientEP.Address.ToString(), clientEP.Port);
lock (LockIndex)
{
connections = Interlocked.Increment(ref connections);
Program.AddMessage("已连接,sn:" + sn + ",当前连接数:" + CServerIntance.connections.ToString());
}
CSocketDAO socketDao = new CSocketDAO(socketArgsPool, bufferPool, socketEntityPool, acceptSocket, sn);
CSingleton<CClientMgr>.GetInstance().AddOnlineClient(socketDao);
}
else
{
Program.AddMessage("超过最大连接数:" + MAX_CLIENTCOUNT.ToString() + ",拒接连接");
}
}
}
//continue to accept!
ListenForConnection(e);
}
服务器数据处理
void ReceiveAsyncCompleted(object sender, SocketAsyncEventArgs e)
{
if (!this.m_connected) return;
try
{
m_EventArgs = e;
if (m_EventArgs.BytesTransferred == 0)
{
SocketCatchError("BytesTransferred=0"); //Graceful disconnect
return;
}
if (m_EventArgs.SocketError != SocketError.Success)
{
SocketCatchError("SocketError=" + (e.SocketError).ToString()); //NOT graceful disconnect
return;
}
//数据存储
recvTime = DateTime.Now;
m_bufferRecv.Put(e);
m_analysis.BagStatus = CProtocolAnalysis.EBagStatus.BagNone;
// 粘包处理
while (m_bufferRecv.HasRemaining())
{
// 掉包处理
if (CProtocolAnalysis.EBagStatus.BagLost == m_analysis.BagStatus) break;
m_handle.Process(m_bufferRecv, m_analysis, m_strSn);// 数据解析(垃圾包处理)
if (string.IsNullOrEmpty(m_strUid))
{
if (!string.IsNullOrEmpty(m_analysis.Uid))
{
m_strUid = m_analysis.Uid;
CSingleton<CClientMgr>.GetInstance().AddClientUid(m_strUid, m_strSn, this);
}
}
if (m_analysis.WhetherToSend)
{
string data = CProtocolBase.GetProtocol(m_analysis);
SendRealTime(data);
}
}
ListenForData(e);
}
catch (Exception ex)
{
CLogHelp.AppendLog("[Error] ReceiveAsyncCompleted,errmsg:" + ex.Message);
}
}
高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)的更多相关文章
- 转 C#高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)
原创性申明 本文作者:小竹zz 博客地址:http://blog.csdn.net/zhujunxxxxx/article/details/43573879转载请注明出处引言 我一直在探寻一个高性能 ...
- C#高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)
网址:http://blog.csdn.net/zhujunxxxxx/article/details/43573879 引言 我一直在探寻一个高性能的Socket客户端代码.以前,我使用Socket ...
- 项目-高性能TcpServer - 目录
1.项目-高性能TcpServer - 1.网络通信协议 https://blog.csdn.net/arno1988/article/details/82463225 2.项目-高性能TcpServ ...
- 高性能TcpServer(C#) - 1.网络通信协议
高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...
- 高性能TcpServer(C#) - 3.命令通道(处理:掉包,粘包,垃圾包)
高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...
- 高性能TcpServer(C#) - 4.文件通道(处理:文件分包,支持断点续传)
高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...
- 高性能TcpServer(C#) - 5.客户端管理
高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...
- 高性能TcpServer(C#) - 6.代码下载
高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...
- 【RL-TCPnet网络教程】第19章 RL-TCPnet之BSD Socket服务器
第19章 RL-TCPnet之BSD Socket服务器 本章节为大家讲解RL-TCPnet的BSD Socket,学习本章节前,务必要优先学习第18章的Socket基础知识.有了这些基础知 ...
随机推荐
- PlayJava Day003
今日所学: /* 2019.08.19开始学习,此为补档. */ ①char:只能有一个字段.字符:' ' ②二进制:0000 0000 最后一位为0就不算,为1代表20. 如25为:0001 100 ...
- 虚拟机Centos6.7安装VMTools
安装VMware Tools,设置共享文件夹 一.基本步骤 1.VMware Workstation菜单栏中,选择“虚拟机”,”安装VMware Tools...“.(注:此时下方可能会弹出“确保您已 ...
- 一文解读DDD (转)
从遇到问题开始 当人们要做一个软件系统时,一般总是因为遇到了什么问题,然后希望通过一个软件系统来解决. 比如,我是一家企业,然后我觉得我现在线下销售自己的产品还不够,我希望能够在线上也能销售自己的产品 ...
- emacs 缩进
emacs提供一些编码风格,可以使用M-x c-set-style来选择你喜欢的编码风格. Possible completions are: awk bsd cc-mode ellemtel gnu ...
- Tensorflow之多元线性回归问题(以波士顿房价预测为例)
一.根据波士顿房价信息进行预测,多元线性回归+特征数据归一化 #读取数据 %matplotlib notebook import tensorflow as tf import matplotlib. ...
- UVA10559 方块消除 Blocks(区间dp)
一道区间dp好题,在GZY的ppt里,同时在洛谷题解里看见了Itst orz. 题目大意 有n个带有颜色的方块,没消除一段长度为 \(x\) 的连续的相同颜色的方块可以得到 \(x^2\) 的分数,用 ...
- Educational Codeforces Round 78 (Rated for Div. 2) 题解
Shuffle Hashing A and B Berry Jam Segment Tree Tests for problem D Cards Shuffle Hashing \[ Time Lim ...
- Shell编程——运算符
1.declare命令: 声明变量的类型: -:给变量设定类型属性 +:给变量取消类型属性 -i:将变量声明为整数类型 -x:将变量声明为环境变量 -p:显示变量的类型 其中export是将num变为 ...
- vue_03day
目录 作业: vue组件操作页面渲染: 组件渲染: 作业: vue组件操作页面渲染: 1.有以下广告数据(实际数据命名可以略做调整) ad_data = { tv: [ {img: 'img/tv/0 ...
- LSTM容易混淆的地方
1 如果只是学习怎么用LSTM,那么可以这么理解LSTM LSTM可以看成一个仓库,而这个仓库有三个门卫,他们的功能分别是 遗忘门.决定什么样的物品需要从仓库中丢弃. 输入门.决定输入的什么物品用来存 ...