internal void Initize(BufferManager bufferManager) { for (int i = 0; i < Capacity; i++) { SocketAsyncEventArgs args = new SocketAsyncEventArgs(); if (bufferManager != null) { bufferManager.SetBuffer(args); SocketAsyncToken socketAsyncToken = new SocketAsyncToken(); socketAsyncToken.OriginOffset = args.Offset; socketAsyncToken.OffsetDelta = args.Offset; socketAsyncToken.BufferSize = args.Buffer.Length/Capacity; socketAsyncToken.TokenID = string.Empty; args.UserToken = socketAsyncToken; BufferSize = socketAsyncToken.BufferSize; } Push(args); } }
public UdpSocketAsyncBuffer() { SocketToken = new SocketAsyncToken();LastSocketTime = DateTime.Now; }
/// <summary> /// use protocol and handler to process received message /// </summary> /// <param name="buffer">message buffer</param> /// <param name="bytesTransferred">received message length</param> /// <param name="token">user token</param> /// <returns>user token for next receive </returns> private SocketAsyncToken ReceiveMessageProcess(byte[] buffer, int bytesTransferred, SocketAsyncToken token, IPEndPoint remoteEndPoint) { IMsg request; token.LastLength += bytesTransferred; token.OffsetDelta += bytesTransferred; int usedByte = m_RquestProtocol.ProtocolProcess(buffer, token.OriginOffset, token.LastLength, out request); if (request != null && usedByte > 0) { while (request != null && usedByte > 0) //protocol parse success { token.LastLength -= usedByte; if (token.LastLength > 0) { Buffer.BlockCopy(buffer, token.OriginOffset + usedByte, buffer, token.OriginOffset, token.LastLength); Array.Clear(buffer, token.OriginOffset + token.LastLength, usedByte); token.OffsetDelta -= usedByte; } else { Array.Clear(buffer, token.OriginOffset, usedByte); token.OffsetDelta = token.OriginOffset; token.LastLength = 0; } //request message filter process int index = 0; while (index < m_RequestFilters.Count) { try { bool result = m_RequestFilters[index].ProcessFilter(ref request); //to do use result } catch (Exception ex) { throw new FilterErrorException("request message filter process occur error", m_RequestFilters[index].FilterName, ex); } index++; } request.Context = new Dictionary<string, object>(); request.Context.Add("remoteEndPoint", remoteEndPoint); request.RemoteIpEndPoint = new IPEndPoint(remoteEndPoint.Address, remoteEndPoint.Port); ; //request message handler process foreach (var handler in m_RequestHandlers) { if (handler.HandleCmdCode == request.CommandCode) { try { bool result = handler.ExecuteHandler(request, this); } catch (Exception ex) { m_Logger.ErrorLogger(ex.InnerException.Message, ex.InnerException.StackTrace); } break; } } // continue process last bytes usedByte = m_RquestProtocol.ProtocolProcess(buffer, token.OriginOffset, token.LastLength, out request); } } else if (request == null && usedByte > 0) { Array.Clear(buffer, token.OriginOffset, token.LastLength); token.Reset(); } return token; }