Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.freeone3000.android.elchat2.networklib;
- import android.app.Service;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.IBinder;
- import com.freeone3000.android.elchat2.Client;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.Socket;
- import java.util.concurrent.*;
- /**
- * Create a new instance, sendLogin(), invoke on a new thread, interrupt when needed.
- */
- public class NetworkLibrary extends Service implements Callable<Void> {
- private final PriorityBlockingQueue<Packet> sendQueue = new PriorityBlockingQueue<Packet>();
- private Socket connection;
- private OutputStream output;
- private final NetworkReader reader = new NetworkReader();
- private Thread readThread;
- private Future<Void> thisFuture;
- @Override
- public void onCreate() {
- SharedPreferences pref = getSharedPreferences(Client.DATA_STORE, MODE_PRIVATE);
- String host = pref.getString("host", "game.eternal-lands.com");
- int port = pref.getInt("port", 2001);
- try {
- connection = new Socket(host, port);
- output = connection.getOutputStream();
- reader.setInput(connection.getInputStream());
- readThread = new Thread(reader, "NetworkReader");
- readThread.start();
- } catch(IOException ioe) {
- throw new RuntimeException("", ioe);
- }
- }
- @Override
- public void onStart(Intent intent, int startId) {
- ExecutorService ex = Executors.newSingleThreadExecutor();
- thisFuture = ex.submit(this);
- }
- /**
- * Main loop, queue-runner
- */
- public Void call() throws IOException {
- SharedPreferences pref = getSharedPreferences(Client.DATA_STORE, MODE_PRIVATE);
- String user = pref.getString("username", "");
- String pass = pref.getString("password", "");
- sendLogin(user, pass);
- while(!Thread.interrupted()) {
- Packet p;
- try {
- p = sendQueue.take();
- } catch(InterruptedException ie) {
- return null;
- }
- sendPacket(p);
- }
- return null;
- }
- private void sendLogin(final String username, final String password) throws IOException {
- sendQueue.put(new Packet(PacketTypes.LOG_IN, username + " " + password));
- }
- @Override
- public void onDestroy() {
- if(readThread != null) {
- readThread.interrupt();
- }
- if(connection != null) {
- try {
- connection.close();
- } catch(IOException ioe) {
- throw new RuntimeException("", ioe);
- }
- }
- if(thisFuture != null) {
- thisFuture.cancel(true);
- }
- }
- private void sendPacket(final Packet packet) throws IOException {
- try {
- sendPacket(packet.getType(), packet.getData().getBytes("US-ASCII"));
- } catch(UnsupportedEncodingException uee) {
- throw new RuntimeException("US-ASCII must be a valid charset!", uee);
- }
- }
- private void sendPacket(final byte type, final byte[] payload) throws IOException {
- byte[] dat = new byte[payload.length + 4]; //+1 for type, +2 for size, +1 for trailing '\0'
- dat[0] = type;
- int size = payload.length + 2; //+1 for header, +1 for trailing 0
- dat[1] = (byte)(size & 0xFF);
- dat[2] = (byte)((size >>> 8) & 0xFF);
- System.arraycopy(payload, 0, dat, 3, payload.length);
- output.write(dat);
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null; //TODO Implement
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement