Example #1
0
        private void ReceiveCallback(IAsyncResult _AsyncResult)
        {
            try
            {
                int bytesRead = m_Socket.EndReceive(_AsyncResult);
                if (bytesRead > 0)
                {
                    int bytesHandled = 0;
                    while (bytesHandled != bytesRead)
                    {
                        if (m_CurrentIncommingMessage == null)
                        {
                            m_CurrentIncommingMessage = m_Server._CreateMessage(this);
                        }

                        int bytesAdded = m_CurrentIncommingMessage._AddData(m_RecvBuffer, bytesHandled, bytesRead - bytesHandled);
                        if (m_CurrentIncommingMessage.IsComplete() == true)
                        {
                            //Paketet är färdigt
                            m_Server._AddReceivedMessage(m_CurrentIncommingMessage);
                            m_CurrentIncommingMessage = null;
                        }
                        bytesHandled += bytesAdded;
                    }

                    //Fortsätt receive
                    m_Socket.BeginReceive(m_RecvBuffer, 0, m_RecvBufferSize, 0, new AsyncCallback(ReceiveCallback), this);
                }
                else
                {
                    m_CurrentReceiving = false;
                }
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                m_CurrentReceiving = false;
                if (ex.NativeErrorCode.Equals(10054)) //WSAECONNRESET "An existing connection was forcibly closed by the remote host"
                {
                    //Do nothing. just shutdown connection
                }
                else
                {
                    m_Exceptions.Enqueue(ex);
                }
            }
            catch (Exception ex)
            {
                m_Exceptions.Enqueue(ex);
                m_CurrentReceiving = false;
            }
        }
        private void ReceiveCallback(IAsyncResult _AsyncResult)
        {
            try
            {
                int bytesRead = m_Socket.EndReceive(_AsyncResult);
                if (bytesRead > 0)
                {
                    int bytesHandled = 0;
                    while (bytesHandled != bytesRead)
                    {
                        if (m_CurrentIncommingMessage == null)
                        {
                            m_CurrentIncommingMessage = _CreateMessage();
                        }

                        int bytesAdded = m_CurrentIncommingMessage._AddData(m_RecvBuffer, bytesHandled, bytesRead - bytesHandled);
                        if (m_CurrentIncommingMessage.IsComplete() == true)
                        {
                            //Paketet är färdigt
                            _AddReceivedMessage(m_CurrentIncommingMessage);
                            m_CurrentIncommingMessage = null;
                        }
                        bytesHandled += bytesAdded;
                    }

                    //Fortsätt receive
                    m_Socket.BeginReceive(m_RecvBuffer, 0, m_RecvBufferSize, 0, new AsyncCallback(ReceiveCallback), this);
                }
                else
                {
                    m_CurrentReceiving = false;
                }
            }
            catch (Exception ex)
            {
                m_Connected.Reset();
                m_Exceptions.Enqueue(ex);
                if (m_Socket != null)
                {
                    Logger.LogException(ex);
                }
                m_CurrentReceiving = false;
            }
        }