Esempio n. 1
0
  void turnOnTurretReceiver() throws IOException {
    Global.log("Turning on turret receiver...");
    while (true) {
      if (isDisposed()) throw new IOException("Client game disposed");
      Global.log("Connecting to port " + Global.turretPort() + "...");
      try {
        turretStream = new ClientByteStream(ip, Global.turretPort(), Ship.bufferSize());
        break;
      } catch (IOException ex) {
      }
    }
    Global.log("Connected!");
    timers.add(
        new FixedTimer(
            new FixedTask() {
              public boolean fixedRate() {
                return false;
              }

              public float FPS() {
                return Global.ReceiveFPS;
              }

              public void run() {
                byte[] data = null;
                String name = null;
                try {
                  data = turretStream.read();
                  name = turretStream.readLine();
                } catch (IOException ex) {
                  System.err.println("Cannot read info from turret stream");
                  Global.onException();
                  stop();
                  return;
                }
                if (data == null) return;
                Ship s = new Ship(name, Global.transparent);
                s.setDesign(new Design.Turret(s));
                s.fromBytes(data, false);
                addShip(s);
              }
            }));
  }
Esempio n. 2
0
  void createBytePorts() throws IOException {

    Global.log("Creating byte ports...");

    int n = numPlayers();
    playerByteStreams = new ClientByteStream[n];

    for (int i = 0; i < n; ++i) {

      ClientByteStream stream = null;

      while (true) {

        if (isDisposed()) throw new IOException("Client game disposed");
        Global.log("Connecting to port " + Global.playerPort(i) + "...");
        try {
          stream =
              playerByteStreams[i] =
                  new ClientByteStream(ip, Global.playerPort(i), Ship.bufferSize());
          break;
        } catch (IOException ex) {
        }
      }

      Global.log("Connected!");
      Global.log("Sending client information...");

      // first byte is player ID

      stream.out.write((byte) playerID);
      stream.out.flush();
      Global.log("Done!");
    }

    Global.log("Done creating byte ports!");
  }