Exemplo n.º 1
0
        private void SendCompleted(SocketAsyncEventArgs e)
        {
            SocketAsyncEventArgsX ex = (SocketAsyncEventArgsX)e;

            Buffers.Buffer buffer  = (Buffers.Buffer)ex.BufferX;
            ISession       session = ex.Session;

            try
            {
                if (e.SocketError == SocketError.IOPending || e.SocketError == SocketError.Success)
                {
                    if (session.Server.EnableLog(LogType.Trace))
                    {
                        session.Server.Log(LogType.Trace, session, "{0} send hex:{1}", session.RemoteEndPoint,
                                           BitConverter.ToString(ex.BufferX.Data, 0, e.BytesTransferred).Replace("-", string.Empty).ToLower()
                                           );
                    }
                    if (this.Options.Statistical)
                    {
                        System.Threading.Interlocked.Increment(ref mSendQuantity);
                        System.Threading.Interlocked.Add(ref mSendBytes, e.BytesTransferred);
                    }
                    if (e.BytesTransferred < e.Count)
                    {
                        buffer.Postion = (buffer.Postion + e.BytesTransferred);
                        buffer.SetLength(buffer.Length - e.BytesTransferred);
                        buffer.AsyncTo(session.SendEventArgs, session);
                    }
                    else
                    {
                        IBuffer nextbuf = buffer.Next;
                        buffer.Free();
                        if (nextbuf != null)
                        {
                            ((TcpSession)session).CommitBuffer(nextbuf);
                        }
                        else
                        {
                            ((TcpSession)session).SendCompleted();
                            if (session.SocketProcessHandler != null)
                            {
                                try
                                {
                                    session.SocketProcessHandler.SendCompleted(session, e);
                                }
                                catch (Exception ce_)
                                {
                                    if (EnableLog(LogType.Error))
                                    {
                                        Error(ce_, ex.Session, "{0} send data completed process handler error {1}!", ex.Session.RemoteEndPoint, ce_.Message);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (EnableLog(LogType.Debug))
                    {
                        Log(LogType.Debug, session, $"{session.RemoteEndPoint} send close error {e.SocketError} receive:{e.BytesTransferred}");
                    }
                    Buffers.Buffer.Free(buffer);
                    session.Dispose();
                }
            }
            catch (Exception e_)
            {
                if (EnableLog(LogType.Warring))
                {
                    Error(e_, ex.Session, "send data completed SocketError {0}!", e.SocketError);
                }
            }
        }