~iwarford/do-plugins/fart-plugin-fwiw

« back to all changes in this revision

Viewing changes to SqueezeCenter/src/NetworkStreamTextReader.cs

  • Committer: Jason Jones
  • Date: 2008-12-24 04:45:02 UTC
  • mfrom: (335.1.9 do-plugins)
  • Revision ID: jasonedwardjones@gmail.com-20081224044502-ra56ym06cp1iqs7t
MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
5
//
 
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.
 
10
//
 
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/>.
 
13
 
 
14
using System;
 
15
using System.Net.Sockets;
 
16
using System.Text;
 
17
 
 
18
namespace SqueezeCenter
 
19
{       
 
20
        public class NetworkStreamTextReader
 
21
        {
 
22
                NetworkStream stream;
 
23
                StringBuilder data = new StringBuilder ();
 
24
                byte[] readBuffer = new byte[1024];
 
25
                bool disconnected = false;
 
26
                
 
27
                public NetworkStreamTextReader (System.Net.Sockets.NetworkStream stream)
 
28
                {
 
29
                        this.stream = stream;
 
30
                        stream.BeginRead (readBuffer, 0, readBuffer.Length, new System.AsyncCallback (CB), null);
 
31
                }
 
32
                
 
33
                private void CB (IAsyncResult ar)
 
34
                {
 
35
                        int numberOfBytesRead = stream.EndRead (ar);
 
36
                        if (numberOfBytesRead == 0)
 
37
                        {
 
38
                                // disconnected
 
39
                                this.disconnected = true;
 
40
                                return;
 
41
                        }
 
42
 
 
43
                        lock (this.data)
 
44
                        {
 
45
                                data.Append (Encoding.ASCII.GetString (readBuffer, 0, numberOfBytesRead));
 
46
                        }
 
47
 
 
48
                        // read again
 
49
                        stream.BeginRead (readBuffer, 0, readBuffer.Length, new System.AsyncCallback (CB), null);
 
50
                }
 
51
                
 
52
                public string ReadLine ()
 
53
                {                                               
 
54
                        int i = 0;
 
55
                        
 
56
                        if (this.disconnected)
 
57
                        {
 
58
                                //throw new System.IO.IOException ("Connection closed");
 
59
                        }
 
60
                        
 
61
                        // return first line of data or null if no data is available
 
62
                        lock (this.data)
 
63
                        {
 
64
                                if (data.Length == 0)
 
65
                                        return null;
 
66
                                
 
67
                                i = data.ToString().IndexOf ('\n');
 
68
                                if (i>=0) {
 
69
                                        string result = data.ToString (0, i);
 
70
                                        data = data.Remove (0, i+1);
 
71
                                        return result;
 
72
                                }
 
73
                                else {
 
74
                                        return null;
 
75
                                }               
 
76
                        }
 
77
                }
 
78
                
 
79
        }
 
80
}
 
 
b'\\ No newline at end of file'