Mekanism-tilera-Edition/common/mekanism/client/voice/VoiceClient.java

106 lines
2.3 KiB
Java
Raw Normal View History

2013-10-14 20:08:15 +02:00
package mekanism.client.voice;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
2013-10-27 04:55:11 +01:00
import java.net.ConnectException;
2013-10-14 20:08:15 +02:00
import java.net.Socket;
import javax.sound.sampled.AudioFormat;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2013-10-14 20:08:15 +02:00
@SideOnly(Side.CLIENT)
2013-10-24 23:14:09 +02:00
public class VoiceClient extends Thread
2013-10-14 20:08:15 +02:00
{
public Socket socket;
2013-10-24 23:14:09 +02:00
public String ip;
public int port;
2013-10-14 20:08:15 +02:00
public AudioFormat format = new AudioFormat(11025.0F, 8, 1, true, true);
public VoiceInput inputThread;
public VoiceOutput outputThread;
public DataInputStream input;
public DataOutputStream output;
public boolean running;
2013-10-24 23:14:09 +02:00
public VoiceClient(String s, int i)
{
ip = s;
port = i;
}
@Override
public void run()
2013-10-14 20:08:15 +02:00
{
System.out.println("[Mekanism] VoiceServer: Starting client connection...");
2013-10-14 20:08:15 +02:00
try {
socket = new Socket(ip, port);
running = true;
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
(outputThread = new VoiceOutput(this)).start();
(inputThread = new VoiceInput(this)).start();
2013-10-24 23:14:09 +02:00
System.out.println("[Mekanism] VoiceServer: Successfully connected to server.");
2013-10-27 04:55:11 +01:00
} catch(ConnectException e) {
System.err.println("[Mekanism] VoiceServer: Server's VoiceServer is disabled.");
2013-10-14 20:08:15 +02:00
} catch(Exception e) {
System.err.println("[Mekanism] VoiceServer: Error while starting client connection.");
2013-10-14 20:08:15 +02:00
e.printStackTrace();
}
}
2013-10-24 23:14:09 +02:00
public void disconnect()
2013-10-14 20:08:15 +02:00
{
System.out.println("[Mekanism] VoiceServer: Stopping client connection...");
2013-10-14 20:08:15 +02:00
try {
try {
inputThread.interrupt();
outputThread.interrupt();
} catch(Exception e) {}
2013-10-14 20:08:15 +02:00
2013-10-24 23:14:09 +02:00
try {
interrupt();
} catch(Exception e) {}
try {
inputThread.close();
outputThread.close();
} catch(Exception e) {}
2013-10-14 20:08:15 +02:00
try {
output.flush();
output.close();
output = null;
} catch(Exception e) {}
try {
input.close();
input = null;
} catch(Exception e) {}
try {
socket.close();
socket = null;
} catch(Exception e) {}
2013-10-14 20:08:15 +02:00
running = false;
} catch(Exception e) {
System.err.println("[Mekanism] VoiceServer: Error while stopping client connection.");
2013-10-14 20:08:15 +02:00
e.printStackTrace();
}
}
}