高性能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. Vue-nodeJS环境搭建

    Node.jsNode.js是一个基于Chrome V8引擎的[JavaScript运行环境]. Node.js使用了一个事件驱动.非阻塞式I/O 的模型.Node.js是一个让JavaScript运 ...

  2. 并发编程-epoll模型的探索与实践

    前言 我们知道nginx的效率非常高,能处理上万级的并发,其之所以高效离不开epoll的支持, epoll是什么呢?,epoll是IO模型中的一种,属于多路复用IO模型; 到这里你应该想到了,sele ...

  3. Java正则表达式详细解析

    元字符 正则表达式使用一些特定的元字符来检索.匹配和替换符合规则的字符串 元字符:普通字符.标准字符.限定字符(量词).定位字符(边界字符) 正则表达式引擎 正则表达式是一个用正则符号写出来的公式 程 ...

  4. xml解析-jaxp删除结点

    jaxp删除结点 / 删除sex结点 * 1.创建解析器工厂 * 2.根据解析器工厂创建解析器 * 3.解析xml返回document * * 4.得到sex结点 * 5.得到sex的父节点 getP ...

  5. form分辨率

    近期做项目时,遇到开发的winform在自己电脑上可以正常显示,共享到其他电脑就事儿不能显示了: [转载自:http://blog.csdn.net/lcawen88/article/details/ ...

  6. Vue实战狗尾草博客后台管理系统第七章

    Vue实战狗尾草博客后台管理平台第七章 本章内容为借助模块化来阐述Vuex的进阶使用. 在复杂项目的架构中,对于数据的处理是一个非常头疼的问题.处理不当,不仅对维护增加相当的工作负担,也给开发增加巨大 ...

  7. 在C++工程上添加CUDA编译环境

    1.直接在新建工程的时候选择CUDA,这样的工程既能编译C++也能编译CU 2.在已有的C++工程上添加CUDA编译环境 右键工程-->生成依赖项-->生成自定义-->勾选CUDA ...

  8. 具名插槽 slot (二)

    slot 是父组件与子组件的通信方式可以将父组件的内容显示在子组件当中或者说可以将 让你封装的组件变的更加的灵活,强壮! 在子组件中  通过为多个slot进行命名.来接受父组件中的不同内容的数据  这 ...

  9. 批量执行用例,且结果显示在html中,python控制台也会显示运行过程

    下载HTMLTestRunner.py放到python安装目录的Lib下: 如下以登录为例,创建不同的.py文件, ========================================== ...

  10. SpringCloud学习笔记(三、SpringCloud Netflix Eureka)

    目录: 服务发现简介 SpringCloud Netflix Eureka应用 Eureka高可用 Eureka源码分析 >>> Eureka Client初始化(客户端定时获取服务 ...