diff options
| author | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
|---|---|---|
| committer | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
| commit | 00a491d93733d4625ad329b2ba8237f445364b3f (patch) | |
| tree | 4b24c6fa78d7648f4bb7cefafa464bb0b063fec4 /Software/Visual_Studio/Tango.Transport/Adapters/TcpTransportAdapter.cs | |
| parent | 124ad4150f80c6846fdee41dbbda9848c105f6e5 (diff) | |
| download | Tango-00a491d9.tar.gz Tango-00a491d9.zip | |
merge
Diffstat (limited to 'Software/Visual_Studio/Tango.Transport/Adapters/TcpTransportAdapter.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Transport/Adapters/TcpTransportAdapter.cs | 139 |
1 files changed, 7 insertions, 132 deletions
diff --git a/Software/Visual_Studio/Tango.Transport/Adapters/TcpTransportAdapter.cs b/Software/Visual_Studio/Tango.Transport/Adapters/TcpTransportAdapter.cs index 427c335ff..af2a5201b 100644 --- a/Software/Visual_Studio/Tango.Transport/Adapters/TcpTransportAdapter.cs +++ b/Software/Visual_Studio/Tango.Transport/Adapters/TcpTransportAdapter.cs @@ -10,9 +10,7 @@ using System.Reactive.Subjects; using System.Text; using System.Threading; using System.Threading.Tasks; -using Tango.Core; using Tango.Logging; -using Tango.Transport.Compression; namespace Tango.Transport.Adapters { @@ -25,10 +23,7 @@ namespace Tango.Transport.Adapters private TcpClient _socket; private Thread _pullThread; private bool _initializedFromConstructor; - private Thread _pushThread; - private ProducerConsumerQueue<byte[]> _pushQueue; private byte[] _size_buffer; - private object _writeSyncObject = new object(); #region Properties @@ -37,16 +32,6 @@ namespace Tango.Transport.Adapters /// </summary> public int Port { get; set; } - /// <summary> - /// Gets or sets the adapter write mode. - /// </summary> - public TcpTransportAdapterWriteMode WriteMode { get; set; } - - /// <summary> - /// Gets or sets the write interval when using <see cref="TcpTransportAdapterWriteMode.Interval"/> mode. - /// </summary> - public TimeSpan WriteInterval { get; set; } - #endregion #region Constructors @@ -56,11 +41,8 @@ namespace Tango.Transport.Adapters /// </summary> public TcpTransportAdapter() { - ComponentName = $"TCP Adapter {_component_counter++}"; Address = "127.0.0.1"; Port = 9999; - WriteMode = TcpTransportAdapterWriteMode.Interval; - WriteInterval = TimeSpan.FromMilliseconds(10); } /// <summary> @@ -110,27 +92,16 @@ namespace Tango.Transport.Adapters SetSocketProperties(); } - LogManager.Log($"TCP adapter ({Address}) Connected..."); - State = TransportComponentState.Connected; _pullThread = new Thread(PullThreadMethod); - _pullThread.Name = $"{ComponentName} Pull Thread"; _pullThread.IsBackground = true; _pullThread.Start(); - - if (WriteMode == TcpTransportAdapterWriteMode.Interval) - { - _pushThread = new Thread(PushThreadMethod); - _pushThread.IsBackground = true; - _pushThread.Name = $"{ComponentName} Push Thread"; - _pushQueue = new ProducerConsumerQueue<byte[]>(); - _pushThread.Start(); - } + LogManager.Log("TCP adapter Connected..."); } } catch (Exception ex) { - throw LogManager.Log(ex, $"Could not connect the TCP adapter ({Address})."); + throw LogManager.Log(ex, "Could not connect the TCP adapter."); } }); } @@ -149,19 +120,12 @@ namespace Tango.Transport.Adapters { State = TransportComponentState.Disconnected; _socket.Close(); - - try - { - _pushThread.Abort(); - } - catch { } - - LogManager.Log($"TCP adapter ({Address}) disconnected."); + LogManager.Log("TCP adapter disconnected."); } } catch (Exception ex) { - LogManager.Log(ex, $"Could not disconnect the TCP adapter ({Address})."); + LogManager.Log(ex, "Could not disconnect the TCP adapter."); } })); } @@ -170,35 +134,18 @@ namespace Tango.Transport.Adapters /// Writes the specified data to the stream. /// </summary> /// <param name="data">The data.</param> - /// <param name="immidiate">Writes the data as soon as possible while ignoring any message queuing and batching.</param> - public override void Write(byte[] data, bool immidiate = false) + public override void Write(byte[] data) { ThrowIfDisposed(); try { - if (EnableCompression) - { - data = GZipHelper.Compress(data); - } - data = PostProcessBuffer(data); - - if (WriteMode == TcpTransportAdapterWriteMode.Direct || immidiate) - { - lock (_writeSyncObject) - { - _socket.GetStream().Write(data, 0, data.Length); - } - } - else - { - _pushQueue.BlockEnqueue(data); - } + _socket.GetStream().Write(data, 0, data.Length); } catch (Exception ex) { - OnFailed(LogManager.Log(ex, $"Error writing to TCP adapter ({Address}).")); + OnFailed(LogManager.Log(ex)); } } @@ -247,25 +194,6 @@ namespace Tango.Transport.Adapters } } - if (EnableCompression) - { - try - { - data = GZipHelper.Decompress(data); - } - catch (Exception ex) - { - if (ex.Message.Contains("GZip")) - { - //Temporarily ignore, probably switching protocol definitions... - } - else - { - throw ex; - } - } - } - OnDataAvailable(data); } else @@ -284,59 +212,6 @@ namespace Tango.Transport.Adapters #endregion - #region Push Thread - - private void PushThreadMethod() - { - try - { - while (State == TransportComponentState.Connected) - { - List<byte[]> dataCollection = new List<byte[]>(); - - var data = _pushQueue.BlockDequeue(); - var first = true; - - while (_pushQueue.Count > 0 || first) - { - if (!first) - { - data = _pushQueue.BlockDequeue(); - } - else - { - first = false; - } - - dataCollection.Add(data); - } - - if (dataCollection.Count > 0) - { - try - { - byte[] allData = dataCollection.SelectMany(a => a).ToArray(); - - lock (_writeSyncObject) - { - _socket.GetStream().Write(allData, 0, allData.Length); - } - } - catch (Exception ex) - { - OnFailed(LogManager.Log(ex, $"Error writing to TCP adapter ({Address}).")); - return; - } - } - - Thread.Sleep(WriteInterval); - } - } - catch (ThreadAbortException) { } - } - - #endregion - #region Private Methods private void SetSocketProperties() |
