Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RowTcpClient.cs
1using System.Text;
2using System.Threading.Tasks;
3#if WINDOWS_UWP
4using Windows.Networking.Sockets;
5#else
6using System.Net.Sockets;
7#endif
8using Waher.Events;
10
11namespace Waher.Networking
12{
17 public class RowTcpClient : TextTcpClient
18 {
19 private readonly StringBuilder buffer = new StringBuilder();
20 private readonly int maxLen = 0;
21 private char prev = (char)0;
22 private bool error = false;
23 private int len = 0;
24
35 public RowTcpClient(Encoding Encoding, int MaxLength, bool DecoupledEvents, params ISniffer[] Sniffers)
36 : base(Encoding, DecoupledEvents, Sniffers)
37 {
38 this.maxLen = MaxLength;
39 }
40
52 public RowTcpClient(Encoding Encoding, int MaxLength, bool SniffText, bool DecoupledEvents, params ISniffer[] Sniffers)
53 : base(Encoding, SniffText, DecoupledEvents, Sniffers)
54 {
55 this.maxLen = MaxLength;
56 }
57
58#if WINDOWS_UWP
70 public RowTcpClient(StreamSocket Client, Encoding Encoding, int MaxLength, bool DecoupledEvents, params ISniffer[] Sniffers)
71 : base(Client, Encoding, DecoupledEvents, Sniffers)
72 {
73 this.maxLen = MaxLength;
74 }
75
88 public RowTcpClient(StreamSocket Client, Encoding Encoding, int MaxLength, bool SniffText, bool DecoupledEvents, params ISniffer[] Sniffers)
89 : base(Client, Encoding, SniffText, DecoupledEvents, Sniffers)
90 {
91 this.maxLen = MaxLength;
92 }
93#else
105 public RowTcpClient(TcpClient Client, Encoding Encoding, int MaxLength, bool DecoupledEvents, params ISniffer[] Sniffers)
106 : base(Client, Encoding, DecoupledEvents, Sniffers)
107 {
108 this.maxLen = MaxLength;
109 }
110
123 public RowTcpClient(TcpClient Client, Encoding Encoding, int MaxLength, bool SniffText, bool DecoupledEvents, params ISniffer[] Sniffers)
124 : base(Client, Encoding, SniffText, DecoupledEvents, Sniffers)
125 {
126 this.maxLen = MaxLength;
127 }
128#endif
129
134 protected async override Task<bool> TextDataReceived(string Data)
135 {
136 foreach (char ch in Data)
137 {
138 if (ch == '\r' || ch == '\n')
139 {
140 if (this.len == 0)
141 {
142 if (this.prev == ch && !await base.TextDataReceived(string.Empty))
143 return false;
144 }
145 else
146 {
147 if (this.error)
148 {
149 await this.Error("Row too long: " + this.len.ToString() + " characters");
150 this.error = false;
151 }
152 else if (!await base.TextDataReceived(this.buffer.ToString()))
153 return false;
154
155 this.len = 0;
156 this.buffer.Clear();
157 this.prev = ch;
158 }
159 }
160 else
161 {
162 if (this.len++ > this.maxLen)
163 this.error = true;
164 else
165 this.buffer.Append(ch);
166
167 this.prev = (char)0;
168 }
169 }
170
171 return true;
172 }
173
179 public override Task<bool> SendAsync(string Packet)
180 {
181 return this.SendAsync(Packet, null, null);
182 }
183
191 public override Task<bool> SendAsync(string Text, EventHandlerAsync<DeliveryEventArgs> Callback, object State)
192 {
193 return base.SendAsync(Text + "\r\n", Callback, State);
194 }
195
200 protected override Task TextDataSent(string Data)
201 {
202 return base.TextDataSent(Data.Substring(0, Data.Length - 2));
203 }
204
205 }
206}
TcpClient Client
Underlying TcpClient object.
Task Error(string Error)
Called to inform the viewer of an error state.
bool DecoupledEvents
If events raised from the communication layer are decoupled, i.e. executed in parallel with the sourc...
Implements a text-based TCP Client, by using the thread-safe full-duplex BinaryTcpClient....
Definition: RowTcpClient.cs:18
RowTcpClient(Encoding Encoding, int MaxLength, bool SniffText, bool DecoupledEvents, params ISniffer[] Sniffers)
Implements a text-based TCP Client, by using the thread-safe full-duplex BinaryTcpClient....
Definition: RowTcpClient.cs:52
RowTcpClient(Encoding Encoding, int MaxLength, bool DecoupledEvents, params ISniffer[] Sniffers)
Implements a text-based TCP Client, by using the thread-safe full-duplex BinaryTcpClient....
Definition: RowTcpClient.cs:35
RowTcpClient(TcpClient Client, Encoding Encoding, int MaxLength, bool SniffText, bool DecoupledEvents, params ISniffer[] Sniffers)
Implements a text-based TCP Client, by using the thread-safe full-duplex BinaryTcpClient....
async override Task< bool > TextDataReceived(string Data)
Method called when text data has been received.
RowTcpClient(TcpClient Client, Encoding Encoding, int MaxLength, bool DecoupledEvents, params ISniffer[] Sniffers)
Implements a text-based TCP Client, by using the thread-safe full-duplex BinaryTcpClient....
override Task< bool > SendAsync(string Packet)
Sends a text packet.
override Task< bool > SendAsync(string Text, EventHandlerAsync< DeliveryEventArgs > Callback, object State)
Sends a binary packet.
override Task TextDataSent(string Data)
Method called when binary data has been sent.
Implements a text-based TCP Client, by using the thread-safe full-duplex BinaryTcpClient.
Encoding Encoding
Text encoding to use.
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Definition: ISniffer.cs:11