From 0c49bc9e68ebab53b825bd423eca612fa7e18a92 Mon Sep 17 00:00:00 2001 From: veso266 Date: Sat, 10 Mar 2018 21:25:10 +0100 Subject: [PATCH] Added TCPServer because Stereo Tool wants it --- uecp-sharp/UECPConnection.cs | 23 +++++++++++++++++++-- uecp-sharp/UECPEncoder.cs | 40 ++++++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/uecp-sharp/UECPConnection.cs b/uecp-sharp/UECPConnection.cs index e9f2768..87a591e 100644 --- a/uecp-sharp/UECPConnection.cs +++ b/uecp-sharp/UECPConnection.cs @@ -27,12 +27,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE namespace UECP { - public interface Endpoint + public interface UDPEndpoint { void SendData(byte[] data); } - public class UDPSimplexEndpoint : Endpoint + public class UDPSimplexEndpoint : UDPEndpoint { private UdpClient _udpClient; @@ -47,4 +47,23 @@ public void SendData(byte[] data) _udpClient.Send(data, data.Length); } } + public interface TCPEndpoint + { + void SendData(byte[] data); + } + public class TCPSimplexEndpoint : TCPEndpoint + { + private TcpClient _tcpClient; + private NetworkStream TCPStream; + public TCPSimplexEndpoint(string encoderAddress, int encoderPort) + { + _tcpClient = new TcpClient(IPAddress.Parse(encoderAddress).ToString(), encoderPort); + //_tcpClient.Connect(IPAddress.Parse(encoderAddress), encoderPort); + } + public void SendData(byte[] data) + { + TCPStream = _tcpClient.GetStream(); + TCPStream.Write(data, 0, data.Length); + } + } } diff --git a/uecp-sharp/UECPEncoder.cs b/uecp-sharp/UECPEncoder.cs index cef22cb..4779f68 100644 --- a/uecp-sharp/UECPEncoder.cs +++ b/uecp-sharp/UECPEncoder.cs @@ -31,13 +31,36 @@ namespace UECP { public class UECPEncoder { - Endpoint _endpoint; + UDPEndpoint _endpoint; + TCPEndpoint _TCPendpoint; + private string consCheck = ""; - public UECPEncoder(Endpoint ep) + public UECPEncoder(TCPEndpoint ep) + { + _TCPendpoint = ep; + constructorCheck("TCP"); + } + public UECPEncoder(UDPEndpoint ep) { _endpoint = ep; + constructorCheck("UDP"); } + //Checking which Class Constructor is used + public void constructorCheck(string inputCheck) + { + string tell = ""; + if (inputCheck == "TCP") + { + consCheck = "TCP"; + } + if (inputCheck == "UDP") + { + consCheck = "UDP"; + } + //return tell; + } + //Checking which Class Constructor is used public void SetPI(UInt16 pi) { BuildAndSendMessage(MEC.RDS_PI, BitConverter.GetBytes(pi)); @@ -117,8 +140,17 @@ private void BuildAndSendMessage(MessageElement messageElement) { UECPFrame uecpFrame = new UECPFrame(); uecpFrame.MessageElements.Add(messageElement); - - _endpoint.SendData(uecpFrame.GetBytes()); + //We have to check which constructor is used (TCP or UDP) + if (consCheck == "UDP") + { + _endpoint.SendData(uecpFrame.GetBytes()); + } + else if (consCheck == "TCP") + { + _TCPendpoint.SendData(uecpFrame.GetBytes()); + } + //We have to check which constructor is used (TCP or UDP) + } private void FillBytes(List data, byte fillWith, int desiredLength)