private void RecvThread(object obj) { while (true) { CData odata = new CData(); bool bOk = DataMgr.Get_DataMgr().Getdata(out odata); if (bOk) { List <List <string> > ff = odata.m_lstdata; List <string> oCols = odata.m_colName; //进行数据处理 } Thread.Sleep(3000);//每3秒钟检测一次内存缓冲区 } }
private void OnRecvData(string sdata) { if (sdata.Length < 12 && sdata == "login") { } else if (sdata.Length >= 12 && sdata[0] == 'W' && sdata[1] == 'J') { uint iClientNo; uint iBodylen = 0; int iFuncNo; if (JsonOp.UnPacketHead(sdata, out iFuncNo, out iClientNo, out iBodylen)) { switch (iFuncNo) { case (int)enumFunctionNo.LogIn: { string errStr; if (JsonOp.UnpacketResult(sdata, out errStr)) { m_iClientNo = (uint)iClientNo; m_Form1.DispMsg("enumFunctionNo.LogIn ok :" + errStr); } else { m_Form1.DispMsg("enumFunctionNo.LogIn error"); } } break; case (int)enumFunctionNo.ConnectSql: { string errStr; if (JsonOp.UnpacketResult(sdata, out errStr)) { m_iClientNo = (uint)iClientNo; m_Form1.DispMsg("enumFunctionNo.ConnectSql ok: " + errStr); } else { //error tips m_Form1.DispMsg("enumFunctionNo.ConnectSql error: " + errStr); } } break; case (int)enumFunctionNo.ExcSelectCmd: { if (sdata.Length == 12) { m_Form1.DispMsg("enumFunctionNo.ExcSelectCmd empty data"); } else { List <List <string> > lstdata; // = new List<List<string>>(); string errStr; JsonOp.UnpacketDBdata(sdata, out lstdata, out errStr); if (lstdata.Count > 0) { CData odata = new CData(); odata.m_lstdata = lstdata; DataMgr.Get_DataMgr().AddData(odata); m_Form1.DispMsg("enumFunctionNo.ExcSelectCmd get data sucee"); } else { m_Form1.DispMsg("enumFunctionNo.ExcSelectCmd get data error:" + errStr); } } } break; default: break; } } } }
/// <summary> /// TCP读数据的回调函数 /// </summary> /// <param name="ar"></param> private void TCPReadCallBack(IAsyncResult ar) { StateObject state = (StateObject)ar.AsyncState; //主动断开时 if ((state.client == null) || (!state.client.Connected)) { return; } int numberOfBytesRead = 0; NetworkStream mas = state.client.GetStream(); string type = null; try { numberOfBytesRead = mas.EndRead(ar); } catch (Exception dd) { } state.totalBytesRead += numberOfBytesRead; // string strRev = numberOfBytesRead.ToString(); if (numberOfBytesRead > 0) { byte[] dd = new byte[numberOfBytesRead]; Array.Copy(state.buffer, 0, dd, 0, numberOfBytesRead); // OnGetData(dd); char [] szchar = new char[dd.Length]; for (int i = 0; i < dd.Length; i++) { szchar[i] = (char)dd[i]; } string sdata = new string(szchar); if (sdata.Length >= 12 && sdata[0] == 'W' && sdata[1] == 'J') { uint iClientNo; uint iBodylen = 0; int iFuncNo; if (JsonOp.UnPacketHead(sdata, out iFuncNo, out iClientNo, out iBodylen)) { if (iBodylen + 12 > sdata.Length) { RecvDataStruct oRecvDataStruct = new RecvDataStruct(); oRecvDataStruct.ifd = 0; oRecvDataStruct.itotallen = iBodylen + 12; oRecvDataStruct.iRemainlen = iBodylen + 12; lock (DataMgr.Get_DataMgr().m_RecvLst) { DataMgr.Get_DataMgr().m_RecvLst.Add(oRecvDataStruct); } } } } bool bRecOK = true; lock (DataMgr.Get_DataMgr().m_RecvLst) { if (DataMgr.Get_DataMgr().m_RecvLst.Count > 0) { List <RecvDataStruct> lst = DataMgr.Get_DataMgr().Get(); RecvDataStruct oRecvDataStruct = lst[0]; oRecvDataStruct.iRemainlen -= (uint)sdata.Length; oRecvDataStruct.strrecvData += sdata; if (oRecvDataStruct.iRemainlen <= 0) { sdata = oRecvDataStruct.strrecvData; DataMgr.Get_DataMgr().Remove(); } else { DataMgr.Get_DataMgr().Set(oRecvDataStruct); bRecOK = false; } } } if (bRecOK) { OnRecvData(sdata); } mas.BeginRead(state.buffer, 0, StateObject.BufferSize, new AsyncCallback(TCPReadCallBack), state); } else { //被动断开时 mas.Close(); state.client.Close(); m_Form1.DispMsg("connect close"); mas = null; state = null; } }