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;
|
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 {
|
public enum ClientConnectionState {
|
||||||
CONNECTING(LocalClientHandlerConnecting.class),
|
CONNECTING(),
|
||||||
RETRIEVING_DATA(LocalClientHandlerRetrievingData.class),
|
RETRIEVING_DATA(),
|
||||||
CONNECTED(LocalClientHandlerConnected.class),
|
CONNECTED(),
|
||||||
DISCONNECTED(LocalClientHandlerDisconnected.class);
|
DISCONNECTED();
|
||||||
|
|
||||||
private final Class<? extends PacketHandler> clazz;
|
ClientConnectionState() {}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+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.Ts3Crypt;
|
||||||
import com.github.manevolent.ts3j.util.Ts3Debugging;
|
import com.github.manevolent.ts3j.util.Ts3Debugging;
|
||||||
import com.github.manevolent.ts3j.util.Pair;
|
import com.github.manevolent.ts3j.util.Pair;
|
||||||
|
import org.apache.commons.lang.reflect.ConstructorUtils;
|
||||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||||
|
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
@@ -144,6 +145,8 @@ public abstract class AbstractTeamspeakClientSocket
|
|||||||
return packetStatistics.get(kind);
|
return packetStatistics.get(kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract Class<? extends PacketHandler> getHandlerClass(ClientConnectionState state);
|
||||||
|
|
||||||
protected void start() {
|
protected void start() {
|
||||||
Thread networkThread = new Thread(networkReader);
|
Thread networkThread = new Thread(networkReader);
|
||||||
networkThread.setDaemon(true);
|
networkThread.setDaemon(true);
|
||||||
@@ -195,12 +198,18 @@ public abstract class AbstractTeamspeakClientSocket
|
|||||||
if (handler != null)
|
if (handler != null)
|
||||||
((LocalClientHandler) handler).handleConnectionStateChanging(state);
|
((LocalClientHandler) handler).handleConnectionStateChanging(state);
|
||||||
|
|
||||||
if (handler == null || handler.getClass() != state.getHandlerClass()) {
|
Class<? extends PacketHandler> handlerClass = getHandlerClass(state);
|
||||||
Ts3Debugging.debug("Assigning " + state.getHandlerClass().getName() + " handler...");
|
|
||||||
|
|
||||||
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.header.PacketHeader;
|
||||||
import com.github.manevolent.ts3j.protocol.packet.PacketBody0Voice;
|
import com.github.manevolent.ts3j.protocol.packet.PacketBody0Voice;
|
||||||
import com.github.manevolent.ts3j.protocol.packet.PacketBody2Command;
|
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.statistics.PacketStatistics;
|
||||||
import com.github.manevolent.ts3j.protocol.packet.transformation.InitPacketTransformation;
|
import com.github.manevolent.ts3j.protocol.packet.transformation.InitPacketTransformation;
|
||||||
import com.github.manevolent.ts3j.util.HighPrecisionRecurrentTask;
|
import com.github.manevolent.ts3j.util.HighPrecisionRecurrentTask;
|
||||||
@@ -170,6 +175,22 @@ public class LocalTeamspeakClientSocket
|
|||||||
return this.listeners.remove(listener);
|
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
|
@Override
|
||||||
protected NetworkPacket readNetworkPacket(int timeout) throws
|
protected NetworkPacket readNetworkPacket(int timeout) throws
|
||||||
IOException,
|
IOException,
|
||||||
@@ -273,6 +294,14 @@ public class LocalTeamspeakClientSocket
|
|||||||
remote = null;
|
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
|
* Initiates a connection to a server
|
||||||
|
|||||||
@@ -30,13 +30,10 @@ public class ServerConnectionTest {
|
|||||||
client.setNickname("ts3j issue 2");
|
client.setNickname("ts3j issue 2");
|
||||||
client.setHWID("TestTestTest");
|
client.setHWID("TestTestTest");
|
||||||
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
client.connect(
|
client.connect(
|
||||||
new InetSocketAddress("178.32.109.167", 11079),
|
"teamlixo.net",
|
||||||
null,
|
|
||||||
10000L
|
10000L
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user