user7183273
user7183273

Reputation:

SendAsync and SocketAsyncEventArgs correct usage with fixed tickrate

im currently working on a TCP Server using Sockets in C#. I encountered a problem and i'm not able to solve it..

My Server accepts connections and creates a new NetEntity for each client. These NetEntities handle Sending/Receive packets and are updated in a loop that ticks 30 times per second.

Problem : After the 4th client is connected i receive this exception(only on the 4th NetEntity).

An asynchronous socket operation is already in progress using this SocketAsyncEventArgs instance.

If i use if (Interlocked.Read(ref m_sending) == 1) return; to make sure it is done sending, the 4th NetEntity never send any data. When deleting this line the exception occurs.

I just cant wrap my head around using SendAsync and SocketAsyncEventArgs properly with a fixed tickrate.

        public void Send(Packet packet)
        {
            OutgoingQueue.Enqueue(packet);
        }

        private void QueueSend()
        {
            Interlocked.Exchange(ref m_sending, 1);

            var sendTask = new Task(Send);
            sendTask.ContinueWith((t) => t.Dispose());
            sendTask.Start();
        }

        private void Send()
        {
            if (Interlocked.Read(ref m_sending) == 1) return;
            if (!Socket.Connected) return;

            if (OutgoingQueue.TryDequeue(out var packet))
            {
                packet.Serialize(m_packetWriter);
            }

            var writerBuffer = m_packetWriter.ToArray();
            Array.Copy(writerBuffer, m_sendBuffer, writerBuffer.Length);


            try
            {
                SendArgs.SetBuffer(SendArgs.Offset, writerBuffer.Length);

                if (!Socket.SendAsync(SendArgs))
                {
                    SendCompleted(Socket, SendArgs);
                }
            }
            catch (SocketException)
            {
                //Client disconnect
            }
            catch (Exception ex)
            {
                Log.Error($"NetEntity {m_id} send exception: {ex.Message} - {ex.StackTrace} - {ex.InnerException}");
            }
        }

        private void SendCompleted(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                if (OutgoingQueue.Count > 0)
                {
                    //QueueSend();
                }

                m_packetWriter.Reset();
            }
            else
            {
                Interlocked.Exchange(ref m_sending, 0);
            }
        }

Setup looks like this

    SendArgs = new SocketAsyncEventArgs();
    SendArgs.SetBuffer(m_sendBuffer, 0, BufferSize);
    SendArgs.Completed += SendCompleted;

I appreciate any help and code Improvements.

Thanks!

Upvotes: 0

Views: 1009

Answers (0)

Related Questions