void Receive(ref List <byte[]> a) { if (socketdata.stream == null) { return; } while (socketdata.stream.DataAvailable) { try { byte[] recieve = new Byte[8192]; int len = socketdata.stream.Read(recieve, 0, network_utils.nData.Instance.getSize <network_utils.HEADER>()); network_utils.HEADER header = network_utils.nData.Instance.DeserializeMsg <network_utils.HEADER>(recieve); if (header.signum == network_utils.SIGNUM.BIN) { int restlaenge = header.size - network_utils.nData.Instance.getSize <network_utils.HEADER>(); if (restlaenge > 0) { len += socketdata.stream.Read(recieve, network_utils.nData.Instance.getSize <network_utils.HEADER>(), restlaenge); } } else { return; } byte[] b = new byte[len]; System.Array.Copy(recieve, 0, b, 0, len); a.Add(b); } catch (Exception e) { return; } } }
void Receive(ref List <network_utils.cl_data> back) { for (int i = 0; i < socketUsedIDList.Count; i++) { try { while (socketList[socketUsedIDList[i]].stream.DataAvailable) { byte[] recieve = new Byte[1024]; int len = socketList[socketUsedIDList[i]].stream.Read(recieve, 0, network_utils.nData.Instance.getSize <network_utils.HEADER>()); network_utils.HEADER header = network_utils.nData.Instance.DeserializeMsg <network_utils.HEADER>(recieve); if (header.signum == network_utils.SIGNUM.BIN) { int restlaenge = header.size - network_utils.nData.Instance.getSize <network_utils.HEADER>(); if (restlaenge > 0) { len += socketList[socketUsedIDList[i]].stream.Read(recieve, network_utils.nData.Instance.getSize <network_utils.HEADER>(), restlaenge); } } byte[] recieved = new Byte[len]; System.Array.Copy(recieve, 0, recieved, 0, len); network_utils.cl_data cl = new network_utils.cl_data(socketUsedIDList[i], recieved); cl.containerID = socketUsedIDList[i]; back.Add(cl); } } catch (Exception e) { SocketData s = socketList[socketUsedIDList[i]]; socketList[socketUsedIDList[i]] = s; } } }
// 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; } }
}// 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; } }
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.cmove_player: { network_data.move_player com = network_utils.nData.Instance.DeserializeMsg <network_data.move_player>(message); if (Channel.GetNetwork().IsClient()) { GameObject g = Channel.GetEntity(com.header.containerID); ship s = g.GetComponent <ship>(); if (s != null) { if (!s.IsCockpitUsedByMe()) { g.GetComponent <ship>().SetTransform(com.position, com.rotation, puppet.trans_flag_position | puppet.trans_flag_rotation, com.time); g.GetComponent <ship>().SetMoveVector(com.position); g.GetComponent <ship>().SetVelocity(com.velocity); } else { g.GetComponent <ship>().SetTransform(com.position, com.rotation, puppet.trans_flag_position, com.time); } } } else { // g.transform.rotation = com.rotation; GameObject g = Channel.GetEntity(com.header.containerID); if (g == null) { Debug.Log("ShipID " + com.header.containerID + " not found"); } else { g.GetComponent <ship>().SetTransform(com.position, com.rotation, puppet.trans_flag_rotation, Time.time); // g.GetComponent<ship>().SetTransform(com.position,com.rotation); g.GetComponent <ship>().SetMoveVector(com.position); g.GetComponent <ship>().SetVelocity(com.velocity); byte[] data1 = network_utils.nData.Instance.SerializeMsg <network_data.move_player>(com); // g.GetComponent<ship>().GetChannel().SendToChannel(ref data1); } } /* if (Channel.GetNetwork().IsClient()) * { * int ownid = Channel.GetNetwork().GetComponent<client>().ingameContID; * GameObject g = Channel.GetEntity(com.header.containerID); * if (g != null) * { * g.GetComponent<puppet>().SetTransform(com.position, com.rotation); * } * } * else * { * GameObject g = Channel.GetEntity(com.header.containerID); * if (g != null) * { * g.GetComponent<puppet>().SetTransform(com.position, com.rotation); * g.GetComponent<ship>().SetVelocity(com.velocity); * } * } */ } break; } }