Exemple #1
0
        void BeginReceive(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
            {
                byte[] data = new byte[e.BytesTransferred];
                Buffer.BlockCopy(e.Buffer, e.Offset, data, 0, data.Length);
                if (this.BinaryInput != null)
                {
                    this.BinaryInput(data, e);
                }

                if (!e.AcceptSocket.ReceiveAsync(e))
                {
                    BeginReceive(e);
                }
            }
            else
            {
                string message = string.Format("User Disconnect :{0}", e.AcceptSocket.RemoteEndPoint.ToString());

                LogOutEvent(null, LogType.Error, message);

                if (MessageInput != null)
                {
                    MessageInput(message, e, 0);
                }

                e.AcceptSocket = null;
                BuffManagers.FreeBuffer(e);
                SocketAsynPool.Push(e);
                if (SocketAsynPool.Count == 1)
                {
                    Accept();
                }
            }
        }
        void BeginReceive(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
            {
                if (!IsOffsetInput)
                {
                    byte[] data = new byte[e.BytesTransferred];

                    Buffer.BlockCopy(e.Buffer, e.Offset, data, 0, data.Length);

                    if (this.BinaryInput != null)
                    {
                        this.BinaryInput(data, e);
                    }
                }
                else
                {
                    if (this.BinaryOffsetInput != null)
                    {
                        this.BinaryOffsetInput(e.Buffer, e.Offset, e.BytesTransferred, e);
                    }
                }

                if (!e.AcceptSocket.ReceiveAsync(e))
                {
                    BeginReceive(e);
                }
            }
            else
            {
                if (e.AcceptSocket != null && e.AcceptSocket.RemoteEndPoint != null)
                {
                    string message;

                    try
                    {
                        message = string.Format("User Disconnect :{0}", e.AcceptSocket.RemoteEndPoint.ToString());
                    }
                    catch (System.NullReferenceException)
                    {
                        message = "User Disconect";
                    }


                    LogOutEvent(null, LogType.Error, message);

                    if (MessageInput != null)
                    {
                        MessageInput(message, e, 0);
                    }
                }
                else
                {
                    if (MessageInput != null)
                    {
                        MessageInput("User Disconnect But cannot get Ipaddress", e, 0);
                    }
                }

                e.AcceptSocket = null;
                BuffManagers.FreeBuffer(e);
                SocketAsynPool.Push(e);
                if (SocketAsynPool.Count == 1)
                {
                    Accept();
                }
            }
        }