using System; using System.Collections.Generic; #if DEBUG using System.Diagnostics; #endif // #if DEBUG using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using SilverMessaging; namespace MonitorApp { /// /// Declaration of PolicyServer class /// public class PolicyServer : IDisposable { ///////////////////////////////////////////////////////////// // Constants and Enumerations private const int Port = 943; private const string CommsPortMarker = "[PORT]"; private const string DomainMarker = "[DOMAIN]"; private static string FileName = "clientaccesspolicy.xml"; ///////////////////////////////////////////////////////////// // Attributes private static byte[] m_policyBuffer; public static byte[] PolicyBuffer { get { return m_policyBuffer; } } private TcpListener m_listener; private bool m_bDisposed = false; ///////////////////////////////////////////////////////////// // Construction static PolicyServer() { // Read the file content as a string string strPolicy = File.ReadAllText(FileName); // Replace the markers strPolicy = strPolicy.Replace(CommsPortMarker, SilverMessageParams.CommsPort.ToString()); strPolicy = strPolicy.Replace(DomainMarker, SilverMessageParams.Domain); // Dump the policy #if DEBUG Trace.TraceInformation("[START OF POLICY]"); Trace.TraceInformation(strPolicy); Trace.TraceInformation("[END OF POLICY]"); #endif // #if DEBUG // Convert to buffer m_policyBuffer = Encoding.Unicode.GetBytes(strPolicy); } public PolicyServer() { // Create the listener m_listener = new TcpListener(IPAddress.Any, Port); m_listener.Start(); // Wait for a connection m_listener.BeginAcceptTcpClient(OnAcceptTcpClient, null); } ~PolicyServer() { this.Dispose(false); } ///////////////////////////////////////////////////////////// // Implementation of IDisposable public void Dispose() { // Call sibling function Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool bDisposing) { // Check to see if Dispose has already been called if (!this.m_bDisposed) { // Note disposing has been done m_bDisposed = true; // If disposing equals true, dispose all managedy and unmanaged resources if (bDisposing) { // Clean up _cleanUp(); } } } private void _cleanUp() { // If listener object valid... if (m_listener != null) { try { // Stop the listener m_listener.Stop(); m_listener = null; } catch (Exception err) { // Dump the error Console.WriteLine(err.Message); } } } ///////////////////////////////////////////////////////////// // Operations public void OnAcceptTcpClient(IAsyncResult ar) { // Shortcuts if (m_bDisposed) return; // Wait for the next connection m_listener.BeginAcceptTcpClient(OnAcceptTcpClient, null); // Handle this connection. try { // Get the connecting client TcpClient client = m_listener.EndAcceptTcpClient(ar); // Construct a policy connection PolicyConnection policyConnection = new PolicyConnection(client); policyConnection.HandleRequest(); } catch (Exception err) { // Dump the error Console.WriteLine(err.Message); } } } /// /// Declaration of PolicyConnection class /// public class PolicyConnection { ///////////////////////////////////////////////////////////// // Constants and Enumerations private static string policyRequestString = ""; ///////////////////////////////////////////////////////////// // Attributes private TcpClient m_client; ///////////////////////////////////////////////////////////// // Construction public PolicyConnection( TcpClient client ) { // Initialize member variables m_client = client; } public void HandleRequest() { // PROCESS THE POLICY REQUEST // Declare a return stream Stream s = m_client.GetStream(); // Read the policy request byte[] buffer = new byte[policyRequestString.Length]; // Give the client 5 seconds to ask m_client.ReceiveTimeout = 5000; s.Read(buffer, 0, buffer.Length); // Send the policy s.Write(PolicyServer.PolicyBuffer, 0, PolicyServer.PolicyBuffer.Length); // Close the connection m_client.Close(); // Console deump Console.WriteLine("Served policy file."); } } }