1 package net.sourceforge.simplegamenet.framework.transport; 2 3 import java.io.IOException; 4 import java.io.ObjectInputStream; 5 import java.io.ObjectOutputStream; 6 import java.net.Socket; 7 8 public class ServerConnection extends Connection { 9 10 protected ServerModem serverModem; 11 protected Integer playerID; 12 13 protected boolean gameEndUnprocessed = false; 14 15 public ServerConnection(ServerModem serverModem, Integer playerID, Socket socket, 16 ObjectInputStream objectInputStream, 17 ObjectOutputStream objectOutputStream) { 18 super(socket, objectInputStream, objectOutputStream); 19 this.serverModem = serverModem; 20 this.playerID = playerID; 21 } 22 23 public void run() { 24 while (active) { 25 try { 26 Packet packet = (Packet) objectInputStream.readObject(); 27 if (active) { 28 serverModem.processPacketInServerThread(playerID, packet); 29 } 30 } catch (ClassNotFoundException exception) { 31 active = false; 32 serverModem.processDisconnectionInEventThread(playerID); 33 } catch (ClassCastException exception) { 34 active = false; 35 serverModem.processDisconnectionInEventThread(playerID); 36 } catch (IOException exception) { 37 active = false; 38 serverModem.processDisconnectionInEventThread(playerID); 39 } 40 } 41 } 42 43 public void sendPacket(Packet packet) { 44 if (active) { 45 try { 46 objectOutputStream.writeObject(packet); 47 } catch (IOException exception) { 48 active = false; 49 serverModem.processDisconnectionInEventThread(playerID); 50 } 51 } 52 } 53 54 public void setGameEndUnprocessed(boolean gameEndUnprocessed) { 55 this.gameEndUnprocessed = gameEndUnprocessed; 56 } 57 58 public void close() { 59 active = false; 60 if (gameEndUnprocessed) { 61 try { 62 serverModem.gameEnded(playerID); 63 } catch (ClassCastException e) { 64 e.printStackTrace(); 65 } 66 } 67 super.close(); 68 } 69 70 }