public void GetDataTest()
        {
            List <byte[]>    rawPackets = DataGenerator.GetInfoPackets();
            InfoPacket       packet1    = new InfoPacket(rawPackets[0]);
            InfoPacket       packet2    = new InfoPacket(rawPackets[1]);
            PacketCollection packets    = new PacketCollection();

            packets.Add(packet1);
            packets.Add(packet2);


            byte[] result = packets.GetData();
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Length > 0);
            Assert.IsTrue(result.Length == packet1.GetData().Length + packet2.GetData().Length);
        }
        public void GetDataStringTest()
        {
            List <byte[]>    rawPackets = DataGenerator.GetInfoPackets();
            InfoPacket       packet1    = new InfoPacket(rawPackets[0]);
            InfoPacket       packet2    = new InfoPacket(rawPackets[1]);
            PacketCollection packets    = new PacketCollection();

            packets.Add(packet1);
            packets.Add(packet2);

            string dataString1 = packet1.GetDataString();
            string dataString2 = packet2.GetDataString();
            string result      = packets.GetDataString();

            Assert.IsTrue(!String.IsNullOrWhiteSpace(result));
            Assert.AreEqual <string>(packet1.GetDataString() + packet2.GetDataString(), result);
        }
Ejemplo n.º 3
0
        public PacketCollection QueryServer(NetworkSettings nwSettings)
        {
            if (nwSettings == null)
                throw new ArgumentNullException("nwSettings");

            PacketCollection packets = new PacketCollection();

            using (UdpClient client = new UdpClient(nwSettings.LocalPort))
            {
                byte[] request, response,
                    timestamp = Helpers.GetTimeStamp();

                IPEndPoint remoteIpEndpoint = new IPEndPoint(IPAddress.Parse(nwSettings.Host), nwSettings.RemotePort);
                client.Client.ReceiveTimeout = nwSettings.ReceiveTimeout;
                client.Connect(remoteIpEndpoint);

                request = GetChallengeRequest(timestamp);
                client.Send(request, request.Length);
                response = client.Receive(ref remoteIpEndpoint);
                ChallengePacket pChallenge = new ChallengePacket(response);

                request = GetInfosRequest(timestamp, pChallenge.GetChallenge());
                client.Send(request, request.Length);

                while (true)
                {
                    try
                    {
                        response = client.Receive(ref remoteIpEndpoint);
                        packets.Add(new InfoPacket(response));
                    }
                    catch (SocketException ex)
                    {
                        if (ex.ErrorCode == (int)SocketError.TimedOut)
                            break;
                        else
                            throw;
                    }
                }
            }

            return packets;
        }