Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Windows 10 IoT StreamSocketListener randomly stops listening in release mode

Ive been working on some communication between a PC and a Raspberry PI 2 with Windows 10 IoT (build 10586 TH2) using "StreamSocketListener". This seems to work fine in debug mode, but when testing the code in release mode the "StreamSocketListener" seems to randomly stop responding to requests.

In debug mode, on the RPI2, I've had sessions with 100k requests without a single issue, but when I push the release build it stops, randomly, quite quickly (typically after a couple of hundreds requests). The request is of static type, so the input is the same every time.

Has anyone experienced the same issue, and is there a solution to this problem?

The code is based on this blog post:

A simple in-process HTTP server for Windows 8 Metro apps

 private void Listen()
 {
      _listener = new StreamSocketListener();
      _listener.ConnectionReceived += (s, e) => ProcessRequestAsync(e.Socket);
      _listener.BindServiceNameAsync(requestPort.ToString());
 }
private async void ProcessRequestAsync(StreamSocket socket)
    {
        try
        {
            // this works for text only
            StringBuilder request = new StringBuilder();
            using (IInputStream input = socket.InputStream)
            {
                byte[] data = new byte[BufferSize];
                IBuffer buffer = data.AsBuffer();
                uint dataRead = BufferSize;
                while (dataRead == BufferSize)
                {
                    await input.ReadAsync(buffer, BufferSize, InputStreamOptions.Partial);
                    request.Append(Encoding.UTF8.GetString(data, 0, data.Length));
                    dataRead = buffer.Length;
                }
            }

            using (IOutputStream output = socket.OutputStream)
            {
                string requestMethod = request.ToString().Split('\n')[0];
                string[] requestParts = requestMethod.Split(' ');

                if (requestParts[0] == "GET")
                    await WriteResponseAsync(requestParts[1], output);
                else
                    throw new InvalidDataException("HTTP method not supported: "
                                                   + requestParts[0]);
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine("Main ex: " + e);
        }
        RequestCount++;
    }
like image 689
Gnm Avatar asked Sep 25 '22 15:09

Gnm


1 Answers

After a whole day long term testing with the "Compile with .NET Native tool chain" setting turned ON for my UWP project this issue seems to be fixed. When disabling this feature the issue occur after just a couple of minutes.

Because I cant accept a comment as an answer, I'll just answer my own question, and accept it. But all thanks to Matt for pointing me in the right direction.

Interesting... does this still reproduce if you disable .NET Native compilation? (Project properties > BUILD > "Enable .NET Native toolchain") – Matt Whilden

like image 60
Gnm Avatar answered Sep 28 '22 05:09

Gnm