public async Task SendExecutionState(string sessionId, ExecutionStateDto state) { Logger.LogLine($"SignalRClientService: Dispatching ExecutionStateDto to client.."); try { await hubConnection.InvokeAsync(nameof(CodeHub.DispatchAppStateToClient), sessionId, state); } catch (Exception ex) { Logger.LogLine($"SignalRClientService.SendExecutionState: {ex.Message}"); } }
public override void Write(string value) { var execState = new ExecutionStateDto { SessionId = sessionid, State = RemoteAppState.WriteOutput, Output = HttpUtility.HtmlEncode(value.ToString()) }; networkStream.WriteByte((byte)MessageType.WorkerExecutionState); Serializer.SerializeWithLengthPrefix(networkStream, execState, PrefixStyle.Fixed32); }
public override string ReadLine() { try { var execState = new ExecutionStateDto { SessionId = sessionid, State = RemoteAppState.WaitForInputLine }; networkStream.WriteByte((byte)MessageType.WorkerExecutionState); Serializer.SerializeWithLengthPrefix(networkStream, execState, PrefixStyle.Fixed32); Logger.LogLine($"CLIENT: sent remote inputline request (unfinished queue: {unfinishedRequests.Count})"); unfinishedRequests.Enqueue(execState); isWaitingForInput = true; while (isWaitingForInput) { if (client.Available > 0) { byte msgHeader = (byte)networkStream.ReadByte(); MessageType msgType = (MessageType)msgHeader; unfinishedRequests.Dequeue(); isWaitingForInput = false; if (msgType == MessageType.ServerRemoteInput) { var remoteInput = Serializer.DeserializeWithLengthPrefix <RemoteInputDto>(networkStream, PrefixStyle.Fixed32); receivedInput = remoteInput.Input; Logger.LogLine($"CLIENT: received remote input {receivedInput} of length {receivedInput.Length}"); } else { Logger.LogLine($"CLIENT: expected msgtype {MessageType.ServerRemoteInput} but got {msgType}"); } } else { Thread.Sleep(100); } } Logger.LogLine($"CLIENT: returning remote input \"{receivedInput}\" of length {receivedInput.Length} to execution flow"); return(receivedInput); } finally { receivedInput = null; } }
protected void Listener_WorkerExecutionState(TcpClient workerClient, ExecutionStateDto state) { var session = GetSession(state.SessionId); if (session != null) { session.LastAppState = state.State; if (session.LastAppState == RemoteAppState.Running) { session.Heartbeat(); //idle timer reset on start of user code execution } } clientService.SendExecutionState(state.SessionId, state); }
public void RunApplication(string sessionid, TcpClient client, byte[] assemblyBytes) { var networkStream = client.GetStream(); var outputRedirector = new ConsoleOutputService(sessionid, client); var inputRedirector = new ConsoleInputService(sessionid, client); ExecutionStateDto execState; var assembly = Assembly.Load(assemblyBytes); try { execState = new ExecutionStateDto { SessionId = sessionid, State = RemoteAppState.Running }; networkStream.WriteByte((byte)MessageType.WorkerExecutionState); Serializer.SerializeWithLengthPrefix(networkStream, execState, PrefixStyle.Fixed32); Logger.LogLine($"CLIENT: sent execution state {execState.State}"); //redirect console Console.SetOut(outputRedirector); Console.SetIn(inputRedirector); //invoke main method var mainParms = assembly.EntryPoint.GetParameters(); if (mainParms.Count() == 0) { assembly.EntryPoint.Invoke(null, null); } else { if (mainParms[0].ParameterType == typeof(string[])) { assembly.EntryPoint.Invoke(null, new string[] { null }); } else { assembly.EntryPoint.Invoke(null, null); } } //reset console redirection Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }); Console.SetIn(new StreamReader(Console.OpenStandardInput())); execState = new ExecutionStateDto { SessionId = sessionid, State = RemoteAppState.Ended }; networkStream.WriteByte((byte)MessageType.WorkerExecutionState); Serializer.SerializeWithLengthPrefix(networkStream, execState, PrefixStyle.Fixed32); Logger.LogLine($"CLIENT: sent execution state {execState.State}"); } catch (SocketException socketEx) { Logger.LogLine($"CLIENT Error: {socketEx.Message}"); } catch (Exception ex) { execState = new ExecutionStateDto { SessionId = sessionid, State = RemoteAppState.Crashed, Exception = ExceptionDto.FromException(ex) }; networkStream.WriteByte((byte)MessageType.WorkerExecutionState); Serializer.SerializeWithLengthPrefix(networkStream, execState, PrefixStyle.Fixed32); } finally { } }
/// <summary> /// Should only be used from the server side /// </summary> /// <returns></returns> public async Task DispatchAppStateToClient(string targetConnectionId, ExecutionStateDto state) { Logger.LogLine($"CodeHub: Sending Appstate to {targetConnectionId} -- {state.State}"); await Clients.Client(targetConnectionId).SendAsync("ApplicationStateChanged", state); }