public bool ReceiveMessage(out string receivedMessage, bool decrypted = true, bool isCommand = true) { receivedMessage = ""; byte[] receivedBytes; bool bytes = this.ReceiveBytes(out receivedBytes, decrypted); if (!bytes) { return(false); } try { receivedMessage = Encoding.UTF8.GetString(receivedBytes, 0, receivedBytes.Length); } catch (DecoderFallbackException ex) { if (isCommand) { this.StatusMessage = "Failed to decode server response."; this.m_lastCommandSucceeded = false; this.OnPropertyChanged("Status"); } return(false); } if (isCommand) { this.StatusMessage = receivedMessage; this.m_lastCommandSucceeded = RconStaticLibrary.IsSuccessReply(receivedMessage); this.OnPropertyChanged("Status"); } return(bytes); }
protected void OnConnected(Task connectionTask) { try { try { connectionTask.Wait(); } catch (AggregateException ex) { ex.Handle((Func <Exception, bool>)(x => { throw x; })); } } catch (ArgumentNullException ex) { this.OnConnectionProblems("Provided hostname is null."); return; } catch (ArgumentOutOfRangeException ex) { this.OnConnectionProblems("Invalid portnumber provided."); return; } catch (SocketException ex) { this.OnConnectionProblems("The provided address and port are inaccessible."); return; } try { this.m_communicationMutex.WaitOne(); if (!this.ReceiveBytes(out this.m_xorKey, false)) { this.StatusMessage = "No response from the server. Are the address and port correct?"; } else { this.SendMessage(string.Format(ServerSession.s_rconLoginCommand, (object)RconCommand.QuoteString(this.ConnectionDetails.ServerPassword)), true); string receivedMessage; this.m_lastCommandSucceeded = this.ReceiveMessage(out receivedMessage, true, true) && RconStaticLibrary.IsSuccessReply(receivedMessage); this.Authenticated = this.m_lastCommandSucceeded; this.OnPropertyChanged("Disconnected"); this.OnPropertyChanged("Status"); if (this.Authenticated) { this.StatusMessage = "Login successful."; this.UpdateServerInfo(); } else { this.StatusMessage = "Login failed. Is your password correct?"; } } } catch (ObjectDisposedException ex) { this.OnConnectionProblems("Internal error: mutex has been disposed."); } catch (AbandonedMutexException ex) { this.OnConnectionProblems("Internal error: mutex has been abandoned."); } catch (InvalidOperationException ex) { this.OnConnectionProblems("Internal error: invalid mutex operation."); } catch (ArgumentNullException ex) { this.OnConnectionProblems("Empty login string provided."); this.m_communicationMutex.ReleaseMutex(); } catch (FormatException ex) { this.OnConnectionProblems("Invalid login string provided."); this.m_communicationMutex.ReleaseMutex(); } }