From 992bd92d5f873a4981d577f2e11afc41ad7a5600 Mon Sep 17 00:00:00 2001 From: satellite Date: Tue, 6 Apr 2021 20:33:07 -0400 Subject: [PATCH] Work on networking --- .../java/electrosphere/cfg/MainConfig.java | 10 ++++ .../game/simcell/physical/PhysicalCell.java | 9 ++++ .../game/simcell/virtual/VirtualCell.java | 9 ++++ src/main/java/electrosphere/main/Globals.java | 9 ++++ src/main/java/electrosphere/main/Main.java | 51 +++++++++++------- .../electrosphere/net/client/Connection.java | 52 +++++++++++++++++++ .../java/electrosphere/net/server/Client.java | 12 ++++- .../java/electrosphere/net/server/Server.java | 52 ++++++++++++++++++- 8 files changed, 183 insertions(+), 21 deletions(-) create mode 100644 src/main/java/electrosphere/game/simcell/physical/PhysicalCell.java create mode 100644 src/main/java/electrosphere/game/simcell/virtual/VirtualCell.java diff --git a/src/main/java/electrosphere/cfg/MainConfig.java b/src/main/java/electrosphere/cfg/MainConfig.java index 51a290a1..71d985e6 100644 --- a/src/main/java/electrosphere/cfg/MainConfig.java +++ b/src/main/java/electrosphere/cfg/MainConfig.java @@ -30,7 +30,14 @@ public class MainConfig { // //Networking related // + //should we run a server at all? public boolean runServer; + + + // + //Rendering related + // + //should the renderer run at all? public boolean runRenderer; @@ -45,6 +52,9 @@ public class MainConfig { + /** + * Generate a localconfig.json and save it to our resources dir + */ public static void generateMainConfig(){ System.out.println("localconfig.json either doesn't exist or is out of date"); System.out.println("Would you like to generate a new one?(1-y/0-n)"); diff --git a/src/main/java/electrosphere/game/simcell/physical/PhysicalCell.java b/src/main/java/electrosphere/game/simcell/physical/PhysicalCell.java new file mode 100644 index 00000000..5630b7d6 --- /dev/null +++ b/src/main/java/electrosphere/game/simcell/physical/PhysicalCell.java @@ -0,0 +1,9 @@ +package electrosphere.game.simcell.physical; + +/** + * + * @author satellite + */ +public class PhysicalCell { + +} diff --git a/src/main/java/electrosphere/game/simcell/virtual/VirtualCell.java b/src/main/java/electrosphere/game/simcell/virtual/VirtualCell.java new file mode 100644 index 00000000..94a78154 --- /dev/null +++ b/src/main/java/electrosphere/game/simcell/virtual/VirtualCell.java @@ -0,0 +1,9 @@ +package electrosphere.game.simcell.virtual; + +/** + * + * @author satellite + */ +public class VirtualCell { + +} diff --git a/src/main/java/electrosphere/main/Globals.java b/src/main/java/electrosphere/main/Globals.java index b897deb1..b780525b 100644 --- a/src/main/java/electrosphere/main/Globals.java +++ b/src/main/java/electrosphere/main/Globals.java @@ -13,6 +13,7 @@ import electrosphere.cfg.MainConfig; import electrosphere.entity.Entity; import electrosphere.entity.EntityManager; import electrosphere.game.cell.CellManager; +import electrosphere.net.client.Connection; import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -38,6 +39,14 @@ public class Globals { + // + //Client connection to server + // + public static Connection connection; + + + + // //Generic OpenGL Statements // diff --git a/src/main/java/electrosphere/main/Main.java b/src/main/java/electrosphere/main/Main.java index 0b337f52..1daefc88 100644 --- a/src/main/java/electrosphere/main/Main.java +++ b/src/main/java/electrosphere/main/Main.java @@ -12,6 +12,8 @@ import electrosphere.entity.Entity; import electrosphere.entity.EntityUtil; import electrosphere.game.cell.CellManager; import electrosphere.game.terrain.TerrainManager; +import electrosphere.net.client.Connection; +import electrosphere.net.server.Server; import electrosphere.renderer.ModelUtils; import electrosphere.terraingen.TerrainGen; import electrosphere.terraingen.models.TerrainModel; @@ -89,8 +91,8 @@ public class Main { public static boolean CAMERA_IS_ORBIT = true; public static float camera_Orbit_Length = 1.0f; - static final int playerStartRealX = 580 * 200; - static final int playerStartRealY = 900 * 200; + static int playerStartRealX = 580 * 200; + static int playerStartRealY = 900 * 200; // public static Camera cam_Player_Orbit; //Camera angles using theta-phi system @@ -114,23 +116,29 @@ public class Main { // // + if(Globals.mainConfig.runServer){ + forkServer(); + } + //run initialization stuff + initTerrainManager(); - initWorld(); - - - //Create opengl - RenderUtils.createOpenglContext(); + if(Globals.mainConfig.runRenderer){ + //Create opengl + RenderUtils.createOpenglContext(); + } //init global variables Globals.initGlobals(); + if(Globals.mainConfig.runRenderer){ + forkConnection(); + } - - initWorld(); - - initCellManager(); + if(Globals.mainConfig.runRenderer){ + initCellManager(); + } initSkybox(); @@ -148,10 +156,10 @@ public class Main { Camera camera_player_chase = new Camera(); Camera cam_Player_Orbit = new Camera(); - + boolean running = true; //main loop - while (!glfwWindowShouldClose(Globals.window)) { + while (running) { /* Frame calculation @@ -278,6 +286,10 @@ public class Main { //check and call events and swap the buffers glfwSwapBuffers(Globals.window); glfwPollEvents(); + + if(glfwWindowShouldClose(Globals.window)){ + running = false; + } } //Terminate the program. glfwTerminate(); @@ -340,7 +352,7 @@ public class Main { - static void initWorld(){ + static void initTerrainManager(){ float[][] elevation; terrainManager = new TerrainManager(2000,200,100); @@ -393,6 +405,9 @@ public class Main { } } + playerStartRealX = playerStartX * chunkSize; + playerStartRealY = playerStartY * chunkSize; + Globals.player = new Entity(); Globals.player.putData("position", new Vector3f(playerStartX*chunkSize,terrainManager.getHeightAtPosition(playerStartX*chunkSize, playerStartY*chunkSize),playerStartY*chunkSize)); @@ -403,12 +418,12 @@ public class Main { } - public static void initServer(){ - + public static void forkServer(){ + Server.startServerThread(); } - public static void initClient(){ - + public static void forkConnection(){ + Connection.startConnection(); } } diff --git a/src/main/java/electrosphere/net/client/Connection.java b/src/main/java/electrosphere/net/client/Connection.java index 8f67810a..6a7a6fbc 100644 --- a/src/main/java/electrosphere/net/client/Connection.java +++ b/src/main/java/electrosphere/net/client/Connection.java @@ -1,6 +1,10 @@ package electrosphere.net.client; +import electrosphere.main.Globals; +import java.io.IOException; import java.net.Socket; +import java.util.logging.Level; +import java.util.logging.Logger; import org.apache.commons.crypto.stream.CryptoInputStream; import org.apache.commons.crypto.stream.CryptoOutputStream; @@ -9,11 +13,59 @@ import org.apache.commons.crypto.stream.CryptoOutputStream; * @author amaterasu */ public class Connection { + + enum ConnectionState { + HANDSHAKE_START, + HANDSHAKE_SUCCESS, + HANDSHAKE_FAILURE, + } + + ConnectionState state; + + static int port = 42536; + Socket socket; CryptoInputStream inputStream; CryptoOutputStream outputStream; + public Connection(String address, int port){ + try { + this.socket = new Socket(address,port); + } catch (IOException ex) { + System.err.println("Failed to connect!"); + ex.printStackTrace(); + System.exit(1); + } + } + + public void processAllPackets(){ + + } + + + public static void startConnection(){ + Globals.connection = new Connection("localhost",port); + + + } + + class ConnectionProtocol implements Runnable { + Connection connection; + + public ConnectionProtocol(Connection conn){ + connection = conn; + } + + @Override + public void run() { + boolean running = true; +// while(running){ +// if(connection.inputStream.read) +// } + } + + } } diff --git a/src/main/java/electrosphere/net/server/Client.java b/src/main/java/electrosphere/net/server/Client.java index a0ff6aed..d8310825 100644 --- a/src/main/java/electrosphere/net/server/Client.java +++ b/src/main/java/electrosphere/net/server/Client.java @@ -8,7 +8,7 @@ import org.apache.commons.crypto.stream.CryptoOutputStream; * * @author satellite */ -public class Client { +public class Client implements Runnable { Socket socket; @@ -17,4 +17,14 @@ public class Client { CryptoOutputStream outputStream; + + public Client(Socket socket) { + this.socket = socket; + } + + @Override + public void run() { + System.out.println("aaaaaaaaaaaeoiu"); + } + } diff --git a/src/main/java/electrosphere/net/server/Server.java b/src/main/java/electrosphere/net/server/Server.java index 904a2c2d..47ebca45 100644 --- a/src/main/java/electrosphere/net/server/Server.java +++ b/src/main/java/electrosphere/net/server/Server.java @@ -1,6 +1,11 @@ package electrosphere.net.server; +import java.io.IOException; import java.net.ServerSocket; +import java.net.Socket; +import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -8,9 +13,52 @@ import java.net.ServerSocket; */ public class Server { + static int port = 42536; + ServerSocket serverSocket; - public Server(int port){ - + HashMap clientMap; + + + + + void initServer(){ + clientMap = new HashMap(); + } + + + Server(int port){ + initServer(); + try { + serverSocket = new ServerSocket(); + } catch (IOException ex) { + System.err.println("Failed to start server socket!"); + ex.printStackTrace(); + System.exit(1); + } + boolean running = true; + while(running){ + Socket newSocket; + try { + newSocket = serverSocket.accept(); + Client newClient = new Client(newSocket); + clientMap.put(newSocket.getInetAddress().getHostAddress(), newClient); + new Thread(newClient).start(); + } catch (IOException ex) { + System.err.println("Socket error on client socket!"); + ex.printStackTrace(); + } + } + } + + + public static void startServerThread(){ + Thread t = new Thread(){ + @Override + public void run(){ + Server s = new Server(port); + } + }; + t.start(); } }