Skip to content

Commit

Permalink
single binary support for both mono and ms dotnet
Browse files Browse the repository at this point in the history
  • Loading branch information
bamboo committed Dec 14, 2003
1 parent 2b3aaec commit 57ff156
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/Bamboo.Prevalence/Implementation/CommandOutput.cs
Expand Up @@ -36,7 +36,7 @@
using System.Runtime.Serialization.Formatters.Binary;
using System.Configuration;

#if MONO
#region Mono Support
namespace System.IO
{
using System.Runtime.CompilerServices;
Expand All @@ -49,7 +49,7 @@ internal sealed class MonoIO
public static extern bool Flush(IntPtr handle, out MonoIOError error);
}
}
#endif
#endregion

namespace Bamboo.Prevalence.Implementation
{
Expand All @@ -63,6 +63,8 @@ internal sealed class CommandLogWriter
private BinaryFormatter _formatter;

private NumberedFileCreator _fileCreator;

private static HardFlushDelegate HardFlush = SelectHardFlushImpl();

public CommandLogWriter(NumberedFileCreator creator, BinaryFormatter formatter)
{
Expand Down Expand Up @@ -103,6 +105,12 @@ public void WriteCommand(ICommand command)
}
*/

// New strategy for dealing with out of space errors,
// sometimes the _output.SetLength above would fail
// leaving the file corrupted.
// This new strategy fixes the problem by always
// growing the file before writing anything to it.

MemoryStream stream = new MemoryStream();
_formatter.Serialize(stream, command);
byte[] bytes = stream.ToArray();
Expand Down Expand Up @@ -183,10 +191,26 @@ private System.IO.FileStream NextOutputLog()
FileMode.CreateNew, FileAccess.Write, FileShare.Read
);
}

#region Mono support

delegate void HardFlushDelegate(System.IO.FileStream stream);

private static HardFlushDelegate SelectHardFlushImpl()
{
// mono linux is 128
if (128 == (int)Environment.OSVersion.Platform)
{
return new HardFlushDelegate(MonoHardFlush);
}
else
{
return new HardFlushDelegate(Win32HardFlush);
}
}

#if MONO
[System.Security.SuppressUnmanagedCodeSecurity]
private static void HardFlush(System.IO.FileStream stream)
private static void MonoHardFlush(System.IO.FileStream stream)
{
System.IO.MonoIOError result = System.IO.MonoIOError.ERROR_SUCCESS;
System.IO.MonoIO.Flush(stream.Handle, out result);
Expand All @@ -196,19 +220,17 @@ private static void HardFlush(System.IO.FileStream stream)
}
}

#else
[System.Security.SuppressUnmanagedCodeSecurity] // optimization...
private static void HardFlush(System.IO.FileStream stream)
private static void Win32HardFlush(System.IO.FileStream stream)
{
if (0 == FlushFileBuffers(stream.Handle))
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
}

// TODO: Remove this dependency on win32 if at all possible

[DllImport("KERNEL32.DLL", EntryPoint="FlushFileBuffers", PreserveSig=true, CallingConvention=CallingConvention.Winapi, SetLastError=true)]
private static extern int FlushFileBuffers(IntPtr handle);
#endif
#endregion
}
}

0 comments on commit 57ff156

Please sign in to comment.