Exemple #1
0
 public static void WriteMessageTo(PmlElement Message, BinaryWriter Writer)
 {
     lock (Writer) {
         WriteElementTo(Message, Writer);
         Writer.Flush();
     }
 }
Exemple #2
0
        public static string GetMessageString(PmlElement Message)
        {
            StringWriter Buffer = new StringWriter();

            WriteMessageTo(Message, Buffer);
            return(Buffer.ToString());
        }
Exemple #3
0
 public static void WriteMessageTo(PmlElement message, TextWriter stream)
 {
     lock (stream) {
         WriteElementTo(message, stream);
         stream.Flush();
     }
 }
Exemple #4
0
 public void SendMessage(PmlElement message)
 {
     _WriteMessage(new PmlDictionary()
     {
         { "CMD", "MSG" }, { "MSG", message }
     });
 }
Exemple #5
0
 public static String EncodeMessage(PmlElement message)
 {
     using (StringWriter stream = new StringWriter()) {
         WriteMessageTo(message, stream);
         return(stream.ToString());
     }
 }
Exemple #6
0
 public static Byte[] EncodeMessage(PmlElement message)
 {
     using (MemoryStream stream = new MemoryStream()) {
         WriteMessageTo(message, stream, Encoding.UTF8);
         return(stream.ToArray());
     }
 }
Exemple #7
0
 public static void WriteMessageTo(PmlElement message, Stream stream, Encoding encoding)
 {
     lock (stream) {
         WriteElementTo(message, stream, encoding);
         stream.Flush();
     }
 }
Exemple #8
0
        public static void WriteMessageToFile(PmlElement Message, string Filename)
        {
            FileStream F = File.Create(Filename);

            WriteMessageToStream(Message, F);
            F.Close();
        }
Exemple #9
0
 internal PmlChannelRequestReceivedEventArgsA(PmlCommunicator communicator, UInt32 sid, PmlElement message)
 {
     _communicator = communicator;
     _data         = message;
     _sid          = sid;
     _accepted     = _rejected = false;
 }
Exemple #10
0
 internal PmlCallReceivedEventArgs(PmlElement request, bool wantReply, UInt32 sid)
 {
     this.Request   = request;
     this.WantReply = wantReply;
     this.SID       = sid;
     this.Reply     = null;
 }
Exemple #11
0
        private static void WriteElementTo(PmlElement Element, BinaryWriter Writer)
        {
            if (Element == null)
            {
                Writer.Write((byte)AmfDataType.Null);
                return;
            }
            switch (Element.Type)
            {
            case PmlType.Null:
                Writer.Write((byte)AmfDataType.Null);
                break;

            case PmlType.Dictionary:
                Writer.Write((byte)AmfDataType.UntypedObject);
                //WriteDictionary(Writer, (PmlDictionary)Element);
                WriteUntypedObject(Writer, (PmlDictionary)Element);
                break;

            case PmlType.Collection:
                Writer.Write((byte)AmfDataType.Array);
                WriteCollection(Writer, (PmlCollection)Element);
                break;

            case PmlType.Binary:
                Writer.Write((byte)AmfDataType.String);
                byte[] bytes = Element.ToByteArray();
                if (bytes.Length > UInt16.MaxValue)
                {
                    Writer.Write((byte)AmfDataType.String);
                    WriteString(Writer, bytes);
                }
                else
                {
                    Writer.Write((byte)AmfDataType.LongString);
                    WriteLongString(Writer, bytes);
                }
                break;

            case PmlType.String:
                string str = Element.ToString();
                if (str.Length < UInt16.MaxValue)
                {
                    Writer.Write((byte)AmfDataType.String);
                    WriteString(Writer, str);
                }
                else
                {
                    Writer.Write((byte)AmfDataType.LongString);
                    WriteLongString(Writer, str);
                }
                break;

            case PmlType.Integer:
                Writer.Write((byte)AmfDataType.Number);
                WriteDouble(Writer, (Element as PmlInteger).ToDouble());
                break;
            }
        }
Exemple #12
0
 public override void WriteMessage(PmlElement message)
 {
     if (_state != ChannelState.Acknowledged)
     {
         throw new InvalidOperationException("The subchannel is not open");
     }
     _communicator.WriteSessionMessage(_id, 1, message);
 }
Exemple #13
0
        public static Byte[] EncodeMessage(PmlElement message)
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8);

            WriteMessageTo(message, writer);
            return(stream.ToArray());
        }
Exemple #14
0
 public override void WriteMessage(PmlElement message)
 {
     if (!_open)
     {
         throw new InvalidOperationException("The channel is not open");
     }
     lock (_rw) _rw.WriteMessage(message);
 }
Exemple #15
0
        public static PmlElement ReadMessageFrom(BinaryReader Reader)
        {
            PmlElement Element = null;

            lock (Reader) {
                Element = ReadElementFrom(Reader);
            }
            return(Element);
        }
Exemple #16
0
 public static void WriteMessageTo(PmlElement message, Stream stream, Encoding encoding)
 {
     lock (stream) {
         StreamWriter writer = new StreamWriter(stream, encoding);
         WriteElementTo(message, writer);
         writer.Flush();
         stream.Flush();
     }
 }
Exemple #17
0
 private void _WriteMessage(PmlElement Message)
 {
     lock (_channel) {
         if (!_channel.IsOpen)
         {
             throw new InvalidOperationException("Could not write message: the channel is not open");
         }
         _channel.WriteMessage(Message);
     }
 }
Exemple #18
0
        public void WriteMessage(PmlElement Message)
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream, pEncoding);

            WriteMessageTo(Message, writer);
            lock (pStream) {
                stream.WriteTo(pStream);
                pStream.Flush();
            }
        }
Exemple #19
0
        private void AsyncReadMessage(object state)
        {
            ReadMessageAsyncResult ar = (ReadMessageAsyncResult)state;

            try {
                PmlElement message = ReadMessage();
                ar.SetCompleted(false, null, message);
            } catch (Exception ex) {
                ar.SetCompleted(false, ex, null);
            }
        }
Exemple #20
0
        public static void WriteMessageToStream(PmlElement Message, Stream Stream, XmlWriterSettings Settings)
        {
            XmlWriter Writer = System.Xml.XmlWriter.Create(Stream, Settings);

            Writer.WriteStartDocument();
            Writer.WriteStartElement("msg");
            WriteElementTo(Message, Writer);
            Writer.WriteEndElement();
            Writer.WriteEndDocument();
            Writer.Flush();
            Writer.Close();
        }
Exemple #21
0
        public IPmlChannel CreateChannel(PmlElement data)
        {
            UInt32     sid;
            SubChannel ch;

            lock (_sessions) {
                sid = GetNextSessionId(ref pNextSession, _sessions);
                ch  = new SubChannel(this, sid, true);
            }
            WriteSessionMessage(sid, 0, data);
            return(ch);
        }
Exemple #22
0
        protected void WriteSessionMessage(UInt32 SID, byte CMD, PmlElement MSG)
        {
            PmlDictionary Msg2 = new PmlDictionary()
            {
                { "CMD", "SES" },
                { "SID", SID },
                { "SCMD", CMD },
            };

            if (MSG != null)
            {
                Msg2.Add("MSG", MSG);
            }
            _WriteMessage(Msg2);
        }
Exemple #23
0
        private static void WriteElementTo(PmlElement Element, string Indent, TextWriter Writer)
        {
            if (Element == null)
            {
                Console.WriteLine("NULL");
                return;
            }
            switch (Element.Type)
            {
            case PmlType.Null:
                Writer.WriteLine("NULL");
                break;

            case PmlType.Collection:
                Writer.WriteLine("COLLECTION {");
                foreach (PmlElement Child in (PmlCollection)Element)
                {
                    Writer.Write(Indent + " ");
                    WriteElementTo(Child, Indent + " ", Writer);
                }

                Writer.WriteLine(Indent + "}");
                break;

            case PmlType.Dictionary:
                Writer.WriteLine("DICTIONARY {");
                foreach (KeyValuePair <string, PmlElement> Child in (PmlDictionary)Element)
                {
                    Writer.Write(Indent + " " + Uri.EscapeDataString(Child.Key) + " ");
                    WriteElementTo(Child.Value, Indent + " ", Writer);
                }

                Writer.WriteLine(Indent + "}");
                break;

            case PmlType.Binary:
                Writer.WriteLine("BINARY " + Convert.ToBase64String(Element.ToByteArray()));
                break;

            case PmlType.Integer:
                Writer.WriteLine("INT " + Element.ToString());
                break;

            case PmlType.String:
                Writer.WriteLine("STRING " + Uri.EscapeDataString(Element.ToString()));
                break;
            }
        }
Exemple #24
0
 public PmlElement GetMessage()
 {
     if (pStack.Count == 1)
     {
         PmlElement Element = pStack.Pop();
         return(Element);
     }
     else if (pStack.Count == 0)
     {
         throw new InvalidOperationException("No stacked element. The top most element should not be ended. All elements, except Dictionary and Collection, are sent automatically.");
     }
     else
     {
         throw new InvalidOperationException("All elements, except for the top most element, should be ended.");
     }
 }
Exemple #25
0
 private void messageReceived(IAsyncResult ar)
 {
     try {
         PmlElement Message = _channel.EndReadMessage(ar);
         processMessage(Message);
         _channel.BeginReadMessage(messageReceived, null);
     } catch (InvalidOperationException ex) {
         Console.WriteLine("InvalidOperationException in LegacyPmlCommunicator.messageReceived: " + ex.Message);
         closed();
         _channel.Close();
     } catch (Exception ex) {
         Console.WriteLine(ex.ToString());
         closed();
         _channel.Close();
     }
 }
Exemple #26
0
        private static void WriteElementTo(PmlElement Element, System.Xml.XmlWriter Writer)
        {
            switch (Element.Type)
            {
            case PmlType.Binary:
                Writer.WriteAttributeString("type", "binary");
                byte[] Bytes = Element.ToByteArray();
                Writer.WriteBase64(Bytes, 0, Bytes.Length);
                break;

            case PmlType.Collection:
                Writer.WriteAttributeString("type", "collection");
                foreach (PmlElement Child in (PmlCollection)Element)
                {
                    Writer.WriteStartElement("item");
                    WriteElementTo(Child, Writer);
                    Writer.WriteEndElement();
                }

                break;

            case PmlType.Dictionary:
                Writer.WriteAttributeString("type", "dictionary");
                foreach (KeyValuePair <string, PmlElement> Child in (PmlDictionary)Element)
                {
                    Writer.WriteStartElement(Child.Key);
                    WriteElementTo(Child.Value, Writer);
                    Writer.WriteEndElement();
                }

                break;

            case PmlType.Integer:
                Writer.WriteAttributeString("type", "integer");
                Writer.WriteString(Element.ToString());
                break;

            case PmlType.Null:
                Writer.WriteAttributeString("type", "null");
                break;

            case PmlType.String:
                Writer.WriteAttributeString("type", "string");
                Writer.WriteString(Element.ToString());
                break;
            }
        }
Exemple #27
0
        public static PmlElement ReadMessageFrom(BinaryReader Reader)
        {
            PmlElement Element = null;

            lock (Reader) {
                if (Reader.ReadByte() != 255)
                {
                    return(null);
                }
                Element = ReadElementFrom(Reader);
                if (Reader.ReadByte() != 255)
                {
                    return(null);
                }
            }
            return(Element);
        }
Exemple #28
0
        private PmlElement AddChildElement(PmlElement Element, bool AddToStack, string ChildName)
        {
            PmlElement Parent;

            if (pStack.Count > 0)
            {
                Parent = pStack.Peek();
                if (Parent is PmlDictionary)
                {
                    if (ChildName == null)
                    {
                        throw new ArgumentNullException("ChildName", "Dictionary items need a Name");
                    }
                    ((PmlDictionary)Parent).Add(ChildName, Element);
                }
                else if (Parent is PmlCollection)
                {
                    if (ChildName != null)
                    {
                        throw new ArgumentOutOfRangeException("ChildName", "Can not add named element to a Collection");
                    }
                    ((PmlCollection)Parent).Add(Element);
                }
                else
                {
                    throw new InvalidOperationException("Invalid Element type in stack: " + Parent.Type.ToString());
                }
            }
            else
            {
                if (ChildName != null)
                {
                    throw new ArgumentOutOfRangeException("ChildName", "Can not create named element without container (Dictionary)");
                }
                else if (!AddToStack)
                {
                    pWriter.WriteMessage(Element);
                }
            }
            if (AddToStack)
            {
                pStack.Push(Element);
            }
            return(Element);
        }
Exemple #29
0
        protected void PushReceivedMessage(PmlElement message)
        {
            ReadMessageAsyncResult asyncWait;

            lock (_queue) {
                asyncWait = Interlocked.Exchange <ReadMessageAsyncResult>(ref _asyncWait, null);
                if (asyncWait == null)
                {
                    _queue.Enqueue(message);
                    Monitor.Pulse(_queue);
                }
            }
            if (asyncWait != null)
            {
                asyncWait.Message = message;
                asyncWait.SetCompleted(false, null);
            }
        }
Exemple #30
0
 public PmlElement EndElement()
 {
     if (pStack.Count > 0)
     {
         PmlElement Element = pStack.Pop();
         if (pStack.Count == 0)
         {
             if (pWriter != null)
             {
                 pWriter.WriteMessage(Element);
             }
         }
         return(Element);
     }
     else
     {
         return(null);
     }
 }