190 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			190 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package electrosphere.net.server;
 | |
| 
 | |
| import electrosphere.entity.types.creature.CreatureTemplate;
 | |
| import electrosphere.entity.types.creature.CreatureUtils;
 | |
| import electrosphere.entity.Entity;
 | |
| import electrosphere.entity.EntityUtils;
 | |
| import electrosphere.entity.state.ironsight.IronSightTree;
 | |
| import electrosphere.entity.types.item.ItemUtils;
 | |
| import electrosphere.entity.types.attach.AttachUtils;
 | |
| import electrosphere.entity.types.collision.CollisionObjUtils;
 | |
| import electrosphere.logger.LoggerInterface;
 | |
| import electrosphere.main.Globals;
 | |
| import electrosphere.main.Main;
 | |
| import electrosphere.net.NetUtils;
 | |
| import electrosphere.net.parser.net.message.AuthMessage;
 | |
| import electrosphere.net.parser.net.message.EntityMessage;
 | |
| import electrosphere.net.parser.net.message.NetworkMessage;
 | |
| import electrosphere.net.parser.net.message.PlayerMessage;
 | |
| import electrosphere.net.parser.net.message.ServerMessage;
 | |
| import electrosphere.net.parser.net.raw.NetworkParser;
 | |
| import electrosphere.net.server.protocol.ServerProtocol;
 | |
| 
 | |
| import java.io.IOException;
 | |
| import java.io.InputStream;
 | |
| import java.io.OutputStream;
 | |
| import java.math.BigInteger;
 | |
| import java.net.Socket;
 | |
| import java.net.SocketException;
 | |
| import java.nio.ByteBuffer;
 | |
| import java.security.spec.RSAKeyGenParameterSpec;
 | |
| import java.util.Properties;
 | |
| import java.util.Random;
 | |
| import java.util.concurrent.CopyOnWriteArrayList;
 | |
| import java.util.concurrent.TimeUnit;
 | |
| import java.util.logging.Level;
 | |
| import java.util.logging.Logger;
 | |
| import javax.crypto.spec.SecretKeySpec;
 | |
| import org.joml.Vector3f;
 | |
| 
 | |
| /**
 | |
|  *
 | |
|  * @author satellite
 | |
|  */
 | |
| public class ServerConnectionHandler implements Runnable {
 | |
|     
 | |
|     static int playerIdIncrementer = 0;
 | |
|     
 | |
|     Socket socket;
 | |
| //    CryptoInputStream inputStream;
 | |
| //    CryptoOutputStream outputStream;
 | |
|     InputStream inputStream;
 | |
|     OutputStream outputStream;
 | |
|     boolean initialized;
 | |
|     boolean isAuthenticated = false;
 | |
|     NetworkParser networkParser;
 | |
|     int playerID;
 | |
|     int playerCharacterID;
 | |
|     CreatureTemplate currentCreatureTemplate;
 | |
|     
 | |
|     ServerProtocol serverProtocol;
 | |
|     
 | |
|     public ServerConnectionHandler(Socket socket) {
 | |
|         this.socket = socket;
 | |
|         playerID = getNewPlayerID();
 | |
|         LoggerInterface.loggerNetworking.INFO("Player ID: " + playerID);
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     public void run() {
 | |
|         LoggerInterface.loggerNetworking.INFO("ServerConnectionHandler start");
 | |
|         initialized = false;
 | |
|         try {
 | |
|             socket.setSoTimeout(100);
 | |
|         } catch (SocketException ex) {
 | |
|             ex.printStackTrace();
 | |
|         }
 | |
|             //        final SecretKeySpec key = new SecretKeySpec(("1234567890123456").getBytes(),"AES");
 | |
| //        final Properties properties = new Properties();
 | |
| //        final RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(4096, BigInteger.probablePrime(4000, new Random()));
 | |
| //        try {
 | |
| //            inputStream = new CryptoInputStream("AES/ECB/PKCS5Padding",properties,socket.getInputStream(),key,spec);
 | |
| //        } catch (IOException ex) {
 | |
| //            ex.printStackTrace();
 | |
| //            System.exit(1);
 | |
| //        }
 | |
| //        try {
 | |
| //            outputStream = new CryptoOutputStream("AES/ECB/PKCS5Padding",properties,socket.getOutputStream(),key,spec);
 | |
| //        } catch (IOException ex) {
 | |
| //            ex.printStackTrace();
 | |
| //            System.exit(1);
 | |
| //        }
 | |
|         try {
 | |
|             inputStream = socket.getInputStream();
 | |
|             outputStream = socket.getOutputStream();
 | |
|             networkParser = new NetworkParser(inputStream,outputStream);
 | |
|             serverProtocol = new ServerProtocol(this);
 | |
|         } catch (IOException ex) {
 | |
|             ex.printStackTrace();
 | |
|             System.exit(1);
 | |
|         }
 | |
| 
 | |
|         networkParser.addOutgoingMessage(ServerMessage.constructPingMessage());
 | |
|         networkParser.addOutgoingMessage(AuthMessage.constructAuthRequestMessage());
 | |
| 
 | |
|         initialized = true;
 | |
|         while(Main.isRunning()){
 | |
|             //attempt poll incoming messages
 | |
|             networkParser.readMessagesIn();
 | |
|             //ponder incoming messages
 | |
|             while(networkParser.hasIncomingMessaage()){
 | |
|                 NetworkMessage message = networkParser.popIncomingMessage();
 | |
|                 serverProtocol.handleMessage(message);
 | |
|             }
 | |
|             //push outgoing message
 | |
|             networkParser.pushMessagesOut();
 | |
|             try {
 | |
|                 //sleep
 | |
|                 TimeUnit.MILLISECONDS.sleep(1);
 | |
|             } catch (InterruptedException ex) {
 | |
|                 ex.printStackTrace();
 | |
|             }
 | |
|         }
 | |
|     }
 | |
|     
 | |
|     public void setPlayerId(int id){
 | |
|         playerID = id;
 | |
|     }
 | |
| 
 | |
|     public int getPlayerId(){
 | |
|         return playerID;
 | |
|     }
 | |
|     
 | |
|     static int getNewPlayerID(){
 | |
|         playerIdIncrementer++;
 | |
|         return playerIdIncrementer;
 | |
|     }
 | |
| 
 | |
|     public void setPlayerCharacterId(int id){
 | |
|         playerCharacterID = id;
 | |
|     }
 | |
| 
 | |
|     public int getPlayerCharacterId(){
 | |
|         return playerCharacterID;
 | |
|     }
 | |
| 
 | |
|     public String getIPAddress(){
 | |
|         return socket.getRemoteSocketAddress().toString();
 | |
|     }
 | |
|     
 | |
|     public void addMessagetoOutgoingQueue(NetworkMessage message){
 | |
|         switch(message.getType()){
 | |
|             case ENTITY_MESSAGE:
 | |
|                 switch(((EntityMessage)message).getMessageSubtype()){
 | |
|                     case MOVEUPDATE:
 | |
|                         if(((EntityMessage)message).getentityID()==playerCharacterID){
 | |
|                             //basically don't send the message if this is the player's character and it's a move update
 | |
|                         } else {
 | |
|                             networkParser.addOutgoingMessage(message);
 | |
|                         }
 | |
|                         break;
 | |
|                     case ATTACKUPDATE:
 | |
|                         if(((EntityMessage)message).getentityID()==playerCharacterID){
 | |
|                             //basically don't send the message if this is the player's character and it's a move update
 | |
|                         } else {
 | |
|                             networkParser.addOutgoingMessage(message);
 | |
|                         }
 | |
|                         break;
 | |
|                     case SPAWNCREATURE:
 | |
|                         break;
 | |
|                     default:
 | |
|                         networkParser.addOutgoingMessage(message);
 | |
|                         break;
 | |
|                 }
 | |
|                 break;
 | |
|             default:
 | |
|                 networkParser.addOutgoingMessage(message);
 | |
|                 break;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public void setCreatureTemplate(CreatureTemplate currentCreatureTemplate){
 | |
|         this.currentCreatureTemplate = currentCreatureTemplate;
 | |
|     }
 | |
| 
 | |
|     public CreatureTemplate getCurrentCreatureTemplate(){
 | |
|         return this.currentCreatureTemplate;
 | |
|     }
 | |
| 
 | |
| }
 |