public ResponseData GetResponseData(string host, short port, int protocolVersion)
        {
            ServerHost = host;
            ServerPort = port;

            var pingPacketReceived = false;
            var info = new ServerInfo();

            _handler = new NetworkHandler(this);
            _handler.OnPacketHandled += RaisePacketHandled;
            _handler.Start();

            #region Handle Early connect error

            while (!Connected)
            {
                if (Crashed)
                {
                    Dispose();
                    return new ResponseData {Info = new ServerInfo(), Ping = int.MaxValue};
                }

                Thread.Sleep(250);
            }

            #endregion

            #region Packet handlers

            FireResponsePacket += packet =>
            {
                // Send Ping Packet after receiving Response Packet
                SendPacket(new PingPacket {Time = DateTime.UtcNow.Millisecond});

                info = ParseResponse(packet);
            };

            FirePingPacket += packet =>
            {
                pingPacketReceived = true;
            };

            #endregion

            RequestServerInfo(protocolVersion);

            #region Waiting for all packets handling and handling if something went wrong

            var watch = Stopwatch.StartNew();
            while (!pingPacketReceived)
            {
                if (_handler == null || _handler.Crashed)
                {
                    watch.Stop();
                    Dispose();
                    return new ResponseData {Info = new ServerInfo(), Ping = int.MaxValue};
                }

                if (watch.ElapsedMilliseconds > 2000)
                {
                    watch.Stop();
                    Dispose();
                    return new ResponseData
                    {
                        Info = new ServerInfo {Version = new Version {Name = "Unsupported", Protocol = 0}},
                        Ping = (int) PingServer(host, port)
                    };
                }

                Thread.Sleep(50);
            }

            watch.Stop();

            #endregion

            var ping = PingServer(host, port);

            Dispose();
            return new ResponseData {Info = info, Ping = (int) ping};
        }
        public int GetPing(string host, short port, int protocolVersion)
        {
            ServerHost = host;
            ServerPort = port;

            var pingPacketReceived = false;

            _handler = new NetworkHandler(this);
            _handler.OnPacketHandled += RaisePacketHandled;
            _handler.Start();

            #region Handle Early connect error

            while (!Connected)
            {
                if (Crashed)
                {
                    Dispose();
                    return int.MaxValue;
                }

                Thread.Sleep(250);
            }

            #endregion

            #region Packet handlers

            FireResponsePacket +=
                packet => SendPacket(new PingPacket {Time = DateTime.UtcNow.Millisecond});

            FirePingPacket += packet =>
            {
                pingPacketReceived = true;
            };

            #endregion

            RequestServerInfo(protocolVersion);

            #region Waiting for all packets handling and handling if something went wrong

            while (!pingPacketReceived)
            {
                if (_handler != null && Connected)
                {
                    Dispose();
                    return int.MaxValue;
                }

                Thread.Sleep(50);
            }

            #endregion

            var ping = PingServer(host, port);

            Dispose();
            return (int) ping;
        }
        public ServerInfo GetServerInfo(string host, short port, int protocolVersion)
        {
            ServerHost = host;
            ServerPort = port;

            var responsePacketReceived = false;
            var info = new ServerInfo();

            _handler = new NetworkHandler(this);
            _handler.OnPacketHandled += RaisePacketHandled;
            _handler.Start();

            #region Handle Early connect error

            while (!Connected)
            {
                if (Crashed)
                {
                    Dispose();
                    return new ServerInfo();
                }

                Thread.Sleep(250);
            }

            #endregion

            #region Packet handlers

            FireResponsePacket += delegate(IPacket packet)
            {
                info = ParseResponse(packet);
                responsePacketReceived = true;
            };

            #endregion

            RequestServerInfo(protocolVersion);

            #region Waiting for all packets handling and handling if something went wrong

            var watch = Stopwatch.StartNew();
            while (!responsePacketReceived)
            {
                if (_handler != null && _handler.Crashed)
                {
                    Dispose();
                    return new ServerInfo();
                }

                if (watch.ElapsedMilliseconds > 2000)
                {
                    watch.Stop();
                    Dispose();
                    return new ServerInfo {Version = new Version {Name = "Unsupported", Protocol = 0}};
                }

                Thread.Sleep(50);
            }

            watch.Stop();

            #endregion

            Dispose();
            return info;
        }
Beispiel #4
0
        public ResponseData GetResponseData(string host, short port, int protocolVersion)
        {
            ServerHost = host;
            ServerPort = port;

            var pingPacketReceived = false;
            var info = new ServerInfo();

            _handler = new NetworkHandler(this);
            _handler.OnPacketHandled += RaisePacketHandled;
            _handler.Start();

            #region Handle Early connect error

            while (!Connected)
            {
                if (Crashed)
                {
                    Dispose();
                    return(new ResponseData {
                        Info = new ServerInfo(), Ping = int.MaxValue
                    });
                }

                Thread.Sleep(250);
            }

            #endregion

            #region Packet handlers

            FireResponsePacket += packet =>
            {
                // Send Ping Packet after receiving Response Packet
                SendPacket(new PingPacket {
                    Time = DateTime.UtcNow.Millisecond
                });

                info = ParseResponse(packet);
            };

            FirePingPacket += packet =>
            {
                pingPacketReceived = true;
            };

            #endregion

            RequestServerInfo(protocolVersion);

            #region Waiting for all packets handling and handling if something went wrong

            var watch = Stopwatch.StartNew();
            while (!pingPacketReceived)
            {
                if (_handler == null || _handler.Crashed)
                {
                    watch.Stop();
                    Dispose();
                    return(new ResponseData {
                        Info = new ServerInfo(), Ping = int.MaxValue
                    });
                }

                if (watch.ElapsedMilliseconds > 2000)
                {
                    watch.Stop();
                    Dispose();
                    return(new ResponseData
                    {
                        Info = new ServerInfo {
                            Version = new Version {
                                Name = "Unsupported", Protocol = 0
                            }
                        },
                        Ping = (int)PingServer(host, port)
                    });
                }

                Thread.Sleep(50);
            }

            watch.Stop();

            #endregion

            var ping = PingServer(host, port);

            Dispose();
            return(new ResponseData {
                Info = info, Ping = (int)ping
            });
        }
Beispiel #5
0
        public int GetPing(string host, short port, int protocolVersion)
        {
            ServerHost = host;
            ServerPort = port;

            var pingPacketReceived = false;

            _handler = new NetworkHandler(this);
            _handler.OnPacketHandled += RaisePacketHandled;
            _handler.Start();

            #region Handle Early connect error

            while (!Connected)
            {
                if (Crashed)
                {
                    Dispose();
                    return(int.MaxValue);
                }

                Thread.Sleep(250);
            }

            #endregion

            #region Packet handlers

            FireResponsePacket +=
                packet => SendPacket(new PingPacket {
                Time = DateTime.UtcNow.Millisecond
            });

            FirePingPacket += packet =>
            {
                pingPacketReceived = true;
            };

            #endregion

            RequestServerInfo(protocolVersion);

            #region Waiting for all packets handling and handling if something went wrong

            while (!pingPacketReceived)
            {
                if (_handler != null && Connected)
                {
                    Dispose();
                    return(int.MaxValue);
                }

                Thread.Sleep(50);
            }

            #endregion

            var ping = PingServer(host, port);

            Dispose();
            return((int)ping);
        }
Beispiel #6
0
        public ServerInfo GetServerInfo(string host, short port, int protocolVersion)
        {
            ServerHost = host;
            ServerPort = port;

            var responsePacketReceived = false;
            var info = new ServerInfo();

            _handler = new NetworkHandler(this);
            _handler.OnPacketHandled += RaisePacketHandled;
            _handler.Start();

            #region Handle Early connect error

            while (!Connected)
            {
                if (Crashed)
                {
                    Dispose();
                    return(new ServerInfo());
                }

                Thread.Sleep(250);
            }

            #endregion

            #region Packet handlers

            FireResponsePacket += delegate(IPacket packet)
            {
                info = ParseResponse(packet);
                responsePacketReceived = true;
            };

            #endregion

            RequestServerInfo(protocolVersion);

            #region Waiting for all packets handling and handling if something went wrong

            var watch = Stopwatch.StartNew();
            while (!responsePacketReceived)
            {
                if (_handler != null && _handler.Crashed)
                {
                    Dispose();
                    return(new ServerInfo());
                }

                if (watch.ElapsedMilliseconds > 2000)
                {
                    watch.Stop();
                    Dispose();
                    return(new ServerInfo {
                        Version = new Version {
                            Name = "Unsupported", Protocol = 0
                        }
                    });
                }

                Thread.Sleep(50);
            }

            watch.Stop();

            #endregion

            Dispose();
            return(info);
        }