Пример #1
0
    // Update is called once per frame
    void Update()
    {
        CheckPingOut();
        checkDel();
        List <network_utils.cl_data> messages = new List <network_utils.cl_data>();

        messages.Clear();
        Receive(ref messages);
        foreach (network_utils.cl_data bb in messages)
        {
            int count = 0;
            bb.data.CopyTo(bb.data, count);
            network_utils.HEADER header = network_utils.nData.Instance.DeserializeMsg <network_utils.HEADER>(bb.data);
            if (header.signum != network_utils.SIGNUM.BIN)
            {
                continue;
            }
            SetPing(bb.containerID);
            switch (header.command)
            {
/*                case (int)network_data.COMMANDS.cend_ingame:
 *                  {
 *                      network_data.end_ingame com = network_utils.nData.Instance.DeserializeMsg<network_data.end_ingame>(bb.data);
 *                      socketList[com.header.containerID].del = true;
 *                  }
 *                  break;
 */
            case (int)network_data.COMMANDS.cping:
            {
                network_data.ping com = network_utils.nData.Instance.DeserializeMsg <network_data.ping>(bb.data);
                com.relfecttime = Timer.ElapsedMilliseconds;
                byte[] data = network_utils.nData.Instance.SerializeMsg <network_data.ping>(com);
                Send(com.header.containerID, data);
            }
            break;

            case (int)network_data.COMMANDS.cset_ingame_param:
            {
                network_data.set_ingame_param com = network_utils.nData.Instance.DeserializeMsg <network_data.set_ingame_param>(bb.data);
                socketList[com.header.containerID].playername = com.playername;
            }
            break;

            default:
            {
                channel c = ChannelObjectList[header.channelID].GetComponent <channel>();
                c.ProcessMessage(ref bb.data, c.gameObject.GetComponent <receiver>());
            }
            break;
            }
        }
        if (beginning == true && socketUsedIDList.Count == 0)
        {
            endgame = true;
        }
    }
Пример #2
0
    }// Update is called once per frame

    void Update()
    {
        if (ingameContID != -1)
        {
            long l        = Timer.ElapsedMilliseconds;
            long duration = Timer.ElapsedMilliseconds - lastPing;
            if (duration > 250)
            {
                lastPing = l;
                network_data.ping m = new network_data.ping();
                m.set(ingameContID, 0);
                m.sendtime = Timer.ElapsedMilliseconds;
                byte[] data = network_utils.nData.Instance.SerializeMsg <network_data.ping>(m);
                Send(ingameContID, data);
            }
        }
        List <byte[]> datas = new List <byte[]>();

        Receive(ref datas);
        if (datas.Count > 0)
        {
            for (int i = 0; i < datas.Count; i++)
            {
                byte[] data = datas[i];
                if (data.Length > 0)
                {
                    network_utils.HEADER header = network_utils.nData.Instance.DeserializeMsg <network_utils.HEADER>(data);
                    if (header.signum == network_utils.SIGNUM.BIN)
                    {
                        switch (header.command)
                        {
                        case (int)network_data.COMMANDS.cping:
                        {
                            long ti = Timer.ElapsedMilliseconds;
                            network_data.ping com = network_utils.nData.Instance.DeserializeMsg <network_data.ping>(data);
                            long t = 0;
                            if (pingInit == false)
                            {
                                t               = com.relfecttime + (ti - com.sendtime) / 2 - ti;
                                pingInit        = true;
                                ServerTime.time = t;
                            }
                            else
                            {
                                long delta1 = (ti - com.sendtime) / 2;
                                long delta  = 0;
//                                        for (int j = 0; j < 8; j++) delta += timedelta[j];
//                                        delta /= 8;
//                                        if (Math.Abs(delta) < Math.Abs(delta1 / 2)) delta1 = 0;
                                timedelta[timedeltaIndex] = delta1;
                                timedeltaIndex            = (timedeltaIndex + 1) % 8;
                                for (int j = 0; j < 8; j++)
                                {
                                    delta += timedelta[j];
                                }
                                delta /= 8;
                                t      = com.relfecttime + delta - ti;
                            }
//                                    lasttimedelta = Mathf.Abs(ServerTime.time - t);
                            lasttimedelta   = ServerTime.time - t;
                            ServerTime.time = t;
                        }
                        break;

                        case (int)network_data.COMMANDS.cset_ingame_param:
                        {
                            network_data.set_ingame_param m = network_utils.nData.Instance.DeserializeMsg <network_data.set_ingame_param>(data);
                            ingameContID = m.header.containerID;
                            //                                        m.playername = main.playername;
                            //                                       byte[] d = network_utils.nData.Instance.SerializeMsg<network_data.set_ingame_param>(m);
                            //                                      connect.Send(d);

                            network_data.enter_ship s = new network_data.enter_ship();
                            s.set(ingameContID, 1);
                            s.shipchannel = m.shipchannel;
                            byte[] data1 = network_utils.nData.Instance.SerializeMsg <network_data.enter_ship>(s);
                            Send(ingameContID, data1);

                            Debug.Log(ingameContID);
                        }
                        break;

                        default:
                        {
                            if (ChannelObjectList.ContainsKey(header.channelID))
                            {
                                channel c = ChannelObjectList[header.channelID].GetComponent <channel>();
                                c.ProcessMessage(ref data, c.gameObject.GetComponent <receiver>());
                            }
                        }
                        break;
                        }
                    }
                }
            }
        }
    }