Пример #1
0
        public void SendBody(TGNetService.NetServiceChannel channel, string str, byte[] data)
        {
            //lock (soc)
            {
                var content = Encoding.UTF8.GetBytes(str);
                var body    = TGNetService.GZip(content, 0, out var length);

                if (JObject.Parse(str)["Type"].ToString() != "Beat")
                {
                    Console.WriteLine("<<< ch:{3} sending {0}->{1} body:{2}", content.Length, length, str, channel);
                }

                var header = new TGNetService.Header
                {
                    Sign        = TGNetService.SIGN,
                    Channel     = (uint)channel,
                    Bodysize    = (uint)length,
                    ContentSize = (uint)content.Length,
                    DataSize    = (data == null) ? 0 : (uint)data.Length,
                };

                var head = Program.RawSerializer.RawSerialize(header);
                _socket.SendTimeout = 10000;
                if (_socket.Send(head) != Marshal.SizeOf(typeof(TGNetService.Header)))
                {
                    throw new Exception("send header failed");
                }

                _socket.SendTimeout = 30000;
                if (_socket.Send(body, length, SocketFlags.None) != length)
                {
                    throw new Exception("send body failed");
                }

                if (data != null)
                {
                    if (_socket.Send(data) != data.Length)
                    {
                        throw new Exception("send data failed");
                    }
                }
            }
        }
Пример #2
0
        private void Receive()
        {
            try
            {
                while (true)
                {
                    var headSize = Marshal.SizeOf(typeof(TGNetService.Header));
                    var head     = new byte[headSize];
                    var headRead = 0;

                    while (headRead < headSize)
                    {
                        if (!_socket.Connected)
                        {
                            return;
                        }
                        _socket.ReceiveTimeout = TGNetService.ReceiveTimeout;
                        var n = _socket.Receive(head, headRead, headSize - headRead, SocketFlags.None);
                        headRead += n;
                    }

                    if (headRead != headSize)
                    {
                        throw new Exception("recv header failed " + headRead.ToString());
                    }

                    var header = Program.RawSerializer.RawDeserialize(head);
                    if (header.Sign != TGNetService.SIGN)
                    {
                        throw new Exception("recv header sign mismatch");
                    }

                    var channel = (TGNetService.NetServiceChannel)header.Channel;

                    var bytesExpected = (int)header.Bodysize;
                    var body          = new byte[bytesExpected];
                    var bytesRead     = 0;

                    while (bytesRead < bytesExpected)
                    {
                        _socket.ReceiveTimeout = 10000;
                        var n = _socket.Receive(body, bytesRead, bytesExpected - bytesRead, SocketFlags.None);
                        bytesRead += n;
                    }

                    var content = TGNetService.GZip(body, (int)header.ContentSize, out var length);

                    if (length != header.ContentSize)
                    {
                        throw new Exception("received body content size mismatch");
                    }

                    var str = Encoding.UTF8.GetString(content, 0, length);
                    if (JObject.Parse(str)["Type"].ToString() != "Beat")
                    {
                        Console.WriteLine(">>> ch:{3} receiving {0}->{1} body:{2}", header.ContentSize, header.Bodysize,
                                          str, channel);
                    }

                    byte[] data = null;
                    if (header.DataSize > 0)
                    {
                        var dataExpected = (int)header.DataSize;
                        data = new byte[dataExpected];
                        var dataRead = 0;

                        while (dataRead < dataExpected)
                        {
                            _socket.ReceiveTimeout = 30000;
                            var n = _socket.Receive(data, dataRead, dataExpected - dataRead, SocketFlags.None);
                            dataRead += n;
                        }
                    }

                    HandlePkt(channel, JObject.Parse(str), data);

                    if (!_socket.Connected)
                    {
                        return;
                    }
                }
            }
            catch (JsonException)
            {
                throw;
                // ignored
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                // ignored
            }
        }