Server Source code
Program
- Host_Monitor()
- Continuously monitors the AI host’s connection and sends a heartbeat packet to ensure the connection is alive. If the AI host is connected, it sends a predefined packet to it every 5 milliseconds.
- Main()
- The entry point of the program. It starts two threads: one for the main server logic (
aMain()
) and another for monitoring the AI host connection (Host_Monitor()
).
- The entry point of the program. It starts two threads: one for the main server logic (
- aMain()
- Initializes the server, starts the game server, and monitors for user input to restart the server if needed. It also starts a thread for handling the AI host logic.
- AIHost_Thread()
- Continuously checks if the AI host is connected. If not, it assigns a new client as the AI host and sends an update packet to that client. It repeats this check every 100 milliseconds.
- StartGame()
- Binds the server socket to a specified IP and port, listens for incoming client connections, and handles the connection process. It assigns the first connected client as the AI host if none exists and starts a thread to handle communication with the new client.
Here are the methods again with brief descriptions for each:
- Host_Monitor()
- Monitors the AI host connection by sending periodic packets to ensure the connection is maintained.
- Main()
- The main entry point of the application that starts the server and host monitor threads.
- aMain()
- Starts the game server and initializes threads for various server tasks, including handling the AI host.
- AIHost_Thread()
- Continuously checks and updates the AI host status, ensuring a connected client is assigned as the AI host.
- StartGame()
- Sets up the game server to listen for incoming client connections, handles client connections, assigns the AI host, and starts a packet handler thread for each client.
PacketHandler:
- IntToUShortByteArray(int number)
- Converts an integer to a
ushort
and then to a byte array.
- Converts an integer to a
- CleanMultiPacket(byte[] inp)
- Processes a byte array to extract individual packets, clean them, and return a combined byte array of cleaned packets.
- Handle(Client NewC)
- Main method for handling incoming packets from a client. It reads the packets, processes them, and handles disconnections or packet forwarding.
- ReadString(byte[] array, int index, int length)
- Reads a string from a byte array starting at the specified index and for the specified length.
- ReadUshort_FromIndex(byte[] array, int index)
- Reads a
ushort
from a byte array starting at the specified index.
- Reads a
- ReadUint_FromIndex(byte[] array, int index)
- Reads an
uint
from a byte array starting at the specified index.
- Reads an
- Broadcast(byte[] recv)
- Sends the received packet to all connected clients.
Here’s a brief overview of each method’s functionality within the server:
- IntToUShortByteArray(int number)
- Converts an integer to an unsigned short (
ushort
), then to a byte array. This is useful for ensuring that packet sizes are stored as a consistent data type and can be easily converted to a byte array for network transmission.
- Converts an integer to an unsigned short (
- CleanMultiPacket(byte[] inp)
- Cleans and processes incoming multi-packet data. It separates individual packets based on their lengths, adds necessary headers, and returns a combined byte array of cleaned packets. This is crucial for handling fragmented or combined network messages.
- Handle(Client NewC)
- Handles incoming data from a client. It reads and processes packets, handles disconnections, and broadcasts valid packets to other clients. This method includes packet size validation, type extraction, and broadcasting logic to maintain client communication.
- ReadString(byte[] array, int index, int length)
- Extracts a string from a specified position and length within a byte array. This is used for reading string data embedded in packets.
- ReadUshort_FromIndex(byte[] array, int index)
- Extracts a
ushort
value from a byte array starting at the given index. This helps in interpreting packet headers or specific fields within the packet data.
- Extracts a
- ReadUint_FromIndex(byte[] array, int index)
- Extracts a
uint
value from a byte array starting at the given index. This method is similar toReadUshort_FromIndex
but foruint
values, providing flexibility in reading different types of numeric data from packets.
- Extracts a
- Broadcast(byte[] recv)
- Sends a received packet to all connected clients. This is essential for ensuring that all clients in a multiplayer game receive updates about the state changes or actions performed by other clients.
Client side:
Spawning / AI Controllers / Titans Replication
- TITAN:
- [2:44 AM]If the player is the host, move the titan to the destination. If the player is not the host, move to the replicated AIDestination variable
- [2:45 AM]
- [2:45 AM]In this tick event, every 1 second the AIDestination variable is replicated on the server.
- cruew — Today at 2:55 AMNonLocal Player controllers process update packets like so Titans and non-local players both use this ai controller. If the ID of the entity is >3000, this will refer to a Titan entity. If the ID <3000: spawn a human player If the ID >3000 & <12999: spawn a titan on begin play. If the ID >12999 & <22999: spawn a skinny titan. If the ID >22999 & <32999: spawn a glut titan
- [2:55 AM]spawning
- [2:55 AM]
- [2:56 AM]
How to edit server IP:
In the server source code, adjust the IPAdd variable. You can also adjust the port in the IPEndPoint (5816 by default)
How to turn on automatically reconnecting to server on Disconnected:
BP_Server is responsible for recieving packets and connecting to the server. It is also responsible for sending outbound packets.