/// <summary> /// Auto detect behavior of instruction and start relative relay server. /// </summary> /// <param name="instruction">Instruction that contain relay params.</param> /// <returns>Established server.</returns> public static RelayServer EstablishAutoRelayServer(RelayInstruction instruction) { switch (instruction.behavior) { case RelayInstruction.RelayBehavior.Duplex: return(EstablishDuplexRelayServer(instruction)); case RelayInstruction.RelayBehavior.Broadcasting: return(EstablishBroadcastingRelayServer(instruction)); default: throw new InvalidCastException("Invalid behavior type. Relay server not started."); } }
/// <summary> /// Redirect recived query from current server to other. /// </summary> /// <param name="controller">Controller that manage curernt transmission.</param> public static byte[] QueryHandler_BroadcastingRelay(BroadcastTransmissionController controller) { // Tries to detect a relay instruction. if (!RelayInstruction.TryToDetectTarget( UniformClient.BaseClient.routingTable.intructions, controller.PipeName, out RelayInstruction relayInstruction)) { Console.WriteLine( "Relay instruction for \"" + controller.PipeName + "\" not found. Add instuction to \"BaseClient.routingTable.intructions\" collection."); return(UniformDataOperator.Binary.BinaryHandler.ToByteArray("Error 404: Routing server not found. Con'tact administrator.")); } // Markers for managing thread. bool relayedMessageRecieved = false; byte[] relayedData = null; // Log Console.WriteLine("Requesting broadcasting: " + relayInstruction.routingIP + "/" + relayInstruction.pipeName); // Requiest message from relaying broadcasting server. UniformClient.BaseClient.ReceiveAnonymousBroadcastMessage( relayInstruction.routingIP, relayInstruction.pipeName, delegate(TransmissionLine lint, UniformQueries.Query query) { // Conver message to string. relayedData = UniformDataOperator.Binary.BinaryHandler.ToByteArray(query); // Unlock thread. relayedMessageRecieved = true; }); // Wait until broadcasting message. while (!relayedMessageRecieved) { Thread.Sleep(15); } // Return recived message. return(relayedData); }
/// <summary> /// Establish server suitable provided instruction that would retranslate broadcasting from target server. /// </summary> /// <param name="instruction">Instruction that contain relay params.</param> /// <returns>Established server.</returns> public static RelayServer EstablishBroadcastingRelayServer(RelayInstruction instruction) { // Check instruction. if (instruction == null) { throw new NullReferenceException("Routing instruction can't be null"); } // Instiniate server. RelayServer serverBufer = new RelayServer { // Set fields. pipeName = instruction.entryPipeName }; // Starting server loop. serverBufer.StartServerThread( instruction.entryPipeName + " #" + Guid.NewGuid(), serverBufer, ThreadingServerLoop_BroadcastingRelay); return(serverBufer); }
static void Main(string[] args) { #region Detect processes conflicts // Check does this instance a new single app, or same app already runned. if (!ServerAppConfigurator.IsProccessisUnique()) { // Log error. Console.WriteLine("\"THB Data Server\" already started. Application not allow multiple instances at single moment."); // Wait a time until exit. Thread.Sleep(2000); return; } #endregion #region Set default data \load DLLs \appling arguments // Set default thread count. Can be changed via args or command. //threadsCount = Environment.ProcessorCount; //longTermServerThreads = new UniformServer.BaseServer[threadsCount]; // React on uniform arguments. ServerAppConfigurator.ArgsReactor(args); // Check direcroties UniformDataOperator.AssembliesManagement.AssembliesHandler.LoadAssemblies( AppDomain.CurrentDomain.BaseDirectory + "libs\\"); // Looking for replaced types that could be used by handlers. UniformDataOperator.AssembliesManagement.Modifiers.TypeReplacer.RescanAssemblies(); #endregion // Request anonymous configuration for system. General.SetLocalSecurityAuthority(SecurityLevel.Anonymous); #region Load routing tables. // Try to load tables. UniformClient.BaseClient.LoadRoutingTables(AppDomain.CurrentDomain.BaseDirectory + "plugins\\"); // Init new if not found. if (UniformClient.BaseClient.routingTable.intructions.Count == 0) { SetDefaultRoutingTable(); } #endregion #region Loaded query handler processors // Draw line ConsoleDraw.Primitives.DrawSpacedLine(); // Initialize Queue monitor. try { _ = UniformQueries.API.QueryHandlers; } catch (Exception ex) { Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); Console.WriteLine("QUERY HANDLER PROCESSORS LOADING TERMINATED:\n{0}", ex.Message); } ConsoleDraw.Primitives.DrawSpacedLine(); Console.WriteLine(); #endregion #region Start queries monitor threads for (int i = 0; i < Environment.ProcessorCount; i++) { // Configuring the routing instruction. RelayInstruction relayInstruction = new RelayInstruction() { entryPipeName = OPEN_CHANEL }; // Instiniating server. var serverBufer = UniformServer.Standard.RelayServer.EstablishDuplexRelayServer(relayInstruction); // Changing thread culture. serverBufer.ServerThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); // Skip line Console.WriteLine(); } #endregion #region Start broadcast relaying chanels. foreach (Instruction instruction in UniformClient.BaseClient.routingTable.intructions) { // Looking for broadcasting relays. if (instruction is RelayInstruction relayInstruction && relayInstruction.behavior == RelayInstruction.RelayBehavior.Broadcasting) { // Start relay server. UniformServer.Standard.RelayServer.EstablishBroadcastingRelayServer(relayInstruction); } } #endregion // Show help. UniformServer.Commands.BaseCommands("help"); #region Main loop // Main loop that will provide server services until application close. while (!UniformServer.ServerAppConfigurator.AppTerminated) { // Check input if (Console.KeyAvailable) { // Log responce. Console.Write("\nEnter command: "); // Read command. string command = Console.ReadLine(); // Processing of entered command. UniformServer.Commands.BaseCommands(command); } Thread.Sleep(UniformServer.ServerAppConfigurator.PreferedThreadsSleepTime); } #endregion #region Finalize Console.WriteLine(); // Stop started servers. ServerAPI.StopAllServers(); // Whait until close. Console.WriteLine("Press any key to exit..."); Console.ReadKey(); #endregion }