Пример #1
0
        private bool Transfer()
        {
            int index = 0;
            DAsyncResultHandler    rh = null;
            DOnExceptionFromServer se = null;
            CClientSocket          cs = AttachedClientSocket;

            if (!cs.Sendable)
            {
                return(false);
            }
            uint sent_buffer_size = cs.BytesInSendingBuffer;

            if (sent_buffer_size > 3 * STREAM_CHUNK_SIZE)
            {
                return(true);
            }
            while (index < m_vContext.Count)
            {
                CContext context = m_vContext[index];
                if (context.Sent)
                {
                    ++index;
                    continue;
                }
                if (context.Uploading && context.Tried && context.File == null)
                {
                    if (index == 0)
                    {
                        if (context.Upload != null)
                        {
                            context.Upload(this, CANNOT_OPEN_LOCAL_FILE_FOR_READING, context.ErrMsg);
                        }
                        m_vContext.RemoveFromFront();
                    }
                    else
                    {
                        ++index;
                    }
                    continue;
                }
                if (context.Uploading)
                {
                    if (!context.Tried)
                    {
                        context.Tried = true;
                        try
                        {
                            FileShare fs = FileShare.None;
                            if ((context.Flags & FILE_OPEN_SHARE_READ) == FILE_OPEN_SHARE_READ)
                            {
                                fs = FileShare.Read;
                            }
                            context.File     = new FileStream(context.LocalFile, FileMode.Open, FileAccess.Read, fs);
                            context.FileSize = context.File.Length;
                            IClientQueue cq = AttachedClientSocket.ClientQueue;
                            if (cq.Available)
                            {
                                if (!cq.StartJob())
                                {
                                    context.File.Close();
                                    context.File = null;
                                    throw new Exception("Cannot start queue job");
                                }
                            }
                            if (!SendRequest(idUpload, context.FilePath, context.Flags, context.FileSize, rh, context.Discarded, se))
                            {
                                return(false);
                            }
                        }
                        catch (Exception err)
                        {
                            context.ErrMsg = err.Message;
                        }
                        finally { }
                    }
                    if (context.File == null)
                    {
                        if (index == 0)
                        {
                            if (context.Upload != null)
                            {
                                context.Upload(this, CANNOT_OPEN_LOCAL_FILE_FOR_READING, context.ErrMsg);
                            }
                            m_vContext.RemoveFromFront();
                        }
                        else
                        {
                            ++index;
                        }
                        continue;
                    }
                    else
                    {
                        using (CScopeUQueue sb = new CScopeUQueue())
                        {
                            if (sb.UQueue.MaxBufferSize < STREAM_CHUNK_SIZE)
                            {
                                sb.UQueue.Realloc(STREAM_CHUNK_SIZE);
                            }
                            byte[] buffer = sb.UQueue.IntenalBuffer;
                            int    ret    = context.File.Read(buffer, 0, (int)STREAM_CHUNK_SIZE);
                            while (ret > 0)
                            {
                                if (!SendRequest(idUploading, buffer, (uint)ret, rh, context.Discarded, se))
                                {
                                    return(false);
                                }
                                sent_buffer_size = cs.BytesInSendingBuffer;
                                if (ret < (int)STREAM_CHUNK_SIZE)
                                {
                                    break;
                                }
                                if (sent_buffer_size >= 5 * STREAM_CHUNK_SIZE)
                                {
                                    break;
                                }
                                ret = context.File.Read(buffer, 0, (int)STREAM_CHUNK_SIZE);
                            }
                            if (ret < (int)STREAM_CHUNK_SIZE)
                            {
                                context.Sent = true;
                                if (!SendRequest(idUploadCompleted, rh, context.Discarded, se))
                                {
                                    return(false);
                                }
                                IClientQueue cq = AttachedClientSocket.ClientQueue;
                                if (cq.Available)
                                {
                                    cq.EndJob();
                                }
                            }
                            if (sent_buffer_size >= 4 * STREAM_CHUNK_SIZE)
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    if (!SendRequest(idDownload, context.FilePath, context.Flags, rh, context.Discarded, se))
                    {
                        return(false);
                    }
                    context.Sent     = true;
                    context.Tried    = true;
                    sent_buffer_size = cs.BytesInSendingBuffer;
                    if (sent_buffer_size > 3 * STREAM_CHUNK_SIZE)
                    {
                        break;
                    }
                }
                ++index;
            }
            return(true);
        }