I'm not exactly sure what the issue is here, but it seems like all of the data that I'm sending to Java is being corrupted. It works completely fine with a Java client and I've validated the Byte sizes of every primitive between the two languages.
I'll start with the C# aspect of the client, as I've made it very simple:
This is the PacketBuilder class.
/// <summary>
/// Used to build a instance of the <see cref="UnityNetworking.Packet"/> class.
/// </summary>
public class PacketBuilder {
/// <summary>
/// The opcode of the packet being built.
/// </summary>
private int Opcode;
/// <summary>
/// The stream to write data to the packet's buffer.
/// </summary>
private MemoryStream stream;
/// <summary>
/// The binary writer to convert data into binary format.
/// </summary>
private BinaryWriter writer;
/// <summary>
/// The String encoder.
/// </summary>
private Encoding encoder;
/// <summary>
/// Create a new PacketBuilder instance with the specified opcode and a default capacity.
/// </summary>
/// <param name="opcode">The opcode.</param>
public static PacketBuilder Create(int opcode) {
return new PacketBuilder (opcode, 512);
}
/// <summary>
/// Create a new PacketBuilder instance with the specified opcode and capacity.
/// </summary>
/// <param name="opcode">The opcode.</param>
/// <param name="capacity">The buffer capacity.</param>
public static PacketBuilder Create(int opcode, int capacity) {
return new PacketBuilder (opcode, capacity);
}
/// <summary>
/// Initializes a new instance of the <see cref="UnityNetworking.PacketBuilder"/> class.
/// </summary>
/// <remarks>Private scope to prevent outside library instantiation.</remarks>
private PacketBuilder (int opcode, int capactiy) {
Opcode = opcode;
stream = new MemoryStream (capactiy);
writer = new BinaryWriter (stream);
encoder = new UTF8Encoding (true, true);
}
/// <summary>
/// Adds the specified data to the builders buffer.
/// </summary>
/// <param name="data">The data.</param>
public PacketBuilder Add(object data) {
if (data is Int16) {
writer.Write ((short)data);
} else if (data is Int32) {
writer.Write ((int)data);
} else if (data is Int64) {
writer.Write ((long)data);
} else if (data is Single) {
writer.Write ((float)data);
} else if (data is Double) {
writer.Write ((double)data);
} else if (data is Byte) {
writer.Write ((byte)data);
} else if (data is Boolean) {
writer.Write ((bool)data);
} else if (data is String) {
string str = (string)data;
byte[] bytes = encoder.GetBytes (str);
writer.Write ((short)bytes.Length);
writer.Write (bytes, 0, bytes.Length);
} else {
throw new Exception ("Unsupported Object Type: " + data);
}
Debug.Log ("Data Type: " + data.GetType() + " || Value: " + data);
return this;
}
public Packet ToPacket() {
return new Packet(Opcode, stream.ToArray());
}
}
As you can see this class makes use of the MemoryStream
and BinaryWriter
for the example that I'm going to send I'm going to be using the long
value or more commonly referred to as Int64
in C#.
PacketBuilder builder = PacketBuilder.Create(1);
builder.Add((byte)1);
builder.Add(1337L);
client.Write(builder.ToPacket());
This is send through the Write(Packet)
method in the Client class, found here:
public NetworkClient Write(Packet packet) {
networkStream.Write (packet.Buffer, 0, packet.Size);
return this;
}
Very basic: Now on the Java end everything is being put into a ByteBuffer
which data is taken from using getX
methods.
The way that I would go about getting this data is the following:
public boolean handle(long userId, Session session, Packet packet) {
ByteBuffer buffer = packet.getBuffer();
byte opcode = buffer.get();
userId = buffer.getLong();
System.out.println("Opcode: " + opcode + " || User ID: " + userId);
}
This outputs the following line:
Opcode: 1 || User ID: 4108690235045445632
I don't understand what's going on at all, especially not how 1337 can turn into god only knows what that number is. When debugging from the client, the following information is presented:
Data Type: System.Byte || Value: 1
Data Type: System.Int64 || Value: 1337
So, if anyone can enlighten me as to WHAT IS GOING ON, that'd be nice.