/// <summary> /// Send the outgoing buffer to all players in the specified channel. /// </summary> void EndSend(bool reliable, Channel channel, TcpPlayer exclude) { mBuffer.EndPacket(); if (mBuffer.size > 1024) reliable = true; for (int i = 0; i < channel.players.size; ++i) { TcpPlayer player = channel.players[i]; if (player.stage == TcpProtocol.Stage.Connected && player != exclude) { if (reliable || player.udpEndPoint == null || !mAllowUdp) { player.SendTcpPacket(mBuffer); } else mUdp.Send(mBuffer, player.udpEndPoint); } } mBuffer.Recycle(); mBuffer = null; }
/// <summary> /// Send the outgoing buffer to all players in the specified channel. /// </summary> void SendToChannel(bool reliable, Channel channel, Buffer buffer) { mBuffer.MarkAsUsed(); if (mBuffer.size > 1024) reliable = true; for (int i = 0; i < channel.players.size; ++i) { TcpPlayer player = channel.players[i]; if (player.stage == TcpProtocol.Stage.Connected) { if (reliable || player.udpEndPoint == null || !mAllowUdp) { player.SendTcpPacket(mBuffer); } else mUdp.Send(mBuffer, player.udpEndPoint); } } mBuffer.Recycle(); }
/// <summary> /// Create a new channel (or return an existing one). /// </summary> Channel CreateChannel(int channelID, out bool isNew) { Channel channel; for (int i = 0; i < mChannels.size; ++i) { channel = mChannels[i]; if (channel.id == channelID) { isNew = false; if (channel.closed) return null; return channel; } } channel = new Channel(); channel.id = channelID; mChannels.Add(channel); isNew = true; return channel; }
/// <summary> /// Join the specified channel. /// </summary> void SendJoinChannel(TcpPlayer player, Channel channel) { if (player.channel == null || player.channel != channel) { // Set the player's channel player.channel = channel; // Everything else gets sent to the player, so it's faster to do it all at once player.FinishJoiningChannel(); // Inform the channel that a new player is joining BinaryWriter writer = BeginSend(Packet.ResponsePlayerJoined); { writer.Write(player.id); writer.Write(string.IsNullOrEmpty(player.name) ? "Guest" : player.name); } EndSend(true, channel, null); // Add this player to the channel now that the joining process is complete channel.players.Add(player); } }