Exemplo n.º 1
0
        public virtual bool Upload(string localFile, string remoteFile, DUpload up, DTransferring trans, DDiscarded discarded, uint flags, DOnExceptionFromServer se)
        {
            if (localFile == null || localFile.Length == 0)
            {
                throw new ArgumentException("localFile cannot be empty");
            }
            if (remoteFile == null || remoteFile.Length == 0)
            {
                throw new ArgumentException("remoteFile cannot be empty");
            }
            CContext context = new CContext(true, flags);

            context.Upload       = up;
            context.Transferring = trans;
            context.Discarded    = discarded;
            context.FilePath     = remoteFile;
            context.LocalFile    = localFile;
            context.Se           = se;
            lock (m_csFile)
            {
                m_vContext.AddToBack(context);
                uint filesOpened = GetFilesOpened();
                if (m_MaxDownloading > filesOpened)
                {
                    ClientCoreLoader.PostProcessing(Socket.Handle, 0, 0);
                    if (filesOpened == 0)
                    {
                        Socket.DoEcho(); //make sure WaitAll works correctly
                    }
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        public virtual bool Upload(string localFile, string remoteFile, DUpload up, DTransferring trans, DDiscarded discarded, uint flags)
        {
            if (localFile == null || localFile.Length == 0)
            {
                return(false);
            }
            if (remoteFile == null || remoteFile.Length == 0)
            {
                return(false);
            }
            CContext context = new CContext(true, flags);

            context.Upload       = up;
            context.Transferring = trans;
            context.Discarded    = discarded;
            context.FilePath     = remoteFile;
            context.LocalFile    = localFile;
            lock (m_csFile) {
                m_vContext.AddToBack(context);
                if (m_vContext.Count == 1)
                {
                    ClientCoreLoader.PostProcessing(AttachedClientSocket.Handle, 0, 0);
                    AttachedClientSocket.DoEcho(); //make sure WaitAll works correctly
                }
            }
            return(true);
        }
Exemplo n.º 3
0
        public virtual bool Upload(string localFile, string remoteFile, DUpload up, DTransferring trans, DDiscarded discarded, uint flags)
        {
            if (localFile == null || localFile.Length == 0)
            {
                return(false);
            }
            if (remoteFile == null || remoteFile.Length == 0)
            {
                return(false);
            }
            CContext context = new CContext(true, flags);

            context.Upload       = up;
            context.Transferring = trans;
            context.Discarded    = discarded;
            context.FilePath     = remoteFile;
            context.LocalFile    = localFile;
            lock (m_csFile) {
                m_vContext.AddToBack(context);
                return(Transfer());
            }
        }
Exemplo n.º 4
0
        public virtual Task <ErrInfo> upload(string localFile, string remoteFile, DTransferring trans = null, uint flags = FILE_OPEN_TRUNCACTED)
        {
            if (localFile == null || localFile.Length == 0)
            {
                throw new ArgumentException("localFile cannot be empty");
            }
            if (remoteFile == null || remoteFile.Length == 0)
            {
                throw new ArgumentException("remoteFile cannot be empty");
            }
            TaskCompletionSource <ErrInfo> tcs = new TaskCompletionSource <ErrInfo>();
            CContext context = new CContext(false, flags);

            context.Download = (file, res, em) =>
            {
                tcs.TrySetResult(new ErrInfo(res, em));
            };
            context.Transferring = trans;
            context.Discarded    = get_aborted(tcs, idUpload);
            context.FilePath     = remoteFile;
            context.LocalFile    = localFile;
            context.Se           = get_se(tcs);
            context.Fut          = tcs;
            lock (m_csFile)
            {
                m_vContext.AddToBack(context);
                uint filesOpened = GetFilesOpened();
                if (m_MaxDownloading > filesOpened)
                {
                    ClientCoreLoader.PostProcessing(Socket.Handle, 0, 0);
                    if (filesOpened == 0)
                    {
                        Socket.DoEcho(); //make sure WaitAll works correctly
                    }
                }
            }
            return(tcs.Task);
        }
Exemplo n.º 5
0
        protected override void OnResultReturned(ushort reqId, CUQueue mc)
        {
            switch (reqId)
            {
            case idDownload:
            {
                int    res;
                string errMsg;
                mc.Load(out res).Load(out errMsg);
                DDownload dl;
                lock (m_csFile)
                {
                    CContext context = m_vContext[0];
                    if (context.File != null)
                    {
                        context.File.Close();
                        context.File = null;
                    }
                    else if (res == 0)
                    {
                        res    = CANNOT_OPEN_LOCAL_FILE_FOR_WRITING;
                        errMsg = context.ErrMsg;
                    }
                    dl = context.Download;
                }
                if (dl != null)
                {
                    dl(this, res, errMsg);
                }
                lock (m_csFile)
                {
                    m_vContext.RemoveFromFront();
                }
            }
            break;

            case idStartDownloading:
                lock (m_csFile)
                {
                    CContext context = m_vContext[0];
                    mc.Load(out context.FileSize);
                    try
                    {
                        FileMode fm;
                        if ((context.Flags & FILE_OPEN_TRUNCACTED) == FILE_OPEN_TRUNCACTED)
                        {
                            fm = FileMode.Create;
                        }
                        else if ((context.Flags & FILE_OPEN_APPENDED) == FILE_OPEN_APPENDED)
                        {
                            fm = FileMode.Append;
                        }
                        else
                        {
                            fm = FileMode.OpenOrCreate;
                        }
                        FileShare fs = FileShare.None;
                        if ((context.Flags & FILE_OPEN_SHARE_WRITE) == FILE_OPEN_SHARE_WRITE)
                        {
                            fs = FileShare.Write;
                        }
                        context.File = new FileStream(context.LocalFile, fm, FileAccess.Write, fs);
                    }
                    catch (Exception err)
                    {
                        context.ErrMsg = err.Message;
                    }
                    finally { }
                }
                break;

            case idDownloading:
            {
                long          downloaded = -1;
                DTransferring trans      = null;
                lock (m_cs)
                {
                    CContext context = m_vContext[0];
                    trans = context.Transferring;
                    if (context.File != null)
                    {
                        byte[] buffer = mc.IntenalBuffer;
                        context.File.Write(buffer, 0, (int)mc.GetSize());
                        downloaded = context.File.Position;
                    }
                }
                mc.SetSize(0);
                if (trans != null)
                {
                    trans(this, downloaded);
                }
            }
            break;

            case idUpload:
            {
                bool    removed = false;
                DUpload upl     = null;
                int     res;
                string  errMsg;
                mc.Load(out res).Load(out errMsg);
                if (res != 0)
                {
                    lock (m_csFile)
                    {
                        CContext context = m_vContext[0];
                        removed = true;
                        upl     = context.Upload;
                        if (context.File != null)
                        {
                            context.File.Close();
                        }
                    }
                }
                if (upl != null)
                {
                    upl(this, res, errMsg);
                }
                if (removed)
                {
                    lock (m_csFile)
                    {
                        m_vContext.RemoveFromFront();
                    }
                }
            }
            break;

            case idUploading:
            {
                DTransferring trans = null;
                long          uploaded;
                mc.Load(out uploaded);
                if (uploaded > 0)
                {
                    lock (m_csFile)
                    {
                        CContext context = m_vContext[0];
                        trans = context.Transferring;
                    }
                }
                if (trans != null)
                {
                    trans(this, uploaded);
                }
            }
            break;

            case idUploadCompleted:
            {
                DUpload upl = null;
                lock (m_csFile)
                {
                    CContext context = m_vContext[0];
                    upl = context.Upload;
                    if (context.File != null)
                    {
                        context.File.Close();
                        context.File = null;
                    }
                }
                if (upl != null)
                {
                    upl(this, 0, "");
                }
                lock (m_csFile)
                {
                    m_vContext.RemoveFromFront();
                }
            }
            break;

            default:
                base.OnResultReturned(reqId, mc);
                break;
            }
            lock (m_csFile)
            {
                Transfer();
            }
        }
Exemplo n.º 6
0
 public bool Download(string localFile, string remoteFile, DDownload dl, DTransferring trans, DDiscarded discarded)
 {
     return(Download(localFile, remoteFile, dl, trans, discarded, FILE_OPEN_TRUNCACTED));
 }
Exemplo n.º 7
0
 public bool Upload(string localFile, string remoteFile, DUpload up, DTransferring trans, DDiscarded discarded)
 {
     return(Upload(localFile, remoteFile, up, trans, discarded, FILE_OPEN_TRUNCACTED));
 }
Exemplo n.º 8
0
 public bool Upload(string localFile, string remoteFile, DUpload up, DTransferring trans)
 {
     return(Upload(localFile, remoteFile, up, trans, null, FILE_OPEN_TRUNCACTED));
 }
Exemplo n.º 9
0
 public bool Download(string localFile, string remoteFile, DDownload dl, DTransferring trans, DDiscarded discarded, uint flags)
 {
     return(Download(localFile, remoteFile, dl, trans, discarded, flags, null));
 }
Exemplo n.º 10
0
 public bool Download(string localFile, string remoteFile, DDownload dl, DTransferring trans)
 {
     return(Download(localFile, remoteFile, dl, trans, null, FILE_OPEN_TRUNCACTED, null));
 }
Exemplo n.º 11
0
 public bool Upload(string localFile, string remoteFile, DUpload up, DTransferring trans, DDiscarded discarded, uint flags)
 {
     return(Upload(localFile, remoteFile, up, trans, discarded, flags, null));
 }
Exemplo n.º 12
0
        protected override void OnResultReturned(ushort reqId, CUQueue mc)
        {
            switch (reqId)
            {
            case idDownload:
            {
                int    res;
                string errMsg;
                mc.Load(out res).Load(out errMsg);
                DDownload dl = null;
                lock (m_csFile)
                {
                    if (m_vContext.Count > 0)
                    {
                        CContext ctx = m_vContext[0];
                        ctx.ErrCode = res;
                        ctx.ErrMsg  = errMsg;
                        dl          = ctx.Download;
                    }
                }
                if (dl != null)
                {
                    dl.Invoke(this, res, errMsg);
                }
                lock (m_csFile)
                {
                    if (m_vContext.Count > 0)
                    {
                        CloseFile(m_vContext.RemoveFromFront());
                    }
                }
                OnPostProcessing(0, 0);
            }
            break;

            case idStartDownloading:
                lock (m_csFile)
                {
                    long   fileSize;
                    string localFile, remoteFile;
                    uint   flags;
                    long   initSize;
                    mc.Load(out fileSize).Load(out localFile).Load(out remoteFile).Load(out flags).Load(out initSize);
                    lock (m_csFile)
                    {
                        if (m_vContext.Count == 0)
                        {
                            CContext ctx = new CContext(false, flags);
                            ctx.LocalFile = localFile;
                            ctx.FilePath  = remoteFile;
                            OpenLocalWrite(ctx);
                            ctx.InitSize = initSize;
                            m_vContext.AddToBack(ctx);
                        }
                        CContext context = m_vContext[0];
                        context.FileSize = fileSize;
                        initSize         = (context.InitSize > 0) ? context.InitSize : 0;
                        if (context.File.Position > initSize)
                        {
                            context.File.SetLength(initSize);
                        }
                    }
                }
                break;

            case idDownloading:
            {
                long          downloaded = 0;
                DTransferring trans      = null;
                CContext      context    = null;
                lock (m_csFile)
                {
                    if (m_vContext.Count > 0)
                    {
                        context = m_vContext[0];
                        trans   = context.Transferring;
                        byte[] buffer = mc.IntenalBuffer;
                        try
                        {
                            context.File.Write(buffer, 0, (int)mc.GetSize());
                            long initSize = (context.InitSize > 0) ? context.InitSize : 0;
                            downloaded = context.File.Position - initSize;
                        }
                        catch (System.IO.IOException err)
                        {
                            context.ErrMsg = err.Message;
#if NO_HRESULT
                            context.ErrCode = CANNOT_OPEN_LOCAL_FILE_FOR_WRITING;
#else
                            context.ErrCode = err.HResult;
#endif
                        }
                    }
                }
                mc.SetSize(0);
                if (context != null && context.HasError)
                {
                    if (context.Download != null)
                    {
                        context.Download.Invoke(this, context.ErrCode, context.ErrMsg);
                    }
                    CloseFile(m_vContext.RemoveFromFront());
                    OnPostProcessing(0, 0);
                }
                else if (trans != null)
                {
                    trans.Invoke(this, downloaded);
                }
            }
            break;

            case idUploadBackup:
                break;

            case idUpload:
            {
                CContext context = null;
                int      res;
                string   errMsg;
                mc.Load(out res).Load(out errMsg);
                if (res != 0 || (errMsg != null && errMsg.Length > 0))
                {
                    lock (m_csFile)
                    {
                        if (m_vContext.Count > 0)
                        {
                            context = m_vContext[0];
                            mc.Load(out context.InitSize);
                            context.ErrCode = res;
                            context.ErrMsg  = errMsg;
                        }
                    }
                }
                else
                {
                    CClientSocket cs = Socket;
                    lock (m_csFile)
                    {
                        if (m_vContext.Count > 0)
                        {
                            context = m_vContext[0];
                            mc.Load(out context.InitSize);
                            using (CScopeUQueue sb = new CScopeUQueue())
                            {
                                DAsyncResultHandler    rh = null;
                                DOnExceptionFromServer se = null;
                                if (sb.UQueue.MaxBufferSize < STREAM_CHUNK_SIZE)
                                {
                                    sb.UQueue.Realloc(STREAM_CHUNK_SIZE);
                                }
                                byte[] buffer = sb.UQueue.IntenalBuffer;
                                try
                                {
                                    context.QueueOk = cs.ClientQueue.StartJob();
                                    bool queue_enabled = cs.ClientQueue.Available;
                                    if (queue_enabled)
                                    {
                                        SendRequest(idUploadBackup, context.FilePath, context.Flags, context.FileSize, context.InitSize, rh, context.Discarded, se);
                                    }
                                    int ret = context.File.Read(buffer, 0, (int)STREAM_CHUNK_SIZE);
                                    while (ret == STREAM_CHUNK_SIZE)
                                    {
                                        if (!SendRequest(idUploading, buffer, (uint)ret, rh, context.Discarded, se))
                                        {
                                            context.ErrCode = cs.ErrorCode;
                                            context.ErrMsg  = cs.ErrorMsg;
                                            break;
                                        }
                                        ret = context.File.Read(buffer, 0, (int)STREAM_CHUNK_SIZE);
                                        if (queue_enabled)
                                        {
                                            //save file into client message queue
                                        }
                                        else if (cs.BytesInSendingBuffer > 40 * STREAM_CHUNK_SIZE)
                                        {
                                            break;
                                        }
                                    }
                                    if (ret > 0 && !context.HasError)
                                    {
                                        if (!SendRequest(idUploading, buffer, (uint)ret, rh, context.Discarded, se))
                                        {
                                            context.ErrCode = cs.ErrorCode;
                                            context.ErrMsg  = cs.ErrorMsg;
                                        }
                                    }
                                    if (ret < STREAM_CHUNK_SIZE && !context.HasError)
                                    {
                                        context.Sent = true;
                                        SendRequest(idUploadCompleted, rh, context.Discarded, se);
                                        if (context.QueueOk)
                                        {
                                            Socket.ClientQueue.EndJob();
                                        }
                                    }
                                }
                                catch (System.IO.IOException err)
                                {
                                    errMsg = err.Message;
#if NO_HRESULT
                                    res = CANNOT_OPEN_LOCAL_FILE_FOR_READING;
#else
                                    res = err.HResult;
#endif
                                    context.ErrCode = res;
                                    context.ErrMsg  = errMsg;
                                }
                            }
                        }
                    }
                }
                if (context != null && context.HasError)
                {
                    if (context.Upload != null)
                    {
                        context.Upload.Invoke(this, context.ErrCode, context.ErrMsg);
                    }
                    lock (m_csFile)
                    {
                        CloseFile(m_vContext.RemoveFromFront());
                    }
                    if (context.QueueOk)
                    {
                        Socket.ClientQueue.AbortJob();
                    }
                    OnPostProcessing(0, 0);
                }
            }
            break;

            case idUploading:
            {
                int           errCode = 0;
                string        errMsg  = "";
                CContext      context = null;
                DTransferring trans   = null;
                long          uploaded;
                mc.Load(out uploaded);
                if (mc.GetSize() >= 8)
                {
                    mc.Load(out errCode).Load(out errMsg);
                }
                lock (m_csFile)
                {
                    if (m_vContext.Count > 0)
                    {
                        context = m_vContext[0];
                        trans   = context.Transferring;
                        if (uploaded < 0 || errCode != 0 || errMsg.Length != 0)
                        {
                            context.ErrCode = errCode;
                            context.ErrMsg  = errMsg;
                            CloseFile(context);
                        }
                        else if (!context.Sent)
                        {
                            using (CScopeUQueue sb = new CScopeUQueue())
                            {
                                DAsyncResultHandler    rh = null;
                                DOnExceptionFromServer se = null;
                                if (sb.UQueue.MaxBufferSize < STREAM_CHUNK_SIZE)
                                {
                                    sb.UQueue.Realloc(STREAM_CHUNK_SIZE);
                                }
                                byte[] buffer = sb.UQueue.IntenalBuffer;
                                try
                                {
                                    int ret = context.File.Read(buffer, 0, (int)STREAM_CHUNK_SIZE);
                                    if (ret > 0)
                                    {
                                        SendRequest(idUploading, buffer, (uint)ret, rh, context.Discarded, se);
                                    }
                                    if (ret < STREAM_CHUNK_SIZE)
                                    {
                                        context.Sent = true;
                                        SendRequest(idUploadCompleted, rh, context.Discarded, se);
                                    }
                                }
                                catch (System.IO.IOException err)
                                {
                                    context.ErrMsg = err.Message;
#if NO_HRESULT
                                    context.ErrCode = CANNOT_OPEN_LOCAL_FILE_FOR_READING;
#else
                                    context.ErrCode = err.HResult;
#endif
                                }
                            }
                        }
                    }
                }
                if (context != null && context.HasError)
                {
                    if (context.Upload != null)
                    {
                        context.Upload.Invoke(this, context.ErrCode, context.ErrMsg);
                    }
                    lock (m_csFile)
                    {
                        CloseFile(m_vContext.RemoveFromFront());
                    }
                    OnPostProcessing(0, 0);
                }
                else if (trans != null)
                {
                    trans.Invoke(this, uploaded);
                }
            }
            break;

            case idUploadCompleted:
            {
                DUpload upl = null;
                lock (m_csFile)
                {
                    if (m_vContext.Count > 0)
                    {
                        if (m_vContext[0].File != null)
                        {
                            upl = m_vContext[0].Upload;
                        }
                        else
                        {
                            m_vContext[0].QueueOk = false;
                            m_vContext[0].Sent    = false;
                            CloseFile(m_vContext[0]);
                        }
                    }
                }
                if (upl != null)
                {
                    upl.Invoke(this, 0, "");
                }
                lock (m_csFile)
                {
                    if (m_vContext.Count > 0)
                    {
                        if (m_vContext[0].File != null)
                        {
                            CloseFile(m_vContext.RemoveFromFront());
                        }
                    }
                }
                OnPostProcessing(0, 0);
            }
            break;

            default:
                base.OnResultReturned(reqId, mc);
                break;
            }
        }