Friday

binary serializer c

below is binary serializer class sample, could be used for serialization objects into strings (not XML , fixed length string) and back.
This class is good for creation network transport protocol layer classes.
here is sample of object to serialize
 
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using CSharp.Core;
using CSharp.Core.Utility;

namespace CCLib.CCEngine
{

namespace COMDATA
{

public class ControlCharacters
{

public const byte STX = 0x2;
public const byte EOT = 0x4;
public const byte NAK = 0x15;
public const byte ACK = 0x06;
public static Char StartOfText = Convert.ToChar(0x02);
public static Char FieldSeparator = Convert.ToChar(0x1C); //Convert.ToChar(28);
public static Char RecordSeparator = Convert.ToChar(0x1D);
public static Char EndOfText = Convert.ToChar(0x03);
public static Char MessageDelimiter = Convert.ToChar(".");
}

public class ControlBytes
{
public static byte StartOfText = 0x02;
public static byte EndOfText = 0x03;
}

/// <summary>
/// Class used for converting data-structures into byte
/// </summary>
public class BinFormatter
{

public static string RemoveContrChars(string s)
{
string rt = String.Empty;
if (!String.IsNullOrEmpty(s))
{
try
{
int si = s.IndexOf(ControlCharacters.StartOfText);
int se = s.IndexOf(ControlCharacters.EndOfText);

int spos = si + 1;
int epos = se - 1;

if (epos > spos) s = s.Substring(spos, epos - spos - 1);
rt = s.Replace(ControlCharacters.FieldSeparator, '|').Replace(ControlCharacters.RecordSeparator,'?');

//replace non printable characters with whitespace
rt = Regex.Replace(rt,"[\x01-\x1F]", "");

}
catch (Exception ex)
{
Logger.prn(ex);
}
}
return rt;
}

public static string Cut(string s)
{
int st = s.IndexOf(ControlCharacters.StartOfText);
int end = s.IndexOf(ControlCharacters.EndOfText);
return s.Substring(st + 1, end - st);
}


public static byte[] AddControlCharsAndLRC(string s)
{
return AddControlCharsAndLRC(Encoding.ASCII.GetBytes(s));
}

public static byte[] AddControlCharsAndLRC(byte[] s)
{
byte[] r = new byte[s.Length + 3];
r[0] = ControlBytes.StartOfText;
s.CopyTo(r, 1);

r[s.Length + 1] = ControlBytes.EndOfText;
r[s.Length + 2] = calculateLRC(r, 1, s.Length + 2);
return r;
}


public static object RawDeserializeStr(string s, Type anytype)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(s);
return RawDeserializeEx(data, anytype);
}

public static object RawDeserializeEx(byte[] rawdatas, Type anytype)
{
int rawsize = Marshal.SizeOf(anytype);
if (rawsize > rawdatas.Length)
return null;
GCHandle handle = GCHandle.Alloc(rawdatas, GCHandleType.Pinned);
IntPtr buffer = handle.AddrOfPinnedObject();
object retobj = Marshal.PtrToStructure(buffer, anytype);
handle.Free();
return retobj;

}

public static byte calculateLRC(byte[] ba, int offset, int length)
{
byte lrc = 0x0;
for (int i = offset; i < length; i++)
lrc ^= ba[i];
return lrc;
}

public static byte calculateLRC(string s, int offset, int length)
{
byte[] b = Encoding.ASCII.GetBytes(s);
byte l = calculateLRC(b, offset, length);
return l;
}
public static byte calculateLRC(string s)
{
byte l = calculateLRC(s, 0, s.Length);
return l;
}
public static byte[] RawSerializeEx(object anything)
{
int rawsize = Marshal.SizeOf(anything);
byte[] rawdatas = new byte[rawsize];
GCHandle handle = GCHandle.Alloc(rawdatas, GCHandleType.Pinned);
IntPtr buffer = handle.AddrOfPinnedObject();
Marshal.StructureToPtr(anything, buffer, false);
handle.Free();
return rawdatas;
}

public string StrFromByte(byte[] des2)
{
ASCIIEncoding utf82 = new ASCIIEncoding();
string uf82 = utf82.GetString(des2);
return uf82;
}

public static string RawSerializeStr(object anything)
{
byte[] des2 = RawSerializeEx(anything);
ASCIIEncoding utf82 = new ASCIIEncoding();
string uf82 = utf82.GetString(des2);
return uf82;
}

public static string sSet(string st, int plen)
{
if (st == "" || st.Length == plen)
{
return st;
}
else
{
char[] pro = Set(st, plen);
return new string(pro);
}
}

public static string CutTill(string st, int plen)
{
if (st.Length > plen)
{
return st.Substring(0, plen);
}
else
{
return st;
}
}


public static char[] Set(string st, int plen)
{
return Set(st, plen, ' ');
}

public static char[] Set(char st)
{
char[] pro = new char[1];
pro[0] = st;
return pro;
}

public static char[] Set(string st, int plen, char white_space)
{
int slen = st.Length;
char[] pro = new string(white_space, plen).ToCharArray();// new char[plen];
int count = slen > pro.Length ? pro.Length : slen;
st.CopyTo(0, pro, 0, count);
return pro;
}




public static string GetStringFromHex(string s)
{
string result = "";
string s2 = s.Replace(" ", "");
for (int i = 0; i < s2.Length; i += 2)
{
result += Convert.ToChar(int.Parse(s2.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));
}
return result;
}

public static string GetLRC(string s)
{
int checksum = 0;
foreach (char c in GetStringFromHex(s))
{
checksum ^= Convert.ToByte(c);
}
return checksum.ToString("X2");
}


}
}
}

No comments:

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...