Пример #1
0
        public bool Send(string message, bool flush = false, bool wait = false)
        {
            if (!_connected)
            {
                return(false);
            }

            try
            {
                var output = Encoding.UTF8.GetBytes(message);
                Debug.Assert(output.Length < IpcLib.ServerInBufferSize);

                _pipe.BeginWrite(output, 0, output.Length, OnAsyncWriteComplete, _pipe);

                if (flush)
                {
                    _pipe.Flush();
                }
                if (wait && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    _pipe.WaitForPipeDrain();
                }
            }
            catch (Exception)
            {
                _pipe.Close();
            }

            return(true);
        }
Пример #2
0
        void DeserializerMain()
        {
            try
            {
                while (true)
                {
                    var msg = Serializer.Deserialize(m_stream);

                    m_msgQueue.Enqueue(msg);

                    var ev = this.NewMessageEvent;
                    if (ev != null)
                    {
                        ev();
                    }
                }
            }
            catch (Exception e)
            {
                m_isConnected = false;

                m_stream.Close();

                trace.TraceInformation("[RX]: error {0}", e.Message);

                var ev = this.NewMessageEvent;
                if (ev != null)
                {
                    ev();
                }
            }

            trace.TraceVerbose("Deserializer thread ending");
        }
Пример #3
0
        public void OnAsyncMessage(PipeStream pipe, Byte[] data, Int32 bytes, Object state)
        {
            string scriptBlock = String.Empty;

            try
            {
                scriptBlock = Encoding.UTF8.GetString(data, 0, bytes);
                // EventLogHelper.WriteToEventLog(Config.ServiceName, 0, "IPC Script Block received: " + Environment.NewLine +
                // "------------------------------------------" + Environment.NewLine +
                // scriptBlock + Environment.NewLine +
                // "------------------------------------------" + Environment.NewLine);
            }
            catch (Exception ex)
            {
                EventLogHelper.WriteToEventLog(Config.ServiceName, 2,
                                               "BluService: There was an error in reading script block as UTF8 string: " + Environment.NewLine + ex.Message);
            }

            // Execute and Write back results
            try
            {
                string psResult      = PowerShellRunspace.ExecuteScriptBlock(scriptBlock);
                byte[] psResultBytes = Encoding.Default.GetBytes(psResult);
                string result        = Encoding.UTF8.GetString(psResultBytes, 0, Encoding.UTF8.GetByteCount(psResult));
                data = Encoding.UTF8.GetBytes(result.ToCharArray());
                pipe.BeginWrite(data, 0, Encoding.UTF8.GetByteCount(result), OnAsyncWriteComplete, pipe);
                pipe.Close();
            }
            catch (Exception ex)
            {
                EventLogHelper.WriteToEventLog(Config.ServiceName, 2,
                                               "There is an error in executing script block UTF8 string: " + Environment.NewLine + ex.Message);
                pipe.Close();
            }
        }
Пример #4
0
        private void requestClientTeardown()
        {
            var handshake = new NKRemotingMessage();

            handshake.command = NKRemotingMessage.Command.NKRemotingClose;
            handshake.args    = new string[] { };
            writeObject(syncPipeOut, handshake);
            try
            {
                asyncPipe.Close();
                syncPipeIn.Close();
                syncPipeOut.Close();
            }
            catch { }
        }
        protected override void Act()
        {
            _pipeStream.Close();

            // give async read time to complete
            _readThread.Join(100);
        }
Пример #6
0
 void CfxRuntime_OnCfxShutdown()
 {
     ShuttingDown = true;
     pipeIn.Close();
     pipeOut.Close();
     RemoteService.RemoveConnection(this);
 }
        protected override void Act()
        {
            _pipeStream.Close();

            // give write time to complete
            _writehread.Join(100);
        }
Пример #8
0
        public void Close()
        {
            if (ioStream != null)
            {
                ioStream.Close();
            }

            bClientConn = false;

            /* Wake up the write thread so it can die */
            newWriteData.Set();

            /* Close connection streams */
            if (waitThread != null)
            {
                if (sStream != null)
                {
                    sStream.Close();
                }
                else if (cStream != null)
                {
                    cStream.Close();
                }
            }
        }
Пример #9
0
        public void Close()
        {
            CloseConnection = true;

            PipeStream.Close();
            PipeStream.Dispose();
        }
Пример #10
0
 public void Close()
 {
     if (ioStream != null)
     {
         ioStream.Close();
     }
 }
        protected void Act()
        {
            _pipeStream.Close();

            // give async write time to complete
            _asyncWriteResult.AsyncWaitHandle.WaitOne(100);
        }
Пример #12
0
        private void Shutdown(Exception?shutdownReason)
        {
            lock (_shutdownLock)
            {
                if (_socketDisposed)
                {
                    return;
                }

                // Make sure to close the connection only after the _aborted flag is set.
                // Without this, the RequestsCanBeAbortedMidRead test will sometimes fail when
                // a BadHttpRequestException is thrown instead of a TaskCanceledException.
                _socketDisposed = true;

                // shutdownReason should only be null if the output was completed gracefully, so no one should ever
                // ever observe the nondescript ConnectionAbortedException except for connection middleware attempting
                // to half close the connection which is currently unsupported.
                _shutdownReason = shutdownReason ?? new ConnectionAbortedException("The pipe transport's send loop completed gracefully.");

                _logger.LogInformation("Shutdown connection {0}: {1}", ConnectionId, _shutdownReason.Message);

                try
                {
                    _stream.Close();
                }
                catch
                {
                    // Ignore any errors from _stream.Close() since we're tearing down the connection anyway.
                }

                _stream.Dispose();
            }
        }
Пример #13
0
        private bool DisconnectStreams()
        {
            // assume that pipeStream has already been created and connected (for server)
            if (pipeStream.IsConnected)
            {
                if (pipeReader != null)
                {
                    pipeReader.Close();
                }

                if (pipeWriter != null)
                {
                    pipeWriter.Close();
                }

                pipeStream.Close();
            }
            else
            {
                pipeReader = null;
                pipeWriter = null;
                pipeStream = null;
            }
            return(true);
        }
Пример #14
0
 public void Close()
 {
     pipeStream.WaitForPipeDrain();
     pipeStream.Close();
     pipeStream.Dispose();
     pipeStream = null;
 }
Пример #15
0
 /// <inheritdoc/>
 public override void Disconnect()
 {
     lock (_instanceLock)
     {
         base.Disconnect();
         _stream.Close();
     }
 }
Пример #16
0
 protected override Task OnDisconnect()
 {
     return(Task.Factory.StartNew((Action)(() =>
     {
         _pipeStream.WaitForPipeDrain();
         _pipeStream.Close();
     })));
 }
Пример #17
0
 private void OnlyClose()
 {
     IsRunning = false;
     timer.Stop();
     writer.Close();
     reader.Close();
     pipeOut.Close();
     pipeIn.Close();
 }
Пример #18
0
 public void Close()
 {
     if (mPipe == null)
     {
         return;
     }
     try { mPipe.Close(); } catch { }
     mPipe = null;
 }
Пример #19
0
 /// <summary>
 /// Close the pipe.
 /// </summary>
 public void Close()
 {
     if (!closed && pipe != null)
     {
         pipe.Close();
         pipe = null;
     }
     closed = true;
 }
Пример #20
0
 public void Close()
 {
     if (_conn == null)
     {
         return;
     }
     _conn.Close();
     _conn = null;
 }
Пример #21
0
 public void Close()
 {
     if (m_conn == null)
     {
         return;
     }
     m_conn.Close();
     m_conn = null;
 }
Пример #22
0
 /// <summary>Close the pipe</summary>
 public void Close()
 {
     if (_pipeStream?.IsConnected ?? false)
     {
         _pipeStream?.WaitForPipeDrain();
     }
     _pipeStream?.Close();
     _pipeStream?.Dispose();
     _pipeStream = null;
 }
Пример #23
0
 /// <summary>
 /// Disconnects from the named pipe
 /// </summary>
 private void Disconnect()
 {
     lock (_streamLock)
     {
         if ((_stream != null) && (_stream.IsConnected))
         {
             _stream.Close();
         }
     }
 }
Пример #24
0
 /// <summary>
 /// Closing a stream
 /// </summary>
 public void Close()
 {
     if (pipeStream.IsConnected)
     {
         pipeStream.WaitForPipeDrain();
     }
     pipeStream.Close();
     pipeStream.Dispose();
     pipeStream = null;
 }
Пример #25
0
 public void Disconnect()
 {
     try
     {
         stream.Close();
     }
     finally
     {
         stream.Dispose();
     }
     this.Disconnected.RaiseEvent(this);
 }
Пример #26
0
    public void OnAsyncMessage(PipeStream pipe, Byte [] data, Int32 bytes, Object state)
    {
        Console.WriteLine("Message: " + (Int32)state + " bytes: " + bytes);
        data = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(data, 0, bytes).ToUpper().ToCharArray());

        // Write results
        try {
            pipe.BeginWrite(data, 0, bytes, OnAsyncWriteComplete, pipe);
        }
        catch (Exception) {
            pipe.Close();
        }
    }
Пример #27
0
        public void OnAsyncMessage(PipeStream pipe, Byte[] data, Int32 bytes, Object state)
        {
            data = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(data, 0, bytes).ToUpper().ToCharArray());

            try
            {
                MessageBox.Show(Encoding.ASCII.GetString(data, 0, bytes));

                pipe.BeginWrite(data, 0, bytes, OnAsyncWriteComplete, pipe);
            }
            catch (Exception)
            {
                pipe.Close();
            }
        }
Пример #28
0
        private void Begin(bool init)
        {
            if (init)
            {
                if (_buffer != null && !_processingHeader)
                {
                    string message = Encoding.Unicode.GetString(_buffer);
                    Log("Received message: [" + message + "] (" + _buffer.Length + " bytes).");

                    if (RaiseReceivedMessage != null)
                    {
                        RaiseReceivedMessage.Invoke(this, message);
                    }
                }
                _processingHeader = !_processingHeader;
                int len = _processingHeader ? 4 : BitConverter.ToInt32(_buffer, 0);
                if (len < 0)
                {
                    Log("Got invalid length, synchro lost. Aborting!");
                    _pipeStream.Close();
                    return;
                }
                _buffer = new byte[len];
                _cursor = 0;
                if (!_processingHeader && _buffer.Length == 0)
                {
                    // we have NO DATA to read, notify of empty message and wait to read again.
                    Log("Empty message.");
                    Begin(true);
                    return;
                }
            }

            try
            {
                int bufferLength = _buffer.Length - _cursor;
                _pipeStream.BeginRead(_buffer, _cursor, bufferLength, WhenReceived, null);
                Log("Waiting to read (" + bufferLength + " bytes).");
            }
            catch (ObjectDisposedException)
            {
                Log("Nothing to read, connection closed.");
            }
            catch (IOException e)
            {
                Log("Begin Read error:" + e);
            }
        }
Пример #29
0
        //------------------------------------------------------------------------------------------------------------------------
        public void Stop()
        {
            lock (this)
            {
                if (!IsActive)
                {
                    return;
                }

                //close stream
                try { Pipe.Close(); } catch { }

                //mark stopped
                IsActive = false;
            }
        }
Пример #30
0
        public void Close()
        {
            running = false;

            try
            {
                pipeClient.Close();
            }
            catch (Exception) { }

            try
            {
                outGoingServerPipe.Close();
            }
            catch (Exception) { }

            try
            {
                inComingServerPipe.Close();
            }
            catch (Exception) { }

            try
            {
                clientOut.Close();
            }
            catch (Exception) { }

            try
            {
                clientIn.Close();
            }
            catch (Exception) { }

            try
            {
                ssw.Close();
            }
            catch (Exception) { }

            try
            {
                csw.Close();
            }
            catch (Exception) { }
        }