Example #1
0
        /// <summary>
        /// Logs a collection of raw packets
        /// </summary>
        /// <param name="packets"></param>
        public static void Log(PacketCollection packets)
        {
            if (packets == null)
            {
                throw new ArgumentNullException("packets");
            }

            foreach (var item in packets)
            {
                Log(item);
            }
        }
Example #2
0
        public static void LogData(PacketCollection packets)
        {
            if (packets == null)
            {
                throw new ArgumentNullException("packets");
            }

            string fileName = String.Format(CultureInfo.InvariantCulture,
                                            "DataStreams\\{0}.bin", GetFileNameHeader());

            Log(fileName, packets.GetData());
        }
        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);
        }
        public void Update()
        {
            try
            {
                QueryClient      client     = new QueryClient();
                PacketCollection packets    = client.QueryServer(this.Settings);
                string           dataString = packets.GetDataString();
#if DEBUG
                Logger.Log(packets);
                Logger.LogData(packets);
                Logger.LogData(dataString);
#endif

                string[] data = Regex.Split(
                    dataString,
                    Encoding.ASCII.GetString(new byte[] { 00, 00, 01 }),
                    RegexOptions.Compiled);

                this.ServerInfo = ServerInfo.Parse(data[0]);
                this.Players    = PlayerCollection.Parse(data[1]);
                ChangeOnlineState(true);
            }
            catch (SocketException ex)
            {
                ChangeOnlineState(false);
                Trace.WriteLine(
                    String.Format(CultureInfo.InvariantCulture, "{0}", ex.Message),
                    "Network Error");
            }
            catch (Exception ex)
            {
                Trace.WriteLine(
                    String.Format(CultureInfo.InvariantCulture, "{0}", ex.Message), "General");
#if DEBUG
                Trace.WriteLine(ex.StackTrace, "Error");
#endif
            }
        }