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};
        }
示例#2
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
            });
        }
        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;
        }
示例#4
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);
        }