public uint ExportSize(object v, Type objType) { uint nExportSize = 8; uint nLen = 0; CUniStructCS ccs = new CUniStructCS(); if (v != null) { for (uint i = 0; i < ((Array)v).Length; i++) { object obj = ((Array)v).GetValue(i); if (obj == null) { continue; } nExportSize += ccs.ExportSize(obj, true, objType.GetElementType()); nLen++; } return(nExportSize); } else { return(0); } }
public byte[] Export(object v, Type objType) { uint nExportSize = 8; uint nDindex = 0; byte[] retBytes = null; uint nLen = 0; CUniStructCS ccs = new CUniStructCS(); if (v != null) { for (uint i = 0; i < ((Array)v).Length; i++) { object obj = ((Array)v).GetValue(i); if (obj == null) { continue; } nExportSize += ccs.ExportSize(obj, true, objType.GetElementType()); nLen++; } } else { return(new byte[0]); } retBytes = new byte[nExportSize]; CUniStructCS.uint2byte((uint)UNISMAG.UNISARRAY, ref retBytes, nDindex); nDindex += 4; CUniStructCS.uint2byte((uint)nLen, ref retBytes, nDindex); nDindex += 4; if (v != null) { for (uint i = 0; i < ((Array)v).Length; i++) { object obj = ((Array)v).GetValue(i); if (obj == null) { continue; } uint nSize = ccs.Export(obj, true, ref retBytes, nDindex, objType.GetElementType()); nDindex += nSize; } } return(retBytes); }
public uint Import(out object v, byte[] pBuffer, uint _index, Type objType) { uint nIndex = _index; v = null; try { if (pBuffer == null || pBuffer.Length < 8) { throw(new Exception("0")); } uint nMag = CUniStructCS.byte2uint(pBuffer, nIndex); if (nMag != (uint)UNISMAG.UNISARRAY) { throw(new Exception("1")); } nIndex += 4; uint nLength = CUniStructCS.byte2uint(pBuffer, nIndex); nIndex += 4; Array vv = System.Array.CreateInstance(objType.GetElementType(), (int)nLength); v = (object)vv; CUniStructCS ccs = new CUniStructCS(); for (uint i = 0; i < nLength; i++) { object vobj; uint nImported = ccs.Import(out vobj, pBuffer, nIndex, objType.GetElementType()); vv.SetValue(vobj, i); if (nImported == 0) { v = null; throw(new Exception("2,index=" + i)); } nIndex += nImported; } }catch (Exception e) { Logger.Trace("UniStructArray faild:" + e.Message); return(0); } return((uint)nIndex - _index); }
public uint ExportSize <T>(T[] v) where T : new() { uint nExportSize = 8; uint nLen = 0; CUniStructCS ccs = new CUniStructCS(); if (v != null) { for (uint i = 0; i < v.Length; i++) { if (v[i] == null) { continue; } nExportSize += ccs.ExportSize(v[i]); nLen++; } return(nExportSize); } else { return(0); } }
public uint LogonOn(uint nCommand, byte[] aData, out byte[] vData) { uint nResult = (uint)REQUESTCODE.EXECUTE_FAIL; vData = null; m_dwErrCode = (uint)REQUESTCODE.EXECUTE_SUCCESS; m_strErrMsg = ""; Logger.Trace("Request CMD=" + nCommand.ToString("x") + ",Length=" + (aData != null?aData.Length.ToString():"0")); try { if (!m_bConnected) { if (m_gateWay == null) { m_gateWay = new GateWay(); } if (!string.IsNullOrEmpty(m_Config.m_szServerIP)) { if (!m_gateWay.Connect(m_Config.m_szServerIP, m_Config.m_nServerPort)) { m_strErrMsg = "网络连接失败"; return((uint)REQUESTCODE.EXECUTE_FAIL); } } Logger.Trace("UniCOM IsEncode:" + m_gateWay.IsEncode.ToString()); } byte[] sendData = new byte[12 + aData.Length]; sendData[0] = (byte)m_dwFlag; sendData[1] = (byte)(m_dwFlag >> 8); sendData[2] = (byte)(m_dwFlag >> 16); sendData[3] = (byte)(m_dwFlag >> 24); sendData[4] = (byte)m_dwSessionID; sendData[5] = (byte)(m_dwSessionID >> 8); sendData[6] = (byte)(m_dwSessionID >> 16); sendData[7] = (byte)(m_dwSessionID >> 24); sendData[8] = (byte)m_dwStaSN; sendData[9] = (byte)(m_dwStaSN >> 8); sendData[10] = (byte)(m_dwStaSN >> 16); sendData[11] = (byte)(m_dwStaSN >> 24); Array.Copy(aData, 0, sendData, 12, aData.Length); if (mutex != null) { mutex.WaitOne(); } lock (m_gateWay) { for (int t = 0; t < m_RetryTime; t++) { vData = (byte[])(m_gateWay.Logon(nCommand, sendData, out nResult)); if (nResult == (uint)REQUESTCODE.EXECUTE_SUCCESS || vData != null) { break; } else { Logger.Trace("m_gateWay.Logon 失败重试"); if (!m_gateWay.IsConnect) { m_gateWay.Connect(m_Config.m_szServerIP, m_Config.m_nServerPort); } } } } Logger.Trace("Return CMD=" + nCommand.ToString("x") + ",Return=" + ((REQUESTCODE)nResult).ToString() + ", ResultLength=" + (vData != null?vData.Length.ToString():"0")); if (nResult == (uint)REQUESTCODE.EXECUTE_SUCCESS) { if (mutex != null) { mutex.ReleaseMutex(); } return(nResult); } UNICOMMERR_UT vrErr; UniStructCS.CUniStructCS uccs = new UniStructCS.CUniStructCS(); if (uccs.Import(out vrErr, vData) > 0) { Logger.Trace("Error>>"); Logger.Trace(vrErr); m_dwErrCode = (uint)vrErr.dwErrorCode; m_strErrMsg = vrErr.szErrorInfo; if (m_dwErrCode == 0x2002001) { SessionID = 0; } } vData = null; } catch (System.Runtime.InteropServices.COMException exception) { Close(); nResult = (uint)REQUESTCODE.EXECUTE_FAIL; m_strErrMsg = exception.Message; Logger.Trace("m_strErrMsg=" + m_strErrMsg); } catch (Exception exception) { Close(); nResult = (uint)REQUESTCODE.EXECUTE_FAIL; m_strErrMsg = exception.Message; Logger.Trace("m_strErrMsg=" + m_strErrMsg); } if (mutex != null) { mutex.ReleaseMutex(); } return(nResult); }
static public void Trace <T>(T vrObject) where T : new() { if (vrObject == null) { Logger.Trace("null"); return; } Type t = vrObject.GetType(); UniStructCS.CUniStructCS uccs = new UniStructCS.CUniStructCS(); string[] sfiarr = UniWebLib.Struct_ST.GetStructMember(t.Name); if (sfiarr == null) { FieldInfo[] ttfiarr = t.GetFields(); sfiarr = new string[ttfiarr.Length]; for (int fi = 0; fi < ttfiarr.Length; fi++) { sfiarr[fi] = ttfiarr[fi].Name; } //throw (new Exception("1," + t.Name)); } string szMessage = t.Name + "{\r\n"; for (int i = 0; i < sfiarr.Length; i++) { FieldInfo fino = t.GetField(sfiarr[i]); if (fino == null) { continue; } Type itt = fino.FieldType; if (!itt.IsPublic) { continue; } //一般Reserved只放在第一个成员,为加速优化 if (i == 0) { if (itt == typeof(Reserved)) { continue; } } object poValue = fino.GetValue(vrObject); szMessage += sfiarr[i] + ": "; if (poValue == null) { szMessage += "<Empty>\r\n"; } else { szMessage += poValue.ToString() + "\r\n"; } } szMessage += "}"; Logger.Trace(szMessage); }