高性能TcpServer(C#) - 1.网络通信协议

高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)

高性能TcpServer(C#) - 3.命令通道(处理:掉包,粘包,垃圾包)

高性能TcpServer(C#) - 4.文件通道(处理:文件分包,支持断点续传)

高性能TcpServer(C#) - 5.客户端管理

高性能TcpServer(C#) - 6.代码下载

代码解析

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)的更多相关文章

  1. 转 C#高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)

    原创性申明 本文作者:小竹zz  博客地址:http://blog.csdn.net/zhujunxxxxx/article/details/43573879转载请注明出处引言 我一直在探寻一个高性能 ...

  2. C#高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)

    网址:http://blog.csdn.net/zhujunxxxxx/article/details/43573879 引言 我一直在探寻一个高性能的Socket客户端代码.以前,我使用Socket ...

  3. 项目-高性能TcpServer - 目录

    1.项目-高性能TcpServer - 1.网络通信协议 https://blog.csdn.net/arno1988/article/details/82463225 2.项目-高性能TcpServ ...

  4. 高性能TcpServer(C#) - 1.网络通信协议

    高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...

  5. 高性能TcpServer(C#) - 3.命令通道(处理:掉包,粘包,垃圾包)

    高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...

  6. 高性能TcpServer(C#) - 4.文件通道(处理:文件分包,支持断点续传)

    高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...

  7. 高性能TcpServer(C#) - 5.客户端管理

    高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...

  8. 高性能TcpServer(C#) - 6.代码下载

    高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...

  9. 【RL-TCPnet网络教程】第19章 RL-TCPnet之BSD Socket服务器

    第19章      RL-TCPnet之BSD Socket服务器 本章节为大家讲解RL-TCPnet的BSD Socket,学习本章节前,务必要优先学习第18章的Socket基础知识.有了这些基础知 ...

随机推荐

  1. Java生鲜电商平台-订单架构实战

    Java生鲜电商平台-订单架构实战 生鲜电商中订单中心是一个电商后台系统的枢纽,在这订单这一环节上需要读取多个模块的数据和信息进行加工处理,并流向下一环节:因此订单模块对一电商系统来说,重要性不言而喻 ...

  2. 用css做三角形

    <html> <body> <style> .trlangle{ width: 0; height: 0; border-left: 50px solid tran ...

  3. 个人博客如何开启 https

    以前写过利用 wordpress 搭建的一个博客『个人博客搭建( wordpress )』,绑定了域名,但是没开启 https,在浏览博客的时候浏览器会提示不安全.下面来谈下个人博客如何免费申请证书, ...

  4. 版本管理·玩转git(远程仓库配置和配置公钥免密登录)

    git系列的最后一部分内容,我们先来看看如何查看远程仓库. 输入 git remote -v 我们还可以删除远程库,输入 git remote remove origin 删除后再次查询,信息为空. ...

  5. ABP进阶教程9 - CSV导出中文乱码

    点这里进入ABP进阶教程目录 问题描述 功能按钮 - 导出CSV,中文信息导出为乱码. 解决方案 打开展示层(即JD.CRS.Web.Mvc)的\wwwroot\view-resources\View ...

  6. 【JavaWeb】FreeMarker快速入门

    FreeMarker Freemarker是免费开源的模板引擎技术: Freemarker脚本为Freemarker Template Language: Freemarker提供了大量内建函数来简化 ...

  7. .htaccess设置301跳转及常用技巧整理

    在虚拟主机环境中,基本上都是Apache环境.Apache的伪静态的设置,都是在网站根目录设置.htaccess文件,在.htaccess文件中无论是伪静态, 还是301跳转,甚至是防盗链和禁止某个I ...

  8. 卷积层输出feature maps尺寸的计算

    默认feature maps的宽和高相等. 常规卷积 输入的feature maps尺寸为i,卷积核的尺寸为k,stride为s,padding为p,则输出的feature maps的尺寸o为 当pa ...

  9. web前端学习路线(干货)

  10. 【bzoj1941】[Sdoi2010]Hide and Seek(kd-tree)

    bzoj 题意: 给出\(n\)个点,对于每个点,\(d_i\)等于距离其最远的点的距离减去距离最近的点的距离.这里的距离为曼哈顿距离. 求\(min\{d_i\}\). 思路: 考虑直接对每个点暴力 ...