Exemplo n.º 1
0
  public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    Collection collection = (Collection) object;
    int length = collection.size();

    buffer.putInt(length);
    if (length == 0) return;

    Iterator it = collection.iterator();
    Class elementClass = it.next().getClass();
    while (it.hasNext()) {
      Object obj = it.next();

      if (obj.getClass() != elementClass) {
        elementClass = null;
        break;
      }
    }

    if (elementClass != null) {
      buffer.put((byte) 1);
      Serializer.writeClass(buffer, elementClass);
      Serializer serializer = Serializer.getSerializer(elementClass);

      for (Object elem : collection) {
        serializer.writeObject(buffer, elem);
      }
    } else {
      buffer.put((byte) 0);
      for (Object elem : collection) {
        Serializer.writeClassAndObject(buffer, elem);
      }
    }
  }
Exemplo n.º 2
0
  @SuppressWarnings("unchecked")
  public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    int length = data.getInt();

    Collection collection;
    try {
      collection = (Collection) c.newInstance();
    } catch (Exception e) {
      log.log(
          Level.FINE, "[Serializer][???] Could not determine collection type. Using ArrayList.");
      collection = new ArrayList(length);
    }

    if (length == 0) return (T) collection;

    if (data.get() == (byte) 1) {
      SerializerRegistration reg = Serializer.readClass(data);
      Class clazz = reg.getType();
      Serializer serializer = reg.getSerializer();

      for (int i = 0; i != length; ++i) {
        collection.add(serializer.readObject(data, clazz));
      }
    } else {
      for (int i = 0; i != length; ++i) {
        collection.add(Serializer.readClassAndObject(data));
      }
    }
    return (T) collection;
  }
Exemplo n.º 3
0
  @Override
  public void simpleInitApp() {
    MessageUtils.registerDataClasses();
    MessageUtils.registerMessages();

    Serializer.registerClass(ReplayHeader.class);
    Serializer.registerClass(ReplayCmdData.class);
    Serializer.registerClass(ReplayData.class);

    Globals.assets = getAssetManager();
    Globals.app = this;
    world = new World();
    game = new ServerGame();
    physics = new BulletAppState();
    physics.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
    flyCam.setEnabled(false);

    int port = Settings.get().General().getPort();
    if (port <= 0 || port > 65535) {
      System.out.println("Port must be between 0 and 65535");
      System.exit(1);
    }

    try {
      server = Network.createServer(port, port);
      server.start();
    } catch (IOException ex) {
      System.exit(1);
    }

    receiver = new DefaultReceiver();
    server.addMessageListener(receiver, OneTrueMessage.class);

    sender = new RecordingServerSender(server);
    AbstractBuff.setSender(sender);

    receiver.registerCommandHandler(sender);

    listenerManager = new ServerNetListener(this, server);
    sync = new Sync(this);

    ServerInput input = ServerInput.get();

    receiver.registerCommandHandler(listenerManager);
    receiver.registerCommandHandler(input);
    receiver.registerCommandHandler(sync);

    stateManager.attach(sender);
    stateManager.attach(receiver);
    stateManager.attach(sync);
    stateManager.attach(world);
    stateManager.attach(game);
    stateManager.attach(physics);

    sender.setWorld(world);
    // Accuracy should be > 45 or projectiles might "disappear" before
    // exploding. This is because of FogOfWar
    physics.getPhysicsSpace().setAccuracy(1f / Settings.get().General().getPhysicsTicksPerSecond());
  }
Exemplo n.º 4
0
 public static void registerSerializers() {
   Serializer.registerClass(VectorMessage.class);
   Serializer.registerClass(ClientLoginMessage.class);
   Serializer.registerClass(ServerLoginMessage.class);
   Serializer.registerClass(AddPlayerMessage.class);
   Serializer.registerClass(ChatMessage.class);
   Serializer.registerClass(RemovePlayerMessage.class);
   Serializer.registerClass(StartGameMessage.class);
   Serializer.registerClass(MoveMessage.class);
   Serializer.registerClass(AddEntityMessage.class);
   Serializer.registerClass(EnterEntityMessage.class);
 }
  public void Init_server() throws IOException {
    Serializer.registerClass(HelloMessage.class);
    Serializer.registerClass(SMObjectShare.class);
    try {
      Server myServer = Network.createServer(NAME, VERSION, PORT, UDP_PORT);
      SpiderMonkeyObjPlayerServerListener objlistener = new SpiderMonkeyObjPlayerServerListener();
      objlistener.SetApp(this);

      myServer.addMessageListener(new SpiderMonkeyServerListener(), HelloMessage.class);
      myServer.addMessageListener(objlistener, SMObjectShare.class);
      myServer.start();
    } catch (Exception e) {

    }
  }
Exemplo n.º 6
0
  public static void main(String[] args) throws IOException, InterruptedException {
    Serializer.registerClass(SomeObject.class);
    Serializer.registerClass(TestSerializationMessage.class);

    Server server = Network.createServer(5110);
    server.start();

    Client client = Network.connectToServer("localhost", 5110);
    client.start();

    server.addMessageListener(new TestSerialization(), TestSerializationMessage.class);
    client.send(new TestSerializationMessage(true));

    Thread.sleep(10000);
  }
Exemplo n.º 7
0
 /**
  * Creates a server on the given ports.
  *
  * @param tcpPort The port to use for TCP communication.
  * @param updPort The port to use for UDP communication.
  * @throws IOException If the server fails to grab the ports.
  */
 public EVNetworkServer(final int tcpPort, final int updPort) throws IOException {
   super(
       "everVoid",
       EverVoidClient.getVersionAsInt(),
       new SelectorKernel(tcpPort),
       new UdpKernel(updPort));
   Serializer.registerClass(PartialMessage.class);
   addMessageListener(this);
 }
Exemplo n.º 8
0
 /** Creates a message from the properly sized byte buffer and adds it to the messages queue. */
 protected void createMessage(ByteBuffer buffer) {
   try {
     Object obj = Serializer.readClassAndObject(buffer);
     Message m = (Message) obj;
     messages.add(m);
   } catch (IOException e) {
     throw new RuntimeException("Error deserializing object", e);
   }
 }
Exemplo n.º 9
0
  public ServerTest() throws IOException, InterruptedException {
    DiffClassRegistration.registerClasses();
    Serializer.registerClass(GameStateMessage.class);

    myServer = Network.createServer(6143);
    diffHandler = new ServerDiffHandler<GameStateMessage>(myServer);
    myServer.start();

    myServer.addConnectionListener(
        new ConnectionListener() {

          @Override
          public void connectionRemoved(Server server, HostedConnection conn) {
            System.out.println("Connection closed from " + conn.getAddress());
          }

          @Override
          public void connectionAdded(Server server, HostedConnection conn) {
            System.out.println("New connection from " + conn.getAddress());
          }
        });

    List<Float> or = Arrays.asList(new Float[] {0.5f, 0.6f, 0.7f});

    while (true) {
      if (myServer.hasConnections()) {
        List<Float> newPos =
            new ArrayList<Float>(Arrays.asList(new Float[] {(float) serverTicks, 8.0f, 3.0f}));

        GameStateMessage newMessage = new GameStateMessage("test", newPos, or, (byte) 0);

        /* Dispatch same message to all clients */
        diffHandler.dispatchMessage(myServer, Filters.in(myServer.getConnections()), newMessage);

        // send a message that is old (id=1), see what happens
        // myServer.broadcast(new LabeledMessage((short)1, newMessage));

        /* Check if the connection is lagging badly */
        for (HostedConnection conn : myServer.getConnections()) {
          if (diffHandler.getLag(conn) > 20) {
            System.out.println("Client " + conn.getAddress() + " is lagging badly.");
          }
        }
      }
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      serverTicks++;
    }
  }
Exemplo n.º 10
0
  /**
   * Converts a message to a ByteBuffer using the Serializer and the (short length) + data protocol.
   * If target is null then a 32k byte buffer will be created and filled.
   */
  public static ByteBuffer messageToBuffer(Message message, ByteBuffer target) {
    // Could let the caller pass their own in
    ByteBuffer buffer = target == null ? ByteBuffer.allocate(32767 + 2) : target;

    try {
      buffer.position(2);
      Serializer.writeClassAndObject(buffer, message);
      buffer.flip();
      short dataLength = (short) (buffer.remaining() - 2);
      buffer.putShort(dataLength);
      buffer.position(0);

      return buffer;
    } catch (IOException e) {
      throw new RuntimeException("Error serializing message", e);
    }
  }
Exemplo n.º 11
0
 public static void registerSerializers() {
   Serializer.registerClass(ActionMessage.class);
   Serializer.registerClass(AutoControlMessage.class);
   Serializer.registerClass(ChatMessage.class);
   Serializer.registerClass(ClientJoinMessage.class);
   Serializer.registerClass(HandshakeMessage.class);
   Serializer.registerClass(ManualControlMessage.class);
   Serializer.registerClass(ServerAddEntityMessage.class);
   Serializer.registerClass(ServerAddPlayerMessage.class);
   Serializer.registerClass(SyncCharacterMessage.class);
   Serializer.registerClass(ServerEffectMessage.class);
   Serializer.registerClass(ServerEnableEntityMessage.class);
   Serializer.registerClass(ServerDisableEntityMessage.class);
   Serializer.registerClass(ServerEnterEntityMessage.class);
   Serializer.registerClass(ServerEntityDataMessage.class);
   Serializer.registerClass(ServerJoinMessage.class);
   Serializer.registerClass(SyncRigidBodyMessage.class);
   Serializer.registerClass(ServerPlayerDataMessage.class);
   Serializer.registerClass(ServerRemoveEntityMessage.class);
   Serializer.registerClass(ServerRemovePlayerMessage.class);
   Serializer.registerClass(StartGameMessage.class);
 }
Exemplo n.º 12
0
 static {
   Serializer s = new RmiSerializer();
   Serializer.registerClass(RemoteObjectDefMessage.class, s);
   Serializer.registerClass(RemoteMethodCallMessage.class, s);
   Serializer.registerClass(RemoteMethodReturnMessage.class, s);
 }
Exemplo n.º 13
0
 /** Instantiates a new solar wars server. */
 private SolarWarsServer() {
   super();
   // Add messages to serializer so they can be read, DO THIS FIRST
   Serializer.registerClass(StringMessage.class);
   Serializer.registerClass(ChatMessage.class);
   Serializer.registerClass(PlayerConnectingMessage.class);
   Serializer.registerClass(PlayerLeavingMessage.class);
   Serializer.registerClass(PlayerAcceptedMessage.class);
   Serializer.registerClass(StartGameMessage.class);
   Serializer.registerClass(PlanetActionMessage.class);
   Serializer.registerClass(GeneralActionMessage.class);
   Serializer.registerClass(LevelActionMessage.class);
   Serializer.registerClass(PlayerReadyMessage.class);
   Serializer.registerClass(PlayerState.class);
   Serializer.registerClass(Player.class);
 }
Exemplo n.º 14
0
 @Serializable
 public static void initSerializables() {
   Serializer.registerClass(UnitMoveRequest.class);
   Serializer.registerClass(UnitMoveOrder.class);
   Serializer.registerClass(GenerateMapOrder.class);
   Serializer.registerClass(StartGameOrder.class);
   Serializer.registerClass(UpdateConnectedClientsListOrder.class);
   Serializer.registerClass(RegisterClientRequest.class);
   Serializer.registerClass(RejectClientOrder.class);
   Serializer.registerClass(ServerPlayer.class);
   Serializer.registerClass(UpdateLobbyClientRequest.class);
   Serializer.registerClass(StartGameRequest.class);
   Serializer.registerClass(UpdateGameParamsRequest.class);
 }