Exemplo n.º 1
0
        public bool TryGetUnspentTx(UInt256 txHash, out UnspentTx unspentTx)
        {
            CheckTransaction();

            return(CursorTryGet(txHash, out unspentTx, unspentTxes, MakeUnspentTxKey, x => DataDecoder.DecodeUnspentTx(x)));
        }
Exemplo n.º 2
0
        public bool TryGetHeader(UInt256 blockHash, out ChainedHeader header)
        {
            CheckTransaction();

            return(CursorTryGet(blockHash, out header, headers, MakeHeaderKey, x => DataDecoder.DecodeChainedHeader(x)));
        }
Exemplo n.º 3
0
        static void Main()
        {
            XmlConfigurator.Configure();

            IPAddress ip;

            if (!IPAddress.TryParse(ConfigurationManager.AppSettings["ipAddress"], out ip))
            {
                Log.Error("Ip is not valid.");
                throw new ArgumentException("Ip is not valid.");
            }

            int port;

            if (!int.TryParse(ConfigurationManager.AppSettings["port"], out port))
            {
                Log.Error("Port is not valid.");
                throw new ArgumentException("Port is not valid.");
            }

            Task.Run(async() =>
            {
                using (var udpClient = new UdpClient(new IPEndPoint(ip, port)))
                {
                    Log.Info("Listening...");

                    while (true)
                    {
                        //IPEndPoint object will allow us to read datagrams sent from any source.
                        var receivedResults = await udpClient.ReceiveAsync();

                        byte[] data = receivedResults.Buffer;

                        Log.Info(string.Format("Received connection from: {0}", receivedResults.RemoteEndPoint));
                        Log.Info(string.Format("{0} - received [{1}]", DateTime.Now, String.Join("", data.Select(x => x.ToString("X2")).ToArray())));

                        var reader = new ReverseBinaryReader(new MemoryStream(data));

                        // Decode data
                        var avlData = new DataDecoder(reader).DecodeUdpData();

                        // Create response
                        var bytes = new List <byte>();

                        const short packetLength = 2 /* Id */ + 1 /* Type */ + 1 /* Avl packet id */ + 1 /* num of accepted elems */;
                        bytes.AddRange(BitConverter.GetBytes(BytesSwapper.Swap(packetLength)));
                        bytes.AddRange(BitConverter.GetBytes(BytesSwapper.Swap(avlData.PacketId)));
                        bytes.Add(avlData.PacketType);
                        bytes.Add(avlData.AvlPacketId);
                        bytes.Add((byte)avlData.AvlData.DataCount);

                        var response = bytes.ToArray();

                        Log.Info(string.Format("{0} - received [{1}]", DateTime.Now, String.Join("", bytes.Select(x => x.ToString("X2")).ToArray())));

                        await udpClient.SendAsync(response, response.Length, receivedResults.RemoteEndPoint);
                    }
                }
            });

            Console.ReadLine();
        }
Exemplo n.º 4
0
        public void LoadCustomData(string Data)
        {
            DataDecoder dd = new DataDecoder(Data);

            Location = dd.ReadString();
        }
Exemplo n.º 5
0
        public void LoadData()
        {
            UI.SetStatus("Loading Data - First Time Check", 0, 2);
            if (!ContentFile.Exists("nanaData"))                       //Checks for the existence of the nanaData file (Holds tag, group and media information)
            {
                UI.SetStatus("Loading Data - First Time Check", 1, 2); //If not, generate a new file
                DataEncoder encoder = new DataEncoder();               //Initialise the encoding tool
                encoder.Write(VersionMajor);                           //Setting the file version
                encoder.Write(VersionMinor);
                encoder.Write(0);
                encoder.Write(0);   //No tags, groups or media yet
                encoder.Write(0);
                UI.SetStatus("Loading Data - First Time Check", 2, 2);
                ContentFile.WriteFile("nanaData", encoder.ToString()); //Write the data to a nanaData file
                return;                                                //As there is no non-default data, don't try to read any
            }
            UI.SetStatus("Loading Data - Loading Data", 0, 2);

            DataDecoder decoder = new DataDecoder(ContentFile.ReadFile("nanaData"));     //Initialise the decoding tool

            UI.SetStatus("Loading Data - Checking Version", 1, 2);
            //Versioning
            int major = decoder.ReadInt32();
            int minor = decoder.ReadInt32(); //Read the version number

            UI.SetStatus("Loading Data - Checking Version", 2, 2);
            if (major == VersionMajor && minor == VersionMinor)     //Check if the version is the current version
            {
                UI.SetStatus("Loading Data - Fetching Data Quantity", 0, 3);

                //Getting the amount of data to read (No eof for zip streams)
                int groupCount = decoder.ReadInt32();
                UI.SetStatus("Loading Data - Fetching Data Quantity", 1, 3);
                int tagCount = decoder.ReadInt32();
                UI.SetStatus("Loading Data - Fetching Data Quantity", 2, 3);
                int imageCount = decoder.ReadInt32();
                UI.SetStatus("Loading Data - Fetching Data Quantity", 3, 3);
                double operations = groupCount + tagCount + imageCount; //How much data to read. Used to display progress
                double progress   = 0;

                Data.TagLocations.Clear(); //Initialise default values (Mostly used in case data is loaded again)
                Data.Groups.Clear();
                Data.Tags       = new Tag[tagCount];
                Data.HiddenTags = new int[tagCount];

                UI.SetStatus($"Loading Data - Getting Groups 0/{groupCount}", progress, operations);
                //Begin reading group data
                for (int i = 0; i < groupCount; i++)
                {
                    Data.Groups[decoder.ReadInt32()] = decoder.ReadString(); //Groups are just names and an ID, so this can be limited to a single line
                    UI.SetStatus($"Loading Data - Getting Groups {i + 1}/{groupCount}", progress++, operations);
                }

                UI.SetStatus($"Loading Data - Getting Tags 0/{tagCount}", progress, operations);
                int hiddenCount = 0;
                //Begin reading tag data
                for (int i = 0; i < tagCount; i++)
                {
                    bool hidden = decoder.ReadBoolean();                               //First data point is whether the tag is hidden or not
                    int  index  = decoder.ReadInt32();                                 //The ID index of the tag
                    Data.TagLocations.Add(index, i);                                   //Links the tag's ID to its location
                    string name    = decoder.ReadString();                             //Get the name
                    int[]  aliases = decoder.ReadInt32Array().ToArray();               //Read the linked IDs of this tag as an array
                    Data.Tags[i] = new Tag(name, index, decoder.ReadInt32(), aliases); //Last integer in the data is the group ID
                    if (hidden)
                    {
                        Data.HiddenTags[hiddenCount++] = index; //Adds the tag to the hidden tag list if the tag is a hidden tag
                    }
                    UI.SetStatus($"Loading Data - Getting Tags {i + 1}/{tagCount}", progress++, operations);
                }

                UI.SetStatus($"Loading Data - Getting Images 0/{imageCount}", progress, operations);
                //Begin reading image data
                for (int i = 0; i < imageCount; i++)
                {
                    string uID      = decoder.ReadString();                                                                                                         //Get the UID of the media (The key in the database)
                    string fileType = decoder.ReadString();                                                                                                         //Get the file extension of the media (For handling different data types)
                    int[]  tags     = decoder.ReadInt32Array().ToArray();                                                                                           //Read the selected tags' IDs for the media as an array
                    Data.Media.Add(uID, (IMedia)Registry.MediaConstructors[Registry.ExtensionConstructors[fileType]].Invoke(new object[] { uID, tags, fileType })); //Gets which data type can represent the media, and constructs the object with the provided data
                    UI.SetStatus($"Loading Data - Getting Images {i + 1}/{imageCount}", progress++, operations);
                }
            }
            else
            {
                //TODO - CODE TO DIVERT OR ERROR IF WRONG VERSION
            }
        }