Beispiel #1
0
        private void AddCore(PacketObject packet)
        {
            /* === 挿入先ページを取得 === */
            var page = (Page)null;

            if (pages_.Count == 0)
            {
                /* ページが存在しないときは新規ページに追加 */
                page = new Page(GetNewPageFileName());
                pages_.Add(page);
            }
            else if ((pages_.Count == 1) || (packet.MakeTime >= pages_.Last().LastPacketTime))
            {
                /* ページが1つしか存在しないとき or 最後のパケットよりも遅いパケット は最後のページに追加 */
                page = pages_.Last();

                /* 1ページあたりの最大パケット数に達しているときは新規ページに追加 */
                if (page.PacketCount >= PACKET_COUNT_PAGE_MAX)
                {
                    page = new Page(GetNewPageFileName());
                    pages_.Add(page);

                    PageOut();
                }
            }
            else
            {
                /* 挿入先ページを検索する */
                page = pages_.First();
                foreach (var page_tmp in pages_)
                {
                    if (packet.MakeTime > page_tmp.LastPacketTime)
                    {
                        continue;
                    }
                    page = page_tmp;
                }
            }

            page.SortAdd(packet);

            packet_count_++;

            /* パケット最大量をオーバーした場合は最初のページを削除 */
            if (packet_count_ > packet_count_max_)
            {
                Debugger.DebugManager.MessageOut("Remove start");
                if (pages_.Count > 0)
                {
                    page = pages_.First();

                    packet_count_ -= page.PacketCount;
                    page.Dispose();

                    pages_.RemoveAt(0);
                }
                Debugger.DebugManager.MessageOut("Remove stop");
            }
        }
Beispiel #2
0
            public void SortAdd(PacketObject packet)
            {
                PageIn();

                lock (page_sync_) {
                    SortAddCore(packet);
                }
            }
        public void Add(PacketObject packet)
        {
            var over_count = (long)packets_.Count + 1 - (long)packet_count_max_;

            if (over_count > 0)
            {
                packets_.RemoveRange(0, (int)Math.Max(over_count, (long)packet_count_max_ / 10));
            }

            packets_.Add(packet);
        }
Beispiel #4
0
        public void Add(PacketObject packet)
        {
            if (packet == null)
            {
                return;
            }

            lock (page_sync_) {
                AddCore(packet);
            }
        }
Beispiel #5
0
 public virtual bool AttributeCompare(PacketObject obj)
 {
     return((Class == obj.Class) &&
            (Facility == obj.Facility) &&
            (Alias == obj.Alias) &&
            (Priority == obj.Priority) &&
            (Attribute == obj.Attribute) &&
            (Direction == obj.Direction) &&
            (Source == obj.Source) &&
            (Destination == obj.Destination)
            );
 }
Beispiel #6
0
        public static byte[] Serialize(PacketObject packet, bool compless = true)
        {
            if (packet == null)
            {
                return(null);
            }

            /* 圧縮前データを作成 */
            var data = Binary.Serialize(packet);

            if (data == null)
            {
                return(null);
            }

            var data_type = false;

            /* 圧縮後データを作成 */
            if (compless)
            {
                var data_comp = (byte[])null;

                using (var mstream = new MemoryStream()) {
                    using (var cstream = new GZipStream(mstream, CompressionMode.Compress)) {
                        cstream.Write(data, 0, data.Length);
                        cstream.Close();

                        data_comp = mstream.ToArray();
                    }
                }

                /* 圧縮前と圧縮後のデータサイズを比較して書き込むデータを選定 */
                if (data_comp.Length < data.Length)
                {
                    data      = data_comp;
                    data_type = true;
                }
            }

            /* 書込み */
            using (var stream = new MemoryStream()) {
                using (var writer = new BinaryWriter(stream)) {
                    /* データタイプ (1 Byte) */
                    writer.Write(data_type);

                    /* データ (xx Byte) */
                    writer.Write(data);

                    return(stream.ToArray());
                }
            }
        }
            private LayerT SearchInsertLayer(PacketObject packet)
            {
                var index_first = 0;
                var index_last  = layers_.Count;
                var list_index  = 0;
                var list_temp   = layers_.First();

                /* バイナリサーチで挿入レイヤーにあたりを付ける */
                while (index_first < index_last)
                {
                    list_index = index_first + (index_last - index_first) / 2;
                    list_temp  = layers_[list_index];

                    if (list_temp.LastPacketTime < packet.MakeTime)
                    {
                        index_first = list_index + 1;
                    }
                    else
                    {
                        index_last = list_index;
                    }
                }

                /* シーケンシャルサーチで挿入レイヤーを特定する
                 * バイナリサーチで1つずれることがあるので1つ下げる */
                index_first = Math.Max(index_first - 1, 0);

                while (index_first < layers_.Count)
                {
                    if (packet.MakeTime <= layers_[index_first].LastPacketTime)
                    {
                        break;
                    }
                    index_first++;
                }

                if (index_first >= layers_.Count)
                {
                    /* 挿入レイヤーが見つからなかった場合は新規生成 */
                    list_temp = new LayerT();
                    layers_.Add(list_temp);
                }
                else
                {
                    list_temp = layers_[index_first];
                }

                return(list_temp);
            }
        public void Add(PacketObject packet)
        {
            if (packet == null)
            {
                return;
            }

            /* 追加後にパケット最大数を超える場合は最初のレイヤーを削除 */
            if (first_layer_.PacketCount >= packet_count_max_)
            {
                first_layer_.RemoveLayerAt(0);
            }

            first_layer_.Insert(packet);
        }
Beispiel #9
0
 public PacketObject(PacketObject packet, byte[] data)
     : this(
         packet.Class,
         packet.Facility,
         packet.Alias,
         packet.Priority,
         packet.Attribute,
         packet.MakeTime,
         packet.Information,
         packet.Direction,
         packet.Source,
         packet.Destination,
         packet.UserMark,
         packet.Message,
         data
         )
 {
 }
Beispiel #10
0
            public static byte[] Serialize(PacketObject packet)
            {
                using (var stream = new MemoryStream()) {
                    using (var writer = new BinaryWriter(stream)) {
                        /* フォーマットバージョン */
                        writer.Write((byte)FORMAT_VERSION);

                        /* フォーマット別処理 */
                        switch (FORMAT_VERSION)
                        {
                        case SerializeFormat.V001: Serialize_V001(writer, packet); break;

                        case SerializeFormat.V002: Serialize_V002(writer, packet); break;
                        }
                    }

                    return(stream.ToArray());
                }
            }
            public override void Insert(PacketObject packet)
            {
                var layer = (LayerT)null;

                /* 挿入レイヤー取得 */
                if (layers_.Count == 0)
                {
                    layers_.Add(new LayerT());
                }

                if (packet.MakeTime >= layers_.Last().LastPacketTime)
                {
                    layer = layers_.Last();

                    if (layer.LayerCount >= PACKET_COUNT_LAYER_MAX)
                    {
                        layer = new LayerT();
                        layers_.Add(layer);
                    }
                }
                else if (packet.MakeTime <= layers_.First().FirstPacketTime)
                {
                    layer = layers_.First();

                    if (layer.LayerCount >= PACKET_COUNT_LAYER_MAX)
                    {
                        layer = new LayerT();
                        layers_.Insert(0, layer);
                    }
                }
                else
                {
                    layer = SearchInsertLayer(packet);
                }

                /* 挿入 */
                layer.Insert(packet);

                packet_count_++;
            }
Beispiel #12
0
        public PacketObject Compile(PacketObject packet = null)
        {
            if (packet == null)
            {
                packet = packet_base_;
            }

            return(new PacketObject(
                       packet.Class,
                       packet.Facility,
                       packet.Alias,
                       packet.Priority,
                       packet.Attribute,
                       packet.MakeTime,
                       packet.Information,
                       packet.Direction,
                       packet.Source,
                       packet.Destination,
                       packet.UserMark,
                       packet.Message,
                       Data));
        }
            private List <PacketObject> SearchInsertBlock(PacketObject packet)
            {
                var index_first = 0;
                var index_last  = packets_.Count;
                var list_index  = 0;
                var list_temp   = packets_.First();

                /* バイナリサーチで挿入位置にあたりを付ける */
                while (index_first < index_last)
                {
                    list_index = index_first + (index_last - index_first) / 2;
                    list_temp  = packets_[list_index];

                    if (list_temp.Key < packet.MakeTime)
                    {
                        index_first = list_index + 1;
                    }
                    else
                    {
                        index_last = list_index;
                    }
                }

                /* 検出リストの時間が一致しない場合は新規生成 */
                if (list_temp.Key != packet.MakeTime)
                {
                    list_temp = new KeyValuePair <DateTime, List <PacketObject> >(packet.MakeTime, new List <PacketObject>());
                    packets_.Insert(index_first, list_temp);
                }
                else
                {
                    list_temp = packets_[index_first];
                }

                return(list_temp.Value);
            }
Beispiel #14
0
 public PacketBuilder(PacketObject packet)
 {
     packet_base_ = packet;
 }
Beispiel #15
0
            private static void Serialize_V000(BinaryWriter writer, PacketObject packet)
            {
                /* Facility (1 Byte) */
                writer.Write((byte)packet.Facility);

                /* Alias (1 + xx Byte) */
                var alias_data     = Encoding.UTF8.GetBytes(packet.Alias);
                var alias_data_len = Math.Min(alias_data.Length, 255);

                writer.Write((byte)alias_data_len);
                writer.Write(alias_data);

                /* Priority (1 Byte) */
                writer.Write((byte)packet.Priority);

                /* Attribute (1 Byte) */
                writer.Write((byte)packet.Attribute);

                /* Make Time (2 + 1 + 1 + 1 + 1 + 1 + 2 Byte) */
                var dt_utc = packet.MakeTime.ToUniversalTime();

                writer.Write((byte)((dt_utc.Year >> 8) & 0xFF));
                writer.Write((byte)((dt_utc.Year >> 0) & 0xFF));
                writer.Write((byte)dt_utc.Month);
                writer.Write((byte)dt_utc.Day);
                writer.Write((byte)dt_utc.Hour);
                writer.Write((byte)dt_utc.Minute);
                writer.Write((byte)dt_utc.Second);
                writer.Write((byte)((dt_utc.Millisecond >> 8) & 0xFF));
                writer.Write((byte)((dt_utc.Millisecond >> 0) & 0xFF));

                /* Information (2 + xx Byte) */
                var info_data     = Encoding.UTF8.GetBytes(packet.Information);
                var info_data_len = Math.Min(info_data.Length, 65535);

                writer.Write((byte)((info_data_len >> 8) & 0xFF));
                writer.Write((byte)((info_data_len >> 0) & 0xFF));
                writer.Write(info_data);

                /* Direction (1 Byte) */
                writer.Write((byte)packet.Direction);

                /* Source (1 + xx Byte) */
                var src_data     = Encoding.UTF8.GetBytes(packet.Source);
                var src_data_len = (byte)Math.Min(src_data.Length, 255);

                writer.Write((byte)src_data_len);
                writer.Write(src_data);

                /* Destination (1 + xx Byte) */
                var dst_data     = Encoding.UTF8.GetBytes(packet.Destination);
                var dst_data_len = (byte)Math.Min(dst_data.Length, 255);

                writer.Write((byte)dst_data_len);
                writer.Write(dst_data);

                /* User Mark (1 Byte) */
                writer.Write(packet.UserMark);

                /* パケット別 */
                switch (packet.Attribute)
                {
                case PacketAttribute.Control:
                {
                    /* Code (4 Byte) */
                    writer.Write((byte)(0));
                    writer.Write((byte)(0));
                    writer.Write((byte)(0));
                    writer.Write((byte)(0));

                    /* Data (4 + xx Byte) */
                    var data     = packet.Data;
                    var data_len = (uint)data.Length;

                    writer.Write((byte)((data_len >> 24) & 0xFF));
                    writer.Write((byte)((data_len >> 16) & 0xFF));
                    writer.Write((byte)((data_len >> 8) & 0xFF));
                    writer.Write((byte)((data_len >> 0) & 0xFF));
                    writer.Write(data);
                }
                break;

                case PacketAttribute.Message:
                {
                    /* Data (4 + xx Byte) */
                    var data     = Encoding.UTF8.GetBytes(packet.Message);
                    var data_len = (uint)data.Length;

                    writer.Write((byte)((data_len >> 24) & 0xFF));
                    writer.Write((byte)((data_len >> 16) & 0xFF));
                    writer.Write((byte)((data_len >> 8) & 0xFF));
                    writer.Write((byte)((data_len >> 0) & 0xFF));
                    writer.Write(data);
                }
                break;

                case PacketAttribute.Data:
                {
                    /* Data (4 + xx Byte)*/
                    var data     = packet.Data;
                    var data_len = (uint)data.Length;

                    writer.Write((byte)((data_len >> 24) & 0xFF));
                    writer.Write((byte)((data_len >> 16) & 0xFF));
                    writer.Write((byte)((data_len >> 8) & 0xFF));
                    writer.Write((byte)((data_len >> 0) & 0xFF));
                    writer.Write(data);
                }
                break;
                }
            }
Beispiel #16
0
            private static void Serialize_V002(BinaryWriter writer, PacketObject packet)
            {
                /* Facility (1 Byte) */
                writer.Write((byte)packet.Facility);

                /* Alias (1 + xx Byte) */
                var alias_data     = Encoding.UTF8.GetBytes(packet.Alias);
                var alias_data_len = Math.Min(alias_data.Length, 255);

                writer.Write((byte)alias_data_len);
                writer.Write(alias_data);

                /* Priority (1 Byte) */
                writer.Write((byte)packet.Priority);

                /* Attribute (1 Byte) */
                writer.Write((byte)packet.Attribute);

                /* Make Time (2 + 1 + 1 + 1 + 1 + 1 + 3 Byte) */
                var dt_utc = packet.MakeTime.ToUniversalTime();

                writer.Write((byte)((dt_utc.Year >> 8) & 0xFF));
                writer.Write((byte)((dt_utc.Year >> 0) & 0xFF));
                writer.Write((byte)dt_utc.Month);
                writer.Write((byte)dt_utc.Day);
                writer.Write((byte)dt_utc.Hour);
                writer.Write((byte)dt_utc.Minute);
                writer.Write((byte)dt_utc.Second);

                // Make Time (usec)
                var dt_utc_usec = (dt_utc.Ticks / 10) % 1000000;

                writer.Write((byte)((dt_utc_usec >> 16) & 0xFF));
                writer.Write((byte)((dt_utc_usec >> 8) & 0xFF));
                writer.Write((byte)((dt_utc_usec >> 0) & 0xFF));

                /* Information (2 + xx Byte) */
                var info_data     = Encoding.UTF8.GetBytes(packet.Information);
                var info_data_len = Math.Min(info_data.Length, 65535);

                writer.Write((byte)((info_data_len >> 8) & 0xFF));
                writer.Write((byte)((info_data_len >> 0) & 0xFF));
                writer.Write(info_data);

                /* Direction (1 Byte) */
                writer.Write((byte)packet.Direction);

                /* Source (1 + xx Byte) */
                var src_data     = Encoding.UTF8.GetBytes(packet.Source);
                var src_data_len = (byte)Math.Min(src_data.Length, 255);

                writer.Write((byte)src_data_len);
                writer.Write(src_data);

                /* Destination (1 + xx Byte) */
                var dst_data     = Encoding.UTF8.GetBytes(packet.Destination);
                var dst_data_len = (byte)Math.Min(dst_data.Length, 255);

                writer.Write((byte)dst_data_len);
                writer.Write(dst_data);

                /* User Mark (1 Byte) */
                writer.Write(packet.UserMark);

                /* Message (2 + xx Byte) */
                var msg_data     = Encoding.UTF8.GetBytes(packet.Message);
                var msg_data_len = (byte)Math.Min(msg_data.Length, 0xFFFF);

                writer.Write((ushort)msg_data_len);
                writer.Write(msg_data);

                /* Data (4 + xx Byte) */
                var data     = packet.Data;
                var data_len = (uint)data.Length;

                writer.Write((byte)((data_len >> 24) & 0xFF));
                writer.Write((byte)((data_len >> 16) & 0xFF));
                writer.Write((byte)((data_len >> 8) & 0xFF));
                writer.Write((byte)((data_len >> 0) & 0xFF));
                writer.Write(data);
            }