/// <summary> /// Tries to connect to the BoxServer /// </summary> /// <param name="ProcessErrors">Specifies whether to process errors and display them to the user</param> /// <returns>True if succesful</returns> public bool Connect(bool ProcessErrors) { try { var ConnectionString = string.Format( "tcp://{0}:{1}/BoxRemote", Pandora.Profile.Server.Address, Pandora.Profile.Server.Port); m_Remote = Activator.GetObject(typeof(BoxRemote), ConnectionString) as BoxRemote; // Perform Login BoxMessage msg = new LoginMessage(); msg.Username = Pandora.Profile.Server.Username; msg.Password = Pandora.Profile.Server.Password; var data = msg.Compress(); string outType = null; var result = m_Remote.PerformRemoteRequest(msg.GetType().FullName, data, out outType); if (result == null) { MessageBox.Show(Pandora.Localization.TextProvider["Errors.ServerError"]); Connected = false; return(false); } var t = Type.GetType(outType); var outcome = BoxMessage.Decompress(result, t); if (ProcessErrors) { if (!CheckErrors(outcome)) { Connected = false; return(false); } } if (outcome is LoginSuccess) { Connected = true; return(true); } Connected = false; return(false); } catch (Exception err) { Pandora.Log.WriteError(err, "Connection failed to box server"); Connected = false; } return(false); }
/// <summary> /// Handles incoming messages /// </summary> /// <param name="typeName">The name of the type of the message</param> /// <param name="data">The byte array representing the message</param> /// <param name="answerType">Will hold the name of the type of the answer message</param> /// <returns>A stream of bytes</returns> private byte[] BoxRemote_OnMessage(string typeName, byte[] data, out string answerType) { answerType = null; BoxMessage inMsg = null; BoxMessage outMsg = null; Type type = Type.GetType(typeName); if (type != null) { inMsg = BoxMessage.Decompress(data, type); if (inMsg != null) { // Authenticate AuthenticationResult auth = Authentication.Authenticate(inMsg); if (auth == AuthenticationResult.Success) { // Perform message outMsg = inMsg.Perform(); } else { // Return authentication error outMsg = new LoginError(auth); } } else { // Send generic server error outMsg = new ErrorMessage("An error occurred when decompressing the incoming message."); } } else { outMsg = new FeatureNotSupported(); } if (outMsg != null) { answerType = outMsg.GetType().FullName; return(outMsg.Compress()); } else { // Actions that don't require an answer answerType = null; return(null); } }
/// <summary> /// Sends a message to the server. Processes errors too. /// </summary> /// <param name="msg">The message to send to the server</param> /// <returns>A BoxMessage if there is one</returns> public BoxMessage ProcessMessage(BoxMessage msg) { BoxMessage outcome = null; if (!Connected) { Connect(); } if (!Connected) { return(null); } var data = msg.Compress(); string outType = null; try { var result = m_Remote.PerformRemoteRequest(msg.GetType().FullName, data, out outType); if (result == null) { return(null); } var t = Type.GetType(outType); outcome = BoxMessage.Decompress(result, t); if (!CheckErrors(outcome)) { outcome = null; } } catch (Exception err) { Pandora.Log.WriteError(err, "Error when processing a BoxMessage of type: {0}", msg.GetType().FullName); MessageBox.Show(Pandora.Localization.TextProvider["Errors.ConnectionLost"]); Connected = false; outcome = null; } return(outcome); }