Ejemplo n.º 1
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 NamedPipeClientStream 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));
        }
Ejemplo n.º 2
0
 public void SendMessage(string message)
 {
     if (_pipe.IsConnected)
     {
         Connected = true;
         var msg = new PipeMessage(message);
         _pipe.BeginWrite(msg.MessageBytes, 0, PipeMessage.MessageBufferSize, AsyncBeginWriteCallback, _pipe);
     }
     else
     {
         Connected = false;
         throw new Exception("Not connected to pipe.");
     }
 }
 public void Send(string SendStr, string PipeName, int TimeOut = 1000)
 {
     try
     {
         NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);
         pipeStream.Connect(TimeOut);
         byte[] _buffer = Encoding.UTF8.GetBytes(SendStr);
         pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
     }
     catch (TimeoutException oEX)
     {
         Debug.WriteLine("[Manage] Intercomms Client Send Error, " + oEX.Message + ", " + SendStr + ", " + PipeName);
     }
 }
Ejemplo n.º 4
0
 public void Send(string value, string pipeName, int timeout = 1000)
 {
     try
     {
         NamedPipeClientStream pipestream = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, PipeOptions.Asynchronous);
         // The connect function will indefinitely wait for the pipe to become available
         // If that is not acceptable specify a maximum waiting time (in ms)
         pipestream.Connect(timeout);
         byte[] _buffer = Encoding.UTF8.GetBytes(value);
         pipestream.BeginWrite(_buffer, 0, _buffer.Length, new AsyncCallback(AsyncSend), pipestream);
     }
     catch
     {
     }
 }
Ejemplo n.º 5
0
        public bool SendCommand(string cmd, string value)
        {
            if ((pipeClient == null) || (pipeClient.IsConnected == false))
            {
                return(false);
            }
            string data = "{\"cmd\":\"" + cmd + "\",\"value\":\"" + value.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"}";

            byte[] buf2 = UTF8Encoding.UTF8.GetBytes(data);
            byte[] buf1 = BitConverter.GetBytes(buf2.Length + 4);
            byte[] buf  = new byte[4 + buf2.Length];
            Array.Copy(buf1, 0, buf, 0, 4);
            Array.Copy(buf2, 0, buf, 4, buf2.Length);
            if (pipeWritePending == true)
            {
                pipeWrites.Add(buf);
            }
            else
            {
                pipeWritePending = true;
                pipeClient.BeginWrite(buf, 0, buf.Length, new AsyncCallback(WritePipe), null);
            }
            return(true);
        }
 public void Write_to_Server_Async(string message)
 {
     if (clientStream != null)
     {
         if (clientStream.CanWrite && clientStream.IsConnected)
         {
             clientStream.WaitForPipeDrain();
             ASCIIEncoding.ASCII.GetBytes(message).CopyTo(write_buffer, 0);
             clientStream.BeginWrite(write_buffer, 0, write_buffer.Length, new AsyncCallback(Async_Write_Completed), 1);
         }
         else
         {
             close_pipe();
         }
     }
 }
Ejemplo n.º 7
0
 public void Send(byte[] SendStr, string PipeName, int count, int TimeOut = 1000)
 {
     try
     {
         NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);
         // The connect function will indefinitely wait for the pipe to become available
         // If that is not acceptable specify a maximum waiting time (in ms)
         pipeStream.Connect(TimeOut);
         Debug.WriteLine("[Client] Pipe connection established");
         pipeStream.BeginWrite(SendStr, 0, count, AsyncSend, pipeStream);
     }
     catch (TimeoutException oEX)
     {
         Debug.WriteLine(oEX.Message);
     }
 }
        public void Write(byte[] bufferIn, bool waitSync)
        {
            try {
                NamedPipeClientStream pipeClientLocal = new NamedPipeClientStream(
                    ".",
                    SimplePipePeer.FormatPipeName(this.pipeName, !this.client),
                    PipeDirection.InOut,
                    PipeOptions.Asynchronous
                    );

                // The connect function will indefinitely wait for the pipe to become available.\
                // TODO: Is this thread-safe? I don't think it is.
                int localTimeoutCountdown = 0;
                do
                {
                    try {
                        pipeClientLocal.Connect(1000);
                        this.connected = true;
                    } catch (IOException ex) {
                        localTimeoutCountdown++;
                        if (0 <= this.ConnectionTimeout && this.ConnectionTimeout <= localTimeoutCountdown)
                        {
                            throw new TimeoutException(ex.Message);
                        }
                    }
                } while(!this.connected);

                if (waitSync)
                {
                    pipeClientLocal.Write(bufferIn, 0, bufferIn.Length);
                    pipeClientLocal.Flush();
                    pipeClientLocal.Close();
                    pipeClientLocal.Dispose();
                }
                else
                {
                    // Just kick off the process and finish it below.
                    pipeClientLocal.BeginWrite(bufferIn, 0, bufferIn.Length, new AsyncCallback(this.OnWrite), pipeClientLocal);
                }
            } catch (TimeoutException ex) {
                // TODO: Does this still execute finally{} below?
                throw ex;
            } finally {
                this.connected = false;
            }
        }
Ejemplo n.º 9
0
        public void SendMessage(string MessageText, string PipeName, int TimeOut = 1000)
        {
            try
            {
                NamedPipeClientStream clientPipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);

                clientPipeStream.Connect(TimeOut);
                Debug.WriteLine("[ClippyPipeClient] Connection established");

                byte[] messageBuffer = Encoding.UTF8.GetBytes(MessageText);
                clientPipeStream.BeginWrite(messageBuffer, 0, messageBuffer.Length, AsyncSend, clientPipeStream);
            }
            catch (TimeoutException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 10
0
        //private bool _starting = false;


        public static string Send(string message, AsyncCallback PipeWritedCallback = null, string PipeServerName = ".", string pipeName = "ALGZ_PIPE")
        {
            //if (_starting)
            //{
            //    return;
            //}

            //var path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, PipeServerName);

            //var startInfo = new ProcessStartInfo(path)
            //{
            //    UseShellExecute = false,
            //    CreateNoWindow = true
            //};

            try
            {
                //var process = Process.Start(startInfo);

                pipeClient = new NamedPipeClientStream
                             (
                    PipeServerName,
                    pipeName,
                    PipeDirection.InOut,
                    PipeOptions.Asynchronous | PipeOptions.WriteThrough
                             );

                pipeClient.Connect(500);

                //pipeClient.ReadMode = PipeTransmissionMode.Message;

                //message = "Connected!";

                byte[] data = Encoding.UTF8.GetBytes(message);

                pipeClient.BeginWrite(data, 0, data.Length, PipeWritedCallback == null?PipeWriteCallback: PipeWritedCallback, pipeClient);

                //_starting = true;
                return("");
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Ejemplo n.º 11
0
        public void SendToPipe(string SendStr, string PipeName, int TimeOut = 1000)
        {
            try
            {
                NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);

                // The connect function will indefinitely wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                pipeStream.Connect(TimeOut);

                byte[] _buffer = Encoding.UTF8.GetBytes(SendStr);
                pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
            }
            catch (TimeoutException oEX)
            {
                throw oEX;
            }
        }
Ejemplo n.º 12
0
        public void Send(object sentObj, string PipeName, int TimeOut = 1000)
        {
            try
            {
                var pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);

                // The connect function will indefinitely wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                pipeStream.Connect(TimeOut);
                Debug.WriteLine("[Client] Pipe connection established");

                byte[] _buffer = ObjectToByteArray(sentObj);
                pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
            }
            catch (TimeoutException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 13
0
        public void Send(byte[] data, string pipeName, int TimeOut = 1000)
        {
            try
            {
                NamedPipeClientStream pipeStream = new NamedPipeClientStream
                                                       (".", pipeName, PipeDirection.Out, PipeOptions.Asynchronous);


                pipeStream.Connect(TimeOut);



                pipeStream.BeginWrite
                    (data, 0, data.Length, new AsyncCallback(AsyncSend), pipeStream);
            }
            catch (TimeoutException timeoutException)
            {
            }
        }
Ejemplo n.º 14
0
        bool Yaz(NamedPipeClientStream Akış, int ZamanAşımı_msn, byte[] Girdi, int Adet)
        {
            try
            {
                int          Tik   = Environment.TickCount + ZamanAşımı_msn;
                IAsyncResult Döngü = Akış.BeginWrite(Girdi, 0, Adet, null, null);

                while (Environment.TickCount < Tik && !Döngü.IsCompleted)
                {
                    Thread.Sleep(2);
                }
                if (Döngü.IsCompleted)
                {
                    Akış.EndWrite(Döngü);
                    return(true);
                }
            }
            catch (Exception) { }
            return(false);
        }
Ejemplo n.º 15
0
        private void sendIpcRequest(IpcOperationType type, object data, Action <object> callback)
        {
            try
            {
                NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", ipcPipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

                // The connect function will indefinitely wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                pipeStream.Connect(1000);

                string msg = IpcMessageStore.savePayload(type, data);

                byte[] _buffer = Encoding.UTF8.GetBytes(msg);
                pipeStream.BeginWrite(_buffer, 0, _buffer.Length, ipcAsyncCallback, new Tuple <NamedPipeClientStream, Action <object> >(pipeStream, callback));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Arup Issue Tracker could not finish the operation due to the following error:\n" + ex.ToString());
            }
        }
Ejemplo n.º 16
0
        private void OnConnect(object sender, RoutedEventArgs e)
        {
            if (_starting)
            {
                return;
            }



            try
            {
                _pipe = new NamedPipeClientStream
                        (
                    ".",
                    PipeName,
                    PipeDirection.InOut,
                    PipeOptions.Asynchronous | PipeOptions.WriteThrough
                        );

                _pipe.Connect();

                _pipe.ReadMode = PipeTransmissionMode.Message;

                string message = "Connected!";

                byte[] data = encoding.GetBytes(message);

                _pipe.BeginWrite(data, 0, data.Length, PipeWriteCallback, _pipe);
                Console.WriteLine("beginwrite 完了 client");
                message = "Again!";

                data = encoding.GetBytes(message);
                //_pipe.BeginWrite(data, 0, data.Length, PipeWriteCallback, _pipe);
                //Console.WriteLine("又beginwrite 完了 client");
                _starting = true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.StackTrace);
            }
        }
Ejemplo n.º 17
0
        public void Send(string SendStr, string PipeName, int TimeOut = 1000)
        {
            try
            {
                NamedPipeClientStream pipeStream = new NamedPipeClientStream
                                                       (".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);

                // The connect function will indefinitely wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                pipeStream.Connect(TimeOut);
                Debug.WriteLine("[Client] Pipe connection established");

                byte[] _buffer = Encoding.UTF8.GetBytes(SendStr);
                pipeStream.BeginWrite
                    (_buffer, 0, _buffer.Length, new AsyncCallback(AsyncSend), pipeStream);
            }
            catch (TimeoutException oEX)
            {
                Debug.WriteLine("PipeClient.Send:" + oEX.Message);
            }
        }
Ejemplo n.º 18
0
 private void initWriteStringAsync()
 {
     WriteCmd = App.Store.asyncAction <PipeActions.WriteCmd, bool>(
         async(dispatch, getState, instance) => {
         dispatch(instance);
         return(await Task.Run(() => {
             try {
                 NamedPipeClientStream pipeStream = new NamedPipeClientStream(instance.PipeServerName, instance.PipeName, PipeDirection.Out, PipeOptions.Asynchronous);
                 pipeStream.Connect(3000);
                 var str = JsonConvert.SerializeObject(instance.Cmd);
                 byte[] buffer = Encoding.UTF8.GetBytes(str);
                 pipeStream.BeginWrite(buffer, 0, buffer.Length, asyncSend, pipeStream);
                 return true;
             } catch (Exception e) {
                 App.Store.Dispatch(new SimpleAction(PipeActions.WRITE_CMD_FAILED, e));
                 Logger.Error("往管道写入数据失败", e);
             }
             return false;
         }));
     });
 }
Ejemplo n.º 19
0
 private void OnConnect()
 {
     if (_starting)
     {
         return;
     }
     try
     {
         _pipe = new NamedPipeClientStream(_pipeServer, _pipeName, PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.WriteThrough);
         _pipe.Connect();
         _pipe.ReadMode = PipeTransmissionMode.Byte;
         string message = "Connected!";
         byte[] data    = encoding.GetBytes(message);
         _pipe.BeginWrite(data, 0, data.Length, PipeWriteCallback, _pipe);
         _starting = true;
     }
     catch (IOException ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Ejemplo n.º 20
0
        private void send(string SendStr, int TimeOut = 1000)
        {
            try
            {
                var pipeStream = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, PipeOptions.Asynchronous);

                // The connect function will indefinitely wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                if (!pipeStream.IsConnected)
                {
                    pipeStream.Connect(0);
                }
                Debug.WriteLine("[Client] Pipe connection established");

                byte[] _buffer = Encoding.UTF8.GetBytes(SendStr);
                pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
            }
            catch (TimeoutException oEX)
            {
                Debug.WriteLine(oEX.Message);
            }
        }
Ejemplo n.º 21
0
 public override void Write(byte[] bytes)
 {
     try {
         //*
         _stream.BeginWrite(
             bytes, 0, bytes.Length,
             _writeCallback, _stream
             );
         //*/
         //_stream.Write(bytes, 0, bytes.Length);
         //_stream.Flush();
     } catch (IOException) {
         _DebugLog("Write IOException");
         Close();
     } catch (InvalidOperationException) {
         _DebugLog("Write InvalidOperationException");
         Close();
     } catch (NullReferenceException) {
         _DebugLog("Write NullReferenceException");
         Close();
     }
 }
Ejemplo n.º 22
0
        public Task <TaskResult> SendMessage(MessageReceivedEventArgs args)
        {
            var taskCompletionSource = new TaskCompletionSource <TaskResult>();

            try
            {
                if (_pipeClient.IsConnected)
                {
                    args.ReceiverId = this.ReceiverId;

                    var message = JsonConvert.SerializeObject(args);
                    var buffer  = Encoding.UTF8.GetBytes(message);
                    _pipeClient.BeginWrite(buffer, 0, buffer.Length, asyncResult =>
                    {
                        try
                        {
                            taskCompletionSource.SetResult(EndWriteCallBack(asyncResult));
                        }
                        catch (Exception ex)
                        {
                            taskCompletionSource.SetException(ex);
                        }
                    }, null);
                }
                else
                {
                    Logger.Error("Cannot send message, pipe is not connected");
                    throw new IOException("pipe is not connected");
                }
            }
            catch (Exception ex)
            {
                taskCompletionSource.SetException(ex);
            }

            return(taskCompletionSource.Task);
        }
Ejemplo n.º 23
0
 public void Send(string message)
 {
     // Асинхронная отправка данных на сервер
     byte[] output = Encoding.UTF8.GetBytes(message);
     _namedPipeClient.BeginWrite(output, 0, output.Length, WriteDone, null);
 }
Ejemplo n.º 24
0
        static void Main(string[] args)
        {
            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                //first process in memory
                if (Environment.GetCommandLineArgs().Length > 1)
                {
                    GlobalArgs.GlobalArgList.Add(Environment.GetCommandLineArgs()[1]);
                }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form_Loading());
                mutex.ReleaseMutex();
            }
            else
            {
                //process already in memory
                try
                {
                    NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", "3DDEFE04-A2CE-4B4F-AD18-BB175716BC89", PipeDirection.Out, PipeOptions.Asynchronous);

                    // wait up to 60 for the pipe to become available
                    pipeStream.Connect(60000);

                    byte[] _buffer = Encoding.UTF8.GetBytes(Environment.GetCommandLineArgs()[1]);
                    Thread.Sleep(500); //TODO: this shouldn't be needed
                    pipeStream.BeginWrite(_buffer, 0, _buffer.Length, new AsyncCallback(AsyncSend), pipeStream);
                    pipeStream.Flush();
                    pipeStream.Close();
                }
                catch (TimeoutException oEX)
                {
                    MessageBox.Show(oEX.Message);
                }

                // send Win32 message to bring the window on top
                NativeMethods.PostMessage(
                    (IntPtr)NativeMethods.HWND_BROADCAST,
                    NativeMethods.WM_SHOWME,
                    IntPtr.Zero,
                    IntPtr.Zero);
            }
            void AsyncSend(IAsyncResult iar)
            {
                try
                {
                    // Get the pipe
                    NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState;

                    // End the write
                    pipeStream.EndWrite(iar);
                    pipeStream.Flush();
                    pipeStream.Close();
                    pipeStream.Dispose();
                }
                catch (Exception oEX)
                {
                    MessageBox.Show(oEX.Message);
                }
            }
        }
Ejemplo n.º 25
0
        public void Send(byte[] data)
        {
            Connect();

            _pipe.BeginWrite(data, 0, data.Length, OnWriteFinished, null);
        }