Added helper methods to connect()
Refactored packet handlers
This commit is contained in:
+5
-26
@@ -1,32 +1,11 @@
|
||||
|
||||
package com.github.manevolent.ts3j.protocol.client;
|
||||
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.PacketHandler;
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.client.*;
|
||||
import com.github.manevolent.ts3j.protocol.socket.client.TeamspeakClientSocket;
|
||||
import org.apache.commons.lang.reflect.ConstructorUtils;
|
||||
|
||||
public enum ClientConnectionState {
|
||||
CONNECTING(LocalClientHandlerConnecting.class),
|
||||
RETRIEVING_DATA(LocalClientHandlerRetrievingData.class),
|
||||
CONNECTED(LocalClientHandlerConnected.class),
|
||||
DISCONNECTED(LocalClientHandlerDisconnected.class);
|
||||
CONNECTING(),
|
||||
RETRIEVING_DATA(),
|
||||
CONNECTED(),
|
||||
DISCONNECTED();
|
||||
|
||||
private final Class<? extends PacketHandler> clazz;
|
||||
|
||||
ClientConnectionState(Class<? extends PacketHandler> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public Class<? extends PacketHandler> getHandlerClass() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
public PacketHandler createHandler(TeamspeakClientSocket client) {
|
||||
try {
|
||||
return (PacketHandler) ConstructorUtils.invokeConstructor(clazz, client);
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
ClientConnectionState() {}
|
||||
}
|
||||
+16
-4
@@ -24,6 +24,7 @@ import com.github.manevolent.ts3j.util.QuickLZ;
|
||||
import com.github.manevolent.ts3j.util.Ts3Crypt;
|
||||
import com.github.manevolent.ts3j.util.Ts3Debugging;
|
||||
import com.github.manevolent.ts3j.util.Pair;
|
||||
import org.apache.commons.lang.reflect.ConstructorUtils;
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
|
||||
import java.io.EOFException;
|
||||
@@ -144,6 +145,8 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
return packetStatistics.get(kind);
|
||||
}
|
||||
|
||||
protected abstract Class<? extends PacketHandler> getHandlerClass(ClientConnectionState state);
|
||||
|
||||
protected void start() {
|
||||
Thread networkThread = new Thread(networkReader);
|
||||
networkThread.setDaemon(true);
|
||||
@@ -195,12 +198,18 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
if (handler != null)
|
||||
((LocalClientHandler) handler).handleConnectionStateChanging(state);
|
||||
|
||||
if (handler == null || handler.getClass() != state.getHandlerClass()) {
|
||||
Ts3Debugging.debug("Assigning " + state.getHandlerClass().getName() + " handler...");
|
||||
Class<? extends PacketHandler> handlerClass = getHandlerClass(state);
|
||||
|
||||
setHandler(state.createHandler(this));
|
||||
if (handler == null || handler.getClass() != handlerClass) {
|
||||
Ts3Debugging.debug("Assigning " + handlerClass + " handler...");
|
||||
|
||||
Ts3Debugging.debug("Assigned " + state.getHandlerClass().getName() + " handler.");
|
||||
try {
|
||||
setHandler(createHandler(handlerClass));
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Ts3Debugging.debug("Assigned " + handlerClass + " handler.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1430,4 +1439,7 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
}
|
||||
}
|
||||
|
||||
private PacketHandler createHandler(Class<? extends PacketHandler> clazz) throws ReflectiveOperationException {
|
||||
return (PacketHandler) ConstructorUtils.invokeConstructor(clazz, this);
|
||||
}
|
||||
}
|
||||
|
||||
+29
@@ -17,6 +17,11 @@ import com.github.manevolent.ts3j.protocol.client.ClientConnectionState;
|
||||
import com.github.manevolent.ts3j.protocol.header.PacketHeader;
|
||||
import com.github.manevolent.ts3j.protocol.packet.PacketBody0Voice;
|
||||
import com.github.manevolent.ts3j.protocol.packet.PacketBody2Command;
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.PacketHandler;
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnected;
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnecting;
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerDisconnected;
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerRetrievingData;
|
||||
import com.github.manevolent.ts3j.protocol.packet.statistics.PacketStatistics;
|
||||
import com.github.manevolent.ts3j.protocol.packet.transformation.InitPacketTransformation;
|
||||
import com.github.manevolent.ts3j.util.HighPrecisionRecurrentTask;
|
||||
@@ -170,6 +175,22 @@ public class LocalTeamspeakClientSocket
|
||||
return this.listeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<? extends PacketHandler> getHandlerClass(ClientConnectionState state) {
|
||||
switch (state) {
|
||||
case DISCONNECTED:
|
||||
return LocalClientHandlerDisconnected.class;
|
||||
case CONNECTED:
|
||||
return LocalClientHandlerConnected.class;
|
||||
case RETRIEVING_DATA:
|
||||
return LocalClientHandlerRetrievingData.class;
|
||||
case CONNECTING:
|
||||
return LocalClientHandlerConnecting.class;
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NetworkPacket readNetworkPacket(int timeout) throws
|
||||
IOException,
|
||||
@@ -273,6 +294,14 @@ public class LocalTeamspeakClientSocket
|
||||
remote = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a connection to a server
|
||||
* @param hostname Hostname to contact
|
||||
* @param timeout timeout, in milliseconds, to complete a connection.
|
||||
*/
|
||||
public void connect(String hostname, long timeout) throws IOException, TimeoutException {
|
||||
connect(hostname, null, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a connection to a server
|
||||
|
||||
@@ -30,13 +30,10 @@ public class ServerConnectionTest {
|
||||
client.setNickname("ts3j issue 2");
|
||||
client.setHWID("TestTestTest");
|
||||
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
|
||||
client.connect(
|
||||
new InetSocketAddress("178.32.109.167", 11079),
|
||||
null,
|
||||
"teamlixo.net",
|
||||
10000L
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user