using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using skyscraper5.Skyscraper;
using skyscraper5.Skyscraper.IO;

namespace skyscraper5.Ietf.Rfc768
{
    public class UserDatagram : Validatable
    {
        public UserDatagram(byte[] buffer)
        {
            if (buffer.Length < 8)
            {
                Valid = false;
                return;
            }
            MemoryStream ms = new MemoryStream(buffer, false);
            SourcePort = ms.ReadUInt16BE();
            DestinationPort = ms.ReadUInt16BE();
            ushort length = ms.ReadUInt16BE();
            ushort checksum = ms.ReadUInt16BE();
            Payload = ms.ReadBytes(ms.GetAvailableBytes());
            Valid = true;
        }

        public byte[] Payload { get; private set; }

        public ushort DestinationPort { get; set; }

        public ushort SourcePort { get; private set; }
    }
}
