예제 #1
0
        public MessageHeader(
            SocketOperations socketOperations,
            Decoder header,
            ReadOnlySpan <byte> controlBytes,
            bool isMonoRuntime
            )
        {
            this.socketOperations = socketOperations;
            BodySignature         = "";
            while (!header.IsFinished)
            {
                var typeCode = (DbusHeaderType)header.GetByte();
                header.GetSignature(); // variant signature
                switch (typeCode)
                {
                case DbusHeaderType.Path:
                    Path = header.GetObjectPath();
                    break;

                case DbusHeaderType.InterfaceName:
                    InterfaceName = header.GetString();
                    break;

                case DbusHeaderType.Member:
                    Member = header.GetString();
                    break;

                case DbusHeaderType.ErrorName:
                    ErrorName = header.GetString();
                    break;

                case DbusHeaderType.ReplySerial:
                    ReplySerial = header.GetUInt32();
                    break;

                case DbusHeaderType.Destination:
                    Destination = header.GetString();
                    break;

                case DbusHeaderType.Sender:
                    Sender = header.GetString();
                    break;

                case DbusHeaderType.Signature:
                    BodySignature = header.GetSignature();
                    break;

                case DbusHeaderType.UnixFds:
                    var numberOfFds = header.GetUInt32();

                    var cmsgHeaderBytes = controlBytes.Slice(0, sizeofCmsghdr);
                    var cmsgHeader      = MemoryMarshal.Cast <byte, cmsghdr>(cmsgHeaderBytes);

                    var fileDescriptorsBytes = controlBytes.Slice(
                        sizeofCmsghdr,
                        (int)cmsgHeader[0].len - sizeofCmsghdr
                        );
                    var fileDescriptors = MemoryMarshal.Cast <byte, int>(fileDescriptorsBytes);
                    System.Diagnostics.Debug.Assert(numberOfFds == fileDescriptors.Length);

                    UnixFds = new SafeHandle[numberOfFds];
                    for (var i = 0; i < numberOfFds; ++i)
                    {
                        if (isMonoRuntime)
                        {
                            UnixFds[i] = new ReceivedFileDescriptorSafeHandle(fileDescriptors[i]);
                        }
                        else
                        {
                            UnixFds[i] = new SafeFileHandle(new IntPtr(fileDescriptors[i]), true);
                        }
                    }
                    break;
                }
                header.AdvanceToCompoundValue();
            }
        }