Esempio n. 1
0
        //TODO: make use of Locked

        /*
         * protected bool locked = false;
         * public bool Locked
         * {
         *      get {
         *              return locked;
         *      }
         * }
         */

        public void SetHeaderData(byte[] data)
        {
            EndianFlag    endianness = (EndianFlag)data[0];
            MessageReader reader     = new MessageReader(endianness, data);

            Header = (Header)reader.ReadStruct(typeof(Header));
        }
Esempio n. 2
0
		public MessageReader (EndianFlag endianness, byte[] data)
		{
			if (data == null)
				throw new ArgumentNullException ("data");

			this.endianness = endianness;
			this.data = data;
		}
Esempio n. 3
0
        public void SetHeaderData(byte[] data)
        {
            EndianFlag    endianness = (EndianFlag)data[0];
            MessageReader reader     = new MessageReader(endianness, data);

            MethodCaller2 mCaller = ExportObject.GetMCaller(hHandler);

            mCaller(this, reader, null, new MessageWriter());
        }
Esempio n. 4
0
        public MessageReader(EndianFlag endianness, byte[] data)
        {
            if (data == null)
            {
                data = new byte[0];
            }

            this.endianness = endianness;
            this.data       = data;
        }
Esempio n. 5
0
        public MessageReader(EndianFlag endianness, byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            this.endianness = endianness;
            this.data       = data;
        }
Esempio n. 6
0
        public MessageReader(EndianFlag endianness, byte[] data)
        {
            //if (data == null)
            //	throw new ArgumentNullException ("data");
            if (data == null)
                data = new byte[0];

            this.endianness = endianness;
            this.IsNativeEndian = endianness == Connection.NativeEndianness;
            this.data = data;
        }
Esempio n. 7
0
        private MessageReader CreateMessageReader(EndianFlag endianFlag, byte[] data)
        {
            var message = new Message(
                new Header(MessageType.Invalid, endianFlag)
            {
                Sender = string.Empty
            },
                body: data,
                unixFds: null
                );

            return(new MessageReader(message, new MyProxyFactory()));
        }
Esempio n. 8
0
        public MessageReader(EndianFlag endianness, byte[] data)
        {
            //if (data == null)
            //	throw new ArgumentNullException ("data");
            if (data == null)
            {
                data = new byte[0];
            }

            this.endianness     = endianness;
            this.IsNativeEndian = endianness == Connection.NativeEndianness;
            this.data           = data;
        }
Esempio n. 9
0
 public Header(MessageType type, EndianFlag endianness)
 {
     MessageType = type;
     Endianness  = endianness;
     if (type == MessageType.MethodCall)
     {
         ReplyExpected = true;
     }
     else
     {
         Flags = HeaderFlag.NoReplyExpected | HeaderFlag.NoAutoStart;
     }
     MajorVersion = ProtocolInformation.Version;
 }
Esempio n. 10
0
        public void ParseHeader()
        {
            //GetValue (stream, typeof (Header), out Header);

            EndianFlag    endianness = (EndianFlag)HeaderData[0];
            MessageReader reader     = new MessageReader(endianness, HeaderData);

            object valT;

            reader.GetValueStruct(typeof(Header), out valT);
            Header = (Header)valT;

            /*
             * //foreach (HeaderField field in HeaderFields)
             * foreach (KeyValuePair<FieldCode,object> field in Header.Fields)
             * {
             *      //Console.WriteLine (field.Key + " = " + field.Value);
             *      switch (field.Key)
             *      {
             *              case FieldCode.Invalid:
             *                      break;
             *              case FieldCode.Path:
             *                      Path = (ObjectPath)field.Value;
             *                      break;
             *              case FieldCode.Interface:
             *                      Interface = (string)field.Value;
             *                      break;
             *              case FieldCode.Member:
             *                      Member = (string)field.Value;
             *                      break;
             *              case FieldCode.ErrorName:
             *                      ErrorName = (string)field.Value;
             *                      break;
             *              case FieldCode.ReplySerial:
             *                      ReplySerial = (uint)field.Value;
             *                      break;
             *              case FieldCode.Destination:
             *                      Destination = (string)field.Value;
             *                      break;
             *              case FieldCode.Sender:
             *                      Sender = (string)field.Value;
             *                      break;
             *              case FieldCode.Signature:
             *                      Signature = (Signature)field.Value;
             *                      break;
             *      }
             * }
             */
        }
Esempio n. 11
0
        public static Header FromBytes(byte[] data)
        {
            Header     header     = new Header();
            EndianFlag endianness = (EndianFlag)data[0];

            header.Endianness   = endianness;
            header.MessageType  = (MessageType)data[1];
            header.Flags        = (HeaderFlag)data[2];
            header.MajorVersion = data[3];

            var reader = new MessageReader(endianness, data);

            reader.Seek(4);
            header.Length = reader.ReadUInt32();
            header.Serial = reader.ReadUInt32();

            FieldCodeEntry[] fields = reader.ReadArray <FieldCodeEntry> ();
            foreach (var f in fields)
            {
                header[(FieldCode)f.Code] = f.Value;
            }

            return(header);
        }
Esempio n. 12
0
        Message ReadMessageReal()
        {
            byte[] header;
            byte[] body = null;

            int read;

            //16 bytes is the size of the fixed part of the header
            byte[] hbuf = new byte[16];

            read = Read(hbuf, 0, 16);

            if (read == 0)
            {
                return(null);
            }

            if (read != 16)
            {
                throw new Exception("Header read length mismatch: " + read + " of expected " + "16");
            }

            EndianFlag    endianness = (EndianFlag)hbuf[0];
            MessageReader reader     = new MessageReader(endianness, hbuf);

            //discard the endian byte as we've already read it
            reader.ReadByte();

            //discard message type and flags, which we don't care about here
            reader.ReadByte();
            reader.ReadByte();

            byte version = reader.ReadByte();

            if (version < Protocol.MinVersion || version > Protocol.MaxVersion)
            {
                throw new NotSupportedException("Protocol version '" + version.ToString() + "' is not supported");
            }

            if (Protocol.Verbose)
            {
                if (version != Protocol.Version)
                {
                    Console.Error.WriteLine("Warning: Protocol version '" + version.ToString() + "' is not explicitly supported but may be compatible");
                }
            }

            uint bodyLength = reader.ReadUInt32();

            //discard serial
            reader.ReadUInt32();
            uint headerLength = reader.ReadUInt32();

            //this check may become relevant if a future version of the protocol allows larger messages

            /*
             * if (bodyLength > Int32.MaxValue || headerLength > Int32.MaxValue)
             *      throw new NotImplementedException ("Long messages are not yet supported");
             */

            int bodyLen = (int)bodyLength;
            int toRead  = (int)headerLength;

            //we fixup to include the padding following the header
            toRead = Protocol.Padded(toRead, 8);

            long msgLength = toRead + bodyLen;

            if (msgLength > Protocol.MaxMessageLength)
            {
                throw new Exception("Message length " + msgLength + " exceeds maximum allowed " + Protocol.MaxMessageLength + " bytes");
            }

            header = new byte[16 + toRead];
            Array.Copy(hbuf, header, 16);

            read = Read(header, 16, toRead);

            if (read != toRead)
            {
                throw new Exception("Message header length mismatch: " + read + " of expected " + toRead);
            }

            //read the body
            if (bodyLen != 0)
            {
                body = new byte[bodyLen];

                read = Read(body, 0, bodyLen);

                if (read != bodyLen)
                {
                    throw new Exception("Message body length mismatch: " + read + " of expected " + bodyLen);
                }
            }

            Message msg = new Message();

            msg.Connection = this.Connection;
            msg.Body       = body;
            msg.SetHeaderData(header);

            return(msg);
        }
Esempio n. 13
0
 public DValue(EndianFlag endianness, byte[] data)
 {
     this.endianness = endianness;
     this.data = data;
 }
Esempio n. 14
0
 public MessageWriter(EndianFlag endianness)
 {
     this.endianness = endianness;
     stream          = new MemoryStream();
     fdArray         = new UnixFDArray();
 }
Esempio n. 15
0
 public MessageReader(EndianFlag endianness, ArraySegment <byte> data)
 {
     _endianness = endianness;
     _data       = data;
 }
Esempio n. 16
0
        public static Header FromBytes(ArraySegment <byte> data)
        {
            Header     header     = new Header();
            EndianFlag endianness = (EndianFlag)data.Array[data.Offset + 0];

            header.Endianness   = endianness;
            header.MessageType  = (MessageType)data.Array[data.Offset + 1];
            header.Flags        = (HeaderFlag)data.Array[data.Offset + 2];
            header.MajorVersion = data.Array[data.Offset + 3];

            var reader = new MessageReader(endianness, data);

            reader.Seek(4);
            header.Length = reader.ReadUInt32();
            header.Serial = reader.ReadUInt32();

            FieldCodeEntry[] fields = reader.ReadArray <FieldCodeEntry>();
            foreach (var f in fields)
            {
                var fieldCode = f.Code;
                var value     = f.Value;
                switch (fieldCode)
                {
                case FieldCode.Path:
                    header.Path = (ObjectPath)value;
                    break;

                case FieldCode.Interface:
                    header.Interface = (string)value;
                    break;

                case FieldCode.Member:
                    header.Member = (string)value;
                    break;

                case FieldCode.ErrorName:
                    header.ErrorName = (string)value;
                    break;

                case FieldCode.ReplySerial:
                    header.ReplySerial = (uint)value;
                    break;

                case FieldCode.Destination:
                    header.Destination = (string)value;
                    break;

                case FieldCode.Sender:
                    header.Sender = (string)value;
                    break;

                case FieldCode.Signature:
                    header.Signature = (Signature)value;
                    break;

                case FieldCode.UnixFds:
                    header.NumberOfFds = (uint)value;
                    break;
                }
            }

            return(header);
        }
Esempio n. 17
0
		public MessageWriter (EndianFlag endianness)
		{
			this.endianness = endianness;
			stream = new MemoryStream ();
		}
Esempio n. 18
0
 public DValue(EndianFlag endianness, byte[] data)
 {
     this.endianness = endianness;
     this.data       = data;
 }
Esempio n. 19
0
        /*
         * Queue<Message> Outbound = new Queue<Message> ();
         *
         * public void Flush ()
         * {
         *      //should just iterate the enumerator here
         *      while (Outbound.Count != 0) {
         *              Message msg = Outbound.Dequeue ();
         *              WriteMessage (msg);
         *      }
         * }
         *
         * public bool ReadWrite (int timeout_milliseconds)
         * {
         *      //TODO
         *
         *      return true;
         * }
         *
         * public bool ReadWrite ()
         * {
         *      return ReadWrite (-1);
         * }
         *
         * public bool Dispatch ()
         * {
         *      //TODO
         *      Message msg = Inbound.Dequeue ();
         *      //HandleMessage (msg);
         *
         *      return true;
         * }
         *
         * public bool ReadWriteDispatch (int timeout_milliseconds)
         * {
         *      //TODO
         *      return Dispatch ();
         * }
         *
         * public bool ReadWriteDispatch ()
         * {
         *      return ReadWriteDispatch (-1);
         * }
         */

        internal Message ReadMessage()
        {
            //FIXME: fix reading algorithm to work in one step
            //this code is a bit silly and inefficient
            //hopefully it's at least correct and avoids polls for now

            int read;

            byte[] buf = new byte[16];
            read = ns.Read(buf, 0, 16);

            if (read == 0)
            {
                return(null);
            }

            if (read != 16)
            {
                throw new Exception("Header read length mismatch: " + read + " of expected " + "16");
            }

            MemoryStream ms = new MemoryStream();

            ms.Write(buf, 0, 16);

            EndianFlag    endianness = (EndianFlag)buf[0];
            MessageReader reader     = new MessageReader(endianness, buf);

            //discard the endian byte as we've already read it
            byte tmp;

            reader.GetValue(out tmp);

            //discard message type and flags, which we don't care about here
            reader.GetValue(out tmp);
            reader.GetValue(out tmp);

            byte version;

            reader.GetValue(out version);

            if (version < Protocol.MinVersion || version > Protocol.MaxVersion)
            {
                throw new NotSupportedException("Protocol version '" + version.ToString() + "' is not supported");
            }

            if (Protocol.Verbose)
            {
                if (version != Protocol.Version)
                {
                    Console.Error.WriteLine("Warning: Protocol version '" + version.ToString() + "' is not explicitly supported but may be compatible");
                }
            }

            uint bodyLength, serial, headerLength;

            reader.GetValue(out bodyLength);
            reader.GetValue(out serial);
            reader.GetValue(out headerLength);

            //TODO: remove this limitation
            if (bodyLength > Int32.MaxValue || headerLength > Int32.MaxValue)
            {
                throw new NotImplementedException("Long messages are not yet supported");
            }

            int bodyLen = (int)bodyLength;
            int toRead  = (int)headerLength;

            toRead = Protocol.Padded((int)toRead, 8);

            buf = new byte[toRead];

            read = ns.Read(buf, 0, toRead);

            if (read != toRead)
            {
                throw new Exception("Read length mismatch: " + read + " of expected " + toRead);
            }

            ms.Write(buf, 0, buf.Length);

            Message msg = new Message();

            msg.Connection = this;
            msg.HeaderData = ms.ToArray();

            //read the body
            if (bodyLen != 0)
            {
                //FIXME
                //msg.Body = new byte[(int)msg.Header->Length];
                byte[] body = new byte[bodyLen];

                //int len = ns.Read (msg.Body, 0, msg.Body.Length);
                int len = ns.Read(body, 0, bodyLen);

                //if (len != msg.Body.Length)
                if (len != bodyLen)
                {
                    throw new Exception("Message body size mismatch");
                }

                //msg.Body = new MemoryStream (body);
                msg.Body = body;
            }

            //this needn't be done here
            msg.ParseHeader();

            return(msg);
        }
Esempio n. 20
0
 static Environment()
 {
     NativeEndianness = BitConverter.IsLittleEndian ? EndianFlag.Little : EndianFlag.Big;
     IsWindows        = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
 }
        //a default constructor is a bad idea for now as we want to make sure the header and content-type match
        //public MessageWriter () : this (Connection.NativeEndianness)

        public MessageWriter(EndianFlag endianness)
        {
            this.endianness = endianness;
            stream          = new MemoryStream();
        }
Esempio n. 22
0
        Message ReadMessageReal()
        {
            byte[]      header  = null;
            byte[]      body    = null;
            UnixFDArray fdArray = Connection.UnixFDSupported ? new UnixFDArray() : null;

            int read;

            //16 bytes is the size of the fixed part of the header
            if (readBuffer == null)
            {
                readBuffer = new byte[16];
            }
            byte[] hbuf = readBuffer;

            read = Read(hbuf, 0, 16, fdArray);

            if (read == 0)
            {
                return(null);
            }

            if (read != 16)
            {
                throw new Exception("Header read length mismatch: " + read + " of expected " + "16");
            }

            EndianFlag    endianness = (EndianFlag)hbuf[0];
            MessageReader reader     = new MessageReader(endianness, hbuf);

            //discard endian byte, message type and flags, which we don't care about here
            reader.Seek(3);

            byte version = reader.ReadByte();

            if (version < ProtocolInformation.MinVersion || version > ProtocolInformation.MaxVersion)
            {
                throw new NotSupportedException("Protocol version '" + version.ToString() + "' is not supported");
            }

            if (ProtocolInformation.Verbose)
            {
                if (version != ProtocolInformation.Version)
                {
                    Console.Error.WriteLine("Warning: Protocol version '" + version.ToString() + "' is not explicitly supported but may be compatible");
                }
            }

            uint bodyLength = reader.ReadUInt32();

            //discard serial
            reader.ReadUInt32();
            uint headerLength = reader.ReadUInt32();

            int bodyLen = (int)bodyLength;
            int toRead  = (int)headerLength;

            //we fixup to include the padding following the header
            toRead = ProtocolInformation.Padded(toRead, 8);

            long msgLength = toRead + bodyLen;

            if (msgLength > ProtocolInformation.MaxMessageLength)
            {
                throw new Exception("Message length " + msgLength + " exceeds maximum allowed " + ProtocolInformation.MaxMessageLength + " bytes");
            }

            header = new byte[16 + toRead];
            Array.Copy(hbuf, header, 16);

            read = Read(header, 16, toRead, fdArray);

            if (read != toRead)
            {
                throw new Exception("Message header length mismatch: " + read + " of expected " + toRead);
            }

            //read the body
            if (bodyLen != 0)
            {
                body = new byte[bodyLen];

                read = Read(body, 0, bodyLen, fdArray);

                if (read != bodyLen)
                {
                    throw new Exception("Message body length mismatch: " + read + " of expected " + bodyLen);
                }
            }

            Message msg = Message.FromReceivedBytes(Connection, header, body, fdArray);

            return(msg);
        }
Esempio n. 23
0
        public async Task <Message> ReceiveMessageAsync()
        {
            try
            {
                int bytesRead = await ReadCountAsync(_headerReadBuffer, 0, 16, _fileDescriptors).ConfigureAwait(false);

                if (bytesRead == 0)
                {
                    return(null);
                }
                if (bytesRead != 16)
                {
                    throw new ProtocolException("Header read length mismatch: " + bytesRead + " of expected " + "16");
                }

                EndianFlag    endianness = (EndianFlag)_headerReadBuffer[0];
                MessageReader reader     = new MessageReader(endianness, new ArraySegment <byte>(_headerReadBuffer));

                //discard endian byte, message type and flags, which we don't care about here
                reader.Seek(3);

                byte version = reader.ReadByte();
                if (version != ProtocolInformation.Version)
                {
                    throw new NotSupportedException("Protocol version '" + version.ToString() + "' is not supported");
                }

                uint bodyLength = reader.ReadUInt32();

                //discard _methodSerial
                reader.ReadUInt32();

                uint headerLength = reader.ReadUInt32();

                int bodyLen = (int)bodyLength;
                int toRead  = (int)headerLength;

                //we fixup to include the padding following the header
                toRead = ProtocolInformation.Padded(toRead, 8);

                long msgLength = toRead + bodyLen;
                if (msgLength > ProtocolInformation.MaxMessageLength)
                {
                    throw new ProtocolException("Message length " + msgLength + " exceeds maximum allowed " + ProtocolInformation.MaxMessageLength + " bytes");
                }

                byte[] header = new byte[16 + toRead];
                Array.Copy(_headerReadBuffer, header, 16);
                bytesRead = await ReadCountAsync(header, 16, toRead, _fileDescriptors).ConfigureAwait(false);

                if (bytesRead != toRead)
                {
                    throw new ProtocolException("Message header length mismatch: " + bytesRead + " of expected " + toRead);
                }

                var messageHeader = Header.FromBytes(new ArraySegment <byte>(header));

                byte[] body = null;
                //read the body
                if (bodyLen != 0)
                {
                    body = new byte[bodyLen];

                    bytesRead = await ReadCountAsync(body, 0, bodyLen, _fileDescriptors).ConfigureAwait(false);

                    if (bytesRead != bodyLen)
                    {
                        throw new ProtocolException("Message body length mismatch: " + bytesRead + " of expected " + bodyLen);
                    }
                }

                if (_fileDescriptors.Count < messageHeader.NumberOfFds)
                {
                    throw new ProtocolException("File descriptor length mismatch: " + _fileDescriptors.Count + " of expected " + messageHeader.NumberOfFds);
                }

                Message msg = new Message(
                    messageHeader,
                    body,
                    messageHeader.NumberOfFds == 0 ? null :
                    _fileDescriptors.Count == messageHeader.NumberOfFds ? _fileDescriptors.ToArray() :
                    _fileDescriptors.Take((int)messageHeader.NumberOfFds).ToArray()
                    );

                _fileDescriptors.RemoveRange(0, (int)messageHeader.NumberOfFds);

                return(msg);
            }
            catch
            {
                foreach (var fd in _fileDescriptors)
                {
                    CloseSafeHandle.close(fd.Handle);
                }
                throw;
            }
        }