/// <summary> /// Used by other thread to wait for receival of special Reset-type assignment /// </summary> private static void WaitForResetAssignment() { stream = null; ConnectToServer(); if (stream == null) { return; } while (true) { if (stream.DataAvailable) { SetAssembliesNames(); Assignment newAssignment = NetworkSupport.ReceiveObject <Assignment> (stream); if (newAssignment.type == Assignment.AssignmentType.Reset) // Reset assignment signals that RenderClient should expect new render work { resetAssignmentAlreadyReceived = true; return; } } } }
/// <summary> /// Receives all objects which are necessary from render server /// IRayScene - scene representation like solids, textures, lights, camera, ... /// IRenderer - renderer itself including IImageFunction; needed for RenderPixel method /// Sends number of threads available at client to render /// </summary> private static void ExchangeNecessaryInfo() { if (resetAssignmentAlreadyReceived) // if reset assignment was received in WaitForResetAssignment, skip it's receival here { resetAssignmentAlreadyReceived = false; } else { SetAssembliesNames(); Assignment assignment = NetworkSupport.ReceiveObject <Assignment> (stream); if (assignment.type != Assignment.AssignmentType.Reset) { throw new Exception($"Received assignment with {assignment.type}. {Assignment.AssignmentType.Reset} expected."); } } scene = NetworkSupport.ReceiveObject <IRayScene> (stream); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\nData for {0} received and deserialized.", typeof(IRayScene).Name); renderer = NetworkSupport.ReceiveObject <IRenderer> (stream); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Data for {0} received and deserialized.\n", typeof(IRenderer).Name); NetworkSupport.SendInt(stream, threadCount); ClientMaster.singleton = new ClientMaster(null, scene, renderer); }
/// <summary> /// Thread in infinite loop accepting new assignments /// Loop is ended by receiving Ending Assignment /// </summary> private static void ReceiveAssignments() { while (true) { try { if (stream.DataAvailable) { Assignment newAssignment = NetworkSupport.ReceiveObject <Assignment>(stream); if (newAssignment.type == Assignment.AssignmentType.Ending) // Ending assignment signals end of rendering { finished = true; // signal for threads in Consume method lock (consoleLock) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("\nRendering on server finished or no more assignments are expected to be received.\n"); } return; } lock (consoleLock) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(@"Data for assignment [{0}, {1}, {2}, {3}] received and deserialized.", newAssignment.x1, newAssignment.y1, newAssignment.x2, newAssignment.y2); } ClientMaster.singleton.availableAssignments.Enqueue(newAssignment); // adds new assignment to the queue; it is later taken from there by thread in Consume method } } catch (ObjectDisposedException) { ConnectionLost(); } } }