/// <summary> /// from HeadPacket to byte[] /// </summary> /// <param name="p"></param> /// <returns></returns> public static byte[] getBytes(HeadPacket p) { int size = Marshal.SizeOf(p); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(p, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return(arr); }
/// <summary> /// from byte[] to HeadPacket /// </summary> /// <param name="arr"></param> /// <returns></returns> public static HeadPacket fromBytes(byte[] arr) { HeadPacket str = new HeadPacket(); int size = Marshal.SizeOf(str); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.Copy(arr, 0, ptr, size); str = (HeadPacket)Marshal.PtrToStructure(ptr, str.GetType()); Marshal.FreeHGlobal(ptr); return(str); }
/// <summary> /// read from server /// </summary> /// <returns></returns> protected override serverReturnEventArgs receive() { try { Packet packet = new Packet(); int len = Marshal.SizeOf(new HeadPacket()); byte[] receiveData = new byte[len]; HeadPacket headpacket = new HeadPacket(); clientStream.Read(receiveData, 0, len - 4); clientStream.Flush(); String headdata = System.Text.Encoding.UTF8.GetString(receiveData); headpacket = fromBytes(receiveData); packet.headpacket = headpacket; receiveData = new byte[packet.headpacket.size]; clientStream.Read(receiveData, 0, packet.headpacket.size); clientStream.Flush(); String data = System.Text.Encoding.UTF8.GetString(receiveData); return(new serverReturnEventArgs(packet.headpacket.source, packet.headpacket.destination, packet.headpacket.function, packet.headpacket.size, System.Text.Encoding.UTF8.GetString(receiveData))); } catch (System.OutOfMemoryException) { // Server gone wild :/ } catch (System.IO.IOException) { //disconected because you try to reach an other serv } catch (ObjectDisposedException) { //disconected because you try to reach an other serv } return(new serverReturnEventArgs(0, 0, "EERR", 2, "KO")); }
private serverReturnEventArgs receive() { Packet packet = new Packet(); int len = Marshal.SizeOf(new HeadPacket()); byte[] receiveData = new byte[len]; HeadPacket headpacket = new HeadPacket(); clientStream.Read(receiveData, 0, len); clientStream.Flush(); headpacket = fromBytes(receiveData); packet.headpacket = headpacket; receiveData = new byte[packet.headpacket.size]; clientStream.Read(receiveData, 0, packet.headpacket.size); clientStream.Flush(); return(new serverReturnEventArgs(packet.headpacket.source, packet.headpacket.destination, packet.headpacket.function, packet.headpacket.size, System.Text.Encoding.UTF8.GetString(receiveData))); }
/// <summary> /// read from server /// </summary> /// <returns></returns> protected override serverReturnEventArgs receive() { try { Packet packet = new Packet(); int len = Marshal.SizeOf(new HeadPacket()); byte[] receiveData = new byte[len]; HeadPacket headpacket = new HeadPacket(); clientStream.Read(receiveData, 0, len - 4); clientStream.Flush(); String headdata = System.Text.Encoding.UTF8.GetString(receiveData); headpacket = fromBytes(receiveData); packet.headpacket = headpacket; receiveData = new byte[packet.headpacket.size]; clientStream.Read(receiveData, 0, packet.headpacket.size); clientStream.Flush(); String data = System.Text.Encoding.UTF8.GetString(receiveData); return new serverReturnEventArgs(packet.headpacket.source, packet.headpacket.destination, packet.headpacket.function, packet.headpacket.size, System.Text.Encoding.UTF8.GetString(receiveData)); } catch (System.OutOfMemoryException) { // Server gone wild :/ } catch (System.IO.IOException) { //disconected because you try to reach an other serv } catch (ObjectDisposedException) { //disconected because you try to reach an other serv } return new serverReturnEventArgs(0,0,"EERR", 2, "KO"); }
/// <summary> /// from byte[] to HeadPacket /// </summary> /// <param name="arr"></param> /// <returns></returns> public static HeadPacket fromBytes(byte[] arr) { HeadPacket str = new HeadPacket(); int size = Marshal.SizeOf(str); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.Copy(arr, 0, ptr, size); str = (HeadPacket)Marshal.PtrToStructure(ptr, str.GetType()); Marshal.FreeHGlobal(ptr); return str; }
/// <summary> /// from HeadPacket to byte[] /// </summary> /// <param name="p"></param> /// <returns></returns> public static byte[] getBytes(HeadPacket p) { int size = Marshal.SizeOf(p); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(p, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return arr; }
private serverReturnEventArgs receive() { Packet packet = new Packet(); int len = Marshal.SizeOf(new HeadPacket()); byte[] receiveData = new byte[len]; HeadPacket headpacket = new HeadPacket(); clientStream.Read(receiveData, 0, len); clientStream.Flush(); headpacket = fromBytes(receiveData); packet.headpacket = headpacket; receiveData = new byte[packet.headpacket.size]; clientStream.Read(receiveData, 0, packet.headpacket.size); clientStream.Flush(); return new serverReturnEventArgs(packet.headpacket.source, packet.headpacket.destination, packet.headpacket.function, packet.headpacket.size, System.Text.Encoding.UTF8.GetString(receiveData)); }