Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package com.freeone3000.android.elchat2.networklib;
  2.  
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.IBinder;
  7. import com.freeone3000.android.elchat2.Client;
  8.  
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import java.io.UnsupportedEncodingException;
  12. import java.net.Socket;
  13. import java.util.concurrent.*;
  14.  
  15. /**
  16.  * Create a new instance, sendLogin(), invoke on a new thread, interrupt when needed.
  17.  */
  18. public class NetworkLibrary extends Service implements Callable<Void> {
  19.     private final PriorityBlockingQueue<Packet> sendQueue = new PriorityBlockingQueue<Packet>();
  20.  
  21.     private Socket connection;
  22.     private OutputStream output;
  23.     private final NetworkReader reader = new NetworkReader();
  24.     private Thread readThread;
  25.     private Future<Void> thisFuture;
  26.  
  27.     @Override
  28.     public void onCreate() {
  29.         SharedPreferences pref = getSharedPreferences(Client.DATA_STORE, MODE_PRIVATE);
  30.         String host = pref.getString("host", "game.eternal-lands.com");
  31.         int port = pref.getInt("port", 2001);
  32.  
  33.         try {
  34.             connection = new Socket(host, port);
  35.             output = connection.getOutputStream();
  36.             reader.setInput(connection.getInputStream());
  37.             readThread = new Thread(reader, "NetworkReader");
  38.             readThread.start();
  39.         } catch(IOException ioe) {
  40.             throw new RuntimeException("", ioe);
  41.         }
  42.     }
  43.  
  44.     @Override
  45.     public void onStart(Intent intent, int startId) {
  46.         ExecutorService ex = Executors.newSingleThreadExecutor();
  47.         thisFuture = ex.submit(this);
  48.     }
  49.  
  50.     /**
  51.      * Main loop, queue-runner
  52.      */
  53.     public Void call() throws IOException {
  54.         SharedPreferences pref = getSharedPreferences(Client.DATA_STORE, MODE_PRIVATE);
  55.         String user = pref.getString("username", "");
  56.         String pass = pref.getString("password", "");
  57.         sendLogin(user, pass);
  58.  
  59.         while(!Thread.interrupted()) {
  60.             Packet p;
  61.             try {
  62.                 p = sendQueue.take();
  63.             } catch(InterruptedException ie) {
  64.                 return null;
  65.             }
  66.             sendPacket(p);
  67.         }
  68.         return null;
  69.     }
  70.  
  71.     private void sendLogin(final String username, final String password) throws IOException {
  72.         sendQueue.put(new Packet(PacketTypes.LOG_IN, username + " " + password));
  73.     }
  74.  
  75.     @Override
  76.     public void onDestroy() {
  77.         if(readThread != null) {
  78.             readThread.interrupt();
  79.         }
  80.         if(connection != null) {
  81.             try {
  82.                 connection.close();
  83.             } catch(IOException ioe) {
  84.                 throw new RuntimeException("", ioe);
  85.             }
  86.         }
  87.         if(thisFuture != null) {
  88.             thisFuture.cancel(true);
  89.         }
  90.     }
  91.  
  92.     private void sendPacket(final Packet packet) throws IOException {
  93.         try {
  94.             sendPacket(packet.getType(), packet.getData().getBytes("US-ASCII"));
  95.         } catch(UnsupportedEncodingException uee) {
  96.             throw new RuntimeException("US-ASCII must be a valid charset!", uee);
  97.         }
  98.     }
  99.  
  100.     private void sendPacket(final byte type, final byte[] payload) throws IOException {
  101.         byte[] dat = new byte[payload.length + 4]; //+1 for type, +2 for size, +1 for trailing '\0'
  102.         dat[0] = type;
  103.         int size = payload.length + 2; //+1 for header, +1 for trailing 0
  104.         dat[1] = (byte)(size & 0xFF);
  105.         dat[2] = (byte)((size >>> 8) & 0xFF);
  106.         System.arraycopy(payload, 0, dat, 3, payload.length);
  107.         output.write(dat);
  108.     }
  109.  
  110.     @Override
  111.     public IBinder onBind(Intent intent) {
  112.         return null;  //TODO Implement
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement