The problem I'm currently experiencing, is that after a few seconds both my client and server application start claiming 100% of the CPU. I've placed Thread.Sleep() functions in the loops to make sure they're don't claim everything, but it's not helping. Below is the loop function for my 4 threads. Someone who can point out whats causing this?
All Threads rely on the Thread class of .NET as a private member of my components. I've stripped all comments to ensure the code fits. I know some paths in the loops do not enter the sleep, but the logic here says that work needs to be performed. The idea is that the client sends a single request and the server pushes data back untill told to stop.
Parser loop: parses the data received from the tcp socket.
Expand|Select|Wrap|Line Numbers
- void ProcessRoutine()
- {
- m_running = true;
- while(m_running)
- {
- try
- {
- if (Buffer.Length > 0)
- {
- int start_position = 0;
- while (start_position < Buffer.Length - 4)
- {
- string section = Encoding.ASCII.GetString(Buffer.Read(start_position, 4));
- if (string.Equals(section, "@VT:"))
- {
- int binary_size = BitConverter.ToInt32(Buffer.Read(start_position + 8, 4), 0);
- if ((start_position + 12 + binary_size) <= Buffer.Length)
- {
- byte[] symbol = Buffer.Read(start_position + 12 + binary_size, 1);
- if (Encoding.ASCII.GetString(symbol, 0, 1) == "#")
- {
- int end_position = start_position + 12 + binary_size + 1;
- IVTMessage message = IVTMessageBuilder.Construct(Buffer.Read(start_position, end_position));
- Buffer.Delete(start_position, end_position);
- RaiseOnMessageEvent(message);
- start_position = 0;
- break;
- }
- }
- else
- {
- Buffer.Delete(start_position, start_position + 12 + binary_size + 1);
- start_position = 0;
- break;
- }
- }
- else
- {
- start_position++;
- }
- }
- }
- else
- {
- Thread.Sleep(50);
- }
- }
- catch (ThreadAbortException)
- {
- m_running = false;
- Debug.WriteLine("Background thread has been aborted.");
- }
- catch (Exception ex)
- {
- Debug.WriteLine(" Exception message");
- Debug.WriteLine("========================");
- Debug.WriteLine(ex.Message);
- Debug.WriteLine(" InnerException message");
- Debug.WriteLine("========================");
- Debug.WriteLine(ex.InnerException.Message);
- Debug.WriteLine(" StackTrace");
- Debug.WriteLine("========================");
- Debug.WriteLine(ex.StackTrace);
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- void ListenRoutine()
- {
- m_running = true;
- while (m_running)
- {
- try
- {
- if (m_socket != null)
- {
- lock (m_lock)
- {
- if (Connected && (m_socket.Available > 0))
- {
- int data_available = m_socket.Available;
- byte[] buffer = new byte[data_available];
- int bytes_read = m_socket.Receive(buffer, 0, data_available, SocketFlags.None);
- RaiseOnDataEvent(buffer, bytes_read, (m_socket.RemoteEndPoint as IPEndPoint).Address.ToString(), (m_socket.RemoteEndPoint as IPEndPoint).Port);
- }
- }
- Thread.Sleep(50);
- }
- else
- {
- throw new Exception("The internal socket has been closed/disposed.");
- }
- }
- catch (Exception ex)
- {
- RaiseOnErrorEvent("ListenRoutine", ex);
- Disconnect();
- }
- }
- }
Also, code still belongs to me. You can use it if it's usefull, but please put in the source where you got it from.