Exemple #1
3
 void send(Scores scoreTable) {
   if (out == null) return;
   try {
     out.reset();
     out.writeObject(scoreTable);
     out.flush();
   } catch (Exception e) {
     ctrl.serverError("Scores Send error!");
   }
 }
Exemple #2
1
 void send(PlayersList players) {
   if (out == null) return;
   try {
     out.reset();
     out.writeObject(players);
     out.flush();
   } catch (Exception e) {
     ctrl.serverError("PlayersList Send error!");
   }
 }
Exemple #3
1
 void send(TimeStamp timeStamp) {
   if (out == null) return;
   try {
     out.reset();
     out.writeObject(timeStamp);
     out.flush();
   } catch (Exception e) {
     ctrl.serverError("TimeStamp Send error!");
   }
 }
 private void sendAll(Object message) {
   synchronized (oos) {
     try {
       for (ObjectOutputStream out : oos) {
         out.writeObject(message);
         out.reset();
       }
     } catch (IOException ignored) {
     }
   }
 }
  public void sendMessage(String name, Serializable message) throws IOException {
    setCall();

    try {
      if (!statics.isTeamRobot()) {
        throw new IOException("You are not on a team.");
      }
      byteStreamWriter.reset();
      ObjectOutputStream objectStreamWriter = new ObjectOutputStream(byteStreamWriter);

      objectStreamWriter.writeObject(message);
      objectStreamWriter.flush();
      byteStreamWriter.flush();
      final byte[] bytes = byteStreamWriter.toByteArray();

      objectStreamWriter.reset();
      if (bytes.length > MAX_MESSAGE_SIZE) {
        throw new IOException("Message too big. " + bytes.length + ">" + MAX_MESSAGE_SIZE);
      }
      commands.getTeamMessages().add(new TeamMessage(getName(), name, bytes));
    } catch (IOException e) {
      out.printStackTrace(e);
      throw e;
    }
  }
Exemple #6
0
 void send(GameInfo gameInfo) {
   if (out == null) return;
   try {
     out.reset();
     out.writeObject(gameInfo);
     out.flush();
   } catch (Exception e) {
     ctrl.serverError("GameInfo Send error!");
   }
 }
 private void send() {
   try {
     // Reset objectOutputStream
     out.reset();
     // Write arraylist of messages to outputStream
     out.writeObject(messages);
     // Flushes outputStream
     out.flush();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Exemple #8
0
 public void writeTurn(ITurnSnapshot turn, int round, int time) {
   try {
     if (time != recordInfo.turnsInRounds[round]) {
       throw new Error("Something rotten");
     }
     if (time == 0) {
       objectWriteStream.reset();
     }
     recordInfo.turnsInRounds[round]++;
     recordInfo.roundsCount = round + 1;
     objectWriteStream.writeObject(turn);
   } catch (IOException e) {
     logError(e);
   }
 }
Exemple #9
0
  public static void main(String[] args) throws IOException {

    long start = System.currentTimeMillis();

    Socket s = new Socket("localhost", 4444);
    ObjectOutputStream toServer = new ObjectOutputStream(s.getOutputStream());

    Person p = new Person("John", 0);

    for (int id = 0; id < HowMany.times; id++) {
      p.setId(id);
      toServer.writeObject(p);

      toServer.reset();
    }

    long end = System.currentTimeMillis();
    System.out.println("Total time " + (end - start) / 1000 + "s");
  }
 @Override
 public synchronized void run() {
   boolean bName = true;
   while (listening) {
     try {
       Object object = in.readObject();
       if (object.getClass().equals(Boolean.class)) {
         boolean can = (boolean) object;
         int nWay = (int) in.readObject();
         if (can) {
           int position = 0;
           synchronized (players) {
             for (int i = 0; i < players.size(); i++)
               if (players.get(i).getName().equals(player.getName())) {
                 position = i;
                 break;
               }
           }
           if (ways != null) {
             synchronized (ways) {
               if ((ways[position] + nWay) != 3) ways[position] = nWay;
             }
           }
         }
       } else if (object.getClass().equals(Player.class)) {
         player = (Player) object;
         if (bName) {
           bName = false;
           synchronized (players) {
             player = checkPlayerName(player);
             out.writeObject(player);
             out.reset();
             players.add(player);
           }
           sendAll(players);
           sendAll(Const.CHAT + "В комнату вошел игрок " + player.getName() + "\n");
         } else {
           synchronized (players) {
             for (Player p : players) {
               if (p.getName().equals(player.getName())) {
                 p.setReady(player.isReady());
                 p.setPositionOnMap(player.getPositionOnMap());
                 sendAll(players);
                 break;
               }
             }
           }
         }
       } else if (object.getClass().equals(String.class)) {
         String s = (String) object;
         if (s.startsWith(Const.START)) {
           if (checkPlayersPositions()) sendAll(Const.START);
           else
             sendAll(
                 Const.CHAT
                     + "<Сервер>:Начальные позиции всех "
                     + "игроков не должны совпадать\n");
         } else if (s.equals(Const.EXIT)) {
           throw new Exception("Выход игрока по нажатию кнопки");
         } else {
           sendAll(object);
         }
       } else {
         sendAll(object);
       }
     } catch (Exception e) {
       e.printStackTrace();
       synchronized (players) {
         for (Player p : players)
           if (p.getName().equals(player.getName())) {
             players.remove(p);
             break;
           }
       }
       synchronized (oos) {
         oos.remove(out);
       }
       sendAll(players);
       sendAll(Const.CHAT + "Игрок " + player.getName() + " покинул комнату\n");
       if (ServerController.gameIsStarted) thoughtOutPlayer();
       break;
     }
   }
 }
 /**
  * Reset the output stream so we can use the same buffer repeatedly. This would not normally be
  * used, but is necessary in some circumstances when Java refuses to send data that it thinks has
  * been sent.
  */
 public final void forceResetAfterSend() throws IOException {
   output.reset();
 }
Exemple #12
0
 public void reset() throws IOException {
   System.out.println("XXX   ObjectOutputStream.reset called XXXX");
   super.reset();
 }