Code Monkey home page Code Monkey logo

Comments (13)

sochix avatar sochix commented on July 18, 2024

Try to ignore such TcpMessages, maybe it's failure of network.

from tlsharp.

ra0o0f avatar ra0o0f commented on July 18, 2024

@sochix yes i tried to ignore and continute but it happen again and again, and one thing is strange that sometime sequence is valid, take a look at results i'm receiving:

first response:
avaliableBytes: 256
packageLength:1076
seq: 0 => which is correct
// failed

second response:
avaliableBytes: 1608
packageLength:1891970753
seq: 1992984927
// failed

third response:
avaliableBytes: 6644
packageLength: 6644
seq: 2 => correct
// edit: this was a correct response

forth response:
avaliableBytes: 36860
packageLength:30916
seq: 3 => correct
// this was a correct response `HandleRpcResult`
availableBytes 100
packagetLength: 100
seq: 33
// this was a correct response

availableBytes 4380
packagetLength: 37588
seq: 34
// failed

from tlsharp.

ra0o0f avatar ra0o0f commented on July 18, 2024

@sochix i found something, a response can be send in several tcp messages,

i get this response:

first availableBytes: 2920
packetLength: 3188

second availableBytes: 268
packetLength : -332744926

// which 2920 + 268 = 3188

i think it's because i use layer40, and telegram has not documention for it yet

from tlsharp.

ra0o0f avatar ra0o0f commented on July 18, 2024

@sochix changing receive to below solve the problem:

            var stream = _tcpClient.GetStream();

            var packetLengthBytes = new byte[4];
            await stream.ReadAsync(packetLengthBytes, 0, 4);
            int packetLength = BitConverter.ToInt32(packetLengthBytes, 0);

            var seqBytes = new byte[4];
            await stream.ReadAsync(seqBytes, 0, 4);
            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);
                neededToRead -= availableBytes;
                Buffer.BlockCopy(bodyByte, 0, body, readBytes, availableBytes);
                readBytes += availableBytes;
            }
            while (readBytes != packetLength - 12);

            var crcBytes = new byte[4];
            await stream.ReadAsync(crcBytes, 0, 4);
            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);

from tlsharp.

sochix avatar sochix commented on July 18, 2024

Can you please, make a pull request?

from tlsharp.

ra0o0f avatar ra0o0f commented on July 18, 2024

@sochix of course, is the code approved by your standards? any changes?

from tlsharp.

sochix avatar sochix commented on July 18, 2024

seems to be ok =) Thank you!

from tlsharp.

ahmadi173 avatar ahmadi173 commented on July 18, 2024

where do i have to copy that code?

from tlsharp.

ra0o0f avatar ra0o0f commented on July 18, 2024

@siml173 at https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/Network/MtProtoSender.cs#L131

replace _transport.Receieve() with it.

i'm sorry i will send the PR asap

from tlsharp.

ra0o0f avatar ra0o0f commented on July 18, 2024

PR created

from tlsharp.

sochix avatar sochix commented on July 18, 2024

@ra0o0f merged. Thanks!

from tlsharp.

schlumpf90 avatar schlumpf90 commented on July 18, 2024

Hi to all,

I have the same problem : "couldn't read the packet length"

From the first use of my application, I can register my phone account. After this I can send one message successfully from my application. But from the second message I get this error.
I'm using the telegram server 149.154.167.50 I changed it to several other ip adresses, but the result was the same error.

Then I tried the solution from "ra0o0f" from x-mas 2015 (see above), but then I get an arithmetic error at "var body = new byte[packetLength - 12];"

So I think, that the length of the received packet is less than 4 bytes.

Does anyone solved this problem or can help me please !?!?!?

Thanx and best wishes,
Matthias

from tlsharp.

cyberyak avatar cyberyak commented on July 18, 2024

After last update I start receiving this error very often (every about 2 minutes in my telegram client). Couldn't replicate and understand conditions because it appears randomly, but seems still actual problem.

from tlsharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.