[Mono-list] Writing daemons in C#

Paolo Molaro lupus@ximian.com
Wed, 20 Nov 2002 23:01:25 +0100


On 11/20/02 Miguel de Icaza wrote:
> > 1) per-OS assembly
> > 	* faster
> > 	* smaller
> > 	* easier to deploy
> > 2) OS-indep assembly + OS-dependent shared library
> > 	* slower
> > 	* bigger
> > 	* one more thing to deploy
> > 
> > The choice looks like a no brainer to me:-)
> 
> You also have to consider that you will need to wrap structures, for
> example, lets take the `stat' structure.   That is much easier to
> implement by using the scheme I suggested.
> 
> If we want to have a shared-library-less implementation the parser for
> structures will probably be pretty complex to generate a c# version of
> it that matches the Unix version of it.
> 
> Your turn ;)

Dude, you never give up, eh? :-)

There is no difference in supporting the stat call, your method is still
bigger and it requires a shared library in C. And there is no complexity
difference: you don't write a parser for structures, you just need to
know the names of the fields you're interested in and use offsetof to
calc their offset inside the struct.
The same script/C program that retrieves the values for the defines can
also create the C# structure that matches the OS stat structure:

	[StructLayout (LayoutKind.Explicit)]
	struct StatInternal {
		[FieldOffset (0)] int st_dev;
		[FieldOffset (4)] int st_ino;
		[...]
	};

The assembly will export instead a different Stat structure that doesn't
change on different systems (but some fields can be unset):

	public struct Stat {
		int st_dev;
		int st_ino;
		[...]
	};

The script that creates StatInternal will also output a method that
translates a StatInternal to a Stat:

	static void ConvertStat (ref StatInternal from, out Stat to) {
		to.st_dev = from.st_dev;
		[...]
	}

The assembly will have two stat methods, the pinvoke one that uses
StatInternal and the normal one that uses Stat:

	[DllImport (libc", "stat")]
	static int stat_internal (string path, out StatInternal stat);

and

	public static int stat (string path, out Stat stat) {
		StatInternal si;
		int result = stat_internal (path, out si);
		ConvertStat (ref si, out stat);
		return result;
	}

We _could_ make StatInternal and stat_internal() public, so that people
that know what they are doing can poke into it and make their programm
less portable:-)
As you see, still no ugly shared library to depend upon:-)

lupus

-- 
-----------------------------------------------------------------
lupus@debian.org                                     debian/rules
lupus@ximian.com                             Monkeys do it better