Page History
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //관리되지않는 코드 (포인터 사용및 구조체를 시퀜셜하게 만들기위해)
using System.Web.Script.Serialization;
[StructLayout(LayoutKind.Sequential, Pack = 1)] // #pragma pack(1) 과 동일한 효과..
public class ConvertPacket
{
// 받은 패킷 바이트를 구조체화함
public void SetBuffer(byte[] pbyte)
{
unsafe // c#에서는 포인터연산을 통한 메모리복사를 안전하지 않는 코드로 판단, unsafe블록안에 감싸줘야함
{
try
{
fixed (byte* fixed_buffer = pbyte)
{
Marshal.PtrToStructure((IntPtr)fixed_buffer, this);
}
}
catch (Exception e)
{
LogUtil.Instance.writeLog(LOGLEVELTYPE.WARN, "SYSTEM", "LSSOCK", string.Format("ParserError {0} - {1}", e.Message, e.Source) , "ConvertPacket::SetBuffer");
}
}
}
// 보낼패킷 구조체를 byte에 복사함
public void PutBuffer(byte[] pbyte)
{
int mycount = Marshal.SizeOf(this);
unsafe
{
fixed (byte* fixed_buffer = pbyte)
{
Marshal.StructureToPtr(this, (IntPtr)fixed_buffer, true);
for (int i = 0; i < mycount; i++)
{
pbyte[i] = fixed_buffer[i];
}
}
}
}
public int GetSize()
{
return Marshal.SizeOf(this);
}
public byte[] GetBuffer()
{
byte[] resultObj = new byte[ GetSize() ];
PutBuffer( resultObj );
return resultObj;
}
public override string ToString() //이것은 디버깅을위해 패킷을 까보는 활동의 코드입니다.
{
return new JavaScriptSerializer().Serialize(this);
}
} |
...