ln.snmp/types/Sequence.cs

115 lines
3.0 KiB
C#

// /**
// * File: Sequence.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using ln.snmp.asn1;
namespace ln.snmp.types
{
public class Sequence : AbstractSequence
{
private List<Variable> items = new List<Variable>();
public Sequence()
:base(new Identifier(IdentifierClass.UNIVERSAL,true,0x10))
{ }
public Sequence(IEnumerable<Variable> variables)
:this()
{
foreach (Variable variable in variables)
Add(variable);
}
public override Variable[] Items {
get => items.ToArray();
set
{
items = new List<Variable>(value);
}
}
public override void Add(Variable item)
{
this.items.Add(item);
}
public override void Remove(Variable item)
{
this.items.Remove(item);
}
public override void Remove(int n)
{
this.items.RemoveAt(n);
}
}
public abstract class AbstractSequence : Variable
{
protected AbstractSequence(Identifier identifier)
:base(identifier)
{
}
/* Items */
public abstract Variable[] Items { get; set; }
public abstract void Add(Variable item);
public abstract void Remove(Variable item);
public virtual void Remove(int n)
{
Remove(Items[n]);
}
public virtual void RemoveAll()
{
foreach (Variable item in Items)
Remove(item);
}
/* SNMP Variable */
public override object Value {
get => Items;
set => throw new NotImplementedException();
}
public override byte[] Bytes
{
get
{
MemoryStream stream = new MemoryStream();
foreach (byte[] binaryItem in Items.Select((x) => (ASN1Value)x).Select((asn1) => asn1.AsByteArray).ToArray())
{
stream.Write(binaryItem, 0, binaryItem.Length);
}
return stream.ToArray();
}
set
{
List<ASN1Value> items = new List<ASN1Value>();
MemoryStream stream = new MemoryStream(value);
while (stream.Position < stream.Length)
{
items.Add(new ASN1Value(stream));
}
Items = items.Select((x) => (Variable)x).ToArray();
}
}
public override string ToString()
{
return string.Format("[{1} {0}]",String.Join(", ",Items.Select((x) => x?.ToString())),GetType().Name);
}
}
}