高性能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基础知识.有了这些基础知 ...
随机推荐
- python基础(11):函数(一)
1. 什么是函数 1.我们到⽬前为⽌,已经可以完成⼀些软件的基础功能了.那么我们来完成这样⼀个功能: 约会: print("拿出⼿机") print("打开陌陌" ...
- C#NPOI对Excel的操作、导入导出时异常处理、最全的NPOI资料在这里~
一.Excel理论知识 最新版NPOI2.4.1链接:https://pan.baidu.com/s/1iTgJi2hGsRQHyw2S_4dIUw 提取码:adnq • 整个Excel表格叫做工作 ...
- MySQL Linux环境的安装配置
在Kali中已经内置了MySQL(镜像可以从mysql.com/downloads/ 下载安装) 奇怪的是博主我的kali内置的是mariaDB数据库,所以我也懒得弄MySQL了!直接mariaDB吧 ...
- 被 GANs 虐千百遍后,我总结出来的 10 条训练经验
一年前,我决定开始探索生成式对抗网络(GANs).自从我对深度学习产生兴趣以来,我就一直对它们很着迷,主要是因为深度学习能做到很多不可置信的事情.当我想到人工智能的时候,GAN是我脑海中最先出现的一个 ...
- 上传App Store 被拒问题及解决方案总结
最近公司比较忙,一直忙着写代码做新的应用,一连上线了几个应用,我们也是忙得焦头烂额的,都没时间做总结,今天趁APP审核期间,总结一下近期上传App Store遇到的一些问题和解决方法,以便以后查阅. ...
- 第三方库Mantle的源码解析
Mantle是一个用于简化Cocoa或Cocoa Touch程序中model层的第三方库.通常我们的应该中都会定义大量的model来表示各种数据结构,而这些model的初始化和编码解码都需要写大量的代 ...
- 模仿UIApplication创建单例
UIApplicationMain: 1.创建UIApplication--应用程序唯一标识:可设置状态栏.识别联网状态.设置数字.打电话.发邮件.发短信.打开网页 2.创建UIApplication ...
- Bitbucket与git上传源码的使用方法
本文链接:https://blog.csdn.net/nomisshe/article/details/19625555 Bitbucket使用方法 一.软件及SSH keys: 由于我的Bitb ...
- C学习笔记(4)--- 指针的应用(第一部分)
1.指针(pointer)概念: 指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址.就像其他变量或常量一样,您必须在使用指针存储其他变量地址之前,对其进行声明.指针变量声明的一般形式为: ...
- hbase相关操作
hbase安装 下载 wget http://apache.gree.com/apache/hbase/hbase-1.2.11/hbase-1.2.11-bin.tar.gz tar -zxvf b ...