Skip to content

Instantly share code, notes, and snippets.

@Ge-lx
Created November 12, 2014 14:12
Show Gist options
  • Save Ge-lx/0a40ca73b3382751651c to your computer and use it in GitHub Desktop.
Save Ge-lx/0a40ca73b3382751651c to your computer and use it in GitHub Desktop.
ProtocolLib test
package test;
import io.github.gelx_.protocollib.ProtocolConnection;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
/**
* Created by Gelx on 12.11.2014.
*/
public class Test {
public static void main(String... args){
ProtocolConnection server = null, client = null;
try {
server = new ProtocolConnection(new TestProtocol(), new InetSocketAddress("127.0.0.1", 12345), true);
client = new ProtocolConnection(new TestProtocol(), new InetSocketAddress("127.0.0.1", 12345), false);
} catch (IOException e) {
e.printStackTrace();
}
SocketAddress address = client.getRemoteAddress();
System.out.println("Yay!");
System.out.println(((TestProtocol.TestPacketResponse) client.queryPacket(new TestProtocol.TestPacket(client.getRemoteAddress(), "Hello, this is lowercase")) ).getMessage());
}
}
package test;
import io.github.gelx_.protocollib.ProtocolConnection;
import io.github.gelx_.protocollib.protocol.Packet;
import io.github.gelx_.protocollib.protocol.PacketHandler;
import io.github.gelx_.protocollib.protocol.Protocol;
/**
* Created by Gelx on 12.11.2014.
*/
public class TestHandler extends PacketHandler {
public TestHandler(Protocol protocol, ProtocolConnection connection) {
super(protocol, connection);
}
public void handleTestPacket(Packet packet){
TestProtocol.TestPacket testPacket = (TestProtocol.TestPacket) packet;
String message = testPacket.getMessage().toUpperCase();
TestProtocol.TestPacketResponse response = new TestProtocol.TestPacketResponse(packet.getAddress(), message);
this.connection.sendPacket(response);
}
public void handleTestPacketResponse(Packet packet){
System.out.println("TestPacketResponse received!");
}
}
package test;
import io.github.gelx_.protocollib.protocol.Packet;
import io.github.gelx_.protocollib.protocol.PacketInfo;
import io.github.gelx_.protocollib.protocol.Protocol;
import java.net.SocketAddress;
import java.nio.charset.Charset;
import java.util.HashMap;
/**
* Created by Gelx on 12.11.2014.
*/
public class TestProtocol extends Protocol{
@PacketInfo(id = 1, receivable = true, response = TestPacketResponse.class)
public static class TestPacket extends Packet{
private String message;
public TestPacket(SocketAddress src, byte[] data){
super(src, data);
this.message = new String(data, Charset.defaultCharset()).trim();
}
public TestPacket(SocketAddress src, String message){
super(src);
this.message = message;
this.data = Charset.defaultCharset().encode(message).array();
}
@Override
public short getID() {
return 1;
}
public String getMessage(){
return message;
}
}
@PacketInfo(id = 2, receivable = true, response = TestPacketResponse.class)
public static class TestPacketResponse extends TestPacket{
public TestPacketResponse(SocketAddress src, byte[] data) {
super(src, data);
}
public TestPacketResponse(SocketAddress src, String message){
super(src, message);
}
@Override
public short getID(){
return 2;
}
}
public TestProtocol() {
super(TestHandler.class);
}
@Override
public HashMap<Short, Class<? extends Packet>> registerPackets() {
HashMap<Short, Class<? extends Packet>> map = new HashMap<Short, Class<? extends Packet>>();
map.put((short) 1, TestPacket.class);
map.put((short) 2, TestPacketResponse.class);
return map;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment