Package Server

Source Code of Server.ClientConnection

package Server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import serverMessages.ChatEvent;
import serverMessages.MotionEvent;
import serverMessages.NewConnectionEvent;
import serverMessages.SpellCastEvent;
import Global_Package.Enums;

public class ClientConnection extends Thread{
  private Socket sock;
  private ObjectOutputStream out;
  private ObjectInputStream in;
  private int PlayerID;
  private int ConnectionID;
 
  ClientConnection(Socket inSock, int Id) throws IOException {
    sock= inSock;
    System.out.println("Connected to " + sock.getInetAddress().getHostName());
    out = new ObjectOutputStream(sock.getOutputStream());
    out.flush();
    in = new ObjectInputStream(sock.getInputStream());
    PlayerID=Id;
    System.out.println("Shit connected");
   
    if( in.readByte()==0){
     
      out.writeInt(PlayerID); //send id
      out.flush();
      int i=in.readInt();
      ServerMain.logicalUnit.EventQue.add(new NewConnectionEvent(Id,0,i));
      System.out.print("Charater created! New player joined!!");
     
      //new player is there other players whats current game state!?!?!?
     
    }else{
      //exception???
      //Flush complete state at it.
      //reconnected client!!
    }
   
  }
 
  public void run(){
    while (ServerMain.On){
      try {
        long Time=ServerMain.getTime();
        switch(in.read()){
         
        case -1:
          throw new IOException();
         
        case Enums.sendChat:
          //set limit here ar some point
          byte bytesIn[]=new byte[1024];
          in.readFully(bytesIn);
          ServerMain.enQueue(new ChatEvent(PlayerID,Time,bytesIn) );
          break;
       
        case Enums.sendMoveUnit:
          System.out.print("messageIn\n");
          int ID=in.readInt();
          int x=in.readInt();
          int y=in.readInt();
          ServerMain.enQueue(new MotionEvent(PlayerID,Time,ID,x,y) );
          break;
         
        case Enums.sendCastSpell:
          int type=in.readInt();
          int sourceID=in.readInt();
          int lvl=in.readInt();
          int x1=in.readInt();
          int y1=in.readInt();
          double theta=in.readDouble();
          ServerMain.enQueue(new SpellCastEvent(PlayerID,Time,sourceID,x1,y1,type,lvl,theta) );
          break;
       
        }
        System.out.println("Stuff in");
       
      } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.print("Read Exception! From Player:"+ PlayerID +"\n");
        try {
          sock.close();
        } catch (IOException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
        break;
      }
    }
  }
 
}
TOP

Related Classes of Server.ClientConnection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.