Android Open Source - Freebloks-Android N E T_ H E A D E R






From Project

Back to project page Freebloks-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project Freebloks-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.saschahlusiak.freebloks.network;
/*from  w ww  .j  a v a 2s  .  co m*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.Socket;

public class NET_HEADER implements Serializable {
  private static final long serialVersionUID = 1L;

  /* int check1;  */ /* uint8 */
  public int data_length; /* uint16 */
  public int msg_type; /* uint8 */
  /* int check2; */ /* uint8 */
  public static final int HEADER_SIZE = 5;

  byte buffer[];

  public NET_HEADER(int msg_type, int data_length) {
    this.msg_type = msg_type;
    this.data_length = data_length;
  }

  public NET_HEADER(NET_HEADER from) {
    this.data_length = from.data_length;
    this.msg_type = from.msg_type;
    this.buffer = from.buffer;
  }

  /**
   * a java byte is always signed, being -128..127
   * casting (byte)-1 to int will result in (int)-1
   *
   */
  public static final int unsigned(byte b) {
    return (b & 0xFF);
  }

  boolean read(Socket socket, boolean block) throws Exception {
    InputStream is;
    int r;
    int check1, check2;
    if (socket == null)
      return false;
    if (socket.isInputShutdown())
      return false;

    buffer = new byte[HEADER_SIZE];
    is = socket.getInputStream();

    if (!block && (is.available() < HEADER_SIZE))
      return false;

    r = is.read(buffer, 0, HEADER_SIZE);
    if (r < HEADER_SIZE)
      throw new Exception("read error");

    check1 = unsigned(buffer[0]);
    data_length = unsigned(buffer[1]) << 8 | unsigned(buffer[2]);
    msg_type = buffer[3];
    check2 = unsigned(buffer[4]);

    if (data_length < HEADER_SIZE)
      throw new Exception("invalid header data length");

    /* Beiden Checksums erneut berechnen */
    int c1 = (byte) (data_length & 0x0055) ^ msg_type;
    int c2 = (c1 ^ 0xD6) + msg_type;
    /* Bei Ungleichheit Fehler, sonst Nachricht ok */
    if (c1 != check1 || c2 != check2)
      throw new Exception("header checksum failed");

    data_length -= HEADER_SIZE;

    if (is.available() < data_length)
      throw new Exception("short read for package payload");
    buffer = new byte[data_length];

    r = is.read(buffer, 0, data_length);
    if (r < data_length)
      throw new Exception("short read for package payload");

    return true;

  }

  void prepare(ByteArrayOutputStream bos) {
    int l = data_length + HEADER_SIZE;
    int check1, check2;
    check1 = (l & 0x0055) ^ msg_type;
    check2 = (check1 ^ 0xD6) + msg_type;

    bos.write(check1);
    bos.write((l >> 8) & 0xff);
    bos.write(l & 0xff);
    bos.write(msg_type);
    bos.write(check2);
  }

  public boolean send(Socket socket) {
    if (socket == null)
      return false;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    prepare(bos);
    try {
      socket.getOutputStream().write(bos.toByteArray());
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }
}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelperUtils.java
com.google.example.games.basegameutils.GameHelper.java
de.saschahlusiak.freebloks.AboutActivity.java
de.saschahlusiak.freebloks.Global.java
de.saschahlusiak.freebloks.backup.FreebloksBackupAgent.java
de.saschahlusiak.freebloks.controller.GameMode.java
de.saschahlusiak.freebloks.controller.JNIServer.java
de.saschahlusiak.freebloks.controller.PlayerData.java
de.saschahlusiak.freebloks.controller.SpielClientInterface.java
de.saschahlusiak.freebloks.controller.SpielClient.java
de.saschahlusiak.freebloks.controller.Spielleiter.java
de.saschahlusiak.freebloks.database.FreebloksDBOpenHandler.java
de.saschahlusiak.freebloks.database.FreebloksDB.java
de.saschahlusiak.freebloks.database.HighscoreDB.java
de.saschahlusiak.freebloks.donate.DonateActivity.java
de.saschahlusiak.freebloks.game.ActivityInterface.java
de.saschahlusiak.freebloks.game.AddScoreTask.java
de.saschahlusiak.freebloks.game.CustomGameDialog.java
de.saschahlusiak.freebloks.game.FreebloksActivity.java
de.saschahlusiak.freebloks.game.GameFinishActivity.java
de.saschahlusiak.freebloks.game.GameMenu.java
de.saschahlusiak.freebloks.game.RateAppDialog.java
de.saschahlusiak.freebloks.game.RetainedConfig.java
de.saschahlusiak.freebloks.game.SpielClientThread.java
de.saschahlusiak.freebloks.lobby.ChatEntry.java
de.saschahlusiak.freebloks.lobby.ChatListAdapter.java
de.saschahlusiak.freebloks.lobby.ColorAdapter.java
de.saschahlusiak.freebloks.lobby.LobbyDialog.java
de.saschahlusiak.freebloks.model.Player.java
de.saschahlusiak.freebloks.model.Spiel.java
de.saschahlusiak.freebloks.model.Stone.java
de.saschahlusiak.freebloks.model.Turn.java
de.saschahlusiak.freebloks.model.Turnpool.java
de.saschahlusiak.freebloks.network.NET_CHAT.java
de.saschahlusiak.freebloks.network.NET_CURRENT_PLAYER.java
de.saschahlusiak.freebloks.network.NET_GAME_FINISH.java
de.saschahlusiak.freebloks.network.NET_GRANT_PLAYER.java
de.saschahlusiak.freebloks.network.NET_HEADER.java
de.saschahlusiak.freebloks.network.NET_REQUEST_GAME_MODE.java
de.saschahlusiak.freebloks.network.NET_REQUEST_HINT.java
de.saschahlusiak.freebloks.network.NET_REQUEST_PLAYER.java
de.saschahlusiak.freebloks.network.NET_REQUEST_UNDO.java
de.saschahlusiak.freebloks.network.NET_REVOKE_PLAYER.java
de.saschahlusiak.freebloks.network.NET_SERVER_STATUS.java
de.saschahlusiak.freebloks.network.NET_SET_STONE.java
de.saschahlusiak.freebloks.network.NET_START_GAME.java
de.saschahlusiak.freebloks.network.NET_UNDO_STONE.java
de.saschahlusiak.freebloks.network.Network.java
de.saschahlusiak.freebloks.preferences.AboutFragment.java
de.saschahlusiak.freebloks.preferences.DisplayFragment.java
de.saschahlusiak.freebloks.preferences.FreebloksPreferences.java
de.saschahlusiak.freebloks.preferences.InterfaceFragment.java
de.saschahlusiak.freebloks.preferences.MiscFragment.java
de.saschahlusiak.freebloks.preferences.StatisticsFragment.java
de.saschahlusiak.freebloks.preferences.ThemePreference.java
de.saschahlusiak.freebloks.stats.StatisticsActivity.java
de.saschahlusiak.freebloks.stats.StatisticsAdapter.java
de.saschahlusiak.freebloks.view.BackgroundRenderer.java
de.saschahlusiak.freebloks.view.BoardRenderer.java
de.saschahlusiak.freebloks.view.Freebloks3DView.java
de.saschahlusiak.freebloks.view.FreebloksRenderer.java
de.saschahlusiak.freebloks.view.SimpleModel.java
de.saschahlusiak.freebloks.view.effects.AbsEffect.java
de.saschahlusiak.freebloks.view.effects.AbsStoneEffect.java
de.saschahlusiak.freebloks.view.effects.BoardStoneGlowEffect.java
de.saschahlusiak.freebloks.view.effects.EffectSet.java
de.saschahlusiak.freebloks.view.effects.Effect.java
de.saschahlusiak.freebloks.view.effects.PhysicalStoneEffect.java
de.saschahlusiak.freebloks.view.effects.StoneFadeEffect.java
de.saschahlusiak.freebloks.view.effects.StoneRollEffect.java
de.saschahlusiak.freebloks.view.effects.StoneUndoEffect.java
de.saschahlusiak.freebloks.view.model.Board.java
de.saschahlusiak.freebloks.view.model.CurrentStone.java
de.saschahlusiak.freebloks.view.model.Intro.java
de.saschahlusiak.freebloks.view.model.Sounds.java
de.saschahlusiak.freebloks.view.model.Theme.java
de.saschahlusiak.freebloks.view.model.ViewElement.java
de.saschahlusiak.freebloks.view.model.ViewModel.java
de.saschahlusiak.freebloks.view.model.Wheel.java