Exemple #1
0
        public void SendRequest(DsiCommand command, byte[] payload, AfpTransportReplyHandler replyHandler = null)
        {
            DsiHeader header = new DsiHeader()
            {
                command   = command,
                flags     = DsiFlags.Request,
                requestId = this.NextRequestId(),
                errorCodeOrWriteOffset = 0,
                totalDataLength        = (uint)payload.Length
            };

            AfpStream stream = new AfpStream();

            header.Write(stream);
            stream.WriteBytes(payload);

            if (replyHandler != null)
            {
                _replyHandlers[header.requestId] = replyHandler;
            }

            byte[] result = stream.ToByteArray();

            this.SendBuffer(result);
        }
        public AfpResultCode Process(IAfpSession session, DsiHeader dsiHeader, AfpStream requestStream, AfpStream responseStream)
        {
            requestStream.ReadUInt8(); // Padding

            ushort volumeId = requestStream.ReadUInt16();
            uint directoryId = requestStream.ReadUInt32();
            AfpPathType pathType = requestStream.ReadEnum<AfpPathType>();
            string pathName = null;

            switch (pathType) {
                case AfpPathType.kFPLongName:
                case AfpPathType.kFPShortName:
                    pathName = requestStream.ReadPascalString();
                    break;
                case AfpPathType.kFPUTF8Name:
                    pathName = requestStream.ReadUTF8StringWithHint();
                    break;
            }

            AfpPathType newNameType = requestStream.ReadEnum<AfpPathType>();
            string newName = null;

            switch (newNameType) {
                case AfpPathType.kFPLongName:
                case AfpPathType.kFPShortName:
                    newName = requestStream.ReadPascalString();
                    break;
                case AfpPathType.kFPUTF8Name:
                    newName = requestStream.ReadUTF8StringWithHint();
                    break;
            }

            IAfpVolume volume = session.GetVolume(volumeId);

            if (volume == null) {
                return AfpResultCode.FPObjectNotFound;
            }

            IStorageContainer container = null;

            if (directoryId == 2) {
                container = volume.StorageProvider;
            } else {
                container = (volume.GetNode(directoryId) as IStorageContainer);
            }

            if (container == null) {
                return AfpResultCode.FPObjectNotFound;
            }

            IStorageItem renameItem = container.Content(pathName);

            if (renameItem == null) {
                return AfpResultCode.FPObjectNotFound;
            }

            renameItem.Rename(newName);

            return AfpResultCode.FPNoErr;
        }
Exemple #3
0
        private void OnCommandSent(DsiHeader header, byte[] payload)
        {
            AfpTransportCommandReceivedEventArgs args = new AfpTransportCommandReceivedEventArgs(header, payload);

            try {
                CommandSent(this, args);
            } catch { }
        }
Exemple #4
0
        public DsiHeader CreateReply(Afp.Protocol.AfpResultCode resultCode, uint resultPayloadLength)
        {
            DsiHeader newHeader = this;

            newHeader.flags = DsiFlags.Reply;
            newHeader.errorCodeOrWriteOffset = (uint)resultCode;
            newHeader.totalDataLength        = resultPayloadLength;
            return(newHeader);
        }
Exemple #5
0
        public DsiHeader WriteReply(AfpResultCode resultCode, byte[] resultPayload, AfpStream writeToStream)
        {
            DsiHeader replyHeader = this.CreateReply(resultCode, (uint)resultPayload.Length);

            replyHeader.Write(writeToStream);
            writeToStream.WriteBytes(resultPayload);

            return(replyHeader);
        }
Exemple #6
0
        public void SendReply(DsiHeader header, AfpResultCode resultCode, byte[] payload) {
            AfpStream finalStream = new AfpStream();

            DsiHeader replyHeader = header.WriteReply(resultCode, payload, finalStream);
            this.OnCommandSent(replyHeader, payload);

            byte[] result = finalStream.ToByteArray();

            this.SendBuffer(result);
        }
Exemple #7
0
        public void SendReply(DsiHeader header, AfpResultCode resultCode, byte[] payload)
        {
            AfpStream finalStream = new AfpStream();

            DsiHeader replyHeader = header.WriteReply(resultCode, payload, finalStream);

            this.OnCommandSent(replyHeader, payload);

            byte[] result = finalStream.ToByteArray();

            this.SendBuffer(result);
        }
Exemple #8
0
        private void DataReceived(IAsyncResult ar)
        {
            try {
                lock (_synclock) {
                    if (_closed)
                    {
                        return;
                    }

                    int bytesReceived = this.Socket.EndReceive(ar);

                    if (bytesReceived == 0)
                    {
                        this.Close();
                        return;
                    }

                    _currentHeader.WriteByte(_buffer[0]);

                    if (_currentHeader.Length == 16)
                    {
                        _currentHeader.Position = 0;
                        DsiHeader header = DsiHeader.Read(_currentHeader);

                        byte[] payload = new byte[header.totalDataLength];

                        for (int currentOffset = 0; currentOffset < payload.Length;)
                        {
                            bytesReceived = this.Socket.Receive(payload, currentOffset, payload.Length - currentOffset, SocketFlags.None);

                            if (bytesReceived == 0)
                            {
                                this.Close();
                                return;
                            }

                            currentOffset += bytesReceived;
                        }

                        Task.Factory.StartNew(() => {
                            this.OnCommandReceived(header, payload);
                        });

                        _currentHeader.SetLength(0);
                    }

                    this.BeginReceive();
                }
            } catch {
                this.Close();
            }
        }
Exemple #9
0
        public static DsiHeader Read(AfpStream stream)
        {
            DsiHeader header = new DsiHeader();
            header.flags = stream.ReadEnum<DsiFlags>();
            header.command = stream.ReadEnum<DsiCommand>();
            if (header.command == DsiCommand.Invalid) {
                return header;
            }

            header.requestId = stream.ReadUInt16();
            header.errorCodeOrWriteOffset = stream.ReadUInt32();
            header.totalDataLength = stream.ReadUInt32();
            header.reserved = stream.ReadUInt32();

            return header;
        }
Exemple #10
0
        public static DsiHeader Read(AfpStream stream)
        {
            DsiHeader header = new DsiHeader();

            header.flags   = stream.ReadEnum <DsiFlags>();
            header.command = stream.ReadEnum <DsiCommand>();
            if (header.command == DsiCommand.Invalid)
            {
                return(header);
            }

            header.requestId = stream.ReadUInt16();
            header.errorCodeOrWriteOffset = stream.ReadUInt32();
            header.totalDataLength        = stream.ReadUInt32();
            header.reserved = stream.ReadUInt32();

            return(header);
        }
Exemple #11
0
        private void OnCommandReceived(DsiHeader header, byte[] payload)
        {
            if (header.flags == DsiFlags.Reply)
            {
                // Find reply handler.
                AfpTransportReplyHandler handler = null;

                if (!_replyHandlers.TryRemove(header.requestId, out handler))
                {
                    // BUG? Request ID flipped in replies from Mac OS X Snow Leopard?
                    byte[] requestIdData = BitConverter.GetBytes(header.requestId);
                    Array.Reverse(requestIdData);
                    header.requestId = BitConverter.ToUInt16(requestIdData, 0);

                    _replyHandlers.TryRemove(header.requestId, out handler);
                }

                if (handler != null)
                {
                    try {
                        handler(header, payload);
                    } catch { }

                    return;
                }
            }

            switch (header.command)
            {
            case DsiCommand.Tickle:

                break;

            default: {
                AfpTransportCommandReceivedEventArgs args = new AfpTransportCommandReceivedEventArgs(header, payload);
                try {
                    CommandReceived(this, args);
                } catch { }

                break;
            }
            }
        }
Exemple #12
0
        public void SendRequest(DsiCommand command, byte[] payload, AfpTransportReplyHandler replyHandler = null) {
            DsiHeader header = new DsiHeader() {
                command = command,
                flags = DsiFlags.Request,
                requestId = this.NextRequestId(),
                errorCodeOrWriteOffset = 0,
                totalDataLength = (uint)payload.Length
            };

            AfpStream stream = new AfpStream();
            header.Write(stream);
            stream.WriteBytes(payload);

            if (replyHandler != null) {
                _replyHandlers[header.requestId] = replyHandler;
            }

            byte[] result = stream.ToByteArray();

            this.SendBuffer(result);
        }
        public AfpResultCode Process(IAfpSession session, DsiHeader dsiHeader, AfpStream requestStream, AfpStream responseStream)
        {
            requestStream.ReadUInt8(); // Padding

            ushort      volumeId    = requestStream.ReadUInt16();
            uint        directoryId = requestStream.ReadUInt32();
            AfpPathType pathType    = requestStream.ReadEnum <AfpPathType>();
            string      pathName    = null;

            switch (pathType)
            {
            case AfpPathType.kFPLongName:
            case AfpPathType.kFPShortName:
                pathName = requestStream.ReadPascalString();
                break;

            case AfpPathType.kFPUTF8Name:
                pathName = requestStream.ReadUTF8StringWithHint();
                break;
            }

            AfpPathType newNameType = requestStream.ReadEnum <AfpPathType>();
            string      newName     = null;

            switch (newNameType)
            {
            case AfpPathType.kFPLongName:
            case AfpPathType.kFPShortName:
                newName = requestStream.ReadPascalString();
                break;

            case AfpPathType.kFPUTF8Name:
                newName = requestStream.ReadUTF8StringWithHint();
                break;
            }

            IAfpVolume volume = session.GetVolume(volumeId);

            if (volume == null)
            {
                return(AfpResultCode.FPObjectNotFound);
            }

            IStorageContainer container = null;

            if (directoryId == 2)
            {
                container = volume.StorageProvider;
            }
            else
            {
                container = (volume.GetNode(directoryId) as IStorageContainer);
            }

            if (container == null)
            {
                return(AfpResultCode.FPObjectNotFound);
            }

            IStorageItem renameItem = container.Content(pathName);

            if (renameItem == null)
            {
                return(AfpResultCode.FPObjectNotFound);
            }

            renameItem.Rename(newName);

            return(AfpResultCode.FPNoErr);
        }
Exemple #14
0
        private void OnCommandReceived(DsiHeader header, byte[] payload) {
            if (header.flags == DsiFlags.Reply) {
                // Find reply handler.
                AfpTransportReplyHandler handler = null;

                if (!_replyHandlers.TryRemove(header.requestId, out handler)) {
                    // BUG? Request ID flipped in replies from Mac OS X Snow Leopard?
                    byte[] requestIdData = BitConverter.GetBytes(header.requestId);
                    Array.Reverse(requestIdData);
                    header.requestId = BitConverter.ToUInt16(requestIdData, 0);

                    _replyHandlers.TryRemove(header.requestId, out handler);
                }

                if (handler != null) {
                    try {
                        handler(header, payload);
                    } catch { }

                    return;
                }
            }

            switch (header.command) {
                case DsiCommand.Tickle:

                    break;
                default: {
                        AfpTransportCommandReceivedEventArgs args = new AfpTransportCommandReceivedEventArgs(header, payload);
                        try {
                            CommandReceived(this, args);
                        } catch { }

                        break;
                    }
            }
        }
Exemple #15
0
 public AfpTransportCommandReceivedEventArgs(DsiHeader header, byte[] payload)
 {
     this.Header  = header;
     this.Payload = payload;
 }
Exemple #16
0
 public AfpTransportCommandReceivedEventArgs(DsiHeader header, byte[] payload) {
     this.Header = header;
     this.Payload = payload;
 }
Exemple #17
0
 private void OnCommandSent(DsiHeader header, byte[] payload) {
     AfpTransportCommandReceivedEventArgs args = new AfpTransportCommandReceivedEventArgs(header, payload);
     try {
         CommandSent(this, args);
     } catch { }
 }