Android Open Source - ConnectedSpace Network Handler






From Project

Back to project page ConnectedSpace.

License

The source code is released under:

Copyright (c) 2015, Shreyas Raman <skraman1999@gmail.com>.

If you think the Android project ConnectedSpace 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 me.shreyasr.connected.network;
//w  w w .j a v a2s.  c  om
import com.badlogic.gdx.Gdx;

import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;

import me.shreyasr.connected.ConnectedSpace;
import me.shreyasr.connected.entities.Laser;

public class NetworkHandler implements Runnable {

    ConnectedSpace game;
    Socket socket;

    public NetworkHandler(Socket socket) {
        this.socket = socket;
    }

    public void setGame(ConnectedSpace game) {
        this.game = game;
    }

    @Override
    public void run() {
        byte[] arr = new byte[8];
        ByteBuffer buffer = ByteBuffer.wrap(arr);
        while (true) {
            try {
                Thread.sleep(16);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            buffer.rewind();
            int numRead = -1;
            try {
                numRead = socket.getInputStream().read(arr);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (numRead != 8)
                continue;
            double lx = buffer.getDouble();
            if (game != null)
                game.addLasers.add(new Laser((1-lx)*Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false));
        }
    }

    public void send(Laser laser) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putDouble(laser.x / Gdx.graphics.getWidth());
        try {
            socket.getOutputStream().write(buffer.array());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




Java Source Code List

me.shreyasr.connected.ConnectedSpace.java
me.shreyasr.connected.android.AndroidLauncher.java
me.shreyasr.connected.android.activity.ClientListAdapter.java
me.shreyasr.connected.android.activity.LobbyActivity.java
me.shreyasr.connected.android.networking.BroadcastListenerThread.java
me.shreyasr.connected.android.networking.ConnectionThread.java
me.shreyasr.connected.android.networking.HeartbeatThread.java
me.shreyasr.connected.android.networking.LobbyManager.java
me.shreyasr.connected.android.networking.ServerSocketThread.java
me.shreyasr.connected.android.util.AndroidAddressUtils.java
me.shreyasr.connected.android.util.Client.java
me.shreyasr.connected.desktop.DesktopLauncher.java
me.shreyasr.connected.entities.Laser.java
me.shreyasr.connected.entities.Star.java
me.shreyasr.connected.network.NetworkHandler.java
me.shreyasr.connected.util.MathHelper.java
me.shreyasr.connected.util.SpriteSheet.java