!B (Sandbox) (CE-21795) Importing models with multisubmaterials via fbx switches...
[CRYENGINE.git] / Code / Tools / Statoscope / Statoscope / SocketLogDataStream.cs
blob03b33786fab7df4d223175a56b04e4aafe507f9a
1 // Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
3 using System;
4 using System.Collections.Generic;
5 using System.Net.Sockets;
7 namespace Statoscope
9 class SocketLogDataStream : ILogDataStream
11 NetworkStream m_netStream;
12 Queue<string> m_lines = new Queue<string>();
13 string m_partLine = "";
14 Byte[] m_buffer = new Byte[16 * 1024];
16 public SocketLogDataStream(NetworkStream netStream)
18 m_netStream = netStream;
21 public void Dispose()
25 public int Peek()
27 while (m_lines.Count == 0)
29 GetMoreLines();
32 string nextLine = m_lines.Peek();
34 //System.Console.WriteLine(nextLine);
36 return nextLine != "" ? nextLine[0] : 0;
39 public string ReadLine()
41 while (m_lines.Count == 0)
43 GetMoreLines();
46 return m_lines.Dequeue();
49 public bool IsEndOfStream
51 get
53 if (m_lines.Count == 0)
55 GetMoreLines();
58 return (m_lines.Count == 0);
62 void GetMoreLines()
64 while (m_lines.Count == 0)
66 while (!m_netStream.DataAvailable)
70 //we may need a few read attempts to get a whole line (eg for screen shots)
71 int numBytesRead = m_netStream.Read(m_buffer, 0, m_buffer.Length);
73 if (numBytesRead <= 0)
75 throw new Exception("NetworkStream Read failed");
78 string str = System.Text.Encoding.ASCII.GetString(m_buffer, 0, numBytesRead);
79 string[] lines = str.Split(new char[] { '\n' });
80 int numLines = lines.Length;
81 bool strEndsWithNewLine = str.EndsWith("\n");
83 if (strEndsWithNewLine)
85 // Split will have put an empty string on the end of lines
86 numLines--;
89 m_partLine += lines[0];
91 if (numLines > 1 || strEndsWithNewLine)
93 // m_partLine must now be complete
94 m_lines.Enqueue(m_partLine);
95 m_partLine = "";
98 if (numLines > 1 && !strEndsWithNewLine)
100 // the last line is incomplete so keep it in m_partLine for later completion
101 // note: the previous m_partLine must have already been added to m_lines above
102 numLines--;
103 m_partLine = lines[numLines];
106 // don't take the first one as this will have been taken already to complete the last part line
107 for (int i = 1; i < numLines; i++)
109 m_lines.Enqueue(lines[i]);
112 //if (m_lines.Count == 0)
114 //System.Console.WriteLine("Waiting for lines");
118 if (m_lines.Count == 0)
120 System.Console.WriteLine("No more lines");