Package voxo.client.threads

Source Code of voxo.client.threads.NetworkReceiver$PacketParser

package voxo.client.threads;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.EnumSet;

import voxo.client.actions.ServerErrorAction;
import voxo.client.controllers.ClientController;
import voxo.common.entities.Packet;
import voxo.common.enums.EnumVerbose;

public class NetworkReceiver implements Runnable {

  private ClientController  c;
  private boolean        go;

  public NetworkReceiver(ClientController c) {
    this.c = c;
    this.go = true;
  }

  @Override
  public void run() {
    ServerSocket srvSock = null;
    Socket sock = null;
    InputStream in = null;
    ObjectInputStream oin = null;

    try {
      // Open Server Socket (Before loop to avoid reopening)
      srvSock = new ServerSocket(53335);

      while (go) {
        try {
          srvSock.setReuseAddress(true);
          sock = srvSock.accept();
          in = sock.getInputStream();
          oin = new ObjectInputStream(in);

          Packet p = (Packet) oin.readObject();

          // Close all the readers and the socket instance to be ready
          // for next packet
          new PacketParser(p).start();
         
          oin.close();
          oin = null;
          in.close();
          in = null;
          sock.close();
          sock = null;

        } catch (Exception e) {
          if (oin != null) {
            oin.close();
            oin = null;
          }
          if (in != null) {
            in.close();
            in = null;
          }
          if (sock != null) {
            sock.close();
            sock = null;
          }
          c.getVerbose().addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
          new ServerErrorAction(c, "Erreur de communication au serveur.");
        }

      }
    } catch (IOException e1) {
      c.getVerbose().addConsoleMsg(e1, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
      new ServerErrorAction(c, "Erreur de communication au serveur.");
    }
    if (oin != null) {
      try {
        oin.close();
      } catch (IOException e) {
        c.getVerbose().addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
      }
      oin = null;
    }
    if (in != null) {
      try {
        in.close();
      } catch (IOException e) {
        c.getVerbose().addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
      }
      in = null;
    }
    if (sock != null) {
      try {
        sock.close();
      } catch (IOException e) {
        c.getVerbose().addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
      }
      sock = null;
    }

  }
 
  class PacketParser extends Thread {
    private Packet p;
   
    public PacketParser(Packet p) {
      this.p = p;
    }
   
    @Override
    public void run() {
      c.parsePacket(p);
    }
  }

  public boolean isGo() {
    return go;
  }

  public void setGo(boolean go) {
    this.go = go;
  }

}
TOP

Related Classes of voxo.client.threads.NetworkReceiver$PacketParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.