Trying to deserialize more than 1 object at the same time

南楼画角 提交于 2019-12-25 17:16:10

问题


Im trying to send some object from a server to the client. My problem is that when im sending only 1 object, everything works correctly. But at the moment i add another object an exception is thrown - "binary stream does not contain a valid binaryheader" or "No map for object (random number)". My thoughts are that the deserialization does not understand where the stream starts / ends and i hoped that you guys can help me out here.

heres my deserialization code:

public void Listen()
        {
            try
            {
                bool offline = true;
                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(() => offline = Offline));
                while (!offline)
                {
                    TcpObject tcpObject = new TcpObject();
                    IFormatter formatter = new BinaryFormatter();
                    tcpObject = (TcpObject)formatter.Deserialize(serverStream);

                    if (tcpObject.Command == Command.Transfer)
                    {
                        SentAntenna sentAntenna = (SentAntenna)tcpObject.Object;

                        int idx = 0;
                        foreach (string name in SharedProperties.AntennaNames)
                        {
                            if (name == sentAntenna.Name)
                                break;
                            idx++;
                        }

                        if (idx < 9)
                        {
                            PointCollection pointCollection = new PointCollection();
                            foreach (Frequency f in sentAntenna.Frequencies)
                                pointCollection.Add(new Point(f.Channel, f.Intensity));

                            SharedProperties.AntennaPoints[idx] = pointCollection;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); // raise an event
            }
        }

serialization code:

case Command.Transfer:
                            Console.WriteLine("Transfering");
                            Thread transfer = new Thread(new ThreadStart(delegate
                            {
                                try
                                {
                                    string aName = tcpObject.Object.ToString();
                                    int indx = 0;
                                    foreach (string name in names)
                                    {
                                        if (name == aName)
                                            break;
                                        indx++;
                                    }

                                    if (indx < 9)
                                    {
                                        while (true) // need to kill when the father thread terminates
                                        {
                                            if (antennas[indx].Frequencies != null)
                                            {
                                                lock (antennas[indx].Frequencies)
                                                {
                                                    TcpObject sendTcpObject = new TcpObject();
                                                    sendTcpObject.Command = Command.Transfer;
                                                    SentAntenna sa = new SentAntenna(antennas[indx].Frequencies, aName);
                                                    sendTcpObject.Object = sa;
                                                    formatter.Serialize(networkStream, sendTcpObject);
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex) { Console.WriteLine(ex); }
                            }));
                            transfer.Start();
                            break;

回答1:


Interesting. There's nothing particularly odd in your serialization code, and I've seen people use vanilla concatenation for multiple objects in the past, although I've actually always advised against it as BinaryFormatter does not explicitly claim this scenario is OK. But: if it isn't, the only thing I can suggest is to implement your own framing; so your write code becomes:

  • serialize to an empty MemoryStream
  • note the length and write the length to the NetworkStream, for example as a simple fixed-width 32-bit network-byte-order integer
  • write the payload from the MemoryStream to the NetworkStream
  • rinse, repeat

And the read code becomes:

  • read exactly 4 bytes and compute the length
  • buffer that many bytes into a MemoryStream
  • deserialize from the NetworkStream

(Noting in both cases to set the MemoryStream's position back to 0 between write and read)

You can also implement a Stream-subclass that caps the length if you want to avoid a buffer when reading, bit that is more complex.




回答2:


apperantly i came up with a really simple solution. I just made sure only 1 thread is allowed to transfer data at the same time so i changed this line of code:

formatter.Serialize(networkStream, sendTcpObject);

to these lines of code:

if (!transfering) // making sure only 1 thread is transfering data
{
    transfering = true;
    formatter.Serialize(networkStream, sendTcpObject);
    transfering = false;
}


来源:https://stackoverflow.com/questions/13787723/trying-to-deserialize-more-than-1-object-at-the-same-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!