public void Send(NewPackets.PacketType ofType, byte[] packet, ConnectedClient client) { packet = NewPackets.AppendTypeHeader(ofType, packet); countBytes += packet.Length; GameServer.Send(client, packet); }
// For generic packets like tile updates public void Broadcast(NewPackets.PacketType ofType, byte[] packet) { packet = NewPackets.AppendTypeHeader(ofType, packet); countBytes += packet.Length; GameServer.Broadcast(packet); }
// Recieve handler public void ProcessPacket(byte[] packet) { try { if (packet.Length == 0) { Debug.Log("ERROR: null packet on server"); return; } // Pop packet type NewPackets.PacketType type; packet = NewPackets.PopTypeHeader(packet, out type); //Debug.Log($"Server Game Manager: packet is of type {type}..."); // Pop packet faction byte factionID = 0; packet = NewPackets.PopByteHeader(packet, out factionID); //Debug.Log($"Server Game Manager: packet was sent by faction {factionID}..."); // **Packet bytes should now be castable** // Minimized, generalized, client request cases: // - Tile interacts are used for moving and mining // - Entity interacts are used for carrying, building, and combat switch (type) { case NewPackets.PacketType.Deploy: Debug.Log("GOT DEPLOY"); PlayerFactions[factionID].CustomDeploy(new NewPackets.Deploy(packet)); break; case NewPackets.PacketType.Buy: Debug.Log("TRYING TO BUY ON SERVER: " + new NewPackets.Buy(packet).type); PlayerFactions[factionID].Buy(new NewPackets.Buy(packet).type); break; case NewPackets.PacketType.TileInteractRequest: PlayerFactions[factionID].TileInteraction(new NewPackets.TileInteraction(packet)); break; // TODO: will need to split this into different cases (based on header?)? // - Enemy units can only be combatted // - Ore faction can only be grabbed // - Self faction can be grabbed and activated case NewPackets.PacketType.EntityInteractRequest: PlayerFactions[factionID].EntityInteraction(new NewPackets.EntityInteraction(packet)); break; default: Debug.Log("SERVER GAME MANAGER: ERROR packet type unhandled!"); break; } } catch (Exception e) { Debug.Log($"EXCEPTION IN RECIEVE: {e} \n\n STACK TRACE: \n --------------- \n {e.StackTrace}"); } }