Esempio n. 1
0
        private void SerializeCustom()
        {
            var s       = new CustomBinaryFormatter();
            var message = new CustomTypicalMessage()
            {
                StringProp = "hello",
                GuidProp   = Guid.NewGuid(),
                IntProp    = 123,
                DateProp   = DateTime.UtcNow,
            };

            s.Register <CustomTypicalMessage>(0);

            var stream = new MemoryStream();

            s.Serialize(stream, message);
            var bytes = stream.ToArray();

            RunTest("Greg Young Custom Serializer", () =>
            {
                var stream2 = new MemoryStream();
                s.Serialize(stream2, message);
            },
                    () =>
            {
                stream.Position = 0;
                s.Deserialize(stream);
            }, bytes.Length);
        }
Esempio n. 2
0
        public void Send(object o)
        {
            var m = new MemoryStream();

            lock (_clients)
            {
                _formatter.Serialize(m, o);
                byte[] data = m.ToArray();
                SendToClients(data, false);
            }
        }
Esempio n. 3
0
        private void SendFromQueue()
        {
            object message = null;

            lock (queue)
            {
                if (!IsSending && queue.Count > 0)
                {
                    message = queue.Dequeue();
                }
            }
            if (message != null)
            {
                try
                {
                    var m = new MemoryStream();
                    _formatter.Serialize(m, message);
                    var data        = m.ToArray();
                    var encodedData = Convert.ToBase64String(data);
                    var toSend      = new byte[encodedData.Length + 1];
                    Encoding.UTF8.GetBytes(encodedData, 0, encodedData.Length, toSend, 0);
                    _stream.BeginWrite(toSend, 0, toSend.Length, WriteCompleted, _stream);
                }
                catch (Exception ex)
                {
                    //let read reconnect
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public byte[] Serialize()
        {
            CustomBinaryFormatter bf = new CustomBinaryFormatter();
            MemoryStream          ms = new MemoryStream();

            bf.Serialize(ms, this);
            return(ms.ToArray());
        }
 private T serializeDeserialize <T>(T message)
 {
     using (var memStream = new MemoryStream())
     {
         _formatter.Serialize(memStream, message);
         memStream.Seek(0, SeekOrigin.Begin);
         return((T)_formatter.Deserialize(memStream));
     }
 }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sactanet"></param>
        /// <param name="sectorUcs"></param>
        /// <param name="ask"></param>
        void SendSectorization(int sactanet, string[] sectorUcs, ushort UserOrg)
        {
            Debug.Assert(sactanet == 0 || sactanet == 1);

            CustomBinaryFormatter bf = new CustomBinaryFormatter();
            MemoryStream          ms = new MemoryStream();
            SactaMsg msg             = new SactaMsg(SactaMsg.MsgType.Sectorization, _SeqNum, _SectVersion, sectorUcs);

            bf.Serialize(ms, msg);
            byte[] data = ms.ToArray();

            byte[] user = BitConverter.GetBytes(UserOrg);
            Array.Reverse(user);
            Array.Copy(user, 0, data, 6, 2);

            _Comm[sactanet].Send(_EndPoint[sactanet], data);
            MainForm.LogMethod("INFO", sactanet == 0 ? "SACTA1" : "SACTA2", String.Format("Enviando Sectorizacion V-{2} a SCV {0} ({1})", UserOrg, strSect(sectorUcs), _SectVersion));
        }
Esempio n. 7
0
        /// <summary>
        ///
        /// </summary>
        void SendPresence()
        {
            CustomBinaryFormatter bf = new CustomBinaryFormatter();

            MemoryStream ms  = new MemoryStream();
            SactaMsg     msg = new SactaMsg(SactaMsg.MsgType.Presence, _SeqNum++);

            bf.Serialize(ms, msg);
            byte[] data = ms.ToArray();

            foreach (string sUser in Settings.Default.ScvUsers)
            {
                byte[] user = BitConverter.GetBytes(UInt16.Parse(sUser));
                Array.Reverse(user);
                Array.Copy(user, 0, data, 6, 2);

                _Comm[0].Send(_EndPoint[0], data);
                _Comm[1].Send(_EndPoint[1], data);
            }

            _LastPresenceSended = DateTime.Now;
        }
Esempio n. 8
0
        public void ChatRoomInfosSerializationTest()
        {
            var original = new[]
            {
                new ChatRoomInfo {
                    IsPasswordProtected = true, Name = "r1"
                },
                new ChatRoomInfo {
                    IsPasswordProtected = false, Name = "r2"
                }
            };

            // Serializing
            var serializedData = CustomBinaryFormatter.Serialize(original);

            Assert.NotNull(serializedData);
            Assert.True(serializedData.Length > 0);

            // Deserializing
            var reconstructed = CustomBinaryFormatter.Deserialize(serializedData, 0, serializedData.Length);

            CollectionAssert.AreEqual(original, reconstructed, new ChatRoomInfoComparer());
        }
Esempio n. 9
0
        /// <summary>
        ///
        /// </summary>
        void SendInit()
        {
            CustomBinaryFormatter bf = new CustomBinaryFormatter();
            MemoryStream          ms = new MemoryStream();
            SactaMsg msg             = new SactaMsg(SactaMsg.MsgType.Init, SactaMsg.InitId);

            bf.Serialize(ms, msg);
            byte[] data = ms.ToArray();

            foreach (string sUser in Settings.Default.ScvUsers)
            {
                MainForm.LogMethod("INFO", String.Format("Enviado MSG Init a SCV {0}", sUser));

                byte[] user = BitConverter.GetBytes(UInt16.Parse(sUser));
                Array.Reverse(user);
                Array.Copy(user, 0, data, 6, 2);

                _Comm[0].Send(_EndPoint[0], data);
                _Comm[1].Send(_EndPoint[1], data);
            }

            _SeqNum = 0;
        }
Esempio n. 10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sectorUcs"></param>
        /// <param name="ask"></param>
        void SendSectorization(string[] sectorUcs, SactaMsg ask)
        {
            CustomBinaryFormatter bf = new CustomBinaryFormatter();

            MemoryStream ms  = new MemoryStream();
            SactaMsg     msg = new SactaMsg(SactaMsg.MsgType.Sectorization, _SeqNum++, _SectVersion++, sectorUcs);

            bf.Serialize(ms, msg);
            byte[] data = ms.ToArray();

            if (ask != null)
            {
                MainForm.LogMethod("INFO", String.Format("Enviando Sectorizacion a SCV {0} ({1})", ask.UserOrg, strSect(sectorUcs)));

                byte[] user = BitConverter.GetBytes(ask.UserOrg);
                Array.Reverse(user);
                Array.Copy(user, 0, data, 6, 2);

                _Comm[0].Send(_EndPoint[0], data);
                _Comm[1].Send(_EndPoint[1], data);
            }
            else
            {
                foreach (string sUser in Settings.Default.ScvUsers)
                {
                    MainForm.LogMethod("INFO", String.Format("Enviando Sectorizacion V-{0} a SCV {1} ({2})", ((SactaMsg.SectInfo)(msg.Info)).Version, sUser, strSect(sectorUcs)));

                    byte[] user = BitConverter.GetBytes(UInt16.Parse(sUser));
                    Array.Reverse(user);
                    Array.Copy(user, 0, data, 6, 2);

                    _Comm[0].Send(_EndPoint[0], data);
                    _Comm[1].Send(_EndPoint[1], data);
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        ///
        /// </summary>
        void SendInit(int sactanet)
        {
            Debug.Assert(sactanet == 0 || sactanet == 1);

            CustomBinaryFormatter bf = new CustomBinaryFormatter();
            MemoryStream          ms = new MemoryStream();
            SactaMsg msg             = new SactaMsg(SactaMsg.MsgType.Init, SactaMsg.InitId);

            bf.Serialize(ms, msg);
            byte[] data = ms.ToArray();

            foreach (string sUser in Settings.Default.ScvUsers)
            {
                byte[] user = BitConverter.GetBytes(UInt16.Parse(sUser));
                Array.Reverse(user);
                Array.Copy(user, 0, data, 6, 2);

                _Comm[sactanet].Send(_EndPoint[sactanet], data);
                MainForm.LogMethod("INFO", sactanet == 0 ? "SACTA1" : "SACTA2", String.Format("Enviado MSG Init a SCV {0}", sUser));

                //_Comm[1].Send(_EndPoint[1], data);
                //MainForm.LogMethod("INFO", "SACTA2", String.Format("Enviado MSG Init a SCV {0}", sUser));
            }
        }
Esempio n. 12
0
        internal static void AddRecords(List<HistoryRecord> records)
        {
            lock (SyncRoot)
            {
                using (var dataIndexStream = new FileStream(GetFile(true, true), FileMode.OpenOrCreate))
                {
                    using (var dataContentStream = new FileStream(GetFile(true, false), FileMode.OpenOrCreate))
                    {
                        var historyFormatter = new CustomBinaryFormatter(dataIndexStream, dataContentStream);
                        historyFormatter.Register<HistoryRecord>(1);

                        historyFormatter.MoveToEnd();
                        var historyCount = historyFormatter.Count;
                        records.ForEach(r =>
                        {
                            historyFormatter.Serialize<HistoryRecord>(r);
                        });

                        historyFormatter.Flush();

                        using (var dateIndexStream = new FileStream(GetFile(false, true), FileMode.OpenOrCreate))
                        {
                            using (var dateContentStream = new FileStream(GetFile(false, false), FileMode.OpenOrCreate))
                            {
                                var dateFormatter = new CustomBinaryFormatter(dateIndexStream, dateContentStream);
                                dateFormatter.Register<HistoryDate>(1);

                                var dateCount = dateFormatter.Count;
                                var today = DateTime.Now.Date.AddDays(1);
                                var yesterday = DateTime.Now.Date;
                                long todayIndex = -1;
                                var foundYesterday = false;
                                for (long i = 0; i < dateCount; i++)
                                {
                                    var date = dateFormatter.Deserialize<HistoryDate>(false);
                                    if (DateTime.Parse(date.Date) == today)
                                        todayIndex = i;
                                    if (DateTime.Parse(date.Date) == yesterday)
                                        foundYesterday = true;
                                }
                                if (!foundYesterday)
                                {
                                    dateFormatter.MoveToEnd();
                                    dateFormatter.Serialize<HistoryDate>(new HistoryDate { Date = yesterday.ToString(), Index = historyCount });
                                }

                                if (todayIndex != -1)
                                {
                                    dateFormatter.MoveTo(todayIndex);
                                    dateFormatter.Serialize<HistoryDate>(new HistoryDate { Date = today.ToString(), Index = historyFormatter.Count }, true);
                                }
                                else
                                {
                                    dateFormatter.MoveToEnd();
                                    dateFormatter.Serialize<HistoryDate>(new HistoryDate { Date = today.ToString(), Index = historyFormatter.Count });
                                }

                                dateFormatter.Flush();
                            }
                        }
                    }
                }
            }
        }
Esempio n. 13
0
        internal static void AddRecords(List <HistoryRecord> Records)
        {
            lock (_syncRoot)
            {
                using (FileStream dataIndexStream = new FileStream(GetFile(true, true), FileMode.OpenOrCreate))
                {
                    using (FileStream dataContentStream = new FileStream(GetFile(true, false), FileMode.OpenOrCreate))
                    {
                        var historyFormatter = new CustomBinaryFormatter(dataIndexStream, dataContentStream);
                        historyFormatter.Register <HistoryRecord>(1);

                        historyFormatter.MoveToEnd();
                        var historyCount = historyFormatter.Count;
                        Records.ForEach(r =>
                        {
                            historyFormatter.Serialize <HistoryRecord>(r);
                        });

                        historyFormatter.Flush();

                        using (FileStream dateIndexStream = new FileStream(GetFile(false, true), FileMode.OpenOrCreate))
                        {
                            using (FileStream dateContentStream = new FileStream(GetFile(false, false), FileMode.OpenOrCreate))
                            {
                                var dateFormatter = new CustomBinaryFormatter(dateIndexStream, dateContentStream);
                                dateFormatter.Register <HistoryDate>(1);

                                var  dateCount      = dateFormatter.Count;
                                var  today          = DateTime.Now.Date.AddDays(1);
                                var  yesterday      = DateTime.Now.Date;
                                long todayIndex     = -1;
                                var  foundYesterday = false;
                                for (long i = 0; i < dateCount; i++)
                                {
                                    var date = dateFormatter.Deserialize <HistoryDate>(false);
                                    if (DateTime.Parse(date.Date) == today)
                                    {
                                        todayIndex = i;
                                    }
                                    if (DateTime.Parse(date.Date) == yesterday)
                                    {
                                        foundYesterday = true;
                                    }
                                }
                                if (!foundYesterday)
                                {
                                    dateFormatter.MoveToEnd();
                                    dateFormatter.Serialize <HistoryDate>(new HistoryDate {
                                        Date = yesterday.ToString(), Index = historyCount
                                    });
                                }

                                if (todayIndex != -1)
                                {
                                    dateFormatter.MoveTo(todayIndex);
                                    dateFormatter.Serialize <HistoryDate>(new HistoryDate {
                                        Date = today.ToString(), Index = historyFormatter.Count
                                    }, true);
                                }
                                else
                                {
                                    dateFormatter.MoveToEnd();
                                    dateFormatter.Serialize <HistoryDate>(new HistoryDate {
                                        Date = today.ToString(), Index = historyFormatter.Count
                                    });
                                }

                                dateFormatter.Flush();
                            }
                        }
                    }
                }
            }
        }