static void EnqueueToServerBatch(CAsyncQueue sq, string message, int cycles, uint batchSize = 16 * 1024) { Console.WriteLine("Going to enqueue " + cycles + " messages ......"); using (CScopeUQueue sb = new CScopeUQueue()) { CUQueue q = sb.UQueue; byte[] utf8 = Encoding.UTF8.GetBytes(message); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); for (int n = 0; n < cycles; ++n) { CAsyncQueue.BatchMessage(idMessage, utf8, q); if (q.GetSize() >= batchSize) { sq.EnqueueBatch(TEST_QUEUE_KEY, q, (res) => { }); } } if (q.GetSize() > 0) { sq.EnqueueBatch(TEST_QUEUE_KEY, q, (res) => { }); } sq.WaitAll(); sw.Stop(); Console.WriteLine(cycles + " messages sent to server and enqueued within " + sw.ElapsedMilliseconds + " ms"); } }
protected bool SendRouteeResult <T0, T1, T2, T3>(T0 t0, T1 t1, T2 t2, T3 t3, ushort reqId) { CUQueue su = CScopeUQueue.Lock(); su.Save(t0).Save(t1).Save(t2).Save(t3); bool ok = SendRouteeResult(su, reqId); CScopeUQueue.Unlock(su); return(ok); }
protected bool SendRouteeResult <T0>(T0 t0, ushort reqId) { CUQueue su = CScopeUQueue.Lock(); su.Save(t0); bool ok = SendRouteeResult(su, reqId); CScopeUQueue.Unlock(su); return(ok); }
static void InsertBLOBByPreparedStatement(COdbc odbc) { string wstr = ""; while (wstr.Length < 128 * 1024) { wstr += "广告做得不那么夸张的就不说了,看看这三家,都是正儿八经的公立三甲,附属医院,不是武警,也不是部队,更不是莆田,都在卫生部门直接监管下,照样明目张胆地骗人。"; } string str = ""; while (str.Length < 256 * 1024) { str += "The epic takedown of his opponent on an all-important voting day was extraordinary even by the standards of the 2016 campaign -- and quickly drew a scathing response from Trump."; } string sqlInsert = "insert into employee(EmployeeId,CompanyId,name,JoinDate,myimage,DESCRIPTION,Salary)values(?,?,?,?,?,?,?)"; bool ok = odbc.Prepare(sqlInsert, dr); CDBVariantArray vData = new CDBVariantArray(); using (CScopeUQueue sbBlob = new CScopeUQueue()) { //first set of data vData.Add(1); vData.Add(1); //google company id vData.Add("Ted Cruz"); vData.Add(DateTime.Now); sbBlob.Save(wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(wstr); vData.Add(254000.24m); //second set of data vData.Add(2); vData.Add(1); //google company id vData.Add("Donald Trump"); vData.Add(DateTime.Now); sbBlob.UQueue.SetSize(0); sbBlob.Save(str); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(str); vData.Add(20254000.15m); //third set of data vData.Add(3); vData.Add(2); //Microsoft company id vData.Add("Hillary Clinton"); vData.Add(DateTime.Now); sbBlob.Save(wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(wstr); vData.Add(6254000.08m); //send three sets of parameterized data in one shot for processing ok = odbc.Execute(vData, er); } }
public bool Enqueue <T0>(byte[] key, ushort idMessage, T0 t0, DEnqueue e, DDiscarded discarded) { if (key == null) { key = new byte[0]; } using (CScopeUQueue sb = new CScopeUQueue()) { sb.Save(t0); return(Enqueue(key, idMessage, sb.UQueue, e, discarded)); } }
public bool Enqueue <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(byte[] key, ushort idMessage, T0 t0, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, DEnqueue e, DDiscarded discarded) { if (key == null) { key = new byte[0]; } using (CScopeUQueue sb = new CScopeUQueue()) { sb.Save(t0).Save(t1).Save(t2).Save(t3).Save(t4).Save(t5).Save(t6).Save(t7).Save(t8).Save(t9); return(Enqueue(key, idMessage, sb.UQueue, e, discarded)); } }
private Stream m_s; //protected by m_cs private ulong SendDataFromClientToServer() { if (m_ash.AttachedClientSocket.BytesInSendingBuffer > CStreamSerializationHelper.STREAM_CHUNK_SIZE) { return(0); } ulong send = 0; using (CScopeUQueue su = new CScopeUQueue()) { if (m_s == null) { return(0); } uint read = CStreamSerializationHelper.Read(m_s, su.UQueue); while (read > 0) { bool ok = m_ash.SendRequest(CStreamSerializationHelper.idWriteDataFromClientToServer, su.UQueue.m_bytes, read, (ar) => { SendDataFromClientToServer(); }); if (Progress != null) { Progress.Invoke(this, (ulong)m_s.Position); } if (!ok) { m_s = null; break; } send += read; if (m_ash.AttachedClientSocket.BytesInSendingBuffer > 10 * CStreamSerializationHelper.STREAM_CHUNK_SIZE) { break; } read = CStreamSerializationHelper.Read(m_s, su.UQueue); if (read == 0) { if (!m_ash.SendRequest(CStreamSerializationHelper.idUploadCompleted, (dc) => { lock (m_cs) { if (Progress != null) { Progress.Invoke(this, (ulong)m_s.Position); } m_s = null; } })) { m_s = null; } } } } return(send); }
public bool Enqueue <T0, T1, T2, T3, T4>(byte[] key, ushort idMessage, T0 t0, T1 t1, T2 t2, T3 t3, T4 t4, DEnqueue e, DDiscarded discarded) { if (key == null) { key = new byte[0]; } using (CScopeUQueue sb = new CScopeUQueue()) { sb.Save(t0).Save(t1).Save(t2).Save(t3).Save(t4); return(Enqueue(key, idMessage, sb.UQueue, e, discarded)); } }
static void Main(string[] args) { using (CScopeUQueue sb = new CScopeUQueue()) { CMyStruct msOrig = CMyStruct.MakeOne(); sb.Save(msOrig); CMyStruct ms = sb.Load <CMyStruct>(); System.Diagnostics.Debug.Assert(sb.UQueue.GetSize() == 0); //check if both msOriginal and ms are equal in value. } }
public virtual bool Enqueue(byte[] key, ushort idMessage, byte[] bytes, DEnqueue e, DDiscarded discarded) { if (key == null) { key = new byte[0]; } CUQueue sb = CScopeUQueue.Lock(); sb.Save(key).Save(idMessage).Push(bytes); bool ok = SendRequest(idEnqueue, sb, GetRH(e), discarded, (DOnExceptionFromServer)null); CScopeUQueue.Unlock(sb); return(ok); }
static void Main(string[] args) { using (CScopeUQueue su = new CScopeUQueue()) { CMyStruct msOriginal = CMyStruct.MakeOne(); msOriginal.SaveTo(su.UQueue); CMyStruct ms = Load(su.UQueue); System.Diagnostics.Debug.Assert(su.UQueue.GetSize() == 0); //check if both msOriginal and ms are equal in value. } }
public virtual bool Enqueue(byte[] key, ushort idMessage, CUQueue buffer, DEnqueue e, DDiscarded discarded) { CUQueue sb = CScopeUQueue.Lock(); sb.Save(key).Save(idMessage); if (buffer != null) { sb.Push(buffer.IntenalBuffer, buffer.HeadPosition, buffer.Size); } bool ok = SendRequest(idEnqueue, sb, GetRH(e), discarded, (DOnExceptionFromServer)null); CScopeUQueue.Unlock(sb); return(ok); }
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; }
public bool SendUserMessage(object Message, string UserId) { using (CScopeUQueue su = new CScopeUQueue()) { CUQueue q = su.UQueue; q.Save(Message); unsafe { fixed (byte* p = q.m_bytes) { return ServerCoreLoader.SendUserMessage(m_sp.Handle, UserId, p, q.GetSize()); } } } }
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); }
public bool SendUserMessage(object Message, string UserId) { using (CScopeUQueue su = new CScopeUQueue()) { CUQueue q = su.UQueue; q.Save(Message); unsafe { fixed(byte *p = q.m_bytes) { return(ServerCoreLoader.SendUserMessage(m_sp.Handle, UserId, p, q.GetSize())); } } } }
public Task <CScopeUQueue> sendRequest(ushort reqId, byte[] data, uint len) { //use threadless task only TaskCompletionSource <CScopeUQueue> tcs = new TaskCompletionSource <CScopeUQueue>(); if (!SendRequest(reqId, data, len, (ar) => { CScopeUQueue sb = new CScopeUQueue(); sb.UQueue.Swap(ar.UQueue); tcs.TrySetResult(sb); }, get_aborted(tcs, reqId), get_se(tcs))) { raise(reqId); } return(tcs.Task); }
protected bool SendRows(CScopeUQueue sb, bool transferring) { bool batching = (BytesBatched >= UDB.DB_CONSTS.DEFAULT_RECORD_BATCH_SIZE); if (batching) { CommitBatching(); } uint ret = SendResult(transferring ? UDB.DB_CONSTS.idTransferring : UDB.DB_CONSTS.idEndRows, sb.UQueue.IntenalBuffer, sb.UQueue.GetSize()); sb.UQueue.SetSize(0); if (batching) { StartBatching(); } if (ret == REQUEST_CANCELED || ret == SOCKET_NOT_FOUND) { return(false); } return(true); }
/// <summary> /// Start enqueuing messages with transaction style. Currently, total size of queued messages must be less than 4 G bytes /// </summary> /// <param name="key">An ASCII string for identifying a queue at server side</param> /// <param name="qt">A callback for tracking returning error code, which can be one of QUEUE_OK, QUEUE_TRANS_ALREADY_STARTED, and so on</param> /// <returns>true for sending the request successfully, and false for failure</returns> public bool StartQueueTrans(byte[] key, DQueueTrans qt) { if (key == null) key = new byte[0]; using (CScopeUQueue sq = new CScopeUQueue()) { sq.UQueue.Save(key); return SendRequest(idStartTrans, sq, (ar) => { if (qt != null) { int errCode; ar.Load(out errCode); qt(errCode); } else { ar.UQueue.SetSize(0); } }); } }
static Task <CAsyncDBHandler.SQLExeInfo> TestBLOBByPreparedStatement(CMysql mysql) { mysql.Prepare("insert into employee(CompanyId,name,JoinDate,image,DESCRIPTION,Salary)values(?,?,?,?,?,?)"); CDBVariantArray vData = new CDBVariantArray(); using (CScopeUQueue sbBlob = new CScopeUQueue()) { //first set of data vData.Add(1); //google company id vData.Add("Ted Cruz"); vData.Add(DateTime.Now); sbBlob.Save(m_wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(m_wstr); vData.Add(254000.0); //second set of data vData.Add(1); //google company id vData.Add("Donald Trump"); vData.Add(DateTime.Now); sbBlob.UQueue.SetSize(0); sbBlob.Save(m_str); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(m_str); vData.Add(20254000.0); //third set of data vData.Add(2); //Microsoft company id vData.Add("Hillary Clinton"); vData.Add(DateTime.Now); sbBlob.Save(m_wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(m_wstr); vData.Add(6254000.0); //send three sets of parameterized data in one shot for processing return(mysql.execute(vData)); } }
static void Main(string[] args) { Console.WriteLine("This is a client. Remote router host: "); CConnectionContext cc = new CConnectionContext(Console.ReadLine(), 20901, "lb_client", "pwd_lb_client"); using (CSocketPool <Pi> spPi = new CSocketPool <Pi>()) { spPi.QueueName = "lbqueue"; if (!spPi.StartSocketPool(cc, 1)) { Console.WriteLine("No connection to " + cc.Host); Console.WriteLine("Press ENTER key to kill the demo ......"); Console.ReadLine(); return; } Pi pi = spPi.SeekByQueue(); int nDivision = 1000, nNum = 10000000; double dPi = 0, dStep = 1.0 / nNum / nDivision; List <Task <CScopeUQueue> > vtR = new List <Task <CScopeUQueue> >(); for (int n = 0; n < nDivision; ++n) { double dStart = (double)n / nDivision; vtR.Add(pi.sendRequest(piConst.idComputePi, dStart, dStep, nNum)); } foreach (var t in vtR) { CScopeUQueue sb = t.Result; dPi += sb.Load <double>(); Console.WriteLine("dStart: " + sb.Load <double>()); } Console.WriteLine("pi: {0}, returns: {1}", dPi, vtR.Count); Console.WriteLine("Press ENTER key to kill the demo ......"); Console.ReadLine(); } }
public bool Publish(object Message, uint[] Groups) { uint len; if (Groups == null) len = 0; else len = (uint)Groups.Length; using (CScopeUQueue su = new CScopeUQueue()) { CUQueue q = su.UQueue; q.Save(Message); unsafe { fixed (byte* buffer = q.m_bytes) { fixed (uint* p = Groups) { return ServerCoreLoader.Speak(m_sp.Handle, buffer, q.GetSize(), p, len); } } } } }
private bool EndDataSet(DataSet ds, bool needRelations) { 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 a valid dataset object!"); } using (CScopeUQueue UQueue = new CScopeUQueue()) { CUQueue AdoUQueue = UQueue.UQueue; if (needRelations) { m_AdoSerializer.Push(AdoUQueue, ds.Relations); } if (RouteeRequest) { return(SendRouteeResult(AdoUQueue, CAdoSerializationHelper.idEndDataSet)); } return(SendRequest(CAdoSerializationHelper.idEndDataSet, AdoUQueue, m_arh)); } }
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); }
static void InsertBLOBByPreparedStatement(CSqlServer sql) { string wstr = ""; while (wstr.Length < 128 * 1024) { wstr += "广告做得不那么夸张的就不说了,看看这三家,都是正儿八经的公立三甲,附属医院,不是武警,也不是部队,更不是莆田,都在卫生部门直接监管下,照样明目张胆地骗人。"; } string str = ""; while (str.Length < 256 * 1024) { str += "The epic takedown of his opponent on an all-important voting day was extraordinary even by the standards of the 2016 campaign -- and quickly drew a scathing response from Trump."; } string sqlInsert = "insert into employee(EMPLOYEEID, CompanyId,name,JoinDate,myimage,DESCRIPTION,Salary)values(@EMPLOYEEID,@CompanyId,@name,@JoinDate,@myimage,@DESCRIPTION,@Salary)"; CParameterInfoArray vInfo = new CParameterInfoArray(); CParameterInfo info = new CParameterInfo(); info.ParameterName = "@EMPLOYEEID"; info.DataType = tagVariantDataType.sdVT_INT; vInfo.Add(info); info = new CParameterInfo(); info.ParameterName = "@CompanyId"; info.DataType = tagVariantDataType.sdVT_INT; vInfo.Add(info); info = new CParameterInfo(); info.ParameterName = "@name"; info.ColumnSize = 64; info.DataType = tagVariantDataType.sdVT_BSTR; vInfo.Add(info); info = new CParameterInfo(); info.ParameterName = "@JoinDate"; info.DataType = tagVariantDataType.sdVT_DATE; vInfo.Add(info); info = new CParameterInfo(); info.ParameterName = "@myimage"; info.ColumnSize = uint.MaxValue; info.DataType = tagVariantDataType.sdVT_UI1 | tagVariantDataType.sdVT_ARRAY; vInfo.Add(info); info = new CParameterInfo(); info.ParameterName = "@DESCRIPTION"; info.ColumnSize = uint.MaxValue; info.DataType = tagVariantDataType.sdVT_BSTR; vInfo.Add(info); info = new CParameterInfo(); info.ParameterName = "@Salary"; info.ColumnSize = uint.MaxValue; info.DataType = tagVariantDataType.sdVT_DECIMAL; info.Precision = 15; info.Scale = 2; vInfo.Add(info); bool ok = sql.Prepare(sqlInsert, dr, vInfo.ToArray()); CDBVariantArray vData = new CDBVariantArray(); using (CScopeUQueue sbBlob = new CScopeUQueue()) { //first set of data vData.Add(1); vData.Add(1); //google company id vData.Add("Ted Cruz"); vData.Add(DateTime.Now); sbBlob.Save(wstr); byte[] bytes = sbBlob.UQueue.GetBuffer(); vData.Add(bytes); vData.Add(wstr); vData.Add(254000.2460d); //second set of data vData.Add(2); vData.Add(1); //google company id vData.Add("Donald Trump"); vData.Add(DateTime.Now); sbBlob.UQueue.SetSize(0); sbBlob.Save(str); bytes = sbBlob.UQueue.GetBuffer(); vData.Add(bytes); vData.Add(str); vData.Add(20254000.197d); //third set of data vData.Add(3); vData.Add(2); //Microsoft company id vData.Add("Hillary Clinton"); vData.Add(DateTime.Now); sbBlob.Save(wstr); bytes = sbBlob.UQueue.GetBuffer(); vData.Add(bytes); vData.Add(wstr); vData.Add(6254000.5d); //execute multiple sets of parameter data in one short ok = sql.Execute(vData, er); } }
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; } }
private void OnBEx(IntPtr handler, IntPtr senderCe, IntPtr groups, uint count, IntPtr message, uint size) { CMessageSenderCe msc = new CMessageSenderCe(); System.Runtime.InteropServices.Marshal.PtrToStructure(senderCe, msc); using (CScopeUQueue su = new CScopeUQueue()) { ushort vt = (ushort)(tagVariantDataType.sdVT_ARRAY | tagVariantDataType.sdVT_UI4); su.UQueue.Save(vt); su.UQueue.Save(count); su.UQueue.Push(groups, count * sizeof(uint)); object obj; su.UQueue.Load(out obj); byte[] msg = new byte[size]; if (size > 0) { System.Runtime.InteropServices.Marshal.Copy(message, msg, 0, (int)size); } m_PushImpl.OnBEx(handler, ToMessageSender(msc), (uint[])obj, count, msg, size); } }
private ulong SendDataFromClientToServer() { if (m_ash.AttachedClientSocket.BytesInSendingBuffer > CStreamSerializationHelper.STREAM_CHUNK_SIZE) return 0; ulong send = 0; using (CScopeUQueue su = new CScopeUQueue()) { if (m_s == null) return 0; uint read = CStreamSerializationHelper.Read(m_s, su.UQueue); while (read > 0) { bool ok = m_ash.SendRequest(CStreamSerializationHelper.idWriteDataFromClientToServer, su.UQueue.m_bytes, read, (ar) => { SendDataFromClientToServer(); }); if (Progress != null) Progress.Invoke(this, (ulong)m_s.Position); if (!ok) { m_s = null; break; } send += read; if (m_ash.AttachedClientSocket.BytesInSendingBuffer > 10 * CStreamSerializationHelper.STREAM_CHUNK_SIZE) break; read = CStreamSerializationHelper.Read(m_s, su.UQueue); if (read == 0) { if (!m_ash.SendRequest(CStreamSerializationHelper.idUploadCompleted, (dc) => { lock (m_cs) { if (Progress != null) Progress.Invoke(this, (ulong)m_s.Position); m_s = null; } })) m_s = null; } } } return send; }
public bool Publish(object Message, uint[] groups) { if (groups == null || groups.Length == 0) throw new ArgumentException("Must specify an array of group identification numbers"); using (CScopeUQueue su = new CScopeUQueue()) { byte[] msg = su.Save(Message).m_bytes; unsafe { fixed (byte* message = msg) { IntPtr m = new IntPtr(message); fixed (uint* grps = groups) { IntPtr g = new IntPtr(grps); return ClientCoreLoader.Speak(m_cs.Handle, m, su.UQueue.GetSize(), g, groups.Length) != 0; } } } } }
public bool SendUserMessage(object message, string userId) { using (CScopeUQueue su = new CScopeUQueue()) { byte[] msg = su.Save(message).m_bytes; unsafe { fixed (byte* data = msg) { IntPtr m = new IntPtr(data); return ClientCoreLoader.SendUserMessage(m_cs.Handle, userId, m, su.UQueue.GetSize()) != 0; } } } }
static void InsertBLOBByPreparedStatement(CSqlite sqlite, List <KeyValuePair <CDBColumnInfoArray, CDBVariantArray> > ra) { string wstr = ""; while (wstr.Length < 128 * 1024) { wstr += "广告做得不那么夸张的就不说了,看看这三家,都是正儿八经的公立三甲,附属医院,不是武警,也不是部队,更不是莆田,都在卫生部门直接监管下,照样明目张胆地骗人。"; } string str = ""; while (str.Length < 256 * 1024) { str += "The epic takedown of his opponent on an all-important voting day was extraordinary even by the standards of the 2016 campaign -- and quickly drew a scathing response from Trump."; } string sqlInsert = "insert or replace into employee(EMPLOYEEID, CompanyId, name, JoinDate, image, DESCRIPTION, Salary) values(?, ?, ?, ?, ?, ?, ?);select * from employee where employeeid = ?"; bool ok = sqlite.Prepare(sqlInsert, (handler, res, errMsg) => { Console.WriteLine("res = {0}, errMsg: {1}", res, errMsg); }); CDBVariantArray vData = new CDBVariantArray(); using (CScopeUQueue sbBlob = new CScopeUQueue()) { //first set of data vData.Add(1); vData.Add(1); //google company id vData.Add("Ted Cruz"); vData.Add(DateTime.Now); sbBlob.Save(wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(wstr); vData.Add(254000.0); vData.Add(1); //second set of data vData.Add(2); vData.Add(1); //google company id vData.Add("Donald Trump"); vData.Add(DateTime.Now); sbBlob.UQueue.SetSize(0); sbBlob.Save(str); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(str); vData.Add(20254000.0); vData.Add(2); //third set of data vData.Add(3); vData.Add(2); //Microsoft company id vData.Add("Hillary Clinton"); vData.Add(DateTime.Now); sbBlob.Save(wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(wstr); vData.Add(6254000.0); vData.Add(3); } //send three sets of parameterized data in one shot for processing ok = sqlite.Execute(vData, (handler, res, errMsg, affected, fail_ok, id) => { Console.WriteLine("affected = {0}, fails = {1}, oks = {2}, res = {3}, errMsg: {4}, last insert id = {5}", affected, (uint)(fail_ok >> 32), (uint)fail_ok, res, errMsg, id); }, (handler, rowData) => { //rowset data come here int last = ra.Count - 1; KeyValuePair <CDBColumnInfoArray, CDBVariantArray> item = ra[last]; item.Value.AddRange(rowData); }, (handler) => { //rowset header comes here KeyValuePair <CDBColumnInfoArray, CDBVariantArray> item = new KeyValuePair <CDBColumnInfoArray, CDBVariantArray>(handler.ColumnInfo, new CDBVariantArray()); ra.Add(item); }); }
private void OnRProcessed(IntPtr handler, ushort requestId, uint len) { CAsyncServiceHandler ash = Seek(CurrentServiceID); if (ash != null) { using (CScopeUQueue su = new CScopeUQueue()) { uint res = 0; CUQueue q = su.UQueue; if (m_routing) m_os = ClientCoreLoader.GetPeerOs(handler, ref m_endian); q.OS = m_os; q.Endian = m_endian; if (q.MaxBufferSize < (len + 16)) q.Realloc(len + 16); if (len > 0) { unsafe { fixed (byte* buffer = q.m_bytes) { res = ClientCoreLoader.RetrieveResult(handler, buffer, len); } } } if (res != len) { if (res == 0) return; //socket closed //should never come here! string msg = string.Format("Wrong number of bytes retrieved (expected = {0} and obtained = {1})", len, res); throw new InvalidOperationException(msg); } q.SetSize(len); ash.onRR(requestId, q); } } if (RequestProcessed != null) { RequestProcessed.Invoke(this, requestId, len); } }
private void OnPUMessage(IntPtr handler, IntPtr senderCe, IntPtr message, uint size) { CMessageSenderCe msc = new CMessageSenderCe(); System.Runtime.InteropServices.Marshal.PtrToStructure(senderCe, msc); using (CScopeUQueue su = new CScopeUQueue()) { object msg; su.UQueue.Push(message, size).Load(out msg); m_PushImpl.OnPUMessage(handler, ToMessageSender(msc), msg); } }
private void OnExit(IntPtr handler, IntPtr senderCe, IntPtr groups, uint count) { CMessageSenderCe msc = new CMessageSenderCe(); System.Runtime.InteropServices.Marshal.PtrToStructure(senderCe, msc); using (CScopeUQueue su = new CScopeUQueue()) { ushort vt = (ushort)(tagVariantDataType.sdVT_ARRAY | tagVariantDataType.sdVT_UI4); su.UQueue.Save(vt); su.UQueue.Save(count); su.UQueue.Push(groups, count * sizeof(uint)); object obj; su.UQueue.Load(out obj); m_PushImpl.OnExit(handler, ToMessageSender(msc), (uint[])obj, count); } }
/// <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; }
public virtual bool Send(DataSet ds, bool needRelations, uint batchSize) { bool b = false; if (Socket == 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 { lock (m_cs) { 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); }
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); }
static Task <CAsyncDBHandler.SQLExeInfo> TestBatch(CMysql mysql, List <KeyValue> ra, out CDBVariantArray vData) { //sql with delimiter '|' string sql = @" delete from employee;delete from company| INSERT INTO company(ID,NAME,ADDRESS,Income)VALUES(?,?,?,?)| insert into employee(CompanyId,name,JoinDate,image,DESCRIPTION,Salary)values(?,?,?,?,?,?)| SELECT * from company;select * from employee;select curtime()| call sp_TestProc(?,?,?)"; vData = new CDBVariantArray(); using (CScopeUQueue sbBlob = new CScopeUQueue()) { //first set vData.Add(1); vData.Add("Google Inc."); vData.Add("1600 Amphitheatre Parkway, Mountain View, CA 94043, USA"); vData.Add(66000000000.15); vData.Add(1); //google company id vData.Add("Ted Cruz"); vData.Add(DateTime.Now); sbBlob.Save(m_wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(m_wstr); vData.Add(254000.26); vData.Add(1); //input vData.Add(1.4); //input-output vData.Add(0); //output //second set vData.Add(2); vData.Add("Microsoft Inc."); vData.Add("700 Bellevue Way NE- 22nd Floor, Bellevue, WA 98804, USA"); vData.Add(93600000000.37); vData.Add(1); //google company id vData.Add("Donald Trump"); vData.Add(DateTime.Now); sbBlob.UQueue.SetSize(0); sbBlob.Save(m_str); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(m_str); vData.Add(20254000.85); vData.Add(2); //input vData.Add(2.5); //input-output vData.Add(0); //output //third set vData.Add(3); vData.Add("Apple Inc."); vData.Add("1 Infinite Loop, Cupertino, CA 95014, USA"); vData.Add(234000000000.09); vData.Add(2); //Microsoft company id vData.Add("Hillary Clinton"); vData.Add(DateTime.Now); sbBlob.Save(m_wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(m_wstr); vData.Add(6254000.55); vData.Add(0); //input vData.Add(4.5); //input-output vData.Add(0); //output } CMysql.DRows r = (handler, rowData) => { //rowset data come here int last = ra.Count - 1; KeyValuePair <CDBColumnInfoArray, CDBVariantArray> item = ra[last]; item.Value.AddRange(rowData); }; CMysql.DRowsetHeader rh = (handler) => { //rowset header comes here KeyValue item = new KeyValue(handler.ColumnInfo, new CDBVariantArray()); ra.Add(item); }; //first, start a transaction with ReadCommited isolation //second, execute delete from employee;delete from company //third, prepare and execute three sets of // INSERT INTO company(ID,NAME,ADDRESS,Income)VALUES(?,?,?,?) //fourth, prepare and execute three sets of //insert into employee(CompanyId,name,JoinDate,image,DESCRIPTION,Salary)values(?,?,?,?,?,?) //fifth, SELECT * from company;select * from employee;select curtime() //sixth, prepare and three sets of call sp_TestProc(?,?,?) //last, commit transaction if there is no error, and rollback if there is one or more errors return(mysql.executeBatch(tagTransactionIsolation.tiReadCommited, sql, vData, r, rh, "|")); }
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; }
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; }
private void OnSPEvent(uint poolId, tagSocketPoolEvent spe, IntPtr h) { THandler handler = MapToHandler(h); switch (spe) { case tagSocketPoolEvent.speTimer: if (CScopeUQueue.MemoryConsumed / 1024 > CScopeUQueue.SHARED_BUFFER_CLEAN_SIZE) { CScopeUQueue.DestroyUQueuePool(); } break; case tagSocketPoolEvent.speStarted: lock (m_cs) { m_nPoolId = poolId; } break; case tagSocketPoolEvent.speShutdown: lock (m_cs) { m_dicSocketHandler.Clear(); } break; case tagSocketPoolEvent.speUSocketCreated: { CClientSocket cs = new CClientSocket(); cs.Set(h); ClientCoreLoader.SetRecvTimeout(h, m_recvTimeout); ClientCoreLoader.SetConnTimeout(h, m_connTimeout); ClientCoreLoader.SetAutoConn(h, (byte)(m_autoConn ? 1 : 0)); handler = new THandler(); if (handler.SvsID == 0) { handler.m_nServiceId = m_ServiceId; } if (handler.SvsID <= SocketProAdapter.BaseServiceID.sidStartup) { throw new InvalidOperationException("Service id must be larger than SocketProAdapter.BaseServiceID.sidStartup"); } handler.Attach(cs); lock (m_cs) { m_dicSocketHandler[cs] = handler; } } break; case tagSocketPoolEvent.speUSocketKilled: if (handler != null) { lock (m_cs) { m_dicSocketHandler.Remove(handler.AttachedClientSocket); } } break; case tagSocketPoolEvent.speConnecting: break; case tagSocketPoolEvent.speConnected: if (ClientCoreLoader.IsOpened(h) != 0) { CClientSocket cs = handler.AttachedClientSocket; if (DoSslServerAuthentication != null && cs.EncryptionMethod == tagEncryptionMethod.TLSv1 && !DoSslServerAuthentication.Invoke(this, cs)) { return; //don't set password or call SwitchTo in case failure of ssl server authentication on certificate from server } ClientCoreLoader.SetSockOpt(h, tagSocketOption.soRcvBuf, 116800, tagSocketLevel.slSocket); ClientCoreLoader.SetSockOpt(h, tagSocketOption.soSndBuf, 116800, tagSocketLevel.slSocket); ClientCoreLoader.SetSockOpt(h, tagSocketOption.soTcpNoDelay, 1, tagSocketLevel.slTcp); ClientCoreLoader.SetPassword(h, cs.ConnectionContext.GetPassword()); bool ok = ClientCoreLoader.StartBatching(h) != 0; ok = ClientCoreLoader.SwitchTo(h, handler.SvsID) != 0; ok = ClientCoreLoader.TurnOnZipAtSvr(h, (byte)(cs.ConnectionContext.Zip ? 1 : 0)) != 0; ok = ClientCoreLoader.SetSockOptAtSvr(h, tagSocketOption.soRcvBuf, 116800, tagSocketLevel.slSocket) != 0; ok = ClientCoreLoader.SetSockOptAtSvr(h, tagSocketOption.soSndBuf, 116800, tagSocketLevel.slSocket) != 0; ok = ClientCoreLoader.SetSockOptAtSvr(h, tagSocketOption.soTcpNoDelay, 1, tagSocketLevel.slTcp) != 0; ok = (ClientCoreLoader.CommitBatching(h, (byte)0) != 0); } break; case tagSocketPoolEvent.speQueueMergedFrom: m_pHFrom = MapToHandler(h); #if DEBUG IClientQueue cq = m_pHFrom.AttachedClientSocket.ClientQueue; uint remaining = (uint)m_pHFrom.RequestsQueued; if (cq.MessageCount != remaining) { Console.WriteLine("From: Messages = {0}, remaining requests = {1}", cq.MessageCount, remaining); } #endif break; case tagSocketPoolEvent.speQueueMergedTo: { THandler to = MapToHandler(h); m_pHFrom.AppendTo(to); m_pHFrom = null; } break; default: break; } if (SocketPoolEvent != null) { SocketPoolEvent.Invoke(this, spe, handler); } OnSocketPoolEvent(spe, handler); if (spe == tagSocketPoolEvent.speConnected && ClientCoreLoader.IsOpened(h) != 0) { SetQueue(handler.AttachedClientSocket); } }
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; }
static void InsertBLOBByPreparedStatement(CSqlite sqlite, List<KeyValuePair<CDBColumnInfoArray, CDBVariantArray>> ra) { string wstr = ""; while (wstr.Length < 128 * 1024) { wstr += "广告做得不那么夸张的就不说了,看看这三家,都是正儿八经的公立三甲,附属医院,不是武警,也不是部队,更不是莆田,都在卫生部门直接监管下,照样明目张胆地骗人。"; } string str = ""; while (str.Length < 256 * 1024) { str += "The epic takedown of his opponent on an all-important voting day was extraordinary even by the standards of the 2016 campaign -- and quickly drew a scathing response from Trump."; } string sqlInsert = "insert or replace into employee(EMPLOYEEID, CompanyId, name, JoinDate, image, DESCRIPTION, Salary) values(?, ?, ?, ?, ?, ?, ?);select * from employee where employeeid = ?"; bool ok = sqlite.Prepare(sqlInsert, (handler, res, errMsg) => { Console.WriteLine("res = {0}, errMsg: {1}", res, errMsg); }); CDBVariantArray vData = new CDBVariantArray(); using (CScopeUQueue sbBlob = new CScopeUQueue()) { //first set of data vData.Add(1); vData.Add(1); //google company id vData.Add("Ted Cruz"); vData.Add(DateTime.Now); sbBlob.Save(wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(wstr); vData.Add(254000.0); vData.Add(1); //second set of data vData.Add(2); vData.Add(1); //google company id vData.Add("Donald Trump"); vData.Add(DateTime.Now); sbBlob.UQueue.SetSize(0); sbBlob.Save(str); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(str); vData.Add(20254000.0); vData.Add(2); //third set of data vData.Add(3); vData.Add(2); //Microsoft company id vData.Add("Hillary Clinton"); vData.Add(DateTime.Now); sbBlob.Save(wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(wstr); vData.Add(6254000.0); vData.Add(3); } //send three sets of parameterized data in one shot for processing ok = sqlite.Execute(vData, (handler, res, errMsg, affected, fail_ok, id) => { Console.WriteLine("affected = {0}, fails = {1}, oks = {2}, res = {3}, errMsg: {4}, last insert id = {5}", affected, (uint)(fail_ok >> 32), (uint)fail_ok, res, errMsg, id); }, (handler, rowData) => { //rowset data come here int last = ra.Count - 1; KeyValuePair<CDBColumnInfoArray, CDBVariantArray> item = ra[last]; item.Value.AddRange(rowData); }, (handler) => { //rowset header comes here KeyValuePair<CDBColumnInfoArray, CDBVariantArray> item = new KeyValuePair<CDBColumnInfoArray, CDBVariantArray>(handler.ColumnInfo, new CDBVariantArray()); ra.Add(item); }); }
static CDBVariantArray TestBatch(CMysql mysql, List <KeyValuePair <CDBColumnInfoArray, CDBVariantArray> > ra) { //sql with delimiter '|' string sql = @" delete from employee;delete from company| INSERT INTO company(ID,NAME,ADDRESS,Income)VALUES(?,?,?,?)| insert into employee(CompanyId,name,JoinDate,image,DESCRIPTION,Salary)values(?,?,?,?,?,?)| SELECT * from company;select * from employee;select curtime()| call sp_TestProc(?,?,?)"; string wstr = ""; //make test data while (wstr.Length < 128 * 1024) { wstr += "广告做得不那么夸张的就不说了,看看这三家,都是正儿八经的公立三甲,附属医院,不是武警,也不是部队,更不是莆田,都在卫生部门直接监管下,照样明目张胆地骗人。"; } string str = ""; //make test data while (str.Length < 256 * 1024) { str += "The epic takedown of his opponent on an all-important voting day was extraordinary even by the standards of the 2016 campaign -- and quickly drew a scathing response from Trump."; } CDBVariantArray vData = new CDBVariantArray(); using (CScopeUQueue sbBlob = new CScopeUQueue()) { //first set vData.Add(1); vData.Add("Google Inc."); vData.Add("1600 Amphitheatre Parkway, Mountain View, CA 94043, USA"); vData.Add(66000000000.0); vData.Add(1); //google company id vData.Add("Ted Cruz"); vData.Add(DateTime.Now); sbBlob.Save(wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(wstr); vData.Add(254000.0); vData.Add(1); vData.Add(1.4); vData.Add(0); //second set vData.Add(2); vData.Add("Microsoft Inc."); vData.Add("700 Bellevue Way NE- 22nd Floor, Bellevue, WA 98804, USA"); vData.Add(93600000000.0); vData.Add(1); //google company id vData.Add("Donald Trump"); vData.Add(DateTime.Now); sbBlob.UQueue.SetSize(0); sbBlob.Save(str); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(str); vData.Add(20254000.0); vData.Add(2); vData.Add(2.5); vData.Add(0); //third set vData.Add(3); vData.Add("Apple Inc."); vData.Add("1 Infinite Loop, Cupertino, CA 95014, USA"); vData.Add(234000000000.0); vData.Add(2); //Microsoft company id vData.Add("Hillary Clinton"); vData.Add(DateTime.Now); sbBlob.Save(wstr); vData.Add(sbBlob.UQueue.GetBuffer()); vData.Add(wstr); vData.Add(6254000.0); vData.Add(0); vData.Add(4.5); vData.Add(0); } CMysql.DRows r = (handler, rowData) => { //rowset data come here int last = ra.Count - 1; KeyValuePair <CDBColumnInfoArray, CDBVariantArray> item = ra[last]; item.Value.AddRange(rowData); }; CMysql.DRowsetHeader rh = (handler) => { //rowset header comes here KeyValuePair <CDBColumnInfoArray, CDBVariantArray> item = new KeyValuePair <CDBColumnInfoArray, CDBVariantArray>(handler.ColumnInfo, new CDBVariantArray()); ra.Add(item); }; //first, execute delete from employee;delete from company //second, three sets of INSERT INTO company(ID,NAME,ADDRESS,Income)VALUES(?,?,?,?) //third, three sets of insert into employee(CompanyId,name,JoinDate,image,DESCRIPTION,Salary)values(?,?,?,?,?,?) //fourth, SELECT * from company;select * from employee;select curtime() //last, three sets of call sp_TestProc(?,?,?) bool ok = mysql.ExecuteBatch(tagTransactionIsolation.tiUnspecified, sql, vData, er, r, rh, (h) => { //called before rh, r and er //ra.Clear(); }, null, tagRollbackPlan.rpDefault, (h, canceled) => { //called when canceling or socket closed if client queue is NOT used }, "|"); return(vData); }