Skip to content

Receiving Packets

Effectively listening for packets is a key aspect of network communication. The first step is identifying the expected ID for the target packet. Each packet type is associated with a unique ID, which is crucial for correct packet handling.

Packet Handling Process

  1. Identify Packet ID: Know the specific ID associated with the packet you intend to listen for.
  2. Create a Handler Action: Develop an action that accepts a CobraPacket as a parameter. This action is triggered each time a new packet with the specified ID is received. This could for example be a class method.
  3. Utilize CobraPacket: The CobraPacket class provides a ByteReader field, allowing you to convert the received byte array back into C# objects. Additionally, CobraPacket includes other pertinent information like the packet ID and whether a response is required from the packet.

Client-Side Packet Listening

On the client side, the process involves using specific methods to register a handler for incoming packets.

The RegisterHandler method allows clients to set up a listener for specific packet IDs:

/// <summary>
/// Registers a packet handler for a specific packet ID.
/// </summary>
/// <param name="packetID">The ID of the packet to handle.</param>
/// <param name="handler">The handler action to be called 
/// when the packet is received.</param>
public void RegisterHandler(ushort packetID, Action<ClientPacket> handler)

Using this method, you can define how your application should respond when a packet with the designated ID is received. This approach ensures that your client-side application can effectively process and react to incoming network data.

Example: This example demonstrates the basic setup required to listen for packets on the client side, including registering a packet handler and defining the handler method and even responding to whoever sent the packet.

public class PacketListener
{
    private ClientPacketManager packetManager;

    public PacketListener(ClientPacketManager manager)
    {
        this.packetManager = manager;
        packetManager.RegisterHandler(packetID, OnSomePacketReceived);
    }

    private void OnSomePacketReceived(ClientPacket packet)
    {
        // Process the packet data using packet.Reader
        string message = packet.Reader.ReadString();
        int number = packet.Reader.ReadInt();

        Debug.Log("Received message: " + message + " " + number);

        // Check if a response is required and respond accordingly
        if (packet.RequiresResponse)
        {
            // Set the response action for the packet
            packet.Response = responseWriter =>
            {
                responseWriter.WriteString("Acknowledged: " + message);
                responseWriter.WriteInt(number * 2); 
            };      
        }
    }
}

Using this method, you can define how your application should respond when a packet with the designated ID is received. This approach ensures that your client-side application can effectively process and react to incoming network data.

Server-Side Packet Listening

On the server side, handling packets involves setting up listeners for specific packet IDs using the ServerPacketManager. The process is similar to client-side listening but tailored for server-specific needs.

The RegisterHandler method is used on the server to register handlers for incoming packets:

/// <summary>
/// Registers a packet handler for a specific packet ID.
/// </summary>
/// <param name="packetID">The ID of the packet to handle.</param>
/// <param name="handler">The handler action to be 
/// called when the packet is received.</param>
public void RegisterHandler(ushort packetID, Action<ServerPacket> handler)

The ServerPacket class is an extension of the base CobraPacket class, specifically designed for packets that are received on the server. A notable enhancement in this class is the inclusion of a CobraPlayer property, which identifies the sender — the client who originally sent the packet to the server. This additional information simplifies the process of responding to packets, as well as facilitating other operations that may depend on the sender's identity. By having direct access to the sender's details, the server can more effectively manage communication and perform targeted actions based on the specific client that initiated the packet exchange.

Example: This example demonstrates the basic setup required to listen for packets on the server side, including registering a packet handler, processing received packets, and responding to the client that sent the packet.

public class ServerPacketListener
{
    private ServerPacketManager packetManager;

    public ServerPacketListener(ServerPacketManager manager)
    {
        this.packetManager = manager;
        packetManager.RegisterHandler(packetID, OnClientPacketReceived);
    }

    private void OnClientPacketReceived(ServerPacket packet)
    {
        // Process the packet data using packet.Reader
        string message = packet.Reader.ReadString();
        int number = packet.Reader.ReadInt();

        Debug.Log("Received message from client: " + message + " " + number);

        // Check if a response is required and respond accordingly
        if (packet.RequiresResponse)
        {
            // Set the response action for the packet
            packet.Response = responseWriter =>
            {
                responseWriter.WriteString("Server Acknowledged: " + message);
                responseWriter.WriteInt(number + 1); 
            };
        }
    }
}