Beispiel #1
0
        /// <summary>
        /// Set up the pipeline for this client. (note: this call blocks until the client closes, run it asyncrounously if you do not want that.)
        /// </summary>
        public void Hook()
        {
            Dispatcher.Go(() =>
            {
                while (Out.IsOpen)
                {
                    var msg = Out.Receive();
                    if (Out.IsOpen)
                    {
                        //var sd = OnSend.Use();
                        using (OnSend.Use())
                        {
                            try
                            {
                                msg.Write(backing);
                            }
                            catch (Exception) { OnSend.Disuse(); Close(); }
                        }
                        //sd.Dispose();
                        _ = SignalHandler.Signal(SignalMessageSent, msg);
                    }
                }

                if (!KeepAlive)
                {
                    try { backing.Dispose(); }
                    catch
                    {
                    }
                }
            });
            up.Set();
            while (In.IsOpen)
            {
                Message message = null;
                try
                {
                    //var recv = OnReceive.Use();
                    using (OnReceive.Use())
                    {
                        try
                        {
                            message = new Message();
                            message.OnCreateLongStream        = OnLongStreamCreate;
                            message.LongStreamReadCreate      = LongStreamReadCreate;
                            message.OnLongStreamReadComplete += OnLongStreamReadComplete;
                            message.Read(backing);
                        }
                        catch (Exception)
                        {
                            OnReceive.Disuse();
                            Close();
                        }
                    }
                    //recv.Dispose();
                    if (In.IsOpen)
                    {
                        In.Send(message);
                        _ = SignalHandler.Signal(SignalMessageReceived, message);
                    }
                }
                catch (Exception)
                {
                    Close();
                    //OnException(new ClientReadException(e, "Error reading from stream"));
                }
            }
        }