[solved] Sending/Receiving a class (CustomPacket extends Packet)

Hey JGO, lastnight I was goofing around with networking and I wanted to write a project that can send custom packets accross a network, I didn’t get very far lol.

Here’s what I tried doing
:


public class StandardPacket implements Serializable {

      public StandardPacket() { 
      }

}

I was trying to accomplish sending a CustomPacket that extends the serializable StandardPacket but it wouldn’t go through.

When the client/server got the packet it wouldn’t allow my casting:
org.gnetwork.client.packets.TestPacket cannot be cast to org.gnetwork.server.packets.TestPacket.

Questions:

  • How did kryonet go about sending custom packets?
  • How would I go about having a class extend my StandardPacket and be able to send it over the network and have it be received as a CustomPacket vs StandardPacket?
  • Am I following the right path for doing this?

Example of my CustomPacket class:


public class CustomPacket extends StandardPacket {

      private String someCustomData;

      public CustomPacket() {
      }

      /* Getters & Setters for data */

}

Thanks for any help/feedback JGO, if more information is needed please ask.

Here’s how I setup my network. The base class packet stores it’s Type with an enum, and then stores the data as a string. Then, when data is sent across the network, the packet is reconstructed based on it’s header (ie. use a switch case for the enum). Any classes extending the packet class can have any methods they want, but the core is the same.

package game.client;

import java.net.DatagramPacket;

public class Packet {
	
	public enum Type {
		
		INVALID(-0x01),
		LOGIN_REQUEST(0x00),
		CHAT_SUBMIT(0x01),
		CHAT_BROADCAST(0x02),
		LOGIN_RESPONSE(0x03),
		PROFILE(0x04),
		USER_SNAPSHOT(0x05),
		GAME_MAP(0x06);
		
		int header;
		
		Type(int header) {
			this.header = header;
		}
		
		public byte getHeader() {
			return (byte) header;
		}
		
		public static Type fromHeader(int header) {
			for (Type t : values()) {
				if (t.header == header) {
					return t;
				}
			}
			return Type.INVALID;
		}
		
	}
	
	private Type type;
	protected String data;
	
	public Packet(Type type, String data) {
		this.type = type;
		this.data = data;
	}
	
	public byte getHeader() {
		return type.getHeader();
	}
	
	public String getData() {
		return data.trim();
	}
	
	public byte[] getFull() {
		byte[] output = new byte[data.getBytes().length + 1];
		output[0] = getHeader();
		for (int index = 0; index < data.getBytes().length; index++) {
			output[index + 1] = data.getBytes()[index];
		}
		return output;
	}
	
	public DatagramPacket getDatagram() {
		byte[] data = getFull();
		return new DatagramPacket(data, data.length);
	}
	
	public static Packet fromDatagram(DatagramPacket datagram) {
		Type type = Type.fromHeader(datagram.getData()[0]);
		byte[] string = new byte[datagram.getData().length - 1];
		for (int index = 1; index < datagram.getData().length; index++) {
			string[index - 1] = datagram.getData()[index];
		}
		String data = new String(string);
		return new Packet(type, data);
	}
	
}

Hope this helps :slight_smile:

CopyableCougar4

take a look into the KryoSerialization class : https://github.com/EsotericSoftware/kryonet/blob/master/src/com/esotericsoftware/kryonet/KryoSerialization.java.

[icode]kryo.register(class)[/icode] or [icode]kryo.register(class, serialiser)[/icode] seems to be what you’re looking for.

have not tried it by myself, but kryo-net is all about kryo-serialisation. i bet there is an easy way to handle that issue.