/// <summary> /// Gets a <see cref="PacketWriter"/> to use from the internal pool. It is important that this /// <see cref="PacketWriter"/> is disposed of properly when done. /// </summary> /// <param name="id">The <see cref="ServerPacketID"/> that this <see cref="PacketWriter"/> will be writing.</param> /// <returns>The <see cref="PacketWriter"/> to use.</returns> public static PacketWriter GetWriter(ServerPacketID id) { var pw = _writerPool.Acquire(); Debug.Assert(pw.LengthBits == 0); pw.Write(id); return pw; }
public void SendToAll <T>(ServerPacketID id, T message, bool compress = false) where T : BaseMessage { foreach (var client in Clients) { SendNetMessage(client.Value, id, message, compress); } }
public void SendNetMessage <T>(ClientState client, ServerPacketID id, T message, bool compress = false) where T : BaseMessage { var compressFlag = compress && EnableCompression; var body = ZeroFormatterSerializer.Serialize(message); SendRawNetMessage(client, id, body, compressFlag); }
void ProcessReceivedMessage(ServerPacketID id, byte[] message) { _handlers.TryGetValue(id, out var handler); if (handler != null) { handler.ProcessMessage(message); } }
/// <summary> /// Используется в случае, когда надо отправить заранее отформатированный пакет в обход сериализатора (так как он может сильно тормозить на больших объемах данных) /// </summary> /// <param name="client">Клиент, которому отправляем сообщение</param> /// <param name="id">Код команды</param> /// <param name="body">Сообщение - массив байт</param> /// <param name="compress">Сжимать ли сообщение с помощью LZF</param> public void SendRawNetMessage(ClientState client, ServerPacketID id, byte[] body, bool compress = false) { var compressFlag = compress && EnableCompression; var header = new PacketHeader((byte)id, compressFlag, (ushort)body.Length); uint size; if (compressFlag) { var compressedBody = CLZF2.Compress(body); header.ContentLength = (ushort)compressedBody.Length; _server.Send(client.ConnectionID, NetworkUtils.CreateMessageBytes(header, compressedBody, out size)); } else { _server.Send(client.ConnectionID, NetworkUtils.CreateMessageBytes(header, body, out size)); } _packetsSent++; _bytesSent = _bytesSent + size; }
/// <summary> /// Writes a <see cref="ServerPacketID"/> to the BitStream. /// </summary> /// <param name="bitStream">BitStream to write to.</param> /// <param name="serverPacketID">ServerPacketID to write.</param> public static void Write(this BitStream bitStream, ServerPacketID serverPacketID) { bitStream.Write((uint)serverPacketID, _serverPacketIDBits); }