コード例 #1
0
ファイル: BaseServer.cs プロジェクト: bmjoy/FastSocketsAPI
        protected void PingCallback(object sender, System.Net.NetworkInformation.PingCompletedEventArgs e)
        {
            // If the operation was canceled, display a message to the user.
            if (e.Cancelled)
            {
                Console.WriteLine("Ping canceled.");
                Disconnect(((ClientConnection)e.UserState).ThisID);
            }

            // If an error occurred, display the exception to the user.
            if (e.Error != null)
            {
                Console.WriteLine("Ping failed:");
                Disconnect(((ClientConnection)e.UserState).ThisID);
            }

            System.Net.NetworkInformation.PingReply reply = e.Reply;
            int id = ((int)e.UserState);

            Clients[id].Ping = (int)(reply.RoundtripTime / 2);
            PacketDesc_Ping pkt = new PacketDesc_Ping();

            pkt.PacketTarget         = EConnectionType.CLIENT;
            pkt.PacketOriginClientID = UniqueID;
            pkt.ToServerLatency      = Clients[id].Ping; // send clients one way to server latency
            SendPacketToClient(pkt, id);
        }
コード例 #2
0
        /// <summary>
        /// Received the packet ping.
        /// </summary>
        /// <param name="conPkt">The sender connection and packet.</param>
        /// <returns>use how needed</returns>
        protected virtual bool ReceivedPacket_Ping(KeyValuePair <ClientConnection, object> conPkt)
        {
            PacketDesc_Ping packet = (PacketDesc_Ping)conPkt.Value;

            if (ParentServerConnection != null)
            {
                ParentServerConnection.Ping = packet.ToServerLatency;
                //Console.WriteLine("Ping recieved, latency: " + packet.ToServerLatency);
            }

            if (OnPingReceived != null)
            {
                OnPingReceived(conPkt);
            }

            return(true);
        }
コード例 #3
0
        protected void PingCallback(object sender, System.Net.NetworkInformation.PingCompletedEventArgs e)
        {
            // If the operation was canceled, display a message to the user.
            if (e.Cancelled)
            {
                Console.WriteLine("Ping canceled.");
                Disconnect(((ClientConnection)e.UserState).ThisID);
                ((Ping)sender).Dispose();
                PingsDoneCount++;
                if (PingsDoneCount >= Clients.Count)
                {
                    PingsDoneCount = 0;
                }
                return;
            }

            // If an error occurred, display the exception to the user.
            if (e.Error != null)
            {
                Console.WriteLine("Ping failed:");
                Disconnect(((ClientConnection)e.UserState).ThisID);
                ((Ping)sender).Dispose();
                PingsDoneCount++;
                if (PingsDoneCount >= Clients.Count)
                {
                    PingsDoneCount = 0;
                }
                return;
            }

            PingReply        reply = e.Reply;
            int              id    = ((int)e.UserState);
            ClientConnection cc    = null;

            if (!Clients.TryGetValue(id, out cc))
            {
                ((Ping)sender).Dispose();
                PingsDoneCount++;
                if (PingsDoneCount >= Clients.Count)
                {
                    PingsDoneCount = 0;
                }
                return;
            }

            if (!cc.ThisClient.Connected)
            {
                ((Ping)sender).Dispose();
                PingsDoneCount++;
                if (PingsDoneCount >= Clients.Count)
                {
                    PingsDoneCount = 0;
                }
                return;
            }

            cc.Ping = (int)(reply.RoundtripTime / 2);
            PacketDesc_Ping pkt = new PacketDesc_Ping();

            pkt.PacketTarget         = EConnectionType.CLIENT;
            pkt.PacketOriginClientID = UniqueID;
            pkt.ToServerLatency      = cc.Ping; // send clients one way to server latency
            SendPacketToClient(pkt, id);
            ((Ping)sender).Dispose();
            PingsDoneCount++;

            if (PingsDoneCount >= Clients.Count)
            {
                PingsDoneCount = 0;
            }
        }