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

1cd74d6224fffc88eefaaa919c14fb51.png

16334daeea3ebf22c2dab4ead4ff817f.png

2477c2605fc8deb46157ba96362d04ba.png

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.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.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);

}

}

内容来源于网络如有侵权请私信删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值