steavy29 / Telegram.Net

Telegram (http://telegram.org) client library implemented in C#.
17 stars 6 forks source link

HTTP Transport. Need help. #16

Closed andrew-maksimov closed 7 years ago

andrew-maksimov commented 7 years ago

Now Telegram.Net supports one transport (TCP). I would try to write another transport (HTTP).

Someone tell me where best to start, and which way to go? =)


I tried, but it was stuck. Maybe someone will tell you where I'm confused. I am trying to implement the http-transport. When you try to get the answer - Error 404.

    public class HttpTransport : ITransport, IDisposable
    {
        private readonly HttpWebRequest httpWebRequest;
        private int sendCounter = 0;

        public HttpTransport(string address, int port)
        {
            httpWebRequest = (HttpWebRequest)WebRequest.Create("http://" + address);
        }

        public async Task Send(byte[] packet)
        {
            var data = packet;

            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded ; charset=UTF-8";
            httpWebRequest.ContentLength = data.Length;

            using (var stream = httpWebRequest.GetRequestStream())
            {
                await stream.WriteAsync(data, 0, data.Length);
            }

            sendCounter++;
        }

        public async Task<TcpMessage> Receieve()
        {
            var response = (HttpWebResponse)httpWebRequest.GetResponse();
            var stream = response.GetResponseStream();

            var packetLengthBytes = new byte[4];
            if (await stream.ReadAsync(packetLengthBytes, 0, 4) != 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) != 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);
                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) != 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);
        }
    }
steavy29 commented 7 years ago

Hi. Can you explain the motivation of implementing it over http? The only thing I can advice atm is to read docs: https://core.telegram.org/mtproto They have pretty good documentation on the protocol. Also you could reuse a lot of code from this library(f.e. messages encryption and so on)

andrew-maksimov commented 7 years ago

Adding protocol choices, make it more universal library. Where there is a possibility, of course better to use TCP. But not everywhere have the opportunity to use TCP protocol.

steavy29 commented 7 years ago

I'm not telling this is a bad idea. Just interested in the motivation :)

But not everywhere have the opportunity to use TCP protocol.

In which cases is this possible? Http is built on to of tcp.