mirror of
https://bitbucket.org/anguist/ntpa
synced 2026-10-02 16:31:57 +00:00
Optimization & bugfixing
This commit is contained in:
+35
-70
@@ -23,104 +23,69 @@
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.IO;
|
||||
using Ntp.Analyzer.Log;
|
||||
|
||||
#if __MonoCS__
|
||||
using Mono.Unix;
|
||||
using Mono.Unix.Native;
|
||||
#else
|
||||
using System.Threading;
|
||||
#endif
|
||||
|
||||
namespace Ntp.System
|
||||
{
|
||||
#if __MonoCS__
|
||||
using Mono.Unix;
|
||||
|
||||
public static class InterLock
|
||||
{
|
||||
public static bool Release (string name, LogBase log)
|
||||
{
|
||||
using (Socket socket = new Socket (AddressFamily.Unix, SocketType.Stream, ProtocolType.IP)) {
|
||||
try {
|
||||
socket.Connect (new UnixEndPoint (name));
|
||||
byte[] bytes = Encoding.UTF8.GetBytes ("RELEASE");
|
||||
socket.Send (bytes);
|
||||
socket.Close ();
|
||||
} catch (Exception e) {
|
||||
log.WriteLine ("Could not release lock.", Severity.Error);
|
||||
log.WriteLine (e, Severity.Trace);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#if __MonoCS__
|
||||
public static bool Wait (string name, LogBase log)
|
||||
{
|
||||
byte[] buf = new byte[64];
|
||||
UnixEndPoint endPoint = null;
|
||||
UnixSignal[] signals = new UnixSignal[]{
|
||||
new UnixSignal (Signum.SIGTERM)
|
||||
};
|
||||
|
||||
using (Socket socket = new Socket (AddressFamily.Unix, SocketType.Stream, ProtocolType.IP)) {
|
||||
try {
|
||||
// Unix socket "hack"
|
||||
endPoint = new UnixEndPoint (name);
|
||||
if (File.Exists (endPoint.Filename)) {
|
||||
File.Delete (endPoint.Filename);
|
||||
}
|
||||
socket.Bind (endPoint);
|
||||
socket.Listen (5);
|
||||
Socket sock = socket.Accept ();
|
||||
sock.Receive (buf);
|
||||
sock.Close ();
|
||||
} catch (Exception e) {
|
||||
log.WriteLine ("Could not create socket.", Severity.Error);
|
||||
log.WriteLine (e, Severity.Trace);
|
||||
return false;
|
||||
} finally {
|
||||
if (socket != null && socket.IsBound) {
|
||||
socket.Close ();
|
||||
}
|
||||
if (endPoint != null && File.Exists (endPoint.Filename)) {
|
||||
File.Delete (endPoint.Filename);
|
||||
}
|
||||
}
|
||||
try {
|
||||
UnixSignal.WaitAny (signals, -1);
|
||||
log.WriteLine ("Received kill signal.", Severity.Notice);
|
||||
} catch (Exception e) {
|
||||
log.WriteLine ("Error in Signum handling.", Severity.Error);
|
||||
log.WriteLine (e, Severity.Trace);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
public static class InterLock
|
||||
{
|
||||
public static string Release (string name)
|
||||
#else
|
||||
|
||||
public static bool Release (string name, LogBase log)
|
||||
{
|
||||
try {
|
||||
EventWaitHandle handle = EventWaitHandle.OpenExisting (name);
|
||||
handle.Set ();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return e.Message;
|
||||
log.WriteLine ("Error in mutex handling.", Severity.Error);
|
||||
log.WriteLine (e, Severity.Trace);
|
||||
return false;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string Wait (string name)
|
||||
public static bool Wait (string name, LogBase log)
|
||||
{
|
||||
try {
|
||||
EventWaitHandle handle = new EventWaitHandle (
|
||||
false, EventResetMode.ManualReset,
|
||||
name);
|
||||
false, EventResetMode.ManualReset,
|
||||
name);
|
||||
handle.WaitOne ();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return e.Message;
|
||||
log.WriteLine ("Error in mutex handling.", Severity.Error);
|
||||
log.WriteLine (e, Severity.Trace);
|
||||
return false;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@
|
||||
<ConsolePause>false</ConsolePause>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Server|AnyCPU' ">
|
||||
<Optimize>false</Optimize>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Server</OutputPath>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
@@ -43,6 +43,8 @@
|
||||
<Compile Include="ShellCommand.cs" />
|
||||
<Compile Include="InterLock.cs" />
|
||||
<Compile Include="ProcessInfo.cs" />
|
||||
<Compile Include="Options.cs" />
|
||||
<Compile Include="VersionInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<ItemGroup>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+13
-10
@@ -23,22 +23,25 @@
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
#if __MonoCS__
|
||||
using Mono.Unix.Native;
|
||||
#else
|
||||
System.Diagnostics
|
||||
#endif
|
||||
|
||||
namespace Ntp.System
|
||||
{
|
||||
public class ProcessInfo
|
||||
{
|
||||
public static int ProcessId {
|
||||
get { return getpid (); }
|
||||
get {
|
||||
#if __MonoCS__
|
||||
return Syscall.getpid ();
|
||||
#else
|
||||
return Process.GetCurrentProcess().Id;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if __MonoCS__
|
||||
[DllImport ("libc")]
|
||||
private static extern int getpid ();
|
||||
#else
|
||||
private static int getpid () { return 0; }
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -81,4 +81,4 @@ namespace Ntp.System
|
||||
return proc.StandardOutput;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// VersionInfo.cs
|
||||
//
|
||||
// Author:
|
||||
// Carsten Sonne Larsen <cs@innolan.dk>
|
||||
//
|
||||
// Copyright (c) 2016 Carsten Sonne Larsen
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System;
|
||||
|
||||
namespace Ntp.System
|
||||
{
|
||||
public static class VersionInfo
|
||||
{
|
||||
public static readonly string Number = "0.3a1";
|
||||
|
||||
public static readonly string Text = "v" + Number;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user