Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ProxyClientConncetion.cs
1using System;
2using System.Threading.Tasks;
5
7{
11 public class ProxyClientConncetion : CommunicationLayer, IDisposable
12 {
13 private readonly Guid id = Guid.NewGuid();
14 private readonly BinaryTcpClient incoming;
15 private readonly BinaryTcpClient outgoing;
16 private readonly ProxyPort port;
17
26 : base(false, Sniffers)
27 {
28 this.port = Port;
29 this.incoming = Incoming;
30 this.outgoing = Outgoing;
31
32 this.incoming.OnDisconnected += this.Incoming_OnDisconnected;
33 this.incoming.OnError += this.Incoming_OnError;
34 this.incoming.OnReceived += this.Incoming_OnReceived;
35
36 this.outgoing.OnDisconnected += this.Outgoing_OnDisconnected;
37 this.outgoing.OnError += this.Outgoing_OnError;
38 this.outgoing.OnReceived += this.Outgoing_OnReceived;
39 }
40
44 public Guid Id => this.id;
45
49 public void Dispose()
50 {
51 try
52 {
53 this.incoming.DisposeWhenDone();
54 }
55 catch (Exception)
56 {
57 // Ignore
58 }
59
60 try
61 {
62 this.outgoing.DisposeWhenDone();
63 }
64 catch (Exception)
65 {
66 // Ignore
67 }
68 }
69
70
71 private async Task<bool> Outgoing_OnReceived(object Sender, byte[] Buffer, int Offset, int Count)
72 {
73 if (await this.incoming.SendAsync(Buffer, Offset, Count))
74 {
75 this.port.IncUplink(Count);
76 return true;
77 }
78 else
79 return false;
80 }
81
82 private Task Outgoing_OnError(object Sender, Exception Exception)
83 {
84 this.port.Remove(this);
85 return Task.CompletedTask;
86 }
87
88 private Task Outgoing_OnDisconnected(object Sender, EventArgs e)
89 {
90 this.port.Remove(this);
91 return Task.CompletedTask;
92 }
93
94 private async Task<bool> Incoming_OnReceived(object Sender, byte[] Buffer, int Offset, int Count)
95 {
96 if (await this.outgoing.SendAsync(Buffer, Offset, Count))
97 {
98 this.port.IncDownlink(Count);
99 return true;
100 }
101 else
102 return false;
103 }
104
105 private Task Incoming_OnError(object Sender, Exception Exception)
106 {
107 this.port.Remove(this);
108 return Task.CompletedTask;
109 }
110
111 private Task Incoming_OnDisconnected(object Sender, EventArgs e)
112 {
113 this.port.Remove(this);
114 return Task.CompletedTask;
115 }
116
117 }
118}
Implements a binary TCP Client, by encapsulating a TcpClient. It also makes the use of TcpClient safe...
Simple base class for classes implementing communication protocols.
Task Exception(Exception Exception)
Called to inform the viewer of an exception state.
ISniffer[] Sniffers
Registered sniffers.
ProxyClientConncetion(ProxyPort Port, BinaryTcpClient Incoming, BinaryTcpClient Outgoing, ISniffer[] Sniffers)
Maintains one proxy connection
Node acting as a TCP/IP proxy opening a port for incoming communication and proxying it to another po...
Definition: ProxyPort.cs:25
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Definition: ISniffer.cs:11