Beispiel #1
0
            void ExecuteMessage(BinaryMessage msg)
            {
                BinaryMessage response = null;
                var           sw       = System.Diagnostics.Stopwatch.StartNew();

                try {
                    if (msg.Name == "FlushMessages")
                    {
                        response = msg.CreateResponse();
                    }
                    else
                    {
                        response = listener.ProcessMessage(msg);
                    }
                } catch (Exception ex) {
                    if (ex is TargetInvocationException)
                    {
                        ex = ((TargetInvocationException)ex).InnerException;
                    }
                    server.LogError(ex);
                    response = msg.CreateErrorResponse(ex.Message, !(ex is RemoteProcessException));
                    Console.WriteLine(ex);
                }
                if (response != null)
                {
                    response.Id             = msg.Id;
                    response.ProcessingTime = sw.ElapsedMilliseconds;
                    server.SendResponse(response);
                }
                else if (!msg.OneWay)
                {
                    server.SendResponse(msg.CreateErrorResponse("Got no response from server", true));
                }
            }
		void PostMessageInternal (BinaryMessage message)
		{
			if ((status != ConnectionStatus.Connected || disposed) && !message.BypassConnection) {
				ProcessResponse (message.CreateErrorResponse ("Connection is closed"));
				return;
			}

			try {
				if (DebugMode)
					message.SentTime = DateTime.Now;

				// Now send the message. This one will need a response

				if (DebugMode)
					LogMessage (MessageType.Request, message);

				connectionStream.WriteByte ((byte)RequestType.QueueEnd);
				message.Write (connectionStream);

				connectionStream.Flush ();
			}	
			catch (Exception ex){
				if (connection == null || (!connection.Connected && status == ConnectionStatus.Connected)) {
					AbortConnection ("Disconnected from remote process due to a communication error", isAsync: true);
				} else
					ProcessResponse (message.CreateErrorResponse (ex.ToString ()));
			}
		}
		void ProcessResponse (BinaryMessage msg)
		{
			DateTime respTime = DateTime.Now;

			lock (messageWaiters) {
				MessageRequest req;
				if (messageWaiters.TryGetValue (msg.Id, out req)) {
					messageWaiters.Remove (msg.Id);
					try {
						var rt = req.Request.GetResponseType ();
						if (rt != typeof (BinaryMessage)) {
							var resp = (BinaryMessage)Activator.CreateInstance (rt);
							resp.CopyFrom (msg);
							msg = resp;
						}
					} catch (Exception ex) {
						msg = msg.CreateErrorResponse (ex.ToString ());
					}

					if (DebugMode) {
						var time = (int)(respTime - req.Request.SentTime).TotalMilliseconds;
						LogMessage (MessageType.Response, msg, time);
					}

					if (!req.Request.OneWay)
						NotifyResponse (req, msg);
				}
				else if (DebugMode)
					LogMessage (MessageType.Response, msg, -1);
			}
		}
		public Task<BinaryMessage> SendMessage (BinaryMessage message)
		{
			message.ReadCustomData ();
			var interceptor = Interceptor;
			if (interceptor != null && !interceptor.PreProcessMessage (message))
				return Task.FromResult (message.CreateErrorResponse ("Message was refused by interceptor"));

			var cs = new TaskCompletionSource<BinaryMessage> ();
			PostMessage (message, cs, true);
			return cs.Task;
		}
Beispiel #5
0
 public void WriteMessage(byte type, BinaryMessage msg)
 {
     msg.ReadCustomData();
     lock (listeners) {
         if (DebugMode)
         {
             Console.WriteLine("[SERVER] XS << RP " + type + " [" + msg.ProcessingTime + "ms] " + msg);
         }
         outStream.WriteByte(type);
         try {
             msg.Write(outStream);
         } catch (Exception ex) {
             msg.CreateErrorResponse(ex.ToString(), true).Write(outStream);
         }
     }
 }