My idea

english — Tags: , — @ 08:49

Writing a drawing-library is a mental excersive which aims to help MonoUML and to learn something else to allow me to develop better and faster. Of course would love seeing our effort used something else in the future. Nowadays am imagining a new layer to support drawing recognition, you know, draw a circle and… actually draw a circle, will be so cool.

Googling around, I’ve found two nice two nice links related to patterns and software, first one, contains severals links in spanish and some others in english, second one, is a software patterns tutorial-like, must read.

First read, then learn, finally code.

Marshalling problem, explained

english — Tags: , , — @ 22:22

I commented this days ago, now here comes the explanation.

The idea is the following, you have to send through sockets a marshalled structure, this structure will be received by socket server written in C. Then, for example I have:

#pragma pack (1)
struct sample {
int index;
int length; /* Indicates value length */
char *value; /* This value might be any value */
};
typedef struct sample sample_t;
#pragma end

That C structure, in C# is, after marshalling:

[StructLayout (LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct Sample
{
public int Index;
public int DataLength;
[MarshalAs (UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 1)]
public byte []Data;
}

We have our marshalled C# structure. Did you notice the SizeConst value? Well that SizeConst, will, when using Marshal.SizeOf (typeof (Sample)), return a structure known-bytes size. If you are planning to use real-sized structure you MUST marshall them, and there comes the pain. Why? Because you actually need to know about bytes and size of each element send or received by differents processes, in this example, through IP sockets.

C structure uses a dynamic array and I want to use it that way. By setting the size to 1 in C# structure forces me to use 1 byte instead. Really problematic isn’t it? Until today I haven’t yet found the way to do it automatically. My solution is to use Array.Copy to copy the marshalled value (value contained in the byte[]) after the full structure was marshalled, it works, but isn’t too fancy, and when using a Structure that contains a byte[] and setting into that byte[] another Structure that also contains byte[] the problems increase because you need to Array.Copy each one of them.

By the way, I’m using the “google solution” to marshall .NET structures to byte[], am not sure who is the original author of the following code, but seems to be solution to convert from any marshalled structure to bytes array, and viceversa:

public static object RawDeserialize (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[] RawSerialize (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;
}

Be aware that are some tips&tricks while marshalling and you must follow each one of them. Because we are living in managed world and unmanaged is almost a sin.

One year

english — Tags: — @ 23:09

This November 7th is my first anniversary. Let’s celebrate.

Hungry and Foolish

english — Tags: , — @ 18:41

Read, and read it again. Then stay hungry and stay foolish.

Awesome

english — Tags: , — @ 20:10

I never think seeing something like MS/Novell partnership.

Hosted

english — Tags: , — @ 16:16

If you are reading this through monouml.org, then DNS servers are updated and we are using our new hosting.

« Previous Page
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2004-2012 Mario Carrion | powered by WordPress with Barecity