예제 #1
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();
            }
        }
예제 #2
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);
        }
예제 #3
0
        public void OnAsyncMessage(PipeStream pipe, byte[] data, int bytes, object state)
        {
            PushAndPullClient client = (PushAndPullClient)state;

            Console.WriteLine("Server.PingPong({0}): {1}", client.Id, Encoding.ASCII.GetString(data, 0, bytes));
            pipe.BeginWrite(data, 0, bytes, OnWrite, state);
        }
예제 #4
0
        /// <summary>
        /// Extends BeginWrite so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// pipestream.BeginWrite(buffer, offset, count, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this PipeStream pipestream, Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback)
        {
            if (pipestream == null)
            {
                throw new ArgumentNullException("pipestream");
            }

            return(pipestream.BeginWrite(buffer, offset, count, callback, null));
        }
예제 #5
0
        private void BeginWrite(byte[] data, ManualResetEventSlim manualEvent)
        {
            PipeStream.BeginWrite(data, 0, data.Length, (result) =>
            {
                PipeStream.EndWrite(result);
                PipeStream.Flush();

                manualEvent.Set();
            }, null);
        }
 public override void SendMessage(byte[] message)
 {
     lock (_InstanceLock)
     {
         if (_Stream.IsConnected)
         {
             message = message ?? new byte[0];
             _Stream.BeginWrite(message, 0, message.Length, EndSendMessage, null);
         }
     }
 }
예제 #7
0
 public override void SendMessage(MessageData message)
 {
     byte[] buffer = MessageSerializer.Encode(message);
     lock (mNamedPipeLock)
     {
         if (mStream.IsConnected)
         {
             mStream.BeginWrite(buffer, 0, buffer.Length, new AsyncCallback(EndSendMessage), null);
         }
     }
 }
예제 #8
0
        protected void SendString(PipeStream pipe, String text)
        {
            // Although the protocol says empty strings are supported and should be sent to indicate when some kind of data is unavailable,
            // sending an empty string (0;) appears to crash Airfoil; a single space (1; ) seems to have the desired effect instead.
            var finalText = String.IsNullOrEmpty(text) ? " " : text;
            // Format the message according to the protocol (http://weblog.rogueamoeba.com/2014/05/16/developer-note-integrating-with-airfoil-for-windows/).
            var message = $"{Encoding.UTF8.GetByteCount(finalText)};{finalText}";
            // Send the bytes.
            var mBytes = Encoding.UTF8.GetBytes(message);

            pipe.BeginWrite(mBytes, 0, mBytes.Length, OnAsyncWriteComplete, pipe);
        }
예제 #9
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();
        }
    }
예제 #10
0
        /// <summary>
        /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// pipestream.BeginWrite(buffer, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this PipeStream pipestream, Byte[] buffer, AsyncCallback callback)
        {
            if (pipestream == null)
            {
                throw new ArgumentNullException("pipestream");
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            return(pipestream.BeginWrite(buffer, 0, buffer.Length, callback));
        }
예제 #11
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();
            }
        }
예제 #12
0
 public void Write(byte[] buffer, int offset, int length)
 {
     if (!_conn.IsConnected)
     {
         return;
     }
     try
     {
         //doing syncronous writes (to an Asynchronous pipe) seems to be a bad thing
         _conn.BeginWrite(buffer, offset, length, (r) => { _conn.EndWrite(r); }, null);
     }
     catch (IOException)
     {
         Disconnect();
     }
 }
예제 #13
0
파일: Server.cs 프로젝트: ha11owed/RPC
        void IpcCallback.OnAsyncMessage(PipeStream pipe, byte[] data, int bytes, object state)
        {
            ServerClient client  = (ServerClient)state;
            AMessage     message = client.Reader.Process(data, 0, bytes);

            if (message != null)
            {
            }

            // Write answer to client
            try
            {
                pipe.BeginWrite(data, 0, bytes, OnAsyncWriteComplete, pipe);
            }
            catch (Exception)
            {
                pipe.Close();
            }
        }
 public void OnAsyncMessage(PipeStream pipe, Byte[] data, Int32 bytes, Object state)
 {
     if (data != null)
     {
         var raw = new byte[bytes];
         Array.Copy(data, raw, bytes);
         MessageFromClientReceived(this, new MessageEventArgs(raw));
     }
     /* reply with "OK" */
     try
     {
         var ok = System.Text.ASCIIEncoding.ASCII.GetBytes("OK");
         pipe.BeginWrite(ok, 0, ok.Length, OnAsyncWriteComplete, pipe);
     }
     catch (Exception)
     {
         pipe.Close();
     }
 }
예제 #15
0
파일: IPCBase.cs 프로젝트: egshels/Sources
        public virtual bool Send(byte[] data)
        {
            bool result = false;

            if (_pipeStream != null && _pipeStream.IsConnected)
            {
                try
                {
                    _pipeStream.BeginWrite(data, 0, data.Length, SendCallback, null);
                    result = true;
                    return(result);
                }
                catch (IOException ex)
                {
                    _pipeBrokenFlag = true;
                    WeGameHelper.WriteDebugString("Send Exception, {0}", ex.Message);
                    return(result);
                }
            }
            return(result);
        }
예제 #16
0
 public override void SendMessage(byte[] message)
 {
     if (_ConnectGate.WaitOne(100))
     {
         lock (_InstanceLock)
         {
             if (_Stream.IsConnected)
             {
                 message = message ?? new byte[0];
                 _Stream.BeginWrite(message, 0, message.Length, EndSendMessage, null);
                 _Stream.Flush();
             }
             else
             {
                 EnqueMessage(message);
                 StartTryConnect();
             }
         }
     }
     else
     {
         EnqueMessage(message);
     }
 }
예제 #17
0
 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => _underlying.BeginWrite(buffer, offset, count, callback, state);
예제 #18
0
 public IAsyncResult BeginWrite(string str)
 {
     byte[] data = Encoding.ASCII.GetBytes(str);
     return(stream.BeginWrite(data, 0, data.Length, OnWrite, this));
 }