protected override void Disconnect(Event netEvent) { Debug.Log($"Client disconnected - ID: {netEvent.Peer.ID}, IP: {netEvent.Peer.IP} Reason: {netEvent.Data}"); Peer peer = netEvent.Peer; NetworkedObject nobj = null; if (m_peers.TryGetValue(peer.ID, out nobj)) { uint id = peer.ID; m_peers.Remove(peer.ID); Destroy(nobj.gameObject); // notify everyone else if (m_peers.Count > 0) { m_buffer.AddEntityHeader(peer, OpCodes.Destroy); var packet = m_buffer.GetPacketFromBuffer(PacketFlags.Reliable); var command = GameCommandPool.GetGameCommand(); command.Type = CommandType.BroadcastAll; command.Packet = packet; command.Channel = 0; m_commandQueue.Enqueue(command); } } }
private void UpdateLocal() { if (m_isLocal == false) { return; } if (m_newData.HasValue == false) { m_newData = new Vector4( Random.Range(-1f, 1f) * BaseNetworkSystem.kMaxRange, Random.Range(-1f, 1f) * BaseNetworkSystem.kMaxRange, Random.Range(-1f, 1f) * BaseNetworkSystem.kMaxRange, Random.Range(0, 1f) * 360f); } if (CanUpdate()) { m_buffer.AddEntityHeader(ClientNetworkSystem.MyPeer, OpCodes.PositionUpdate); m_buffer.AddVector3(gameObject.transform.position, BaseNetworkSystem.Range); m_buffer.AddFloat(m_toRotate.eulerAngles.y); var command = GameCommandPool.GetGameCommand(); command.Type = CommandType.Send; command.Packet = m_buffer.GetPacketFromBuffer(); command.Channel = 0; m_network.AddCommandToQueue(command); m_nextUpdate += m_updateRate; } }
protected override void OnDestroy() { base.OnDestroy(); var command = GameCommandPool.GetGameCommand(); command.Type = CommandType.StopHost; m_commandQueue.Enqueue(command); }
protected override void Start() { base.Start(); var command = GameCommandPool.GetGameCommand(); command.Type = CommandType.StartHost; command.Host = m_targetHost; command.Port = m_targetPort; command.UpdateTime = 0; command.ChannelCount = 100; m_commandQueue.Enqueue(command); }
/// <summary> /// Only called on the server where the peer is present. /// </summary> public void UpdateSyncs() { if (CanUpdate() == false) { return; } m_nextUpdate += m_updateRate; int dirtyBits = 0; for (int i = 0; i < m_syncs.Count; i++) { if (m_syncs[i].Dirty) { dirtyBits = dirtyBits | m_syncs[i].BitFlag; } } if (dirtyBits == 0) { return; } m_buffer.AddEntityHeader(m_peer, OpCodes.SyncUpdate); m_buffer.AddInt(dirtyBits); for (int i = 0; i < m_syncs.Count; i++) { if (m_syncs[i].Dirty) { m_buffer.AddSyncVar(m_syncs[i]); } } var packet = m_buffer.GetPacketFromBuffer(PacketFlags.Reliable); var command = GameCommandPool.GetGameCommand(); command.Type = CommandType.BroadcastAll; command.Packet = packet; command.Channel = 0; Debug.Log($"Sending dirtyBits: {dirtyBits} Length: {packet.Length}"); m_network.AddCommandToQueue(command); }
protected override void ProcessPacket(Event netEvent) { Profiler.BeginSample("Process Packet"); //Debug.Log($"Packet received from - ID: {netEvent.Peer.ID}, IP: {netEvent.Peer.IP}, Channel ID: {netEvent.ChannelID}, Data Length: {netEvent.Packet.Length}"); m_buffer = netEvent.Packet.GetBufferFromPacket(m_buffer); var buffer = m_buffer; var header = buffer.GetEntityHeader(); var op = header.OpCode; var id = header.ID; if (netEvent.Peer.ID != id) { Debug.LogError($"ID Mismatch! {netEvent.Peer.ID} vs. {id}"); Profiler.EndSample(); return; } switch (op) { case OpCodes.PositionUpdate: Profiler.BeginSample("Process Packet - Position Update"); var command = GameCommandPool.GetGameCommand(); command.Type = CommandType.BroadcastOthers; command.Source = netEvent.Peer; command.Channel = 1; command.Packet = netEvent.Packet; m_commandQueue.Enqueue(command); NetworkedObject nobj = null; if (m_peers.TryGetValue(netEvent.Peer.ID, out nobj)) { nobj.ProcessPacket(op, buffer); } Profiler.EndSample(); break; default: netEvent.Packet.Dispose(); break; } Profiler.EndSample(); }
private void SpawnRemotePlayer(Peer peer) { var go = Instantiate(m_playerGo); NetworkedObject nobj = go.GetComponent <NetworkedObject>(); nobj.ServerInitialize(this, peer); m_buffer.AddEntityHeader(peer, OpCodes.Spawn); m_buffer.AddInitialState(nobj); Packet spawnPlayerPacket = m_buffer.GetPacketFromBuffer(PacketFlags.Reliable); var spawnPlayerCommand = GameCommandPool.GetGameCommand(); spawnPlayerCommand.Type = CommandType.Send; spawnPlayerCommand.Target = peer; spawnPlayerCommand.Channel = 0; spawnPlayerCommand.Packet = spawnPlayerPacket; m_commandQueue.Enqueue(spawnPlayerCommand); var spawnPlayerForOthersCommand = GameCommandPool.GetGameCommand(); spawnPlayerForOthersCommand.Type = CommandType.BroadcastOthers; spawnPlayerForOthersCommand.Source = peer; spawnPlayerForOthersCommand.Channel = 1; spawnPlayerForOthersCommand.Packet = spawnPlayerPacket; m_commandQueue.Enqueue(spawnPlayerForOthersCommand); int packetSize = 0; // individual packets /* * // must send all of the old data * for (int i = 0; i < m_entities.Count; i++) * { * if (m_entities[i].Peer.ID == peer.ID) * continue; * * m_buffer.AddEntityHeader(m_entities[i].Peer, OpCodes.Spawn); * m_buffer.AddVector3(m_entities[i].gameObject.transform.position, SharedStuff.Instance.Range); * m_buffer.AddFloat(m_entities[i].gameObject.transform.eulerAngles.y); * m_buffer.AddEntitySyncData(m_entities[i]); * var spawnOthersPacket = m_buffer.GetPacketFromBuffer(PacketFlags.Reliable); * * var spawnOthersCommand = GameCommandPool.GetGameCommand(); * spawnOthersCommand.Type = CommandType.Send; * spawnOthersCommand.Packet = spawnOthersPacket; * spawnOthersCommand.Channel = 1; * spawnOthersCommand.Target = peer; * * packetSize += spawnOthersPacket.Length; * * m_commandQueue.Enqueue(spawnOthersCommand); * } */ // one large packet m_buffer.AddEntityHeader(peer, OpCodes.BulkSpawn); m_buffer.AddInt(m_peers.Count); for (int i = 0; i < m_peers.Count; i++) { if (m_peers[i].Peer.ID == peer.ID) { continue; } m_buffer.AddEntityHeader(m_peers[i].Peer, OpCodes.Spawn, false); m_buffer.AddInitialState(m_peers[i]); } var spawnOthersPacket = m_buffer.GetPacketFromBuffer(PacketFlags.Reliable); var spawnOthersCommand = GameCommandPool.GetGameCommand(); spawnOthersCommand.Type = CommandType.Send; spawnOthersCommand.Packet = spawnOthersPacket; spawnOthersCommand.Channel = 1; spawnOthersCommand.Target = peer; packetSize += spawnOthersPacket.Length; m_commandQueue.Enqueue(spawnOthersCommand); m_peers.Add(peer.ID, nobj); Debug.Log($"Sent SpawnOthersPacket of size {packetSize.ToString()}"); }