private void BindArgsForUse(SimpleBuffer buf, SocketStreamAsyncArgs args)
 {
     args.InternalBuffer = buf;
     args.CompletionRtn  = new EventHandler <SocketAsyncEventArgs>(this.IO_Completed);
     args.Completed     += args.CompletionRtn;
     args.SetBuffer(buf.Buffer, 0, buf.Buffer.Length);
 }
        private void ReleaseArgsToPools(SocketStreamAsyncArgs args)
        {
            SimpleBuffer internalBuffer = args.InternalBuffer;

            args.Completed     -= args.CompletionRtn;
            args.CompletionRtn  = null;
            args.InternalBuffer = null;
            if (internalBuffer != null)
            {
                this.m_bufPool.TryReturnObject(internalBuffer);
            }
            if (!this.m_asyncArgsPool.TryReturnObject(args))
            {
                args.Dispose();
            }
        }
        private SocketStreamAsyncArgs ObtainInternalBuffer(int readSize)
        {
            SocketStreamAsyncArgs socketStreamAsyncArgs = null;
            SimpleBuffer          simpleBuffer          = null;
            bool flag = false;

            try
            {
                simpleBuffer = this.m_bufPool.TryGetObject(readSize);
                if (simpleBuffer == null)
                {
                    if (readSize <= this.m_bufPool.BufferSize)
                    {
                        ReplayCrimsonEvents.SocketStreamBufferExhaustion.LogPeriodic(Environment.MachineName, Parameters.CurrentValues.DefaultEventSuppressionInterval);
                    }
                    return(null);
                }
                socketStreamAsyncArgs = this.m_asyncArgsPool.TryGetObject();
                if (socketStreamAsyncArgs == null)
                {
                    this.m_extraArgCount++;
                    socketStreamAsyncArgs = new SocketStreamAsyncArgs(false);
                }
                this.BindArgsForUse(simpleBuffer, socketStreamAsyncArgs);
                this.m_readArgs       = socketStreamAsyncArgs;
                this.m_bufIsAllocated = true;
                flag = true;
            }
            finally
            {
                if (!flag)
                {
                    if (socketStreamAsyncArgs != null)
                    {
                        this.ReleaseArgsToPools(socketStreamAsyncArgs);
                        socketStreamAsyncArgs = null;
                    }
                    else if (simpleBuffer != null)
                    {
                        this.m_bufPool.TryReturnObject(simpleBuffer);
                        simpleBuffer = null;
                    }
                }
            }
            return(socketStreamAsyncArgs);
        }