/// <summary> /// Get the load balance server data. /// </summary> /// <returns></returns> public static Nequeo.Net.Data.context GetLoadBalanceServer() { try { string xmlValidationMessage = string.Empty; // Get the xml file location and // the xsd file schema. string xml = Helper.LoadBalanceServerXmlPath; string xsd = Nequeo.Net.Properties.Resources.LoadBalanceServer; // Validate the filter xml file. if (!Validation.IsXmlValidEx(xsd, xml, out xmlValidationMessage)) { throw new Exception("Xml validation. " + xmlValidationMessage); } // Deserialise the xml file into // the log directory list object GeneralSerialisation serial = new GeneralSerialisation(); Nequeo.Net.Data.context loadData = ((Nequeo.Net.Data.context)serial.Deserialise(typeof(Nequeo.Net.Data.context), xml)); // Return the load balance data. return(loadData); } catch (Exception) { return(null); } }
/// <summary> /// Start the http listener. /// </summary> public void Start() { try { // Create a new http listener _listener = new HttpListener(); // Add URI prefixes to listen for. foreach (string uri in _uriList) { _listener.Prefixes.Add(uri); } // Set the Authentication Schemes. _listener.AuthenticationSchemes = _authenticationSchemes; if (_authenticationSchemeSelectorDelegate != null) { _listener.AuthenticationSchemeSelectorDelegate = _authenticationSchemeSelectorDelegate; } // Get the mime types _contextMimeType = ReaderHttp.GetMimeType(); // Start the listener _listener.Start(); // Keep the service in the running start // listen for in-comming requests. while (_running) { // Start a new listening thread for the // current connection request. IAsyncResult result = _listener.BeginGetContext(new AsyncCallback(AsynchronousListenerCallback), _listener); // Wait until the current context is made and processed before continuing. result.AsyncWaitHandle.WaitOne(); } // If the runServer flag gets set to false, // stop the server and close the listener. _listener.Close(); } catch (Exception ex) { // Log the error. LogHandler.WriteTypeMessage( ex.Message, MethodInfo.GetCurrentMethod(), Nequeo.Net.Common.Helper.EventApplicationName); } finally { if (_listener != null) { _listener.Close(); } } }
/// <summary> /// Get a specific mime content type for the current extension. /// </summary> /// <param name="mimeTypeContext">The mime type context container..</param> /// <param name="currentExtension">The current extension.</param> /// <returns>The new mime content type.</returns> internal static string GetMimeContentType(Nequeo.Net.Data.context mimeTypeContext, string currentExtension) { // Find the mime type for the current extension. IEnumerable <Nequeo.Net.Data.contextMimeType> mimeTypes = mimeTypeContext.mimeTypes.Where(u => u.ext.ToLower() == currentExtension.ToLower()); if (mimeTypes != null) { if (mimeTypes.Count() > 0) { return(mimeTypes.First().mime); } } // Default if not found. return("text/plain"); }
/// <summary> /// Get the specific uploaded files list save path. /// </summary> /// <param name="mimeTypeContext">The mime type context container..</param> /// <param name="fileName">The current file; if an uploaded files list file.</param> /// <param name="directory">The directory to find a match for.</param> /// <returns>The local file directory where files are to be saved.</returns> internal static string UploadedFilesListSavePath(Nequeo.Net.Data.context mimeTypeContext, string fileName, string directory) { // Find the matching directory upload path. IEnumerable <Nequeo.Net.Data.contextUploadFile> uploadFiles = mimeTypeContext.uploadFiles.Where(u => (u.basePath.TrimEnd('\\').ToLower() == directory.TrimEnd('\\').ToLower()) && (u.uploadedFilesList.ToLower() == fileName.ToLower())); if (uploadFiles != null) { if (uploadFiles.Count() > 0) { return(uploadFiles.First().savePath.TrimEnd('\\') + "\\"); } } // Default is not found. return(null); }
/// <summary> /// Get the specific upload file save path. /// </summary> /// <param name="mimeTypeContext">The mime type context container..</param> /// <param name="fileName">The current file; if an uploader file.</param> /// <param name="directory">The directory to find a match for.</param> /// <returns>The local file directory where files are to be saved.</returns> internal static long UploaderMaxUploadFileZise(Nequeo.Net.Data.context mimeTypeContext, string fileName, string directory) { // Find the matching directory upload path. IEnumerable <Nequeo.Net.Data.contextUploadFile> uploadFiles = mimeTypeContext.uploadFiles.Where(u => (u.basePath.TrimEnd('\\').ToLower() == directory.TrimEnd('\\').ToLower()) && (u.fileUploader.ToLower() == fileName.ToLower())); if (uploadFiles != null) { if (uploadFiles.Count() > 0) { return(uploadFiles.First().maxUploadFileSize); } } // Default is not found. return(0); }
/// <summary> /// Get the collection of base paths used. /// </summary> /// <param name="mimeTypeContext">The mime type context container..</param> /// <returns>The collection of base paths.</returns> internal static string[] GetBasePaths(Nequeo.Net.Data.context mimeTypeContext) { // Get the list. Nequeo.Net.Data.contextUploadFile[] uploaded = mimeTypeContext.uploadFiles; List <string> uploadedBasePaths = new List <string>(); if (uploaded != null) { if (uploaded.Count() > 0) { foreach (Nequeo.Net.Data.contextUploadFile item in uploaded) { uploadedBasePaths.Add(item.basePath.TrimEnd('\\') + "\\"); } } } // Return the collection of base paths. return(uploadedBasePaths.ToArray()); }
/// <summary> /// Dispose(bool disposing) executes in two distinct scenarios. If disposing /// equals true, the method has been called directly or indirectly by a user's /// code. Managed and unmanaged resources can be disposed. If disposing equals /// false, the method has been called by the runtime from inside the finalizer /// and you should not reference other objects. Only unmanaged resources can /// be disposed. /// </summary> protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this._disposed) { // Note disposing has been done. _disposed = true; // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. if (_server != null) { _server.Dispose(); } if (_contextManager != null) { _contextManager.Dispose(); } if (_clients != null) { CloseClients(); } } // Call the appropriate methods to clean up // unmanaged resources here. _server = null; _contextManager = null; _loadServers = null; _clients = null; _lockObject = null; } }
/// <summary> /// Get the mime type context data. /// </summary> /// <returns>The collection of mime types.</returns> public static Nequeo.Net.Data.context GetMimeType() { try { string xmlValidationMessage = string.Empty; // Get the xml file location and // the xsd file schema. string xml = Nequeo.Net.Common.Helper.MimeTypeXmlPath; string xsd = Nequeo.Net.Properties.Resources.MimeType; // Validate the filter xml file. if (!Validation.IsXmlValidEx(xsd, xml, out xmlValidationMessage)) { throw new Exception("Xml validation. " + xmlValidationMessage); } // Deserialise the xml file into // the log directory list object GeneralSerialisation serial = new GeneralSerialisation(); Nequeo.Net.Data.context mimeTypeData = ((Nequeo.Net.Data.context)serial.Deserialise(typeof(Nequeo.Net.Data.context), xml)); // Return the mime types. return(mimeTypeData); } catch (Exception ex) { // Log the error. LogHandler.WriteTypeMessage( ex.Message, MethodInfo.GetCurrentMethod(), Nequeo.Net.Common.Helper.EventApplicationName); return(null); } }
/// <summary> /// Start the http listener. /// </summary> /// <exception cref="System.ArgumentNullException">Thrown when the urlBaseAddress parameter is missing.</exception> /// <exception cref="System.ArgumentNullException">Thrown when the localBaseDirectory parameter is missing.</exception> public void Start() { // If the server is already running. if (_running) { return; } bool ret = false; try { // Make sure the url base address is set. if (System.String.IsNullOrEmpty(_urlBaseAddress)) { throw new ArgumentNullException("urlBaseAddress"); } // Make sure the local base directory is set. if (System.String.IsNullOrEmpty(_localBaseDirectory)) { throw new ArgumentNullException("localBaseDirectory"); } // Create a new http listener _listener = new Nequeo.Net.Server.HttpServer(_urlBaseAddress, _localBaseDirectory); // Get the mime types _contextMimeType = ReaderHttp.GetMimeType(); // Initialise the listener if (!_initialized) { ret = _listener.Initialize(); _initialized = ret; } // Start the listener if (ret) { ret = _listener.Start(); } // Assign the running indicator. if (ret) { _running = true; } } catch (Exception ex) { // Log the error. LogHandler.WriteTypeMessage( ex.Message, MethodInfo.GetCurrentMethod(), Nequeo.Net.Common.Helper.EventApplicationName); // Stop the http listener. Stop(); } }
/// <summary> /// Initialise the server. /// </summary> private void Init() { try { // Get the data. _remoteServers = new ConcurrentBag <RemoteServer>(); _loadServers = Data.Helper.GetLoadBalanceServer(); // For each load balance server in the file // add to the remote server collection. foreach (Nequeo.Net.Data.contextServer item in _loadServers.servers) { // Add the remote server. _remoteServers.Add( new RemoteServer() { Name = item.name, Host = item.host, Port = item.port, Secure = item.secure } ); } // Get the certificate reader. Nequeo.Security.Configuration.Reader certificateReader = new Nequeo.Security.Configuration.Reader(); Nequeo.Net.Configuration.Reader hostReader = new Nequeo.Net.Configuration.Reader(); string socketProviderHostPrefix = "LoadBalance_"; string hostProviderFullName = socketProviderHostPrefix + "SocketProviderV6"; string hostProviderFullNameSecure = socketProviderHostPrefix + "SocketProviderV6Ssl"; // Start the server. _serverV6 = new Nequeo.Net.ProxyServer(System.Net.IPAddress.IPv6Any, hostReader.GetServerHost(hostProviderFullName).Port, _remoteServers, _algorithmType); _serverV6.Name = "Load Balance Server"; _serverV6.ServiceName = "LoadBalanceServer"; _serverV6.InterceptItems = _interceptItems; _serverV6.Timeout = hostReader.GetServerHost(hostProviderFullName).ClientTimeOut; _serverV6.ReadBufferSize = 32768; _serverV6.WriteBufferSize = 32768; _serverV6.ResponseBufferCapacity = 10000000; _serverV6.RequestBufferCapacity = 10000000; // Start the server. _serverV4 = new Nequeo.Net.ProxyServer(System.Net.IPAddress.Any, hostReader.GetServerHost(hostProviderFullName).Port, _remoteServers, _algorithmType); _serverV4.Name = "Load Balance Server"; _serverV4.ServiceName = "LoadBalanceServer"; _serverV4.InterceptItems = _interceptItems; _serverV4.Timeout = hostReader.GetServerHost(hostProviderFullName).ClientTimeOut; _serverV4.ReadBufferSize = 32768; _serverV4.WriteBufferSize = 32768; _serverV4.ResponseBufferCapacity = 10000000; _serverV4.RequestBufferCapacity = 10000000; // Start the server. _serverSecureV6 = new Nequeo.Net.ProxyServer(System.Net.IPAddress.IPv6Any, hostReader.GetServerHost(hostProviderFullNameSecure).Port, _remoteServers, _algorithmType); _serverSecureV6.Name = "Load Balance Server"; _serverSecureV6.ServiceName = "LoadBalanceServer"; _serverSecureV6.InterceptItems = _interceptItems; _serverSecureV6.Timeout = hostReader.GetServerHost(hostProviderFullNameSecure).ClientTimeOut; _serverSecureV6.ReadBufferSize = 32768; _serverSecureV6.WriteBufferSize = 32768; _serverSecureV6.ResponseBufferCapacity = 10000000; _serverSecureV6.RequestBufferCapacity = 10000000; // Start the server. _serverSecureV4 = new Nequeo.Net.ProxyServer(System.Net.IPAddress.Any, hostReader.GetServerHost(hostProviderFullNameSecure).Port, _remoteServers, _algorithmType); _serverSecureV4.Name = "Load Balance Server"; _serverSecureV4.ServiceName = "LoadBalanceServer"; _serverSecureV4.InterceptItems = _interceptItems; _serverSecureV4.Timeout = hostReader.GetServerHost(hostProviderFullNameSecure).ClientTimeOut; _serverSecureV4.ReadBufferSize = 32768; _serverSecureV4.WriteBufferSize = 32768; _serverSecureV4.ResponseBufferCapacity = 10000000; _serverSecureV4.RequestBufferCapacity = 10000000; try { // Look for the certificate information in the configuration file. // Get the certificate if any. X509Certificate2 serverCertificate = certificateReader.GetServerCredentials(); // If a certificate exists. if (serverCertificate != null) { // Get the secure servers. _serverSecureV6.UseSslConnection = true; _serverSecureV6.X509Certificate = serverCertificate; _serverSecureV4.UseSslConnection = true; _serverSecureV4.X509Certificate = serverCertificate; } } catch { } } catch (Exception) { if (_serverV6 != null) { _serverV6.Dispose(); } if (_serverSecureV6 != null) { _serverSecureV6.Dispose(); } if (_serverV4 != null) { _serverV4.Dispose(); } if (_serverSecureV4 != null) { _serverSecureV4.Dispose(); } _serverV6 = null; _serverSecureV6 = null; _serverV4 = null; _serverSecureV4 = null; throw; } }
/// <summary> /// Initialise the server. /// </summary> private void Init() { try { // Create the client list. _clients = new SortedDictionary <string, Nequeo.Net.Data.ConnectionContext>(); _loadServers = Data.Helper.GetLoadBalanceServer(); _contextManager = new Nequeo.Server.SingleContextManager(); string socketProviderHostPrefix = "LoadBalance_"; string hostProviderFullName = socketProviderHostPrefix + "SocketProviderV6"; string hostProviderFullNameSecure = socketProviderHostPrefix + "SocketProviderV6Ssl"; // Get the certificate reader. Nequeo.Security.Configuration.Reader certificateReader = new Nequeo.Security.Configuration.Reader(); Nequeo.Net.Configuration.Reader hostReader = new Nequeo.Net.Configuration.Reader(); // Create the server endpoint. Nequeo.Net.Sockets.MultiEndpointModel[] model = new Nequeo.Net.Sockets.MultiEndpointModel[] { // None secure. new Nequeo.Net.Sockets.MultiEndpointModel() { Port = hostReader.GetServerHost(hostProviderFullName).Port, Addresses = new System.Net.IPAddress[] { System.Net.IPAddress.IPv6Any, System.Net.IPAddress.Any } }, // Secure. new Nequeo.Net.Sockets.MultiEndpointModel() { Port = hostReader.GetServerHost(hostProviderFullNameSecure).Port, Addresses = new System.Net.IPAddress[] { System.Net.IPAddress.IPv6Any, System.Net.IPAddress.Any } } }; // Start the server. _server = new Nequeo.Net.WebServerSingle(model, _maxClient); _server.ServerContextManager = _contextManager; _server.Name = "Load Balance Server"; _server.ServiceName = "LoadBalanceServer"; _server.SocketProviderHostPrefix = socketProviderHostPrefix; _server.OnClientConnected = (context) => ClientConnected(context); _server.OnClientDisconnected = (context) => ClientDisconnected(context); _server.OnWebContext += Server_OnWebContext; _server.Timeout = hostReader.GetServerHost(hostProviderFullName).ClientTimeOut; _server.ReadBufferSize = 32768; _server.WriteBufferSize = 32768; _server.HeaderTimeout = 30000; _server.RequestTimeout = 30000; _server.ResponseTimeout = 30000; _server.ResponseBufferCapacity = 10000000; _server.RequestBufferCapacity = 10000000; _server.MaximumReadLength = 1000000; // Inititalise. _server.Initialisation(); try { // Look for the certificate information in the configuration file. // Get the certificate if any. X509Certificate2 serverCertificate = certificateReader.GetServerCredentials(); // If a certificate exists. if (serverCertificate != null) { // Get the secure servers. _server.Server[2].UseSslConnection = true; _server.Server[2].X509Certificate = serverCertificate; _server.Server[3].UseSslConnection = true; _server.Server[3].X509Certificate = serverCertificate; } } catch { } // For each server in the collection. for (int i = 0; i < _server.Server.NumberOfServers; i++) { // Set what needs to be polled. _server.Server[i].PollReader(true); _server.Server[i].PollWriter(false); _server.Server[i].PollError(false); } } catch (Exception) { if (_server != null) { _server.Dispose(); } _server = null; _clients = null; throw; } }