Attack on Titan VR: Custom Server Documentation

Server Source code

Program

  1. 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.
  2. 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()).
  3. 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.
  4. 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.
  5. 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:

  1. Host_Monitor()
    • Monitors the AI host connection by sending periodic packets to ensure the connection is maintained.
  2. Main()
    • The main entry point of the application that starts the server and host monitor threads.
  3. aMain()
    • Starts the game server and initializes threads for various server tasks, including handling the AI host.
  4. AIHost_Thread()
    • Continuously checks and updates the AI host status, ensuring a connected client is assigned as the AI host.
  5. 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:

  1. IntToUShortByteArray(int number)
    • Converts an integer to a ushort and then to a byte array.
  2. CleanMultiPacket(byte[] inp)
    • Processes a byte array to extract individual packets, clean them, and return a combined byte array of cleaned packets.
  3. Handle(Client NewC)
    • Main method for handling incoming packets from a client. It reads the packets, processes them, and handles disconnections or packet forwarding.
  4. ReadString(byte[] array, int index, int length)
    • Reads a string from a byte array starting at the specified index and for the specified length.
  5. ReadUshort_FromIndex(byte[] array, int index)
    • Reads a ushort from a byte array starting at the specified index.
  6. ReadUint_FromIndex(byte[] array, int index)
    • Reads an uint from a byte array starting at the specified index.
  7. Broadcast(byte[] recv)
    • Sends the received packet to all connected clients.

Here’s a brief overview of each method’s functionality within the server:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. ReadUint_FromIndex(byte[] array, int index)
    • Extracts a uint value from a byte array starting at the given index. This method is similar to ReadUshort_FromIndex but for uint values, providing flexibility in reading different types of numeric data from packets.
  7. 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

  1. TITAN:Image
  2. [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
  3. [2:45 AM]Image
  4. [2:45 AM]In this tick event, every 1 second the AIDestination variable is replicated on the server.
  5. cruewToday 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 titanImage
  6. [2:55 AM]spawningImage
  7. [2:55 AM]Image
  8. [2:56 AM]Image

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.