-3

I'm guessing this doesn't work because of the StreamReader being non thread safe, (don't know howto fix that, google is no help)

Anyway I've been trying to figure exactly whats wrong with this code, it works 80% of the time, other times it fails to parse incoming packets and will just drop them.

This is a void for a http-like tcp server im writing. it works exactly like an http packet, but the "CONTENT-LENGTH" header tells it the length of the packets data (payload). This is where the problem is happening. Can anyone suggest to me howto improve this and fix this? because I'm completely lost.

    void InternalStart()
    {

        bool continueWhile = true;
        while (continueWhile)
        {
            if (SR.EndOfStream)
            {
                continueWhile = false;
                break;
            }
            if (par_ReadStatus != ReadStatusEnum.WaitingForPayload)
            {
                int charCode = SR.Peek();
                if (charCode == -1)
                {
                    continueWhile = false;
                    break;
                }
                string outputLine = "";
                outputLine = SR.ReadLine();
                ReadLine(outputLine);
            }
            else if (par_ReadStatus == ReadStatusEnum.WaitingForPayload)
            {
                int length = int.Parse(par_ParsingPacket.Attributes["CONTENT-LENGTH"]);
                char[] array = new char[length];
                for (int i = 0; i < length; i++)
                {
                    array.SetValue(Convert.ToChar(SR.Read()), i);
                }
                string payload = new string(array);
                ReadLine(payload);
            }
        }
        if (ReadEnd != null)
        {
            ReadEnd();
        }
    }
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Harold
  • 11
  • 1
  • 3
  • Can you give more detailed information on the error you are encountering? – Grant H. Feb 05 '12 at 07:29
  • possible duplicate of [StreamWriter Multi Threading C#](http://stackoverflow.com/questions/19304209/streamwriter-multi-threading-c-sharp) – Liam Apr 28 '15 at 14:13

1 Answers1

4

StreamReader being non thread safe, (don't know howto fix that, google is no help)

Simple. Beginner programmer level: Do not read the StreamReader from more than one thread. A design trying to do so is a failure to understand what a stream is and how efficient multi thread programming works.

There is no need to have multiple threads hit a single stream reader at all. You have to isolate threads before and assin a stream reader exclusively to a specific thread for the time of handling the data. If you want to get professional and fast you work like IIS and suck data out in infrastructure threads that then feed of work packets into a worker queue multiple threads work off.

And dependingo n performance requriements you may want to work off sockets and use the async socket mechanisms to make sure you are not wasting 1000 threads for 1000 operaions in progress at a great cost without any benefit.

Anyway i've been trying to figure exactly whats wrong with this code,

Ah - nice try. Sadly you neither tell us what problem you really have nor does your code show anything using threads, so at the end your question and the code fail to make any sense in combination.

TomTom
  • 61,059
  • 10
  • 88
  • 148