C # exception

I have a method that is a listener for a TCP client that looks like this:

    private static void ProcessClient(
        Object obj)
    {
        ISession session = (ISession)obj;
        NetworkStream networkStream = null;

        try
        {
            DebugUtility.SetThreadName("Worker: {0}", session.Name);
            networkStream = session.TcpClient.GetStream();
            networkStream.ReadTimeout = Config.ReadTimeout;

            // Loop received packets (blocks untill next packet)
            Int32 packetSize;
            Byte[] buffer = new Byte[session.PacketSize];
            while ((packetSize = networkStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                // Get String from packet  bytes
                String packet = Encoding.UTF8.GetString(buffer, 0, packetSize);

                // Check if packet has data
                if (String.IsNullOrEmpty(packet))
                    continue;

                // Log biggest received package
                DebugUtility.CheckMaxPacketSize(session.Name, packet.Length);

                // Handle packet (in new thread)
                Logger.DebugLog("Received: {0}", packet);
                ThreadPool.QueueUserWorkItem(session.HandlePacket, packet);
            }
        }
        catch (ObjectDisposedException) { }
        catch (NotSupportedException) { }
        catch (TimeoutException) { }
        catch (SocketException) { }
        catch (IOException) { }
        catch (Exception ex)
        {
            Logger.LogException(ex);
        }
        finally
        {
            if (networkStream != null)
                networkStream.Close();

            if (session != null)
                session.Disconnect();
        }
    }

This is for the game service, but when I check my logs, I sometimes see this error:

System.Int32 Read(Byte[], Int32, Int32): The stream does not support reading.
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at BusinessLayer.Listener.ListenerWorker.ProcessClient(Object obj) in C:\path\ListenerWorker.cs:line 141 Line: 0

This is the above file and line 141

        while ((packetSize = networkStream.Read(buffer,....

Now I found that a NotSupportedException throws this error, but why does it go away? Why is it not ignored, but does it go through a regular Exception ex-handler?

Edit: Does anyone know how I can raise this exception? When does this happen? I see that it is returning to my logs to other users, but I do not know when this will happen.

+3
source share
2 answers

NetworkStream.Read InvalidOperationException, NotSupportedException ( ). Reflector:

if (!this.CanRead)
{
    throw new InvalidOperationException(SR.GetString("net_writeonlystream"));
}
+4

, 100% , - . , , , . , OutOfMemoryException StackOverflowException, ? , , :)

0

Source: https://habr.com/ru/post/1786649/


All Articles