Example #1
0
 public JAXMStreamSource(InputStream is) throws IOException {
   if (is instanceof ByteInputStream) {
     this.in = (ByteInputStream) is;
   } else {
     ByteOutputStream bout = new ByteOutputStream();
     bout.write(is);
     this.in = bout.newInputStream();
   }
 }
Example #2
0
 /**
  * Shuts down the server and tells all clients to quit. The local game can continue to run after
  * this is called.
  */
 public void quit() {
   stopListening();
   try {
     ByteOutputStream out = new ByteOutputStream();
     out.writeInt(ServerCommands.Message.SERVER_QUITTING.ordinal());
     sendPacketToAllPlayers(out);
   } catch (IOException ex) {
     ex.printStackTrace();
   }
   clients = null;
   socket = null;
 }
Example #3
0
 /**
  * Sends a packet to all <code>clients</code> who are playing, except those on the <code>
  * excludeList</code>.
  */
 void sendPacketToMostPlayers(ByteOutputStream stream, Set<ClientMachine> excludeList)
     throws IOException {
   byte[] message = stream.toByteArray();
   for (ClientMachine c : clients) {
     if (!excludeList.contains(c)) {
       if (c.isInGame()) sendPacket(c, message);
     }
   }
 }
Example #4
0
  @Override
  void intervalLogic() {
    // Check for timeouts/pongs.
    Iterator<ClientMachine> i = clients.iterator();
    while (i.hasNext()) {
      ClientMachine cm = i.next();

      // The client timed out. Remove him.
      if (cm.shouldTimeout()) {
        // Remove the player.
        if (cm.isInGame()) {
          Game.getInstance().removePlayer(cm.inGamePlayer, " timed out.");

          try {
            // Tell clients.
            ByteOutputStream out = new ByteOutputStream();

            out.writeInt(ServerCommands.Message.PLAYER_QUIT.ordinal());
            out.writeBoolean(true);
            out.writeLong(cm.inGamePlayer.getId());
            sendPacketToAllButOnePlayer(out, cm);
          } catch (IOException ex) {
          }
        }
        i.remove();
      }

      // We haven't sent this client a message in a while. Pong him.
      if (cm.shouldPong()) {
        try {
          ByteOutputStream out = new ByteOutputStream();
          out.writeInt(ServerCommands.Message.PONG.ordinal());
          sendPacket(cm, out);
        } catch (IOException ex) {
        }
      }
    }
  }
Example #5
0
  /**
   * Called when we receive a packet from a client. Deciphers what the client is telling us, and
   * responds.
   */
  @Override
  void parseReceived(DatagramPacket p) {
    try {
      // Ensure this client is on our register.
      ClientMachine client = registerClient(new Machine(p.getAddress(), p.getPort()));

      ByteInputStream in = new ByteInputStream(p.getData());

      // Determine the type of message.
      int command = in.readInt();
      if ((command >= 0) && (command < Client.Message.values().length)) {
        switch (Client.Message.values()[command]) {
            // Client wants to join the game.
          case CONNECT:
            ByteOutputStream out = new ByteOutputStream();

            // If he's using an older netcode version, slam the door in his face.
            int version = in.readInt();
            if (version < Constants.NETCODE_VERSION) {
              out.writeInt(ServerCommands.Message.CONNECT_ERROR_OLDNETCODE.ordinal());
              out.writeInt(Constants.NETCODE_VERSION);
              sendPacket(client, out);
              Main.log(
                  "Connection from "
                      + client.toString()
                      + " refused, using old version: "
                      + version
                      + ".",
                  800);
              return;
            }
            Main.log("Connection from " + client.toString() + " accepted.");

            // Spawn him in (so he'll be included in the update).
            long id = Game.getInstance().addPlayer(in.readUTF(), new Color(in.readInt()));

            // Send him a full update.
            out.writeInt(ServerCommands.Message.FULL_UPDATE.ordinal());
            Game.getInstance().flatten(out);
            out.writeLong(id);

            // Associate this client with the ship.
            client.inGamePlayer = (Ship) Game.getInstance().getObjectManager().getObject(id);

            // Waddle this fat packet out the door!
            sendPacket(client, out);

            // Tell everyone else about the player joining.
            out = new ByteOutputStream();
            out.writeInt(ServerCommands.Message.PLAYER_JOINED.ordinal());
            client.inGamePlayer.flatten(out);
            sendPacketToAllButOnePlayer(out, client);
            break;

            // Client is sending us a keystroke.
          case KEYSTROKE:
            if (!client.isInGame()) break;

            int keyCode = in.readInt();
            Game.getInstance()
                .getActionManager()
                .add(new Action(client.inGamePlayer, keyCode, Game.getInstance().timeStep + 2));
            break;

            // Client is telling us he's still here.
          case PONG:
            client.see();
            break;

            // Client wishes to resume life.
          case QUITTING:
            if (!client.isInGame()) break;

            Game.getInstance().removePlayer(client.inGamePlayer);

            // Tell everyone else.
            out = new ByteOutputStream();

            out.writeInt(ServerCommands.Message.PLAYER_QUIT.ordinal());
            out.writeBoolean(false); // Not a timeout.
            out.writeLong(client.inGamePlayer.getId());

            sendPacketToAllButOnePlayer(out, client);
        }
      }
    } catch (IOException ex) {
      Main.fatalError("Server parsing exception.", ex);
    }
  }
Example #6
0
  /** Sends a packet to all <code>clients</code> who are playing. */
  void sendPacketToAllPlayers(ByteOutputStream stream) throws IOException {
    if (clients == null) return;

    byte[] message = stream.toByteArray();
    for (ClientMachine c : clients) if (c.isInGame()) sendPacket(c, message);
  }