Пример #1
0
        async void ReadMessages()
        {
            byte[] buffer = new byte [1];

            while (!disposed && connectionStream != null)
            {
                BinaryMessage msg;
                byte          type;

                try {
                    int nr = await connectionStream.ReadAsync(buffer, 0, 1, mainCancelSource.Token).ConfigureAwait(false);

                    if (nr == 0)
                    {
                        // Connection closed. Remote process should die by itself.
                        return;
                    }
                    type = buffer [0];
                    msg  = BinaryMessage.Read(connectionStream);
                } catch (Exception ex) {
                    if (disposed)
                    {
                        return;
                    }
                    LoggingService.LogError("ReadMessage failed", ex);
                    StopRemoteProcess(isAsync: true);
                    PostSetStatus(ConnectionStatus.ConnectionFailed, "Connection to remote process failed.");
                    return;
                }

                HandleMessage(msg, type);
            }
        }
Пример #2
0
        public void Run()
        {
            List <BinaryMessage> messages = new List <BinaryMessage> ();

            while (true)
            {
                BinaryMessage msg;
                int           type;
                try {
                    type = inStream.ReadByte();
                    if (type == -1)
                    {
                        break;
                    }
                    msg = BinaryMessage.Read(inStream);
                    msg = LoadMessageData(msg);
                    if (DebugMode)
                    {
                        String mtype = type == MESSAGE_QUEUE_END ? "[M] " : "[Q] ";
                        Console.WriteLine("[SERVER] XS >> RP " + mtype + msg);
                    }
                } catch (Exception e) {
                    Console.WriteLine(e);
                    break;
                }
                if (msg.Name == "Stop" && msg.Target == "Process")
                {
                    try {
                        WriteMessage(0, msg.CreateResponse());
                    } catch {
                        // Ignore
                    }
                    break;
                }
                if (msg.Name == "Ping" && msg.Target == "Process")
                {
                    try {
                        WriteMessage(0, msg.CreateResponse());
                    } catch {
                        // Ignore
                    }
                    continue;
                }
                messages.Add(msg);
                if (type == MESSAGE_QUEUE_END)
                {
                    ProcessMessages(messages);
                    messages.Clear();
                }
            }
        }
        async void ReadMessages()
        {
            byte[] buffer = new byte [1];

            while (!disposed && connectionStream != null)
            {
                BinaryMessage msg;
                byte          type;

                try {
                    int nr = await connectionStream.ReadAsync(buffer, 0, 1, mainCancelSource.Token).ConfigureAwait(false);

                    if (nr == 0)
                    {
                        // Connection closed. Remote process should die by itself.
                        return;
                    }
                    type = buffer [0];
                    msg  = BinaryMessage.Read(connectionStream);
                } catch (Exception ex) {
                    if (disposed)
                    {
                        return;
                    }
                    LoggingService.LogError("ReadMessage failed", ex);
                    StopRemoteProcess(isAsync: true);
                    PostSetStatus(ConnectionStatus.ConnectionFailed, "Connection to remote process failed.");
                    return;
                }

                lock (pendingMessageTasks) {
                    var t = Task.Run(() => {
                        msg = LoadMessageData(msg);
                        if (type == 0)
                        {
                            ProcessResponse(msg);
                        }
                        else
                        {
                            ProcessRemoteMessage(msg);
                        }
                    });
                    t.ContinueWith(ta => {
                        lock (pendingMessageTasks) {
                            pendingMessageTasks.Remove(ta);
                        }
                    }).Ignore();
                    pendingMessageTasks.Add(t);
                }
            }
        }