为什么AutoResetEvent不工作BeginSend?

时间:2013-03-14 18:06:54

标签: c# multithreading asynchronous asyncsocket

我编写了一个服务器端应用程序,使用BeginSend()EndSend()来管理许多客户端

我在过去几个小时里一直在努力确保没有为同一个客户调用后续的BeginSend()来电。

以下代码段似乎无法实现我的目标,并且在第一次完成之前不会阻止对SendAsyncData的第二次调用。

我的错误是什么?

struct SendBuffer
{
    public const int BUFFER_SIZE = 1024 * 128;
    public byte[] BUFFER;
    public int sent;

    public SendBuffer(byte[] data)
    {
        BUFFER = new byte[data.Length];
        Buffer.BlockCopy(data, 0, BUFFER, 0, data.Length);
        sent = 0;
    }

    public void Dispose()
    {
        BUFFER = null;
        sent = 0;
    }
}

private void SendAsyncDataWithHeader(byte[] data
{
    int toSend;
    byte[] dataWithHeader = Combine(BitConverter.GetBytes(data.Length), data);

    SendAutoResetEvent.WaitOne();
    sendBuffer = new SendBuffer(dataWithHeader);

    if (sendBuffer.BUFFER.Length - sendBuffer.sent > SendBuffer.BUFFER_SIZE)
        toSend = SendBuffer.BUFFER_SIZE;
    else
        toSend = sendBuffer.BUFFER.Length - sendBuffer.sent;

    try
    {
        socket.BeginSend(sendBuffer.BUFFER, 0, toSend, SocketFlags.None, sendCallback, null);
    }
    catch (SocketException se)
    {
        switch (se.SocketErrorCode)
        {
            case SocketError.ConnectionAborted:
            case SocketError.ConnectionReset:
                if (Disconnected != null)
                {
                    Disconnected(this);
                }
                break;
        }
    }
    catch (ObjectDisposedException)
    {
    }
    catch (NullReferenceException ex)
    {
    }
    catch (Exception ex)
    {
        Server.logger.Error(ex);
        Disconnected(this);
    }
}

void sendCallback(IAsyncResult ar)
{
    int bytesSent;
    try
    {
        bytesSent = socket.EndSend(ar);
        if (bytesSent == 0)
        {
            if (Disconnected != null)
            {
                Disconnected(this);
                return;
            }
        }
        sendBuffer.sent += bytesSent;
        if (sendBuffer.sent == sendBuffer.BUFFER.Length)
        {
            sendBuffer.Dispose();

            SendAutoResetEvent.Set();
            Server.logger.Info("set {0}", userName);
        }
        else
        {
            int toSend;
            if (sendBuffer.BUFFER.Length - sendBuffer.sent > SendBuffer.BUFFER_SIZE)
                toSend = SendBuffer.BUFFER_SIZE;
            else
                toSend = sendBuffer.BUFFER.Length - sendBuffer.sent;

            socket.BeginSend(sendBuffer.BUFFER, sendBuffer.sent, toSend, SocketFlags.None, sendCallback, null);
        }
    }
    catch (SocketException se)
    {
        switch (se.SocketErrorCode)
        {
            case SocketError.ConnectionAborted:
            case SocketError.ConnectionReset:
                if (Disconnected != null)
                {
                    Disconnected(this);
                    return;
                }
                break;
        }
    }
    catch (ObjectDisposedException ex)
    {
    }
    catch (Exception ex)
    {
        Server.logger.Error(ex);
        Disconnected(this);
    }
}

0 个答案:

没有答案