/// <summary> /// Creates the request packet login. /// </summary> /// <returns>The request packet login.</returns> /// <param name="Message">Message.</param> /// <param name="_fromPeerID">To peer identifier.</param> public static Packet CreateRequestPacketLogin(String Message, uint _fromPeerID) { byte[] data = new byte[1024]; BitBuffer buffer = new BitBuffer(128); buffer.AddInt((int)OpCodes.PlayerLogin) .AddUInt(_fromPeerID) .AddString(Message) .ToArray(data); Packet packet = default(Packet); packet.Create(data); return(packet); }
public override BitBuffer PackVariable(BitBuffer buffer) { // cannot send nulls if (Value == null) { Value = ""; } int len = Value.Length; buffer.AddInt(len); for (int i = 0; i < len; i++) { buffer.AddByte((byte)Value[i]); } return(buffer); }
/// <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); }
public override BitBuffer PackVariable(BitBuffer buffer) { buffer.AddInt(Value); return(buffer); }
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()}"); }