示例#1
0
        public void Save(string path)
        {
            if (!readOnly)
            {
                this.path = path;
                var writer = new XML.Writer(path);

                try
                {
                    this.Serialize(writer);
                }
                catch
                {

                }

                writer.Close();
            }
        }
示例#2
0
        private void ServiceOutgoing()
        {
            if (socket == null) return;
            if (stream == null) return;
            if (messagesOut.Count == 0) return;

            var messageQueue = new List<SocketMessage>();

            try
            {
                // assemble packet
                lock (messagesOut)
                {
                    messageQueue.AddRange(messagesOut);
                    messagesOut.Clear();
                }

                string packetString = "";
                using (var sw = new StringWriter())
                {
                    using (var writer = new XML.Writer(sw))
                    {
                        writer.WriteStartElement("SocketMessages");

                        foreach (SocketMessage message in messageQueue)
                        {
                            message.Serialize(writer);
                        }

                        writer.WriteEndElement();
                    }

                    packetString = sw.ToString();
                }

                // write data to socket
                byte[] packetBytes = this.encoding.GetBytes(packetString.ToString());
                lock (socket)
                {
                    lock (stream)
                    {
                        stream.Write(packetBytes, 0, packetBytes.Length);
                    }
                }

                this.packetsOut += messageQueue.Count;
            }
            catch (System.IO.IOException)
            {
                lock (messagesOut)
                {
                    messagesOut.AddRange(messageQueue);
                }

                resendAttempts++;
                if (resendAttempts >3)
                {
                    this.Log("Failed three send attempts");
                    Thread.Sleep(1000);
                    // TODO: log error
                    //this.messagesOut.Clear();
                    resendAttempts = 0;
                    //this.Disconnect();
                }
            }
            catch (ObjectDisposedException)
            {
                lock (messagesOut)
                {
                    messagesOut.AddRange(messageQueue);
                }

                //this.Disconnect();
            }
        }