}// 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; } } } } } }
public override void ProcessMessage(ref byte[] message) { network_utils.HEADER header = network_utils.nData.Instance.DeserializeMsg <network_utils.HEADER>(message); if (header.signum != network_utils.SIGNUM.BIN) { return; } switch (header.command) { case (int)network_data.COMMANDS.center_ship: { network_data.enter_ship com = network_utils.nData.Instance.DeserializeMsg <network_data.enter_ship>(message); if (Channel.GetNetwork().IsClient() == false) { GameObject g = GetNextSpawnPoint(); SpawnPlayer(com.header.containerID, Channel.GetNetwork().IsClient(), false, g.transform.localPosition, g.transform.localRotation, 0); } } break; case (int)network_data.COMMANDS.ccreate_player: { network_data.create_player com = network_utils.nData.Instance.DeserializeMsg <network_data.create_player>(message); if (Channel.GetNetwork().IsClient() == true) { SpawnPlayer(com.header.containerID, Channel.GetNetwork().IsClient(), Channel.GetNetwork().GetComponent <client>().ingameContID == com.header.containerID, com.position, com.rotation, 0); } } break; case (int)network_data.COMMANDS.cmove_player: { network_data.move_player com = network_utils.nData.Instance.DeserializeMsg <network_data.move_player>(message); if (Channel.GetNetwork().IsClient()) { if (Channel.GetNetwork().GetComponent <client>().ingameContID != com.header.containerID) { GameObject g = Channel.GetEntity(com.header.containerID); if (g != null) { g.GetComponent <puppet>().SetTransform(com.position, com.rotation, puppet.trans_flag_position | puppet.trans_flag_rotation, com.time); } } } else { GameObject g = Channel.GetEntity(com.header.containerID); if (g != null) { g.GetComponent <puppet>().SetTransform(com.position, com.rotation, puppet.trans_flag_position | puppet.trans_flag_rotation, com.time); } } } break; case (int)network_data.COMMANDS.cdisconnect: { network_data.disconnect com = network_utils.nData.Instance.DeserializeMsg <network_data.disconnect>(message); UnregisterEntity(com.header.containerID); } break; case (int)network_data.COMMANDS.ctrigger: { network_data.trigger com = network_utils.nData.Instance.DeserializeMsg <network_data.trigger>(message); if (Triggers.ContainsKey(com.netID)) { trigger t = Triggers[com.netID]; if (Channel.GetNetwork().IsClient()) { if (com.accept) { t.SetTrigger(com.count, com.on); t.GetLink().Accept(com.on, com.header.containerID); t.DoActivate(com.header.containerID); } } else { t.TriggerRequest(com.header.containerID); t.DoActivate(com.header.containerID); } } } break; } }