예제 #1
0
 public static int TimeSpan(PacketDataDefine x)
 {
     return((int)(DateTime.Now.Ticks - x.TimeStamp) / 10000);
 }
예제 #2
0
    public void SendRandomPacket()
    {
        var d = new PacketDataDefine(0, frameCount, Guid.NewGuid().ToString());

        SendString(d.ToJson(), true, Reliability);
    }
예제 #3
0
 public static int TimeSpan(PacketDataDefine x, PacketDataDefine y)
 {
     return((int)(x.TimeStamp - y.TimeStamp) / 10000);
 }
예제 #4
0
    void Update()
    {
        ++frameCount;
        if (client == null)
        {
            return;
        }

        for (p = client.Receive(); p != null; client.DeallocatePacket(p), p = client.Receive())
        {
            packetIdentifier = GetPacketIdentifier(p);
            switch ((DefaultMessageIDTypes)packetIdentifier)
            {
            case DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION:
                Debug.Log("ID_DISCONNECTION_NOTIFICATION");
                break;

            case DefaultMessageIDTypes.ID_ALREADY_CONNECTED:
                Debug.Log("ID_ALREADY_CONNECTED with guid " + p.guid);
                break;

            case DefaultMessageIDTypes.ID_INCOMPATIBLE_PROTOCOL_VERSION:
                Debug.Log("ID_INCOMPATIBLE_PROTOCOL_VERSION ");
                break;

            case DefaultMessageIDTypes.ID_REMOTE_DISCONNECTION_NOTIFICATION:
                Debug.Log("ID_REMOTE_DISCONNECTION_NOTIFICATION ");
                break;

            case DefaultMessageIDTypes.ID_REMOTE_CONNECTION_LOST:             // Server telling the clients of another client disconnecting forcefully.  You can manually broadcast this in a peer to peer enviroment if you want.
                Debug.Log("ID_REMOTE_CONNECTION_LOST");
                break;

            case DefaultMessageIDTypes.ID_CONNECTION_BANNED:             // Banned from this server
                Debug.Log("We are banned from this server.\n");
                break;

            case DefaultMessageIDTypes.ID_CONNECTION_ATTEMPT_FAILED:
                Debug.Log("Connection attempt failed ");
                break;

            case DefaultMessageIDTypes.ID_NO_FREE_INCOMING_CONNECTIONS:
                Debug.Log("Server is full ");
                break;

            case DefaultMessageIDTypes.ID_INVALID_PASSWORD:
                Debug.Log("ID_INVALID_PASSWORD\n");
                break;

            case DefaultMessageIDTypes.ID_CONNECTION_LOST:
                // Couldn't deliver a reliable packet - i.e. the other system was abnormally
                // terminated
                Debug.Log("ID_CONNECTION_LOST\n");
                break;

            case DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED:
                // This tells the client they have connected
                Debug.Log("ID_CONNECTION_REQUEST_ACCEPTED to %s " + p.systemAddress.ToString() + "with GUID " + p.guid.ToString());
                Debug.Log("My external address is:" + client.GetExternalID(p.systemAddress).ToString());
                break;

            case DefaultMessageIDTypes.ID_CONNECTED_PING:
            case DefaultMessageIDTypes.ID_UNCONNECTED_PING:
                Debug.Log("Ping from " + p.systemAddress.ToString(true));
                break;

            default:
                messageCount++;
                System.DateTime now  = System.DateTime.Now;
                string          info = System.Text.Encoding.UTF8.GetString(p.data).Trim();
                try{
                    var data    = PacketDataDefine.FromJson(info);
                    int millsec = PacketDataDefine.TimeSpan(data);
                    int ping    = millsec;
                    if (data.Index != 1 + lastFrame)
                    {
                        ++errorCount;
                    }
                    lastFrame  = data.Index;
                    totalPing += ping;
                    //string[] t = info.Trim().Split();
                    //int serverToClientDuration = (now.Second - int.Parse(t[6])) * 1000 + (now.Millisecond - int.Parse(t[7]));
                    //int clientToServerDuration = (now.Second - int.Parse(t[2])) * 1000 + (now.Millisecond - int.Parse(t[3]));
                    //string outInfo = string.Format("{0}服务器到客户端:{1}ms  客户端到服务器:{2}ms", info, serverToClientDuration, clientToServerDuration);
                    string outInfo = string.Format("Index:{0} Ping:{1}ms Content{2}", data.Index, ping, data.Content);
                    PrintContent(outInfo);

                    statText.text = string.Format("平均Ping:{0}  消息总数:{1}  错误顺序:{2}", AveragePing, messageCount, errorCount - 1);
                }
                catch {
                    PrintContent(info);
                }
                break;
            }
        }

        if (bSendRandomPacket)
        {
            SendRandomPacket();
        }
        else
        {
            lastFrame = frameCount;
        }
    }