Skip to content

Sending Packets

Effective communication in networked environments involves sending and receiving packets efficiently. Here's how to manage packet transmission in Cobra Packets.

  • Packet ID: Define a unique ID for the packet. This ID must be consistent across both server and client for successful subscription and communication.
  • Data Serialization: Utilize the ByteWriter class to effectively convert your data into a byte format suitable for transmission. This class serves as an efficient helper for data serialization, simplifying the process of converting various data types into a byte array.

Client-Side Packet Transmission

Clients have the ability to dispatch two types of packets: simple packets and those necessitating a response from the server.

Below are the key methods provided by the ClientPacketManager class for sending packets:

/// <summary>
/// Sends a packet to the server.
/// </summary>
/// <param name="packetID">The ID of the packet to send.</param>
/// <param name="writeAction">An action that writes data to the packet.</param>
/// <param name="channel">The channel to use for sending the packet 
/// (default is Reliable).</param>
public abstract void Send(
    ushort packetID, 
    Action<ByteWriter> writeAction, 
    PacketChannel channel = PacketChannel.Reliable);

/// <summary>
/// Sends a packet to the server and waits for a response.
/// </summary>
/// <param name="packetID">The ID of the packet to send.</param>
/// <param name="writeAction">An action that writes data to the packet.</param>
/// <param name="responseHandler">An action that 
//// handles the received response.</param>
/// <param name="timeoutSeconds">The timeout in seconds to wait for the response 
/// (default is 5 seconds).</param>
public abstract void SendWithResponse(
    ushort packetID, 
    Action<ByteWriter> writeAction, 
    Action<ByteReader> responseHandler, 
    float timeoutSeconds = 5);


/// <summary>
/// Sends a packet to the server and asynchronously waits for a response.
/// </summary>
/// <param name="packetID">The ID of the packet to send.</param>
/// <param name="writeAction">An action that writes data to the packet.</param>
/// <param name="timeoutSeconds">The timeout in seconds to wait for the response 
/// (default is 5 seconds).</param>
/// <returns>The received response as a ByteReader.</returns>
public async Task<ByteReader> SendWithResponseAsync(
    ushort packetID, 
    Action<ByteWriter> writeAction, 
    float timeoutSeconds = 5f)

Server-Side Packet Transmission

The ServerPacketManager class on the server side offers a variety of packet transmission modes. These capabilities include sending packets to individual clients, broadcasting to multiple clients simultaneously, and the utilization of cached packets.

Cached Packets

Cached packets play a crucial role, especially in scenarios where newly joined clients need access to information that was sent earlier. For example, sending a welcome message as a cached packet ensures that it is delivered to all clients, no matter when they join. This feature guarantees that all clients are kept up-to-date with relevant data.

Below are the key methods provided by the ServerPacketManager class for sending packets:

/// <summary>
/// Sends a packet to all connected clients.
/// </summary>
/// <param name="packetID">The ID of the packet to send.</param>
/// <param name="writer">An action that writes data to the packet.</param>
/// <param name="channel">The channel to use for sending the packet 
/// (default is Reliable).</param>
public abstract void SendAll(
    ushort packetID, 
    Action<ByteWriter> writer, 
    PacketChannel channel = PacketChannel.Reliable);

/// <summary>
/// Sends a packet to a specific client.
/// </summary>
/// <param name="packetID">The ID of the packet to send.</param>
/// <param name="writer">An action that writes data to the packet.</param>
/// <param name="player">The target player to send the packet to.</param>
/// <param name="channel">The channel to use for sending the packet 
/// (default is Reliable).</param>
public abstract void Send(
    ushort packetID, Action<ByteWriter> writer, 
    CobraPlayer player, 
    PacketChannel channel = PacketChannel.Reliable);


/// <summary>
/// Sends a packet to a specific client and waits for a response asynchronously.
/// </summary>
/// <param name="packetID">The ID of the packet to send.</param>
/// <param name="writer">An action that writes data to the packet.</param>
/// <param name="player">The target player to send the packet to.</param>
/// <param name="timeoutSeconds">The timeout in seconds to 
/// wait for the response (default is 5 seconds).</param>
/// <returns>The received response as a ByteReader.</returns>
public async Task<ByteReader> SendWithResponseAsync(
    ushort packetID, 
    Action<ByteWriter> writer, 
    CobraPlayer player, 
    float timeoutSeconds = 5);

/// <summary>
/// Sends a cached packet to all connected clients.
/// </summary>
/// <param name="packetID">The ID of the packet to send.</param>
/// <param name="writeAction">An action that writes data to the packet.</param>
public abstract void SendAllCached(ushort packetID, Action<ByteWriter> writeAction);

/// <summary>
/// Sends a packet to a specific client and waits for a response.
/// </summary>
/// <param name="packetID">The ID of the packet to send.</param>
/// <param name="writeAction">An action that writes data to the packet.</param>
/// <param name="player">The target player to send the packet to.</param>
/// <param name="responseHandler">An action that handles the received response.</param>
/// <param name="timeout">The timeout in seconds 
/// to wait for the response (default is 5 seconds).</param>
public abstract void SendWithResponse(
    ushort packetID, 
    Action<ByteWriter> writeAction, 
    CobraPlayer player, 
    Action<ByteReader> responseHandler, 
    float timeout = 5);