public virtual bool Send(DataSet ds, bool needRelations, uint batchSize)
        {
            bool b = false;

            if (AttachedClientSocket == null)
            {
                throw new InvalidOperationException("The asynchronous handler must be attached to an instance of CClientSocket first!");
            }
            if (ds == null)
            {
                throw new ArgumentNullException("Must pass in an valid DataSet object!");
            }
            bool bBatching = Batching;

            if (!bBatching)
            {
                StartBatching();
            }
            using (CScopeUQueue UQueue = new CScopeUQueue())
            {
                CUQueue AdoUQueue = UQueue.UQueue;
                do
                {
                    m_AdoSerializer.PushHeader(AdoUQueue, ds);
                    if (RouteeRequest)
                    {
                        b = SendRouteeResult(AdoUQueue, CAdoSerializationHelper.idDataSetHeaderArrive);
                    }
                    else
                    {
                        b = SendRequest(CAdoSerializationHelper.idDataSetHeaderArrive, AdoUQueue, m_arh);
                    }
                    AdoUQueue.SetSize(0);
                    if (!b)
                    {
                        break;
                    }
                    foreach (DataTable dt in ds.Tables)
                    {
                        b = Send(dt, batchSize);
                        AdoUQueue.SetSize(0);
                        if (!b)
                        {
                            break;
                        }
                    }
                    if (!b)
                    {
                        break;
                    }
                    b = EndDataSet(ds, needRelations);
                    AdoUQueue.SetSize(0);
                } while (false);
                if (!bBatching)
                {
                    CommitBatching(true);
                }
            }
            return(b);
        }
Exemple #2
0
            private void OnChatComing(ulong hSocket, tagChatRequestID chatRequestID, uint len)
            {
                uint        res;
                CSocketPeer sp = Seek(hSocket);

                if (sp == null)
                {
                    return;
                }
                sp.m_CurrReqID = (ushort)chatRequestID;
                CUQueue q = sp.m_qBuffer;

                q.SetSize(0);
                if (len > q.MaxBufferSize)
                {
                    q.Realloc(len);
                }
                unsafe
                {
                    fixed(byte *buffer = q.m_bytes)
                    {
                        res = ServerCoreLoader.RetrieveBuffer(hSocket, len, buffer, true);
                    }
                }
                System.Diagnostics.Debug.Assert(res == len);
                q.SetSize(res);
                sp.OnChatComing(chatRequestID);
            }
Exemple #3
0
 private void StartBLOB(uint lenExpected)
 {
     m_Blob.SetSize(0);
     if (lenExpected > m_Blob.MaxBufferSize)
     {
         m_Blob.Realloc(lenExpected);
     }
     m_Blob.Push(UQueue.IntenalBuffer, UQueue.HeadPosition, UQueue.GetSize());
     UQueue.SetSize(0);
 }
Exemple #4
0
 /// <summary>
 /// Query queue keys opened at server side
 /// </summary>
 /// <param name="gk">A callback for tracking a list of key names</param>
 /// <param name="discarded">a callback for tracking cancel or socket closed event</param>
 /// <returns>true for sending the request successfully, and false for failure</returns>
 public virtual bool GetKeys(DGetKeys gk, DDiscarded discarded)
 {
     return(SendRequest(idGetKeys, (ar) =>
     {
         CUQueue q = ar.UQueue;
         if (gk != null)
         {
             uint size;
             q.Load(out size);
             string[] v = new string[size];
             for (uint n = 0; n < size; ++n)
             {
                 byte[] bytes;
                 q.Load(out bytes);
                 if (bytes != null)
                 {
                     v[n] = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                 }
             }
             gk((CAsyncQueue)ar.AsyncServiceHandler, v);
         }
         else
         {
             q.SetSize(0);
         }
     }, discarded, (DOnExceptionFromServer)null));
 }
Exemple #5
0
        private bool DataFromServerToClient(CAsyncServiceHandler sender, ushort reqId, CUQueue qData)
        {
            bool processed = false;

            switch (reqId)
            {
            case CStreamSerializationHelper.idReadDataFromServerToClient:
                if (qData.GetSize() > 0)
                {
                    lock (m_cs)
                    {
                        CStreamSerializationHelper.Write(m_s, qData);
                        if (Progress != null)
                        {
                            Progress.Invoke(this, (ulong)m_s.Position);
                        }
                    }
                    qData.SetSize(0);
                    processed = true;
                }
                break;

            default:
                break;
            }
            return(processed);
        }
        /// <summary>
        /// Read data from a source stream at server side and send its content onto a client
        /// </summary>
        /// <param name="PeerHandle">A peer socket handle to represent a client</param>
        /// <param name="source">A stream to a source file or other object</param>
        /// <returns>The number of data sent in bytes</returns>
        public static ulong ReadDataFromServerToClient(ulong PeerHandle, Stream source)
        {
            uint  res;
            ulong sent = 0;

            using (CScopeUQueue su = new CScopeUQueue())
            {
                CUQueue q    = su.UQueue;
                uint    read = CStreamSerializationHelper.Read(source, q);
                while (read != 0)
                {
                    unsafe
                    {
                        fixed(byte *p = q.m_bytes)
                        {
                            res = ServerCoreLoader.SendReturnData(PeerHandle, CStreamSerializationHelper.idReadDataFromServerToClient, read, p);
                        }
                    }
                    if (res == CSocketPeer.REQUEST_CANCELED || res == CSocketPeer.SOCKET_NOT_FOUND)
                    {
                        break;
                    }
                    sent += res;
                    q.SetSize(0);
                    read = CStreamSerializationHelper.Read(source, q);
                }
            }
            return(sent);
        }
Exemple #7
0
 public static void Unlock(CUQueue UQueue)
 {
     if (UQueue != null)
     {
         UQueue.SetSize(0);
         m_sQueue.Enqueue(UQueue);
     }
 }
Exemple #8
0
 internal static uint Read(Stream s, CUQueue q)
 {
     uint res;
     if (q.MaxBufferSize < STREAM_CHUNK_SIZE + 16)
         q.Realloc(STREAM_CHUNK_SIZE + 16);
     res = (uint)s.Read(q.m_bytes, 0, (int)STREAM_CHUNK_SIZE);
     q.SetSize(res);
     return res;
 }
Exemple #9
0
 uint EndDataSet(DataSet ds, bool bNeedRelations, CUQueue UQueue)
 {
     UQueue.SetSize(0);
     if (bNeedRelations && ds.Relations != null)
     {
         m_AdoSerializer.Push(UQueue, ds.Relations);
     }
     return(SendResult(CAdoSerializationHelper.idEndDataSet, UQueue));
 }
Exemple #10
0
    private void Chunk()
    {
        CUQueue q = UQueue;

        if (q.GetSize() > 0)
        {
            m_Blob.Push(q.IntenalBuffer, q.GetSize());
            q.SetSize(0);
        }
    }
Exemple #11
0
 public static void Unlock(CUQueue UQueue)
 {
     if (UQueue != null)
     {
         UQueue.SetSize(0);
         lock (m_cs)
         {
             m_sQueue.Add(UQueue);
         }
     }
 }
Exemple #12
0
        internal static uint Read(Stream s, CUQueue q)
        {
            uint res;

            if (q.MaxBufferSize < STREAM_CHUNK_SIZE + 16)
            {
                q.Realloc(STREAM_CHUNK_SIZE + 16);
            }
            res = (uint)s.Read(q.m_bytes, 0, (int)STREAM_CHUNK_SIZE);
            q.SetSize(res);
            return(res);
        }
Exemple #13
0
 protected override void OnResultReturned(ushort reqId, CUQueue mc)
 {
     switch (reqId)
     {
         case idClose:
         case idEnqueue:
             mc.SetSize(0);
             break;
         case idBatchSizeNotified:
             mc.Load(out m_nBatchSize);
             break;
         default:
             break;
     }
 }
Exemple #14
0
 public bool EnqueueBatch(byte[] key, CUQueue q, DEnqueue e)
 {
     if (key == null)
         key = new byte[0];
     if (q == null || q.GetSize() < 2 * sizeof(uint))
     {
         throw new InvalidOperationException("Bad operation");
     }
     CUQueue sb = CScopeUQueue.Lock();
     sb.Save(key).Push(q.IntenalBuffer, q.HeadPosition, q.Size);
     q.SetSize(0);
     bool ok = SendRequest(idEnqueueBatch, sb, GetRH(e));
     CScopeUQueue.Unlock(sb);
     return ok;
 }
Exemple #15
0
        public static void Unlock(CUQueue UQueue)
        {
            if (UQueue != null)
            {
                UQueue.SetSize(0);
#if TASKS_ENABLED
                m_sQueue.Push(UQueue);
#else
                lock (m_cs)
                {
                    m_sQueue.Push(UQueue);
                }
#endif
            }
        }
Exemple #16
0
            public virtual bool EnqueueBatch(byte[] key, CUQueue q, DEnqueue e, DDiscarded discarded)
            {
                if (q == null || q.GetSize() < 2 * sizeof(uint))
                {
                    throw new InvalidOperationException("Bad operation");
                }
                CUQueue sb = CScopeUQueue.Lock();

                sb.Save(key).Push(q.IntenalBuffer, q.HeadPosition, q.Size);
                q.SetSize(0);
                bool ok = SendRequest(idEnqueueBatch, sb, GetRH(e), discarded, (DOnExceptionFromServer)null);

                CScopeUQueue.Unlock(sb);
                return(ok);
            }
Exemple #17
0
    private bool SendRows(CUQueue q, bool transferring)
    {
        uint ret;
        bool batching = (BytesBatched >= DB_CONSTS.DEFAULT_RECORD_BATCH_SIZE);

        if (batching)
        {
            CommitBatching();
        }
        ret = SendResult(transferring ? DB_CONSTS.idTransferring : DB_CONSTS.idEndRows, q.IntenalBuffer, q.GetSize());
        if (batching)
        {
            StartBatching();
        }
        if (ret != q.GetSize())
        {
            return(false); //socket closed or request canceled
        }
        q.SetSize(0);
        return(true);
    }
Exemple #18
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;
            }
        }
            protected override void OnResultReturned(ushort reqId, CUQueue mc)
            {
                switch (reqId)
                {
                case DB_CONSTS.idRowsetHeader:
                {
                    m_Blob.SetSize(0);
                    if (m_Blob.MaxBufferSize > ONE_MEGA_BYTES)
                    {
                        m_Blob.Realloc(ONE_MEGA_BYTES);
                    }
                    CDBColumnInfoArray vColInfo;
                    mc.Load(out vColInfo).Load(out m_indexRowset);
                    KeyValuePair <DRowsetHeader, DRows> p = new KeyValuePair <DRowsetHeader, DRows>();
                    lock (m_csCache)
                    {
                        m_vData.Clear();
                        if (m_mapRowset.ContainsKey(m_indexRowset))
                        {
                            p = m_mapRowset[m_indexRowset];
                        }
                    }
                    if (p.Key != null)
                    {
                        p.Key.Invoke(vColInfo);
                    }
                }
                break;

                case DB_CONSTS.idBeginRows:
                    m_Blob.SetSize(0);
                    m_vData.Clear();
                    break;

                case DB_CONSTS.idTransferring:
                    while (mc.GetSize() > 0)
                    {
                        object vt;
                        mc.Load(out vt);
                        m_vData.Add(vt);
                    }
                    break;

                case DB_CONSTS.idEndRows:
                    if (mc.GetSize() > 0 || m_vData.Count > 0)
                    {
                        object vt;
                        while (mc.GetSize() > 0)
                        {
                            mc.Load(out vt);
                            m_vData.Add(vt);
                        }
                        DRows row = null;
                        lock (m_csCache)
                        {
                            if (m_mapRowset.ContainsKey(m_indexRowset))
                            {
                                row = m_mapRowset[m_indexRowset].Value;
                            }
                        }
                        if (row != null)
                        {
                            row.Invoke(m_vData);
                        }
                    }
                    m_vData.Clear();
                    break;

                case DB_CONSTS.idStartBLOB:
                {
                    m_Blob.SetSize(0);
                    uint len;
                    mc.Load(out len);
                    if (len != uint.MaxValue && len > m_Blob.MaxBufferSize)
                    {
                        m_Blob.Realloc(len);
                    }
                    m_Blob.Push(mc.IntenalBuffer, mc.HeadPosition, mc.GetSize());
                    mc.SetSize(0);
                }
                break;

                case DB_CONSTS.idChunk:
                    m_Blob.Push(mc.IntenalBuffer, mc.GetSize());
                    mc.SetSize(0);
                    break;

                case DB_CONSTS.idEndBLOB:
                    if (mc.GetSize() > 0 || m_Blob.GetSize() > 0)
                    {
                        m_Blob.Push(mc.IntenalBuffer, mc.GetSize());
                        mc.SetSize(0);
                        unsafe
                        {
                            fixed(byte *p = m_Blob.IntenalBuffer)
                            {
                                uint *len = (uint *)(p + m_Blob.HeadPosition + sizeof(ushort));

                                if (*len >= BLOB_LENGTH_NOT_AVAILABLE)
                                {
                                    //length should be reset if BLOB length not available from server side at beginning
                                    *len = (m_Blob.GetSize() - sizeof(ushort) - sizeof(uint));
                                }
                            }
                        }
                        object vt;
                        m_Blob.Load(out vt);
                        m_vData.Add(vt);
                    }
                    break;

                default:
                    base.OnResultReturned(reqId, mc);
                    break;
                }
            }
Exemple #20
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();
            }
        }
Exemple #21
0
        public virtual ulong Send(IDataReader dr, uint batchSize)
        {
            uint  res;
            ulong nSize = 0;
            bool  bSuc;

            if (dr == null)
            {
                throw new ArgumentException("Must pass in a valid data reader interface!");
            }
            using (CScopeUQueue su = new CScopeUQueue())
            {
                CUQueue UQueue    = su.UQueue;
                bool    bBatching = Batching;
                if (!bBatching)
                {
                    bSuc = StartBatching();
                }
                do
                {
                    UQueue.SetSize(0);
                    m_AdoSerializer.PushHeader(UQueue, dr);
                    if (batchSize < 2048)
                    {
                        batchSize = 2048;
                    }
                    UQueue.Save(batchSize);
                    nSize = res = SendResult(CAdoSerializationHelper.idDataReaderHeaderArrive, UQueue);
                    UQueue.SetSize(0);

                    //monitor socket close event and cancel request
                    if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                    {
                        break;
                    }

                    while (dr.Read())
                    {
                        m_AdoSerializer.Push(UQueue, dr);
                        if (UQueue.GetSize() > batchSize)
                        {
                            res = SendResult(CAdoSerializationHelper.idDataReaderRecordsArrive, UQueue);
                            UQueue.SetSize(0);

                            //monitor socket close event and cancel request
                            if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                            {
                                nSize = res;
                                break;
                            }
                            else
                            {
                                nSize += res;
                                if (BytesBatched > 2 * batchSize)
                                {
                                    //if we find too much are stored in batch queue, we send them and start a new batching
                                    bSuc = CommitBatching();
                                    bSuc = StartBatching();
                                }
                            }
                        }
                    }
                    if (UQueue.GetSize() > 0) //remaining
                    {
                        res = SendResult(CAdoSerializationHelper.idDataReaderRecordsArrive, UQueue);
                        UQueue.SetSize(0);

                        //monitor socket close event and cancel request
                        if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                        {
                            nSize = res;
                            break;
                        }
                        nSize += res;
                    }
                } while (false);
                UQueue.SetSize(0);
                res = SendResult(CAdoSerializationHelper.idEndDataReader);
                //monitor socket close event and cancel request
                if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                {
                    nSize = res;
                }
                else
                {
                    nSize += res;
                }

                if (!bBatching)
                {
                    bSuc = CommitBatching();
                }
            }
            return(nSize);
        }
Exemple #22
0
        public virtual ulong Send(DataSet ds, bool bNeedRelations, uint batchSize)
        {
            bool  bSuc;
            uint  res;
            ulong nSize = 0;

            if (ds == null)
            {
                throw new ArgumentException("Must pass in an valid DataSet object!");
            }
            using (CScopeUQueue su = new CScopeUQueue())
            {
                CUQueue UQueue = su.UQueue;
                m_AdoSerializer.PushHeader(UQueue, ds);
                bool bBatching = Batching;
                if (!bBatching)
                {
                    bSuc = StartBatching();
                }
                do
                {
                    nSize = res = SendResult(CAdoSerializationHelper.idDataSetHeaderArrive, UQueue);
                    UQueue.SetSize(0);

                    //monitor socket close event and cancel request
                    if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                    {
                        break;
                    }
                    foreach (DataTable dt in ds.Tables)
                    {
                        ulong rtn = Send(dt, batchSize);
                        UQueue.SetSize(0);

                        if (rtn == CClientPeer.REQUEST_CANCELED || rtn == CClientPeer.SOCKET_NOT_FOUND)
                        {
                            nSize = rtn;
                            break;
                        }
                        else
                        {
                            nSize += rtn;
                        }
                    }

                    res = EndDataSet(ds, bNeedRelations, UQueue);
                    if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                    {
                        nSize = res;
                        break;
                    }
                    else
                    {
                        nSize += res;
                    }
                } while (false);
                UQueue.SetSize(0);
                if (!bBatching && Batching)
                {
                    bSuc = CommitBatching();
                }
            }
            return(nSize);
        }
        public virtual bool Send(IDataReader dr, uint batchSize)
        {
            bool bSuc = false;

            if (dr == null)
            {
                throw new ArgumentNullException("Must pass in a valid data reader interface!");
            }
            if (AttachedClientSocket == null)
            {
                throw new InvalidOperationException("The asynchronous handler must be attached to an instance of CClientSocket first!");
            }
            bool rr        = RouteeRequest;
            bool bBatching = Batching;

            if (!bBatching)
            {
                StartBatching();
            }
            using (CScopeUQueue UQueue = new CScopeUQueue())
            {
                CUQueue AdoUQueue = UQueue.UQueue;
                do
                {
                    m_AdoSerializer.PushHeader(AdoUQueue, dr);
                    if (batchSize < 2048)
                    {
                        batchSize = 2048;
                    }
                    AdoUQueue.Save(batchSize);
                    if (rr)
                    {
                        bSuc = SendRouteeResult(AdoUQueue, CAdoSerializationHelper.idDataReaderHeaderArrive);
                    }
                    else
                    {
                        bSuc = SendRequest(CAdoSerializationHelper.idDataReaderHeaderArrive, AdoUQueue, m_arh);
                    }
                    AdoUQueue.SetSize(0);
                    //monitor socket close event
                    if (!bSuc)
                    {
                        break;
                    }
                    while (dr.Read())
                    {
                        m_AdoSerializer.Push(AdoUQueue, dr);
                        if (AdoUQueue.GetSize() > batchSize)
                        {
                            if (rr)
                            {
                                bSuc = SendRouteeResult(AdoUQueue, CAdoSerializationHelper.idDataReaderRecordsArrive);
                            }
                            else
                            {
                                bSuc = SendRequest(CAdoSerializationHelper.idDataReaderRecordsArrive, AdoUQueue, m_arh);
                            }
                            AdoUQueue.SetSize(0);
                            if (!bSuc)
                            {
                                break;
                            }
                            if (AttachedClientSocket.BytesBatched > 2 * batchSize)
                            {
                                //if we find too much are stored in batch queue, we send them and start a new batching
                                CommitBatching(true);
                                StartBatching();
                            }
                            if (AttachedClientSocket.BytesInSendingBuffer > 60 * 1024)
                            {
                                CommitBatching(true);
                                //if we find there are too much data in sending buffer, we wait until all of data are sent and processed.
                                WaitAll();
                                StartBatching();
                            }
                        }
                    }
                    if (!bSuc)
                    {
                        break;
                    }
                    if (AdoUQueue.GetSize() > 0) //remaining
                    {
                        if (rr)
                        {
                            bSuc = SendRouteeResult(AdoUQueue, CAdoSerializationHelper.idDataReaderRecordsArrive);
                        }
                        else
                        {
                            bSuc = SendRequest(CAdoSerializationHelper.idDataReaderRecordsArrive, AdoUQueue, m_arh);
                        }
                        AdoUQueue.SetSize(0);
                    }
                    if (!bSuc)
                    {
                        break;
                    }
                } while (false);
                if (bSuc)
                {
                    if (rr)
                    {
                        bSuc = SendRouteeResult(CAdoSerializationHelper.idEndDataReader);
                    }
                    else
                    {
                        bSuc = SendRequest(CAdoSerializationHelper.idEndDataReader, m_arh);
                    }
                }
                if (!bBatching)
                {
                    CommitBatching(true);
                }
            }
            return(bSuc);
        }
        public virtual bool Send(DataTable dt, uint batchSize)
        {
            bool bSuc = false;

            if (AttachedClientSocket == null)
            {
                throw new InvalidOperationException("The asynchronous handler must be attached to an instance of CClientSocket first!");
            }
            if (dt == null)
            {
                throw new ArgumentNullException("Must pass in a valid data table object!");
            }
            bool rr        = RouteeRequest;
            bool bBatching = Batching;

            if (!bBatching)
            {
                StartBatching();
            }
            using (CScopeUQueue UQueue = new CScopeUQueue())
            {
                CUQueue AdoUQueue = UQueue.UQueue;
                do
                {
                    AdoUQueue.SetSize(0);
                    m_AdoSerializer.PushHeader(AdoUQueue, dt, false, false);
                    if (batchSize < 2048)
                    {
                        batchSize = 2048;
                    }
                    AdoUQueue.Save(batchSize);
                    if (rr)
                    {
                        bSuc = SendRouteeResult(AdoUQueue, CAdoSerializationHelper.idDataTableHeaderArrive);
                    }
                    else
                    {
                        bSuc = SendRequest(CAdoSerializationHelper.idDataTableHeaderArrive, AdoUQueue, m_arh);
                    }
                    AdoUQueue.SetSize(0);
                    if (!bSuc)
                    {
                        break;
                    }
                    foreach (DataRow dr in dt.Rows)
                    {
                        m_AdoSerializer.Push(AdoUQueue, dr);
                        if (AdoUQueue.GetSize() > batchSize)
                        {
                            if (rr)
                            {
                                bSuc = SendRouteeResult(AdoUQueue, CAdoSerializationHelper.idDataTableRowsArrive);
                            }
                            else
                            {
                                bSuc = SendRequest(CAdoSerializationHelper.idDataTableRowsArrive, AdoUQueue, m_arh);
                            }
                            AdoUQueue.SetSize(0);
                            if (!bSuc)
                            {
                                break;
                            }
                            if (AttachedClientSocket.BytesBatched > 2 * batchSize)
                            {
                                //if we find too much are stored in batch queue, we send them and start a new batching
                                CommitBatching(true);
                                StartBatching();
                            }
                            uint nBytesInSendBuffer = AttachedClientSocket.BytesInSendingBuffer;
                            if (nBytesInSendBuffer > 6 * CAdoSerializationHelper.DEFAULT_BATCH_SIZE) //60k
                            {
                                CommitBatching(true);
                                //if we find there are too much data in sending buffer, we wait until all of data are sent and processed.
                                WaitAll();
                                StartBatching();
                            }
                        }
                    }
                    if (!bSuc)
                    {
                        break;
                    }
                    if (AdoUQueue.GetSize() > 0)
                    {
                        if (rr)
                        {
                            bSuc = SendRouteeResult(AdoUQueue, CAdoSerializationHelper.idDataTableRowsArrive);
                        }
                        else
                        {
                            bSuc = SendRequest(CAdoSerializationHelper.idDataTableRowsArrive, AdoUQueue, m_arh);
                        }
                        AdoUQueue.SetSize(0);
                    }
                    if (!bSuc)
                    {
                        break;
                    }
                } while (false);
                if (bSuc)
                {
                    if (rr)
                    {
                        SendRouteeResult(CAdoSerializationHelper.idEndDataTable);
                    }
                    else
                    {
                        SendRequest(CAdoSerializationHelper.idEndDataTable, m_arh);
                    }
                }
                if (!bBatching)
                {
                    CommitBatching(true);
                }
            }
            return(bSuc);
        }
Exemple #25
0
        public virtual ulong Send(DataTable dt, uint batchSize)
        {
            uint  res;
            ulong nSize;
            bool  bSuc;

            if (dt == null)
            {
                throw new ArgumentException("Must pass in an valid DataTable object!");
            }
            using (CScopeUQueue su = new CScopeUQueue())
            {
                CUQueue UQueue    = su.UQueue;
                bool    bBatching = Batching;
                if (!bBatching)
                {
                    bSuc = StartBatching();
                }
                do
                {
                    //m_AdoSerializer->PushHeader(UQueue, dt, bNeedParentRelations, bNeedChildRelations);
                    m_AdoSerializer.PushHeader(UQueue, dt, false, false);
                    if (batchSize < 2048)
                    {
                        batchSize = 2048;
                    }
                    UQueue.Save(batchSize);
                    nSize = res = SendResult(CAdoSerializationHelper.idDataTableHeaderArrive, UQueue);
                    UQueue.SetSize(0);

                    //monitor socket close event and cancel request
                    if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                    {
                        break;
                    }

                    foreach (DataRow dr in dt.Rows)
                    {
                        m_AdoSerializer.Push(UQueue, dr);
                        if (UQueue.GetSize() > batchSize)
                        {
                            res = SendResult(CAdoSerializationHelper.idDataTableRowsArrive, UQueue);
                            UQueue.SetSize(0);

                            //monitor socket close event and cancel request
                            if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                            {
                                nSize = res;
                                break;
                            }
                            else
                            {
                                if (BytesBatched > 2 * batchSize)
                                {
                                    bSuc = CommitBatching();
                                    bSuc = StartBatching();
                                }
                                nSize += res;
                            }
                        }
                    }
                    if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                    {
                        break;
                    }
                    if (UQueue.GetSize() > 0) //remaining
                    {
                        res = SendResult(CAdoSerializationHelper.idDataTableRowsArrive, UQueue);
                        UQueue.SetSize(0);

                        //monitor socket close event and cancel request
                        if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                        {
                            nSize = res;
                            break;
                        }
                        else
                        {
                            nSize += res;
                        }
                    }
                } while (false);
                UQueue.SetSize(0);
                res = SendResult(CAdoSerializationHelper.idEndDataTable);
                if (res == CClientPeer.REQUEST_CANCELED || res == CClientPeer.SOCKET_NOT_FOUND)
                {
                    nSize = res;
                }
                else
                {
                    nSize += res;
                }
                if (!bBatching)
                {
                    bSuc = CommitBatching();
                }
            }
            return(nSize);
        }
 internal void PushHeader(CUQueue UQueue, DataSet ds)
 {
     byte bData = 0;
     UQueue.SetSize(0);
     /*          if (ds.RemotingFormat == SerializationFormat.Xml)
                   bData += 1;*/
     if (ds.CaseSensitive)
         bData += 2;
     if (ds.EnforceConstraints)
         bData += 4;
     UQueue.Save(ds.Tables.Count);
     UQueue.Save(bData);
     UQueue.Save(ds.DataSetName);
     UQueue.Save(ds.Namespace);
     UQueue.Save(ds.Prefix);
 }
 internal void PushHeader(CUQueue UQueue, DataTable dt, bool bNeedParentRelations, bool bNeedChildRelations)
 {
     int n;
     int nSize;
     if (dt == null)
         return;
     UQueue.SetSize(0);
     m_dts = new tagDataTypeSupported[dt.Columns.Count];
     UQueue.Save(dt.Rows.Count);
     byte bData = 0;
     /*          if (dt.RemotingFormat == SerializationFormat.Xml)
                   bData += 1;*/
     if (bNeedParentRelations)
         bData += 2;
     if (bNeedChildRelations)
         bData += 4;
     UQueue.Save(bData);
     UQueue.Save(dt.TableName);
     Push(UQueue, dt.Columns);
     for (n = 0; n < dt.Columns.Count; n++)
     {
         m_dts[n] = GetDT(dt.Columns[n].DataType.FullName);
     }
     UQueue.Save(dt.DisplayExpression);
     UQueue.Save(dt.MinimumCapacity);
     UQueue.Save(dt.Namespace);
     UQueue.Save(dt.Prefix);
     nSize = dt.PrimaryKey.Length;
     UQueue.Save(nSize);
     for (n = 0; n < nSize; n++)
     {
         UQueue.Save(dt.PrimaryKey[n].Ordinal);
     }
     if (bNeedParentRelations)
     {
         Push(UQueue, dt.ParentRelations);
     }
     if (bNeedChildRelations)
     {
         Push(UQueue, dt.ChildRelations);
     }
 }
        internal void PushHeader(CUQueue UQueue, IDataReader dr)
        {
            int n;
            int nCount;
            if (dr == null)
                throw new ArgumentNullException("Must pass in a valid data reader object");
            ArrayList al = new ArrayList();
            UQueue.SetSize(0);
            UQueue.Save(dr.FieldCount);
            UQueue.Save(dr.RecordsAffected);
            DataTable dtSchema = dr.GetSchemaTable();
            int nColumnName = dtSchema.Columns.IndexOf("ColumnName");
            int nColumnSize = dtSchema.Columns.IndexOf("ColumnSize");
            int nDataType = dtSchema.Columns.IndexOf("DataType");
            int nIsLong = dtSchema.Columns.IndexOf("IsLong");
            int nAllowDBNull = dtSchema.Columns.IndexOf("AllowDBNull");
            int nIsReadOnly = dtSchema.Columns.IndexOf("IsReadOnly");
            int nIsRowVersion = dtSchema.Columns.IndexOf("IsRowVersion");
            int nIsUnique = dtSchema.Columns.IndexOf("IsUnique");
            int nIsKey = dtSchema.Columns.IndexOf("IsKey");
            int nIsAutoIncrement = dtSchema.Columns.IndexOf("IsAutoIncrement");
            m_dts = new tagDataTypeSupported[dr.FieldCount];
            for (n = 0; n < dr.FieldCount; n++)
            {
                tagDataTypeSupported dts = GetDT(dr.GetFieldType(n));
                m_dts[n] = dts;
                UQueue.Save((short)dts);
            }
            foreach (DataRow row in dtSchema.Rows)
            {
                int nData = 0;
                string str = null;
                if (nIsAutoIncrement != -1)
                {
                    if (row[nIsAutoIncrement].Equals(true))
                        nData |= (int)tagColumnBit.cbIsAutoIncrement;
                }

                if (nIsKey != -1)
                {
                    if (row[nIsKey].Equals(true))
                        nData |= (int)tagColumnBit.cbIsKey;
                }

                if (nAllowDBNull != -1)
                {
                    if (row[nAllowDBNull].Equals(true))
                        nData |= (int)tagColumnBit.cbAllowDBNull;
                }

                if (nIsReadOnly != -1)
                {
                    if (row[nIsReadOnly].Equals(true))
                        nData |= (int)tagColumnBit.cbIsReadOnly;
                }

                if (nIsRowVersion != -1)
                {
                    if (row[nIsRowVersion].Equals(true))
                        nData |= (int)tagColumnBit.cbIsRowVersion;
                }

                if (nIsUnique != -1)
                {
                    if (row[nIsUnique].Equals(true))
                        nData |= (int)tagColumnBit.cbIsUnique;
                }

                if (nIsLong != -1)
                {
                    if (row[nIsLong].Equals(true))
                        nData |= (int)tagColumnBit.cbIsLong;
                }

                UQueue.Save(nData);

                nData = 0;
                if (nColumnSize != -1)
                {
                    nData = (int)row[nColumnSize];
                }
                UQueue.Save(nData);

                if (nColumnName != -1)
                {
                    str = (string)row[nColumnName];
                    nCount = CountNames(al, str);
                    if (nCount > 0)
                        str += nCount.ToString();
                    al.Add(str);
                }
                UQueue.Save(str);
            }
        }
Exemple #29
0
            private void OnReqArrive(ulong hSocket, ushort usRequestID, uint len)
            {
                CSocketPeer sp = Seek(hSocket);

                if (sp == null)
                {
                    return;
                }
                sp.m_CurrReqID = usRequestID;
                CUQueue q = sp.m_qBuffer;

                q.SetSize(0);
                if (len > q.MaxBufferSize)
                {
                    q.Realloc(len);
                }
                if (len > 0)
                {
                    uint res;
                    unsafe
                    {
                        fixed(byte *buffer = q.m_bytes)
                        {
                            if (m_nMainThreads <= 1)
                            {
                                CUQueue.CopyMemory(buffer, (void *)ServerCoreLoader.GetRequestBuffer(hSocket), len);
                                res = len;
                            }
                            else
                            {
                                res = ServerCoreLoader.RetrieveBuffer(hSocket, len, buffer, false);
                            }
                        }
                    }
                    System.Diagnostics.Debug.Assert(res == len);
                    q.SetSize(res);
                }
                if (m_svsId != BaseServiceID.sidHTTP)
                {
                    q.OS     = sp.m_os;
                    q.Endian = sp.m_endian;
                    if ((tagBaseRequestID)usRequestID == tagBaseRequestID.idInterrupt)
                    {
                        CClientPeer cp = (CClientPeer)sp;
                        ulong       options;
                        q.Load(out options);
                        cp.OnIntNotified(options);
                        return;
                    }
                }
                else
                {
                    CHttpPeerBase hp = (CHttpPeerBase)sp;
                    hp.m_WebRequestName = null;
                    hp.m_vArg.Clear();
                    if (usRequestID == (ushort)tagHttpRequestID.idUserRequest)
                    {
                        uint    count;
                        sbyte[] reqName;
                        q.Load(out reqName);
                        hp.m_WebRequestName = CUQueue.ToString(reqName);
                        q.Load(out count);
                        for (uint n = 0; n < count; ++n)
                        {
                            object arg;
                            q.Load(out arg);
                            hp.m_vArg.Add(arg);
                        }
                    }
                }
                sp.OnRArrive(usRequestID, len);
            }
Exemple #30
0
 uint EndDataSet(DataSet ds, bool bNeedRelations, CUQueue UQueue)
 {
     UQueue.SetSize(0);
     if (bNeedRelations && ds.Relations != null)
         m_AdoSerializer.Push(UQueue, ds.Relations);
     return SendResult(CAdoSerializationHelper.idEndDataSet, UQueue);
 }
Exemple #31
0
 private bool DataFromServerToClient(CAsyncServiceHandler sender, ushort reqId, CUQueue qData)
 {
     bool processed = false;
     switch (reqId)
     {
         case CStreamSerializationHelper.idReadDataFromServerToClient:
             if (qData.GetSize() > 0)
             {
                 lock (m_cs)
                 {
                     CStreamSerializationHelper.Write(m_s, qData);
                     if (Progress != null)
                         Progress.Invoke(this, (ulong)m_s.Position);
                 }
                 qData.SetSize(0);
                 processed = true;
             }
             break;
         default:
             break;
     }
     return processed;
 }