private async Task SendAsync(BaseClientState state, Message msg) { try { await state.SendAsync(msg); } catch (Exception ex) { this.Logger.Danger(ex); this.TerminateProcess(state.Name); ProcessUpdateData endData = new ProcessUpdateData(true, state.Name); await this.BroadcastMessageAsync(MessageConstants.TERMINATE_IDENTIFIER, endData.Serialize()); } }
private async Task ListenAsync(TCPSocketClientState state) { try { do { Stream stream = state.Stream; int bytesRead = await stream.ReadAsync(state.Buffer); if (bytesRead <= 0) { TcpState tcpState = state.GetTcpState(); if (tcpState != TcpState.Established) { this.Dispatcher.TerminateProcess(state.Name); ProcessUpdateData updateData = new ProcessUpdateData(true, state.Name); await this.Dispatcher.BroadcastMessageAsync(MessageConstants.TERMINATE_IDENTIFIER, updateData.Serialize()); return; } state.ClearBuffer(); await Task.Delay(10); continue; } string data = Encoding.UTF8.GetString(state.Buffer, 0, bytesRead); state.ClearBuffer(); List <Message> msgs = state.Reader.Read(data); await this.Dispatcher.HandleMessagesAsync(state, msgs); // Maximum register payload size is 395 bytes, the client is sending garbage. if (!state.IsRegistered && state.Reader.Size >= 600) { state.Reader.Clear(); } }while (!state.IsDisposed && state.IsRegistered); } catch (Exception e) { await this.ExceptionHandler.OnClientStateExceptionAsync(state, e); } }
private async Task TerminateProcessAsync(string name, string token) { ProcessUpdateData data; if (token != this.Token) { this.Logger.Warning($"Attempt to terminate a remote process ({name}) with an invalid token."); data = new ProcessUpdateData(false, name); await this.BroadcastMessageAsync(MessageConstants.TERMINATE_IDENTIFIER, data.Serialize()); } else if (!this.States.ContainsKey(name)) { this.Logger.Warning($"Attempt to terminate a non-existing remote process ({name}). Discarding."); data = new ProcessUpdateData(false, name); await this.BroadcastMessageAsync(MessageConstants.TERMINATE_IDENTIFIER, data.Serialize()); } else { data = new ProcessUpdateData(true, name); await this.BroadcastMessageAsync(MessageConstants.TERMINATE_IDENTIFIER, data.Serialize()); this.States.Remove(name, out BaseClientState state); state.Dispose(); this.Logger.Nice("Process", ConsoleColor.Magenta, $"Terminating remote process | {name}"); } }