diff --git a/TLSharp.Core/Network/TLClient.cs b/TLSharp.Core/Network/TLClient.cs new file mode 100644 index 0000000..f68219b --- /dev/null +++ b/TLSharp.Core/Network/TLClient.cs @@ -0,0 +1,57 @@ +using NetCoreServer; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace TLSharp.Core.Network +{ + class TLClient : TcpClient + { + public delegate void ReceiveQueue(TcpMessage message); + private readonly ReceiveQueue receiveQueue; + public TLClient(IPAddress address, int port, ReceiveQueue queue) : base(address, port) + { + receiveQueue = queue; + } + + protected override void OnReceived(byte[] buffer, long offset, long size) + { + var packetLengthBytes = new byte[4]; + packetLengthBytes = buffer.Take(4).ToArray(); + int packetLength = BitConverter.ToInt32(packetLengthBytes, 0); + + var seqBytes = new byte[4]; + seqBytes = buffer.Skip(4).Take(4).ToArray(); + int seq = BitConverter.ToInt32(seqBytes, 0); + + int readBytes = 0; + var body = new byte[packetLength - 12]; + body = buffer.Skip(8).Take(packetLength - 12).ToArray(); + + var crcBytes = new byte[4]; + crcBytes = buffer.Skip(packetLength - 4).Take(4).ToArray(); + int checksum = BitConverter.ToInt32(crcBytes, 0); + + byte[] rv = new byte[packetLengthBytes.Length + seqBytes.Length + body.Length]; + + System.Buffer.BlockCopy(packetLengthBytes, 0, rv, 0, packetLengthBytes.Length); + System.Buffer.BlockCopy(seqBytes, 0, rv, packetLengthBytes.Length, seqBytes.Length); + System.Buffer.BlockCopy(body, 0, rv, packetLengthBytes.Length + seqBytes.Length, body.Length); + var crc32 = new Ionic.Crc.CRC32(); + crc32.SlurpBlock(rv, 0, rv.Length); + var validChecksum = crc32.Crc32Result; + + if (checksum != validChecksum) + { + throw new InvalidOperationException("invalid checksum! skip"); + } + + receiveQueue(new TcpMessage(seq, body)); + } + + + } +} diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index 3750a76..338c773 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Concurrent; +using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Threading; @@ -10,103 +12,64 @@ namespace TLSharp.Core.Network public class TcpTransport : IDisposable { - private readonly TcpClient tcpClient; - private readonly NetworkStream stream; + private readonly TLClient tcpClient; + private readonly BlockingCollection ReceievedMessage = new BlockingCollection(); + //private readonly NetworkStream stream; private int sendCounter = 0; public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null) { - if (handler == null) - { - var ipAddress = IPAddress.Parse(address); - var endpoint = new IPEndPoint(ipAddress, port); + //if (handler == null) + //{ + var ipAddress = IPAddress.Parse(address); + tcpClient = new TLClient(ipAddress, port, ReceiveMessage); + tcpClient.ConnectAsync(); + //} + //else + // tcpClient = handler(address, port); + } - tcpClient = new TcpClient(ipAddress.AddressFamily); - tcpClient.Connect(endpoint); - } - else - tcpClient = handler(address, port); + public void ReceiveMessage(TcpMessage message) + { + ReceievedMessage.Add(message); + //handle - if (tcpClient.Connected) - { - stream = tcpClient.GetStream(); - } } public async Task Send(byte[] packet, CancellationToken token = default(CancellationToken)) { - if (!tcpClient.Connected) + if (!tcpClient.IsConnected) throw new InvalidOperationException("Client not connected to server."); var tcpMessage = new TcpMessage(sendCounter, packet); - await stream.WriteAsync(tcpMessage.Encode(), 0, tcpMessage.Encode().Length, token).ConfigureAwait(false); + tcpClient.SendAsync(tcpMessage.Encode()); sendCounter++; } public async Task Receive(CancellationToken token = default(CancellationToken)) { - var packetLengthBytes = new byte[4]; - if (await stream.ReadAsync(packetLengthBytes, 0, 4, token).ConfigureAwait(false) != 4) - throw new InvalidOperationException("Couldn't read the packet length"); - int packetLength = BitConverter.ToInt32(packetLengthBytes, 0); - - var seqBytes = new byte[4]; - if (await stream.ReadAsync(seqBytes, 0, 4, token).ConfigureAwait(false) != 4) - throw new InvalidOperationException("Couldn't read the sequence"); - int seq = BitConverter.ToInt32(seqBytes, 0); - - int readBytes = 0; - var body = new byte[packetLength - 12]; - int neededToRead = packetLength - 12; - - do - { - var bodyByte = new byte[packetLength - 12]; - var availableBytes = await stream.ReadAsync(bodyByte, 0, neededToRead, token).ConfigureAwait(false); - neededToRead -= availableBytes; - Buffer.BlockCopy(bodyByte, 0, body, readBytes, availableBytes); - readBytes += availableBytes; - } - while (readBytes != packetLength - 12); - - var crcBytes = new byte[4]; - if (await stream.ReadAsync(crcBytes, 0, 4, token).ConfigureAwait(false) != 4) - throw new InvalidOperationException("Couldn't read the crc"); - int checksum = BitConverter.ToInt32(crcBytes, 0); - - byte[] rv = new byte[packetLengthBytes.Length + seqBytes.Length + body.Length]; - - Buffer.BlockCopy(packetLengthBytes, 0, rv, 0, packetLengthBytes.Length); - Buffer.BlockCopy(seqBytes, 0, rv, packetLengthBytes.Length, seqBytes.Length); - Buffer.BlockCopy(body, 0, rv, packetLengthBytes.Length + seqBytes.Length, body.Length); - var crc32 = new Ionic.Crc.CRC32(); - crc32.SlurpBlock(rv, 0, rv.Length); - var validChecksum = crc32.Crc32Result; - - if (checksum != validChecksum) - { - throw new InvalidOperationException("invalid checksum! skip"); - } - - return new TcpMessage(seq, body); + return ReceievedMessage.Take(); } + + public bool IsConnected { get { - return this.tcpClient.Connected; + ; return this.tcpClient.IsConnected; } } public void Dispose() { - if (tcpClient.Connected) + if (tcpClient.IsConnected) { - stream.Close(); - tcpClient.Close(); + //stream.Close(); + tcpClient.DisconnectAsync(); + tcpClient.Dispose(); } } } diff --git a/TLSharp.Core/TLSharp.Core.csproj b/TLSharp.Core/TLSharp.Core.csproj index c09f394..790d5cf 100644 --- a/TLSharp.Core/TLSharp.Core.csproj +++ b/TLSharp.Core/TLSharp.Core.csproj @@ -9,8 +9,9 @@ Properties TLSharp.Core TLSharp.Core - v4.5 + v4.6 512 + true @@ -34,6 +35,9 @@ ..\packages\MarkerMetro.Unity.Ionic.Zlib.2.0.0.14\lib\net35\Ionic.ZLib.dll True + + ..\packages\NetCoreServer.1.9.1\lib\net46\NetCoreServer.dll + @@ -70,6 +74,7 @@ + diff --git a/TLSharp.Core/packages.config b/TLSharp.Core/packages.config index 4966d6d..4115ba4 100644 --- a/TLSharp.Core/packages.config +++ b/TLSharp.Core/packages.config @@ -2,4 +2,5 @@ + \ No newline at end of file diff --git a/TLSharp.Tests.NUnit/TLSharp.Tests.NUnit.csproj b/TLSharp.Tests.NUnit/TLSharp.Tests.NUnit.csproj index e947f4e..d1ee36c 100644 --- a/TLSharp.Tests.NUnit/TLSharp.Tests.NUnit.csproj +++ b/TLSharp.Tests.NUnit/TLSharp.Tests.NUnit.csproj @@ -1,5 +1,5 @@ - - + + Debug AnyCPU @@ -7,7 +7,8 @@ Library TLSharp.Tests.NUnit TLSharp.Tests.NUnit - v4.5 + v4.6 + true @@ -51,4 +52,4 @@ TLSharp.Tests - + \ No newline at end of file diff --git a/TLSharp.Tests.VS/TLSharp.Tests.VS.csproj b/TLSharp.Tests.VS/TLSharp.Tests.VS.csproj index a44fff4..a26efba 100644 --- a/TLSharp.Tests.VS/TLSharp.Tests.VS.csproj +++ b/TLSharp.Tests.VS/TLSharp.Tests.VS.csproj @@ -8,7 +8,7 @@ Properties TLSharp.Tests.VS TLSharp.Tests.VS - v4.5 + v4.6 512 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 10.0 @@ -16,6 +16,7 @@ $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages False UnitTest + true diff --git a/TLSharp.Tests/TLSharp.Tests.csproj b/TLSharp.Tests/TLSharp.Tests.csproj index e2277f1..383c6a9 100644 --- a/TLSharp.Tests/TLSharp.Tests.csproj +++ b/TLSharp.Tests/TLSharp.Tests.csproj @@ -9,8 +9,9 @@ Properties TLSharp.Tests TLSharp.Tests - v4.5 + v4.6 512 + true diff --git a/TLSharp.sln b/TLSharp.sln index 4f55f1b..9a060ae 100644 --- a/TLSharp.sln +++ b/TLSharp.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.24720.0 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29424.173 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TLSharp.Core", "TLSharp.Core\TLSharp.Core.csproj", "{400D2544-1CC6-4D8A-A62C-2292D9947A16}" EndProject @@ -46,6 +46,12 @@ Global {E90B705B-19FA-43BA-B952-69957976D12C}.Release|Any CPU.ActiveCfg = Release|Any CPU {E90B705B-19FA-43BA-B952-69957976D12C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {46567A1C-7985-4A6C-AC6A-A8308DF9CE15} + EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution Policies = $0 $0.TextStylePolicy = $1 @@ -81,7 +87,4 @@ Global $2.inheritsScope = text/x-csharp $2.scope = text/x-csharp EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection EndGlobal diff --git a/TeleSharp.Generator/App.config b/TeleSharp.Generator/App.config index 88fa402..2d2a12d 100644 --- a/TeleSharp.Generator/App.config +++ b/TeleSharp.Generator/App.config @@ -1,6 +1,6 @@ - + - + - \ No newline at end of file + diff --git a/TeleSharp.Generator/TeleSharp.Generator.csproj b/TeleSharp.Generator/TeleSharp.Generator.csproj index 04dbaca..ec8fd85 100644 --- a/TeleSharp.Generator/TeleSharp.Generator.csproj +++ b/TeleSharp.Generator/TeleSharp.Generator.csproj @@ -9,9 +9,10 @@ Properties TeleSharp.Generator TeleSharp.Generator - v4.5 + v4.6 512 true + AnyCPU diff --git a/TeleSharp.TL/TeleSharp.TL.csproj b/TeleSharp.TL/TeleSharp.TL.csproj index 7c6b30c..86a2cd7 100644 --- a/TeleSharp.TL/TeleSharp.TL.csproj +++ b/TeleSharp.TL/TeleSharp.TL.csproj @@ -9,8 +9,9 @@ Properties TeleSharp.TL TeleSharp.TL - v4.5 + v4.6 512 + true