protected static void Write(BinaryBitWriter writer, string text)
        {
            Page?currentPage = null;

            for (int i = 0; i < text.Length; i++)
            {
                var ch = text[i];

                Page page = FindPage(ch);

                if (page == Page.UNICODE_FORCE)
                {
                    string str = text.Substring(i, text.Length - i);

                    writer.Write6((byte)Page.UNICODE_FORCE);
                    writer.Write(Encoding.UTF8.GetBytes(str));
                    writer.Write((byte)Page.END);
                    return;
                }

                if (page != currentPage)
                {
                    writer.Write6((byte)page);
                }

                writer.Write6((byte)(InPage(ch, page) + 8));

                currentPage = page;
            }
            writer.Write6((byte)Page.END);
        }
Exemple #2
0
        protected override void pack(BinaryBitWriter writer)
        {
            writer.Write(TargetGroup);

            writer.Write((byte)ResendIndexes.Length);

            foreach (byte index in ResendIndexes)
            {
                writer.Write(index);
            }
        }
        protected static void WriteBytes(BinaryBitWriter writer, Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);

            var bytes = ReadFully(stream);
            var hash  = Md5.Get(bytes.Skip(bytes.Length - 5).ToArray()).Take(5).ToArray();

            writer.Write(bytes);

            //HASH
            writer.Write(hash);

            ///END
            writer.Write((byte)0);
            writer.Write((byte)0);
            writer.Write((byte)0);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="writer"></param>
        protected override void pack(BinaryBitWriter writer)
        {
            WriteLocation(writer);

            if (this.Version >= ProtocolVersion.v4__WeatherExtension)
            {
                if (RequestLat != null && RequestLon != null)
                {
                    writer.Write(true);
                    writer.Write((float)RequestLat, true, 7, 13);
                    writer.Write((float)RequestLon, true, 8, 13);
                    writer.Write((uint)PointKey, 4);
                }
                else
                {
                    writer.Write(false);
                }
            }
        }
        protected override void pack(BinaryBitWriter writer)
        {
            writer.Write((uint)(Time - START).TotalDays, 14);
            writer.Write((uint)Time.Hour, 5);
            writer.Write((uint)(Time.Minute / 15d), 2);

            if (MonthlyBegin == null && MonthlyNext == null)
            {
                writer.Write(false);
            }
            else
            {
                writer.Write(true);

                writer.Write((uint)(MonthlyBegin.Value - START).TotalDays, 14);
                writer.Write((uint)(MonthlyNext.Value - START).TotalDays, 14);
            }

            writer.Write((int?)Balance, 15);
            writer.Write((int?)Units, 15);
            writer.Write((int?)Usages, 15);
        }
        protected override void pack(BinaryBitWriter writer)
        {
            CheckFlags flags = CheckFlags.EMPTY;

            // --->

            if (ByskyToken != null)
            {
                flags |= CheckFlags.HasByskyToken;
            }

            //
            // --->
            //
            writer.Write((byte)((byte)flags));
            //
            // --->
            //

            if (flags.HasFlag(CheckFlags.HasByskyToken))
            {
                writer.Write(ByskyToken.Value.Guid.ToByteArray());
            }
        }
        public void Write6(byte b)
        {
            BinaryBitWriter writer = this;

            if (b > 0x3f)
            {
                throw new InvalidOperationException();
            }
            writer.Write((bool)((b & 1) == 1));
            writer.Write((bool)(((b >> 1) & 1) == 1));
            writer.Write((bool)(((b >> 2) & 1) == 1));
            writer.Write((bool)(((b >> 3) & 1) == 1));
            writer.Write((bool)(((b >> 4) & 1) == 1));
            writer.Write((bool)(((b >> 5) & 1) == 1));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="writer"></param>
        protected void WriteLocation(BinaryBitWriter writer)
        {
            if (Version >= ProtocolVersion.v2__LocationFix)
            {
                writer.Write((float)Lat, true, 7, 13);
                writer.Write((float)Lon, true, 8, 13);
            }
            else
            {
                writer.Write((float)Lat, true, 7, 9);
                writer.Write((float)Lon, true, 8, 9);
            }


            if (Alt != null)
            {
                writer.Write(true);
                writer.Write((uint)Math.Min(16383, Math.Max(0, Alt.Value)), 14);
            }
            else
            {
                writer.Write(false);
            }
        }
 protected override void pack(BinaryBitWriter writer)
 {
     writer.Write(SentGroup);
 }
Exemple #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="writer"></param>
        protected override void pack(BinaryBitWriter writer)
        {
            Flags flags = Flags.EMPTY;

            // --->

            if (this.Subscriber != null)
            {
                flags |= Flags.HasSubscriber;
            }
            if (this.Conversation.HasValue)
            {
                flags |= Flags.HasConversation;
            }
            if (this.Id.HasValue)
            {
                flags |= Flags.HasId;
            }
            if (!string.IsNullOrEmpty(this.Subject))
            {
                flags |= Flags.HasSubject;
            }
            if (!string.IsNullOrEmpty(base.Text))
            {
                flags |= Flags.HasText;
            }
            if (Lat != null && Lon != null)
            {
                flags |= Flags.HasLocation;
            }
            if (ByskyToken != null)
            {
                flags |= Flags.HasByskyToken;
            }
            if (File != null)
            {
                flags |= Flags.HasFile;
            }
            //
            // --->
            //
            writer.Write((byte)((byte)flags));
            //
            // --->
            //
            if (flags.HasFlag(Flags.HasSubscriber))
            {
                Write(writer, this.Subscriber.Value.Number);
                writer.Write((uint)this.Subscriber.Value.Network, 5);
            }
            if (flags.HasFlag(Flags.HasId))
            {
                writer.Write(this.Id.Value);
            }
            if (flags.HasFlag(Flags.HasConversation))
            {
                writer.Write(this.Conversation.Value);
            }
            if (flags.HasFlag(Flags.HasSubject))
            {
                Write(writer, this.Subject);
            }
            if (flags.HasFlag(Flags.HasText))
            {
                Write(writer, base.Text);
            }
            if (flags.HasFlag(Flags.HasLocation))
            {
                WriteLocation(writer);
            }
            if (flags.HasFlag(Flags.HasByskyToken))
            {
                writer.Write(base.ByskyToken.Value.Guid.ToByteArray());
            }
            if (flags.HasFlag(Flags.HasFile))
            {
                writer.Write((uint)base.FileExtension, 4);

                if (base.FileExtension.Value.IsImage())
                {
                    writer.Write((uint)base.ImageQuality, 2);
                }

                WriteBytes(writer, base.File);
            }
        }
        private List <Packet> __Pack(byte group = 0, bool returnEmptyPackets = false)
        {
            List <Packet> payloads = new List <Packet>();

            byte[] content;

            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryBitWriter writer = new BinaryBitWriter((Stream)stream))
                {
                    this.pack(writer);
                    writer.Flush();
                    content = stream.GetBuffer();
                    Array.Resize <byte>(ref content, (int)stream.Length);
                }
            }


            //var compressed = Compress(content);

            //if (this.Length > 0x4b0)
            //{
            //    throw new ArgumentException("Message too long!", "Length");
            //}

            ///8 - длина заголовков
            int length = MAX_PACKAGE_LENGTH - 8;

            int parts = (int)Math.Max(1, Math.Ceiling(content.Length / (double)length));

            if (returnEmptyPackets)
            {
                var list = new List <Packet>();
                for (int i = 0; i < parts; i++)
                {
                    list.Add(null);
                }
                return(list);
            }

            if (parts > byte.MaxValue)
            {
                throw new ArgumentException("Message too long!");
            }

            this.TotalParts = (byte)parts;
            this.Index      = 0;
            this.Group      = group;

            this.Composite = TotalParts > 1 ? Composite.Complex : Composite.Simple;


            while (true)
            {
                this.Payload = content.Skip(this.Index * length).Take(length).ToArray();

                if (this.Payload.Length == 0 && Index > 0)
                {
                    break;
                }

                using (MemoryStream stream2 = new MemoryStream())
                {
                    using (BinaryBitWriter writer2 = new BinaryBitWriter((Stream)stream2))
                    {
                        writer2.Write(SIGNATURE);
                        writer2.Write((bool)(this.Direction == Iridium360.Connect.Framework.Messaging.Direction.MO));
                        writer2.Write((bool)(this.Composite == Iridium360.Connect.Framework.Messaging.Composite.Complex));
                        writer2.Write((uint)this.Version, 3);
                        writer2.Write((bool)((((this.Length & 0x700) >> 8) & 4) == 4));
                        writer2.Write((bool)((((this.Length & 0x700) >> 8) & 2) == 2));
                        writer2.Write((bool)((((this.Length & 0x700) >> 8) & 1) == 1));


                        if (this.Composite == Iridium360.Connect.Framework.Messaging.Composite.Complex)
                        {
                            writer2.Write(this.Group);
                            writer2.Write(this.TotalParts);
                            writer2.Write(this.Index);
                        }

                        writer2.Write((byte)this.Type);
                        writer2.Write((byte)(this.Length & 0xff));
                        writer2.Write(this.Payload);
                        byte[] array = stream2.GetBuffer();
                        byte   num   = 0;
                        int    index = 0;
                        while (true)
                        {
                            if (index >= stream2.Length)
                            {
                                writer2.Write(num);
                                writer2.Flush();
                                array = stream2.GetBuffer();
                                Array.Resize <byte>(ref array, (int)stream2.Length);
                                this.Payload = array;
                                break;
                            }
                            num += array[index];
                            index++;
                        }
                    }
                }

                payloads.Add(new Packet()
                {
                    Direction  = this.Direction == Direction.MO ? PacketDirection.Outbound : PacketDirection.Inbound,
                    Index      = this.Index,
                    Group      = this.Group,
                    TotalParts = this.TotalParts,
                    Payload    = this.Payload,
                });

                this.Index++;
            }


            return(payloads);
        }
        /// <summary>
        /// https://docs.google.com/spreadsheets/d/1tB0OXeDGpwxoqD_LBXcdUXCvs6o8JoNoyXLs8IM4N0M/edit#gid=0
        /// </summary>
        /// <param name="writer"></param>
        protected override void pack(BinaryBitWriter writer)
        {
            bool extended = false;

            if (this.Version >= ProtocolVersion.v3__WeatherExtension)
            {
                extended = Forecast.Forecasts.Any(x => x.WindGust != null || x.CloudHeight != null || x.Visibility != null);
                writer.Write(extended);
            }


            writer.Write((uint)Forecast.TimeOffset + 12, 5); //[-12...14]h часовой пояс

            if (Forecast.Forecasts.Count != 16)
            {
                throw new ArgumentOutOfRangeException("Forecasts != 16");
            }

            i360Forecast prev = null;


            foreach (var ff in Forecast.Forecasts)
            {
                bool sameDay = prev?.Date.Date == ff.Date.Date;
                prev = ff;

                ///Дата не изменилась?
                writer.Write(sameDay);

                ///Если изменилась - сохраняем день от начала месяца
                if (!sameDay)
                {
                    writer.Write((uint)ff.Date.Day, 5);
                }


                writer.Write((uint)ff.HourOffset, 5);       //[0...24]h час
                writer.Write((uint)ff.Temperature + 70, 7); //[-70...50]C температура

                ///->

                double?_pressure = ff.Pressure;

                if (_pressure != null)
                {
                    _pressure -= 580;
                }

                writer.Write((uint?)_pressure, 8);  //[0...221] == [580...800]мм рт давление

                ///->

                int?_cloud = ff.Cloud;

                if (_cloud != null)
                {
                    _cloud = (int)Math.Round(_cloud.Value / 15d);
                }

                writer.Write((uint?)_cloud, 4); //[0...15] == [0...100]% облачность

                ///->

                double?_precipitation = ff.Precipitation;

                if (_precipitation.Value >= 0.05 && _precipitation.Value <= 0.1)
                {
                    _precipitation = 1;
                }
                else
                {
                    _precipitation = (int)Math.Round(_precipitation.Value * 4d);
                }

                writer.Write((uint?)_precipitation, 8); //[0...240] == [0...60]мм осадки

                ///->

                int?_windDirection = ff.WindDirection;

                if (_windDirection != null)
                {
                    _windDirection = (int)(_windDirection.Value / 45d);
                }

                writer.Write((uint?)_windDirection, 4); //[0...8] == [0...360] направление ветра

                ///->

                double?_windSpeed = ff.WindSpeed;

                if (_windSpeed != null)
                {
                    _windSpeed = (int)Math.Round(_windSpeed.Value);
                }

                writer.Write((uint?)_windSpeed, 6); //[0...60]м/с скорость ветра


                ///->

                writer.Write(ff.SnowRisk);  //[0...1] вероятность снега



                if (this.Version >= ProtocolVersion.v3__WeatherExtension && extended)
                {
                    ///->

                    int?cloudHeight = ff.CloudHeight;

                    if (cloudHeight != null)
                    {
                        cloudHeight = (int)(cloudHeight.Value / 70d);
                    }

                    writer.Write((uint?)cloudHeight, 8);  //[0..256] == [0..18000] м высота облаков

                    ///->

                    int?visibility = ff.Visibility;

                    if (visibility != null)
                    {
                        visibility = (int)(visibility.Value / 10d);
                    }

                    writer.Write((uint?)visibility, 10);

                    ///->

                    double?_gust = ff.WindGust;

                    if (_gust != null)
                    {
                        _gust = (int)Math.Round(_gust.Value);
                    }

                    writer.Write((uint?)_gust, 6); //[0...60]м/с порывы ветра

                    ///->
                }
            }

            if (this.Version >= ProtocolVersion.v4__WeatherExtension)
            {
                if (PointKey != null)
                {
                    writer.Write(true);
                    writer.Write((uint)PointKey, 4);
                }
                else
                {
                    writer.Write(false);
                }
            }
        }
 protected override void pack(BinaryBitWriter writer)
 {
     writer.Write(this.Content);
 }
Exemple #14
0
 protected override void pack(BinaryBitWriter writer)
 {
     byte[] bytes = Encoding.UTF8.GetBytes(this.Greeting);
     writer.Write(bytes);
 }