1 // Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
4 using System
.Collections
.Generic
;
5 using System
.Net
.Sockets
;
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
;
27 while (m_lines
.Count
== 0)
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)
46 return m_lines
.Dequeue();
49 public bool IsEndOfStream
53 if (m_lines
.Count
== 0)
58 return (m_lines
.Count
== 0);
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
89 m_partLine
+= lines
[0];
91 if (numLines
> 1 || strEndsWithNewLine
)
93 // m_partLine must now be complete
94 m_lines
.Enqueue(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
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");