Home Forums Support Serializing Objects Reply To: Serializing Objects



It’s tough to follow exactly what you are doing but a couple of notes:

1. Setting networkComms.net up like this can be dangerous as you are potentially trying to receive two different objects using the some incoming packetType.

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

2. Check your run time folder for potential error log messages.

3.  If you want to step through the conversion, incoming raw bytes are converted back to objects by the methods:


4. If you want the server to simply be forwarding  data without the need for the server to understand it it will be much more efficient to use the following structure. The main advantage of this approach is that you never have to change anything on the server as you add new classes to the clients.

Declare the following objects:

public class ClientObject
    public string ClientMessage { get; set; }

    private ClientObject() { }

    public ClientObject(string clientMessage)
        this.ClientMessage = clientMessage;

public class ServerForward
    public string ClientIPAddress { get; set; }

    public int ClientPort { get; set; }

    public string PacketType { get; set; }

    public byte[] ForwardObjectBytes { get; set; }

    private ServerForward() { }

    public ServerForward(string clientIPAddress, int clientPort, string packetType, byte[] forwardObjectBytes)
        this.ClientIPAddress = clientIPAddress;
        this.ClientPort = clientPort;
        this.PacketType = packetType;
        this.ForwardObjectBytes = forwardObjectBytes;

Configure the server with the following packet handler:

NetworkComms.AppendGlobalIncomingPacketHandler<ServerForward>("ServerForward", (connection, header, serverForward) =>
                    Console.WriteLine("Recieved server forward object from " + connection);
                    TCPConnection.GetConnection(new ConnectionInfo(serverForward.ClientIPAddress, serverForward.ClientPort)).SendObject(serverForward.PacketType, serverForward.ForwardObjectBytes);

The sending client uses the following:

ClientObject clientObject = new ClientObject("this is a forwarded client object");
            ServerForward serverForward = new ServerForward("",10000, "ClientObjectRaw", DPSManager.GetDataSerializer<ProtobufSerializer>().SerialiseDataObject<ClientObject>(clientObject).ThreadSafeStream.ToArray());
            TCPConnection.GetConnection(new ConnectionInfo(serverIP, serverPort)).SendObject("ServerForward", serverForward);

The receiving client uses the following:

NetworkComms.AppendGlobalIncomingPacketHandler<byte[]>("ClientObjectRaw", (connection, header, rawBytes) =>
                    ClientObject incomingClientObject = DPSManager.GetDataSerializer<ProtobufSerializer>().DeserialiseDataObject<ClientObject>(rawBytes);
                    Console.WriteLine("Server forwarded client object which says + " incomingClientObject.ClientMessage);