Advertisement
Guest User

WIP Multiplayer Design

a guest
Oct 16th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. package Engine;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8.  
  9. public class Multiplayer
  10. {
  11.  
  12.     public static boolean IsConnected = false;
  13.    
  14.     public static void InvertIfConnected()
  15.     {  
  16.         if (IsConnected = false)
  17.         {
  18.             IsConnected = true;
  19.         }
  20.        
  21.         else if(IsConnected = true)
  22.         {
  23.             IsConnected = false;
  24.         }
  25.     }
  26.    
  27.     public static class Client
  28.     {
  29.    
  30.         public Multiplayer.DataTypes DataType;
  31.        
  32.         public byte OutputData;
  33.         public byte InputData;
  34.        
  35.         public Thread Ping;
  36.        
  37.         public Object PingData;
  38.        
  39.         public static Socket Client;
  40.         public static DataOutputStream Output;
  41.         public static DataInputStream Input;
  42.  
  43.         public void Connect(String IP, int Port)
  44.         {
  45.             try{
  46.                
  47.                 this.Client = new Socket( IP , Port);
  48.                 this.Output = new DataOutputStream(Client.getOutputStream());
  49.                 this.Input = new DataInputStream(Client.getInputStream());
  50.                
  51.             } catch (Exception e)
  52.             {
  53.                 System.out.println(e);
  54.             }
  55.         }
  56.    
  57.         public static void Disconnect()
  58.         {
  59.            
  60.             Game.SavePlayer();
  61.            
  62.             try {
  63.                
  64.                 Output.close();
  65.                 Input.close();
  66.                 Client.close();
  67.                
  68.             } catch (IOException e) {
  69.                 e.printStackTrace();
  70.             }
  71.  
  72.         }
  73.    
  74.         public void UpdateServer(Multiplayer.DataTypes Type)
  75.         {
  76.            
  77.             switch (Type)
  78.             {
  79.                
  80.                 case Player:
  81.                
  82.                     Object Player = Game.GetPlayer();
  83.                    
  84. //                  this.Output.writeExternal(Player);
  85.                    
  86. /*                  try {
  87.                        
  88. //                      this.Output.writeByte(OutputData);
  89.                        
  90.                     } catch (IOException e) {
  91.                         e.printStackTrace();
  92.                     }
  93.                     */
  94.                     break;
  95.                
  96.                 case ModifyTile:
  97.                    
  98.                     break;
  99.                    
  100.                 case PingServer:
  101.                    
  102.                     break;
  103.                
  104.             }
  105.            
  106.            
  107.         }
  108.    
  109.         public byte GetInfo()
  110.         {
  111.            
  112.             byte Data = 0;
  113.            
  114.             try {
  115.                
  116.                 Data = this.Input.readByte();
  117.                
  118.             } catch (IOException e) {
  119.                 e.printStackTrace();
  120.             }
  121.            
  122.             return Data;
  123.         }
  124.        
  125.         public void FlushData()
  126.         {
  127.             try {
  128.                 this.Output.flush();
  129.             } catch (IOException e) {
  130.                 e.printStackTrace();
  131.             }
  132.         }
  133.        
  134.         public void run()
  135.         {
  136.             while (true)
  137.             {
  138.                
  139.                 UpdateServer(Multiplayer.DataTypes.PingServer);
  140.                
  141.                 try {
  142.                    
  143.                     Ping.sleep(1000);
  144.                    
  145.                 } catch (InterruptedException e) {
  146.                    
  147.                     e.printStackTrace();
  148.                 }
  149.             }
  150.         }
  151.    
  152.         public Client()
  153.         {
  154.            
  155.             this.PingData = new ClientInfo();
  156.            
  157.         }
  158.        
  159.     }
  160.    
  161.     public class Server
  162.     {
  163.        
  164.         public byte OutputData;
  165.         public byte InputData;
  166.        
  167.         Object[] Players;  
  168.        
  169.         public ServerSocket Server;
  170.         public DataOutputStream Output;
  171.         public DataInputStream Input;
  172.  
  173.         public Socket Client;
  174.        
  175.         public int MaxPlayers;
  176.        
  177.         public void CreateServer(int Port)
  178.         {
  179.             try{
  180.                 this.Server = new ServerSocket(Port);
  181.                 this.Output = new DataOutputStream(Client.getOutputStream());
  182.                 this.Input = new DataInputStream(Client.getInputStream());
  183.             } catch (Exception e)
  184.             {
  185.                 System.out.println(e);
  186.             }
  187.         }
  188.    
  189.         public void Disconnect()
  190.         {
  191.            
  192.             try {
  193.                
  194.                 this.Output.close();
  195.                 this.Input.close();
  196.                 this.Server.close();
  197.                
  198.             } catch (IOException e) {
  199.                 e.printStackTrace();
  200.             }
  201.  
  202.         }
  203.    
  204.         public void PingBack()
  205.         {
  206.  
  207.         }
  208.    
  209.         public byte GetInfo()
  210.         {
  211.            
  212.             byte Data = 0;
  213.            
  214.             try {
  215.                
  216.                 Data = this.Input.readByte();
  217.                
  218.             } catch (IOException e) {
  219.                 e.printStackTrace();
  220.             }
  221.            
  222.             return Data;
  223.         }
  224.    
  225.         public void SendInfo(byte OutputData)
  226.         {
  227.             try {
  228.                
  229.                 this.Output.writeByte(OutputData);
  230.                
  231.             } catch (IOException e) {
  232.                 e.printStackTrace();
  233.             }
  234.         }
  235.        
  236.         public void FlushData()
  237.         {
  238.             try {
  239.                 this.Output.flush();
  240.             } catch (IOException e) {
  241.                 e.printStackTrace();
  242.             }
  243.         }
  244.        
  245.         public void AcceptClient()
  246.         {
  247.             try {
  248.                 this.Client = Server.accept();
  249.             } catch (IOException e) {
  250.                 // TODO Auto-generated catch block
  251.                 e.printStackTrace();
  252.             }
  253.         }
  254.                
  255.         public Server(int MaxNumberOfPlayers)
  256.         {
  257.             Players = new Object[MaxNumberOfPlayers];
  258.         }
  259.        
  260.     }
  261.  
  262.    
  263.     public enum DataTypes
  264.     {
  265.         Player, ModifyTile, PingServer, PingClient
  266.     }
  267.    
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement