Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetworkComms custom object error - "objectToSerialize must implement IExplicitlySerialize"

I get this error from the SendObject method when I try to serialize an object just as the NetworkComms example says to do it. This seems like something silly, but I can't figure out what I need to do, and I don't think implementing the IExplicitlySerialize interface is the answer. Here is my calling method and the serialized class:

public static void SendTestPacket()
        {
            var message = "This is a test packet";
            NetworkComms.SendObject("PacketPrintToConsole", "192.168.1.105", 5614, new PacketPrintToConsole(message));
        }


    [ProtoContract]
    public class PacketPrintToConsole
    {
        [ProtoMember(1)]
        public string Message { get; set; }


        public PacketPrintToConsole() { }

        public PacketPrintToConsole(string message)
        {
            this.Message = message;
        }

    }
like image 666
Taylor Hill Avatar asked Jul 27 '26 08:07

Taylor Hill


1 Answers

Maybe you already figured it out, but for everyone else finding this here (like me). Here´s the answer.

That error message tells you, that you have to define a serializer. You have an object you want to send, but you didn´t tell NetworkComms what serializer to use. So you can either implement IExplicitlySerialize in your object or use a serializer that has already been created for protobuf-net.

You need to use the NetworkCommsDotNet.DPSBase.ProtobufSerializer. You have to reference the ProtobufSerializer.dll compiled from the NetworkComms source you can get on github and then define the SendReceiveOptions.

Example:

SendReceiveOptions customSendReceiveOptions = new SendReceiveOptions<ProtobufSerializer>();
ConnectionInfo connectionInfo = new ConnectionInfo("192.168.1.105", 5614);
TCPConnection serverConnection = TCPConnection.GetConnection(connectionInfo, customSendReceiveOptions);

var message = "This is a test packet";
serverConnection.SendObject("PacketPrintToConsole", new PacketPrintToConsole(message));

This has only to be done where the object will be serialized to be sent. A receiving client just need´s to have protobuf-net with the same object class to deserialize into.

Example with SendReceiveObject to request a message.

SendReceiveOptions customSendReceiveOptions = new SendReceiveOptions<ProtobufSerializer>();
NetworkComms.AppendGlobalIncomingPacketHandler<int>("GetMessage", GetMessageRequest, customSendReceiveOptions);

The method to send the result:

private static void GetMessageRequest(PacketHeader packetheader, Connection connection, int incomingobject)
{
    connection.SendObject("MessageReply", new MessageObject(message));
}

And on the client side then:

ConnectionInfo connectionInfo = new ConnectionInfo("192.168.2.105", 5614);
TCPConnection serverConnection = TCPConnection.GetConnection(connectionInfo);
MessageObject myMessageObject = serverConnection.SendReceiveObject<ImageWrap>("GetMessage", "MessageReply", 1000);

if (myMessageObject != null)
{
    Console.WriteLine(myMessageObject.Message);
}
like image 99
Otto Römmich Avatar answered Jul 28 '26 21:07

Otto Römmich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!