Home Forums Support Serializing Objects Reply To: Serializing Objects

#487
Anonymous
Inactive

Finally figured out how to do code in here.
I did the following and it works for declaring data as an object..

[ProtoContract]
 [ProtoInclude(10, typeof(ModulePacketHeader3))]
 public abstract class ModulePacketHeader3
 {
 public static ModulePacketHeader3 Create (T data, string to, string from, string packetType)
 {
 return new ModulePacketHeader3(data, to, from, packetType);
 }
public object Data
 {
 get { return DataImpl; }
 set { DataImpl = value; }
 }
protected abstract object DataImpl {get;set;}
 protected ModulePacketHeader3() { }
public string packetType { get; set; }
 public string from { get; set; }
 public string to { get; set; }
 }
[ProtoContract]
 public sealed class ModulePacketHeader3 : ModulePacketHeader3
 {
 public ModulePacketHeader3() { }
public ModulePacketHeader3(T data, string PacketType, string To, string From)
 { Data = data; packetType = PacketType; to = To; from = From; }
[ProtoMember(1)]
 public string packetType { get; set; }
 [ProtoMember(2)]
 public string from { get; set; }
 [ProtoMember(3)]
 public string to { get; set; }
 [ProtoMember(4)]
 public new T Data { get; set; }
protected override object DataImpl
 {
 get { return Data; }
 set { Data = (T)value; }
 }
}

Its a bit messy. On the client, I do this:

 

ModulePacketHeader3 myHeader = ModulePacketHeader3.Create(new ModulePacketType2_authRequest(this.moduleName, "ModuleManager", this.moduleAuthCode,
 this.moduleType), "ModuleManager", this.moduleName, "Auth-Request");
 myHeader.from = this.moduleName;
 myHeader.to = "ModuleManager";
 myHeader.packetType = "Auth-Request";


And it doesn’t give me an errors during serialization.

On the server side, I have:

// Incoming data callback
 NetworkComms.AppendGlobalIncomingPacketHandler("CustomObject", incomingData);
 NetworkComms.AppendGlobalIncomingPacketHandler("CustomObject", incomingData2);


and the following functions:

 private void incomingData(NetworkCommsDotNet.PacketHeader header, NetworkCommsDotNet.Connection connection, ModulePacketType2_authRequest packet)
 {
 Console.WriteLine("Testing..");
 }

 

private void incomingData2(NetworkCommsDotNet.PacketHeader header, NetworkCommsDotNet.Connection connection, ModulePacketHeader3 packet)
 {
 Console.WriteLine("Testing..");
 }

I put a breakpoint on each of the “Testing..” console writelines. When the packet comes in, it always calls the function for ModulePacketType2_authRequest and not ModulePacketHeader3. Plus all the variables are set to null. I checked the outgoing packet, and all the variables are set to their correct values.

But if I use the following code:

ModulePacketType2_authRequest authPacket =
 new ModulePacketType2_authRequest(this.moduleName, "ModuleManager", this.moduleAuthCode,
 this.moduleType);
myConnection.SendObject("CustomObject", authPacket);

Then I get exactly what I sent from the client to the server.

So I know that it’s not my processor or compressor that is at fault. It seems that if I use an abstract class so I can send a ModulePacketHeader3, the recieving code still thinks – because I’m using the type ModulePacketType2_authRequest – that it is an authRequest class and thus I end up with null values. I’m not sure where in the NetworkComm code this is occuring.

I went back to TriggerGlobalPacketHandlers and TriggerSpecificPacketHandlers. Right now unless anybody else has an ideas, the best way to not lose time is for me to modify one of those functions (probably the global one) so that any unknown packets will be sent to me if they do not match anything on the list.

Just wanted to post this to help others that may have this issue in the future. And I will post any code modifications I make for the same reasons.