Created base GlobalTerminalService classes.

This commit is contained in:
Greg Edwards 2014-05-26 12:42:47 -04:00
parent 649bf3521d
commit 8e7495f007
8 changed files with 241 additions and 32 deletions

View File

@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace GlobalTerminalService
{
public abstract class GTServerBase
{
public GTServerBase(int port) : this(port, false, 4)
{
}
public GTServerBase(int port, bool useSsl)
: this(port, useSsl, 4)
{
}
public GTServerBase(int port, bool useSsl, int threads)
{
Threads = threads;
UseSsl = useSsl;
m_workers = new List<Thread>(threads);
// todo: enumerate nic bindings
m_listener = new TcpListener(port);
if (UseSsl)
{
m_cert = new X509Certificate2("cert.pfx", "letmein");
}
}
public int Threads
{
get;
private set;
}
protected bool UseSsl
{
get;
set;
}
private List<Thread> m_workers;
private object m_lock = new object();
private bool m_closing;
private TcpListener m_listener;
private X509Certificate m_cert;
public void BeginPolling()
{
lock (m_lock)
{
if (m_workers.Count > 0) return;
m_closing = false;
m_listener.Start();
for (int x = 0; x < Threads; x++)
{
Thread t = new Thread(MainLoop);
t.Start();
}
}
}
public void EndPolling()
{
lock (m_lock)
{
if (m_workers.Count == 0) return;
m_listener.Stop();
m_closing = true;
// wait for worker threads to exit
while (m_workers.Count > 0) { }
}
}
private void MainLoop(object o)
{
while (!m_closing)
{
if (!m_listener.Pending())
{
Thread.Sleep(5);
continue;
}
Stream s = AcceptRequest();
if (s == null)
{
Thread.Sleep(5);
continue;
}
byte[] data = new byte[4];
s.Read(data, 0, 4);
int length = BitConverter.ToInt32(data, 0);
data = new byte[length];
BitConverter.GetBytes(length).CopyTo(data, 0);
s.Read(data, 4, length - 4); // todo: stop DoS by timing out blocking requests
ProcessRequest(data, s);
}
m_workers.Remove(Thread.CurrentThread);
}
private Stream AcceptRequest()
{
// todo: handle case that another thread took the request and return null
TcpClient c = m_listener.AcceptTcpClient();
if (UseSsl)
{
SslStream sslClient = new SslStream(c.GetStream());
sslClient.AuthenticateAsServer(m_cert);
return sslClient;
}
else return c.GetStream();
}
protected virtual void ProcessRequest(byte[] data, Stream response);
}
}

View File

@ -5,11 +5,11 @@
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{1DC69104-D141-4C5E-943D-80F037BD471B}</ProjectGuid>
<OutputType>Exe</OutputType>
<ProjectGuid>{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>bvServer4</RootNamespace>
<AssemblyName>bvServer4</AssemblyName>
<RootNamespace>GlobalTerminalService</RootNamespace>
<AssemblyName>GlobalTerminalService</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
@ -40,9 +40,17 @@
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="GTServerBase.cs" />
<Compile Include="Service1.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Service1.Designer.cs">
<DependentUpon>Service1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace GlobalTerminalService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}

View File

@ -5,11 +5,11 @@ using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("bvServer4")]
[assembly: AssemblyTitle("GlobalTerminalService")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("bvServer4")]
[assembly: AssemblyProduct("GlobalTerminalService")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -20,7 +20,7 @@ using System.Runtime.InteropServices;
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("5f8f1b1e-98ed-4063-acdc-0cdf30f1c421")]
[assembly: Guid("47274bc4-d452-4eeb-acfb-6aedfe702a65")]
// Version information for an assembly consists of the following four values:
//

View File

@ -0,0 +1,37 @@
namespace GlobalTerminalService
{
partial class Service1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}
#endregion
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace GlobalTerminalService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
}

View File

@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace bvServer4
{
class Program
{
static void Main(string[] args)
{
}
}
}

View File

@ -15,7 +15,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sslProxy", "sslProxy\sslPro
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bvCrawler5", "bvCrawler5\bvCrawler5.csproj", "{BEA49E66-2204-4C10-8ED6-17F58018C2BD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bvServer4", "bvServer4\bvServer4.csproj", "{1DC69104-D141-4C5E-943D-80F037BD471B}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GlobalTerminalService", "GlobalTerminalService\GlobalTerminalService.csproj", "{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -97,16 +97,16 @@ Global
{BEA49E66-2204-4C10-8ED6-17F58018C2BD}.Release|Mixed Platforms.Build.0 = Release|x86
{BEA49E66-2204-4C10-8ED6-17F58018C2BD}.Release|x86.ActiveCfg = Release|x86
{BEA49E66-2204-4C10-8ED6-17F58018C2BD}.Release|x86.Build.0 = Release|x86
{1DC69104-D141-4C5E-943D-80F037BD471B}.Debug|Any CPU.ActiveCfg = Debug|x86
{1DC69104-D141-4C5E-943D-80F037BD471B}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{1DC69104-D141-4C5E-943D-80F037BD471B}.Debug|Mixed Platforms.Build.0 = Debug|x86
{1DC69104-D141-4C5E-943D-80F037BD471B}.Debug|x86.ActiveCfg = Debug|x86
{1DC69104-D141-4C5E-943D-80F037BD471B}.Debug|x86.Build.0 = Debug|x86
{1DC69104-D141-4C5E-943D-80F037BD471B}.Release|Any CPU.ActiveCfg = Release|x86
{1DC69104-D141-4C5E-943D-80F037BD471B}.Release|Mixed Platforms.ActiveCfg = Release|x86
{1DC69104-D141-4C5E-943D-80F037BD471B}.Release|Mixed Platforms.Build.0 = Release|x86
{1DC69104-D141-4C5E-943D-80F037BD471B}.Release|x86.ActiveCfg = Release|x86
{1DC69104-D141-4C5E-943D-80F037BD471B}.Release|x86.Build.0 = Release|x86
{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}.Debug|Any CPU.ActiveCfg = Debug|x86
{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}.Debug|Mixed Platforms.Build.0 = Debug|x86
{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}.Debug|x86.ActiveCfg = Debug|x86
{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}.Debug|x86.Build.0 = Debug|x86
{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}.Release|Any CPU.ActiveCfg = Release|x86
{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}.Release|Mixed Platforms.ActiveCfg = Release|x86
{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}.Release|Mixed Platforms.Build.0 = Release|x86
{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}.Release|x86.ActiveCfg = Release|x86
{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE