public AfpResultCode Process(IAfpSession session, DsiHeader dsiHeader, AfpStream requestStream, AfpStream responseStream)
        {
            byte flag = requestStream.ReadUInt8();
            short forkId = requestStream.ReadInt16();
            long offset = requestStream.ReadInt64();
            long reqCount = requestStream.ReadInt64();

            IAfpFork fork = session.GetFork(forkId);

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

            byte[] writeData = requestStream.ReadBytes((uint)reqCount);
            long position = 0;

            if (flag == 1) {
                // End of the fork
                position = (fork.DataProvider.Length - offset);
            } else {
                position = offset;
            }

            fork.DataProvider.Write(position, writeData, 0, writeData.Length);
            responseStream.WriteUInt64((ulong)(position + writeData.LongLength));

            return AfpResultCode.FPNoErr;
        }
        public AfpResultCode Process(IAfpSession session, DsiHeader dsiHeader, AfpStream requestStream, AfpStream responseStream)
        {
            byte  flag     = requestStream.ReadUInt8();
            short forkId   = requestStream.ReadInt16();
            long  offset   = requestStream.ReadInt64();
            long  reqCount = requestStream.ReadInt64();

            IAfpFork fork = session.GetFork(forkId);

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

            byte[] writeData = requestStream.ReadBytes((uint)reqCount);
            long   position  = 0;

            if (flag == 1)
            {
                // End of the fork
                position = (fork.DataProvider.Length - offset);
            }
            else
            {
                position = offset;
            }

            fork.DataProvider.Write(position, writeData, 0, writeData.Length);
            responseStream.WriteUInt64((ulong)(position + writeData.LongLength));

            return(AfpResultCode.FPNoErr);
        }
        public AfpResultCode Process(IAfpSession session, DsiHeader dsiHeader, AfpStream requestStream, AfpStream responseStream)
        {
            requestStream.ReadUInt8(); // Pad

            AfpSessionTokenTypes type = requestStream.ReadEnum<AfpSessionTokenTypes>();
            int idLength = requestStream.ReadInt32();
            int? timestamp = null;

            if (type == AfpSessionTokenTypes.kLoginWithTimeAndID ||
                type == AfpSessionTokenTypes.kReconnWithTimeAndID) {
                timestamp = requestStream.ReadInt32();
            }

            Guid clientToken = new Guid(requestStream.ReadBytes((uint)idLength));

            switch (type) {
                case AfpSessionTokenTypes.kLoginWithID: {
                        // Find existing session and disconnect it.
                        IAfpSession existingSession = session.Server.FindSession(clientToken, AfpSessionSearchType.ClientIssued);

                        if (existingSession != null) {
                            existingSession.Close();
                        }

                        break;
                    }
                case AfpSessionTokenTypes.kLoginWithTimeAndID: {
                        if (!timestamp.HasValue) {
                            return AfpResultCode.FPParamErr;
                        }

                        // Find an existing session.
                        IAfpSession existingSession = session.Server.FindSession(clientToken, AfpSessionSearchType.ClientIssued);

                        if (existingSession != null && existingSession != session) {
                            // Existing session found, transfer resources if timestamp matches.
                            if (!existingSession.Timestamp.HasValue || existingSession.Timestamp.Value != timestamp.Value) {
                                // Timestamp is different, close old session.
                                existingSession.Close();
                            }
                        }

                        break;
                    }
            }

            session.Timestamp = timestamp;
            session.ClientToken = clientToken;
            session.ServerToken = Guid.NewGuid();

            byte[] token = session.ServerToken.Value.ToByteArray();

            responseStream.WriteInt32(token.Length);
            responseStream.WriteBytes(token);

            return AfpResultCode.FPNoErr;
        }
        public AfpResultCode Process(IAfpSession session, DsiHeader dsiHeader, AfpStream requestStream, AfpStream responseStream)
        {
            requestStream.ReadUInt8(); // Padding
            requestStream.ReadInt16(); // Type (always zero)

            int tokenLength = requestStream.ReadInt32();
            byte[] tokenData = requestStream.ReadBytes((uint)tokenLength);

            Guid token = new Guid(tokenData);

            IAfpSession otherSession = session.Server.FindSession(token, AfpSessionSearchType.ServerIssued);

            if (otherSession == null) {
                return AfpResultCode.FPMiscErr;
            }

            otherSession.Recover(session);

            return AfpResultCode.FPNoErr;
        }
        public AfpResultCode Process(IAfpSession session, DsiHeader dsiHeader, AfpStream requestStream, AfpStream responseStream)
        {
            requestStream.ReadUInt8(); // Padding
            requestStream.ReadInt16(); // Type (always zero)

            int tokenLength = requestStream.ReadInt32();

            byte[] tokenData = requestStream.ReadBytes((uint)tokenLength);

            Guid token = new Guid(tokenData);

            IAfpSession otherSession = session.Server.FindSession(token, AfpSessionSearchType.ServerIssued);

            if (otherSession == null)
            {
                return(AfpResultCode.FPMiscErr);
            }

            otherSession.Recover(session);

            return(AfpResultCode.FPNoErr);
        }
        public AfpResultCode Process(IAfpSession session, DsiHeader dsiHeader, AfpStream requestStream, AfpStream responseStream)
        {
            requestStream.ReadUInt8(); // Pad

            AfpSessionTokenTypes type = requestStream.ReadEnum <AfpSessionTokenTypes>();
            int idLength  = requestStream.ReadInt32();
            int?timestamp = null;

            if (type == AfpSessionTokenTypes.kLoginWithTimeAndID ||
                type == AfpSessionTokenTypes.kReconnWithTimeAndID)
            {
                timestamp = requestStream.ReadInt32();
            }

            Guid clientToken = new Guid(requestStream.ReadBytes((uint)idLength));

            switch (type)
            {
            case AfpSessionTokenTypes.kLoginWithID: {
                // Find existing session and disconnect it.
                IAfpSession existingSession = session.Server.FindSession(clientToken, AfpSessionSearchType.ClientIssued);

                if (existingSession != null)
                {
                    existingSession.Close();
                }

                break;
            }

            case AfpSessionTokenTypes.kLoginWithTimeAndID: {
                if (!timestamp.HasValue)
                {
                    return(AfpResultCode.FPParamErr);
                }

                // Find an existing session.
                IAfpSession existingSession = session.Server.FindSession(clientToken, AfpSessionSearchType.ClientIssued);

                if (existingSession != null && existingSession != session)
                {
                    // Existing session found, transfer resources if timestamp matches.
                    if (!existingSession.Timestamp.HasValue || existingSession.Timestamp.Value != timestamp.Value)
                    {
                        // Timestamp is different, close old session.
                        existingSession.Close();
                    }
                }

                break;
            }
            }

            session.Timestamp   = timestamp;
            session.ClientToken = clientToken;
            session.ServerToken = Guid.NewGuid();

            byte[] token = session.ServerToken.Value.ToByteArray();

            responseStream.WriteInt32(token.Length);
            responseStream.WriteBytes(token);

            return(AfpResultCode.FPNoErr);
        }