using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class HMessage
{
public int m_iCount;
public int m_iPos;//當前讀取或者寫入位置
public byte[] m_byBufferData = new byte[1000];
void memset(byte[] byBufferData, byte byValue, int iLen)
{
for (int i = 0; i < iLen; i++)
{
byBufferData[i] = byValue;
}
}
public static void memcpy(byte[] byBufferData, byte[] pBuffer, int iLen)
{
for (int i = 0; i < iLen; i++)
{
byBufferData[i] = pBuffer[i];
}
}
public static void memcpy(byte[] byBufferData, byte[] pBuffer, int iLen, int iPosDest)
{
for (int i = 0; i < iLen; i++)
{
byBufferData[i + iPosDest] = pBuffer[i];
}
}
public static void memcpy(byte[] byBufferData, byte[] pBuffer, int iLen, int iPosDest, int iPosSource)
{
for (int i = 0; i < iLen; i++)
{
byBufferData[i + iPosDest] = pBuffer[i + iPosSource];
}
}
//BYTE unsigned char
//WORD unsigned short
//DWORD unsigned int
//QWORD unsigned long long(unsigned long)
public HMessage()
{
m_iCount = 0;
m_iPos = 0;
memset(m_byBufferData, 0, 1000);
}
public static int GetMsgLen(byte[] pBuffer,int iPos)
{
byte[] byBufferData = pBuffer;
UInt16 wRet = 0;
wRet = (UInt16)(byBufferData[0 + iPos] << 8);
wRet = (UInt16)(wRet | (UInt16)(byBufferData[1 + iPos] << 0));
return (int)wRet;
}
public void Init(byte[] pBuffer, int iLen)
{
m_iPos = 0;
m_iCount = iLen;
memcpy(m_byBufferData, pBuffer, iLen);
}
public void PutString(string strData)//序列化
{
byte[] byData = Encoding.UTF8.GetBytes(strData);
UInt16 iLen = (UInt16)byData.Length;
PutUShort(iLen);
memcpy(m_byBufferData, byData, iLen, m_iPos);
AddSize(iLen);
}
public byte[] GetData()
{
SetLen((UInt16)this.m_iCount);
byte[] byBuffer = new byte[this.m_iCount];
memcpy(byBuffer, m_byBufferData, this.m_iCount);
return (byte[])byBuffer;
}
public void PutChar(byte cData)//序列化
{
m_byBufferData[m_iPos] = cData;
AddSize(1);
}
// 0123456789
// pos =8 cont 15
// [10]
// pos =12 cont 15
bool GetByteArrary(byte[] szBuffer, UInt16 wLen)
{
if (CheckGet(wLen) == false)
return false;
memcpy(szBuffer, m_byBufferData, wLen, 0, m_iPos);
AddSize(wLen);
return true;
}
public bool GetString(ref string strData)//序列化
{
UInt16 wLen = 0;
bool bCheck = GetUShort(ref wLen);
if (bCheck == false)
return false;
byte[] szBuffer = new byte[wLen];
bCheck = GetByteArrary(szBuffer, wLen);
if (bCheck == true)
{
strData = Encoding.UTF8.GetString(szBuffer);
}
return bCheck;
}
void AddSize(int iSize)
{
m_iPos += iSize;
if (m_iPos > m_iCount)
m_iCount = m_iPos;
}
void SetLen(UInt16 wLen)//修正長度
{
m_byBufferData[0] = (byte)(wLen >> 8);
m_byBufferData[1] = (byte)((wLen) & 0xff);
}
public void PutUShort(UInt16 dwData)//序列化
{
m_byBufferData[m_iPos + 0] = (byte)(dwData >> 8);
m_byBufferData[m_iPos + 1] = (byte)((dwData) & 0xff);
AddSize(sizeof(UInt16));
}
public bool GetUShort(ref UInt16 wData)//反序列化
{
UInt16 wRet = 0;
if (CheckGet(sizeof(UInt16)) == false)
return false;
wRet = (UInt16)((UInt16)m_byBufferData[m_iPos + 0] << 8);
wRet = (UInt16)(wRet | m_byBufferData[m_iPos + 1] << 0);
m_iPos += sizeof(UInt16);
wData = wRet;
return true;
}
public void PutUInt(UInt32 dwData)//序列化
{
m_byBufferData[m_iPos + 0] = (byte)(dwData >> 24);
m_byBufferData[m_iPos + 1] = (byte)((dwData >> 16) & 0xff);
m_byBufferData[m_iPos + 2] = (byte)((dwData >> 8) & 0xff);
m_byBufferData[m_iPos + 3] = (byte)((dwData) & 0xff);
AddSize(sizeof(UInt32));
// dwData = 0x78563412;
}
public bool GetUInt(ref UInt32 dwData)//反序列化
{
UInt32 dwRet = 0;
if (CheckGet(sizeof(UInt32)) == false)
return false;
dwRet = (UInt32)(m_byBufferData[m_iPos + 0] << 24);
dwRet = (UInt32)(dwRet | (UInt32)m_byBufferData[m_iPos + 1] << 16);
dwRet = (UInt32)(dwRet | (UInt32)m_byBufferData[m_iPos + 2] << 8);
dwRet = (UInt32)(dwRet | (UInt32)m_byBufferData[m_iPos + 3]);
m_iPos += 4;
dwData = dwRet;
return true;
}
public void PutULong(UInt64 dwData)//序列化
{
m_byBufferData[m_iPos + 0] = (byte)(dwData >> 56);
m_byBufferData[m_iPos + 1] = (byte)((dwData >> 48));
m_byBufferData[m_iPos + 2] = (byte)((dwData >> 40) & 0xff);
m_byBufferData[m_iPos + 3] = (byte)(dwData >> 32) ;
m_byBufferData[m_iPos + 4] = (byte)(dwData >> 24) ;
m_byBufferData[m_iPos + 5] = (byte)(dwData >> 16) ;
m_byBufferData[m_iPos + 6] = (byte)(dwData >> 8) ;
m_byBufferData[m_iPos + 7] = (byte)(dwData) ;
AddSize(sizeof(UInt64));
// dwData = 0x78563412;
}
public bool GetULong(ref UInt64 qwData)//反序列化
{
UInt64 qwRet = 0;
if (CheckGet(sizeof(UInt64)) == false)
return false;
qwRet = (UInt64)m_byBufferData[m_iPos + 0] << 56;
qwRet = (UInt64)(qwRet |(UInt64) m_byBufferData[m_iPos + 1] << 48);
qwRet = (UInt64)(qwRet |(UInt64) m_byBufferData[m_iPos + 2] << 40);
qwRet = (UInt64)(qwRet |(UInt64) m_byBufferData[m_iPos + 3] << 32);
qwRet = (UInt64)(qwRet |(UInt64) m_byBufferData[m_iPos + 4] << 24);
qwRet = (UInt64)(qwRet |(UInt64) m_byBufferData[m_iPos + 5] << 16);
qwRet = (UInt64)(qwRet |(UInt64) m_byBufferData[m_iPos + 6] << 8);
qwRet = (UInt64)(qwRet |(UInt64) m_byBufferData[m_iPos + 7]);
m_iPos += sizeof(UInt64);
qwData = qwRet;
return true;
}
bool CheckGet(int iSize)
{
if (m_iPos + iSize > m_iCount)
return false;
return true;
}
};
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/91911.html
標籤:非技術版
上一篇:阿里云服務器linux 本地后臺是filezilla.exe 服務器配置的環境是什么樣的 新手
下一篇:計算機語言
