summaryrefslogtreecommitdiffstats
path: root/Server/Main.cs
blob: d292909e3873f917dfe5037368045cebaa934e7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;

namespace BigEyes.Server
{
	public class EntryPoint
	{
		[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
		private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
		private static void Main(string[] args)
		{
			new Thread(new ThreadStart(reduceMemory)).Start();
			Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
			foreach(Process p in Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
			{
				if(p.Id != Process.GetCurrentProcess().Id)
				{
					p.Kill();
				}
			}
			try
			{
				System.IO.StreamReader sr = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("BigEyes.Server.viewer.txt"));
				Server s = new Server(new IPEndPoint(Dns.GetHostEntry(sr.ReadToEnd()).AddressList[0], 2201));
				sr.Close();
				s.Error+=new Server.Invoker(s_Error);
				System.Threading.Thread.CurrentThread.Suspend();
			}
			catch
			{
				Main(args);
			}
		}
		private static void s_Error()
		{
			Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
		}
		private static void reduceMemory()
		{
			for (; ; )
			{
				Thread.Sleep(400);
				try
				{
					GC.WaitForPendingFinalizers();
					GC.Collect();
					if (Environment.OSVersion.Platform == PlatformID.Win32NT)
					{
						SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
					}
				}
				catch { }
			}
		}
	}
}