0

I've written a server side application that should manage many clients. Everything is done asynchronously using BeginSend() and EndSend() methods.

In some cases I need to send two subsequent requests to one of the clients. I wish to make sure that between first BeginSend() and the last EndSend() no new BeginSend()s are called. I though about doing this with AutomaticResetEvent, such that if the server tries to send two messages two the client, the second message will block until the first one was sent

  1. Does this make sense to do it in the way above?
  2. The code blow sometime throws "NullReferenceException". and I can't seem to understand why?

Thanks for the insights.

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;
        }

    }


public void SendAsyncData(string str, CommandsToClient cmd)
        {
            byte[] data = Combine(BitConverter.GetBytes((int)(cmd)), BitConverter.GetBytes((int)(str.Length)), Encoding.ASCII.GetBytes(str));
            SendAutoResetEvent.WaitOne();
            SendAsyncDataWithHeader(data);
        }


        private void SendAsyncDataWithHeader(byte[] data)
        {
            int toSend;
            byte[] dataWithHeader = Combine(BitConverter.GetBytes(data.Length), data);
            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)
            {

                Disconnected(this);
            }

        }

        void sendCallback(IAsyncResult ar)
        {
            try
            {
                int bytesSent = socket.EndSend(ar);
                if (bytesSent == 0)
                {
                    if (Disconnected != null)
                    {
                        Disconnected(this);
                        return;

                    }
                }
                sendBuffer.sent += bytesSent;
                if (sendBuffer.sent == sendBuffer.BUFFER.Length)
                {
                    //all data was sent. do some
                    //do some processing...
                    sendBuffer.Dispose();
                    //let new send request in
                    SendAutoResetEvent.Set();
                    return;
                }
                int toSend;
                // exception says that in the following line, sendBuffer.Buffer is null....
                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 (NullReferenceException ex)
            {
                //here an exception is thrown, 
            }

            catch (Exception ex)
            {

                Disconnected(this);
            }

        }

    }
}
Daniel
  • 1,319
  • 14
  • 19

0 Answers0