Esempio n. 1
0
        private void HandleLinkMessage(JsonSocketTunnel link, string msg)
        {
            try
            {
                var ipc_msg = IpcMessage.Deserialize(msg);

                if (!ipc_msg.IsResponse)
                {
                    return;
                }

                if (!_waiting_for_response.ContainsKey(ipc_msg.Id))
                {
                    return;
                }

                if (!string.IsNullOrWhiteSpace(ipc_msg.PayloadText))
                {
                    _responses[ipc_msg.Id] = JsonConvert.DeserializeObject <IpcResult>(ipc_msg.PayloadText, SerializerSettings);
                }

                _waiting_for_response[ipc_msg.Id].Set();
                _waiting_for_response.Remove(ipc_msg.Id);
            }
            catch (Exception ex)
            {
                Log.Warn("Exception occurred while trying to handle incoming IPC message");
                Log.Warn(ex);
            }
        }
Esempio n. 2
0
        private void ListenerLoop()
        {
            while (Socket.IsBound && Running)
            {
                try
                {
                    var incoming_socket = Socket.Accept();
                    var tunnel          = new JsonSocketTunnel(incoming_socket);

                    tunnel.MessageReceived += HandleLinkMessage;
                    tunnel.TunnelClosed    += HandleLinkClose;

                    tunnel.StartMessageLoop();
                    IncomingConnections.Add(tunnel);
                }
                catch (SocketException ex)
                {
                    // we're probably unbound
                    Log.Warn("SocketException in ListenerLoop, stopped listening");
                    Log.Warn(ex);
                    StopListening();
                }
                catch
                {
                    throw;
                }
            }
        }
Esempio n. 3
0
        public void Connect()
        {
            InitializeSocket();
            Socket.Connect(SocketEndPoint);
            Tunnel = new JsonSocketTunnel(Socket);

            Tunnel.MessageReceived += HandleLinkMessage;

            Tunnel.StartMessageLoop();
        }
Esempio n. 4
0
        private void HandleLinkMessage(JsonSocketTunnel tunnel, string message)
        {
            try
            {
                var ipc_message = IpcMessage.Deserialize(message);

                Log.Debug($"Received IPC message from {tunnel}");
                Log.Debug($"Calling {ipc_message.Endpoint} from {ipc_message.SourceName}");

                var ipc_function = IpcFunctionRegistry.GetFunction(ipc_message.Endpoint);
                int ipc_id       = ipc_message.Id;

                var deserialized_payload = ipc_message.PayloadText.Length != 0 ? JsonConvert.DeserializeObject <object[]>(ipc_message.PayloadText, SerializerSettings) : null;

                var ipc_response = new IpcResult();

                try
                {
                    var ret = ipc_function.Execute(deserialized_payload);
                    ipc_response = new IpcResult(true, ret);
                }
                catch (TargetInvocationException ex)
                {
                    Log.Warn($"IPC function {ipc_function} threw an exception: {ex.InnerException.Message}");
                    Log.Warn(ex.InnerException);

                    ipc_response = new IpcResult(false, ex.InnerException.Message);
                }
                catch (Exception ex)
                {
                    Log.Error($"Unknown exception occurred while executing IPC function {ipc_function}");
                    Log.Error(ex);

                    ipc_response = new IpcResult(false, ex.Message);
                }

                tunnel.Send(new IpcMessage(ipc_message, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ipc_response, SerializerSettings))).Serialize());
            }
            catch (Exception ex)
            {
                Log.Warn("Exception occurred in IpcListener.HandleLinkMessage");
                Log.Warn(ex);
            }
        }
Esempio n. 5
0
 private void HandleLinkClose(JsonSocketTunnel tunnel)
 {
     IncomingConnections.Remove(tunnel);
 }