1
// This program is free software: you can redistribute it and/or modify
2
// it under the terms of the GNU General Public License as published by
3
// the Free Software Foundation, either version 3 of the License, or
4
// (at your option) any later version.
6
// This program is distributed in the hope that it will be useful,
7
// but WITHOUT ANY WARRANTY; without even the implied warranty of
8
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
// GNU General Public License for more details.
11
// You should have received a copy of the GNU General Public License
12
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
using System.Net.Sockets;
18
namespace SqueezeCenter
20
public class NetworkStreamTextReader
23
StringBuilder data = new StringBuilder ();
24
byte[] readBuffer = new byte[1024];
25
bool disconnected = false;
27
public NetworkStreamTextReader (System.Net.Sockets.NetworkStream stream)
30
stream.BeginRead (readBuffer, 0, readBuffer.Length, new System.AsyncCallback (CB), null);
33
private void CB (IAsyncResult ar)
35
int numberOfBytesRead = stream.EndRead (ar);
36
if (numberOfBytesRead == 0)
39
this.disconnected = true;
45
data.Append (Encoding.ASCII.GetString (readBuffer, 0, numberOfBytesRead));
49
stream.BeginRead (readBuffer, 0, readBuffer.Length, new System.AsyncCallback (CB), null);
52
public string ReadLine ()
56
if (this.disconnected)
58
//throw new System.IO.IOException ("Connection closed");
61
// return first line of data or null if no data is available
67
i = data.ToString().IndexOf ('\n');
69
string result = data.ToString (0, i);
70
data = data.Remove (0, i+1);
b'\\ No newline at end of file'