private void Async_Write_Completed(IAsyncResult result)
        {
            namedPipeServerStream.EndWrite(result);
            namedPipeServerStream.Flush();

            Console.WriteLine("Written To Client => " + ASCIIEncoding.ASCII.GetString(write_buffer));
        }
Example #2
0
    private void WriteCommandAsyncCallback(IAsyncResult result)
    {
        if (CommandStream != null && CommandStream.IsConnected)
        {
            CommandStream.EndWrite(result);

            CommandStream.Flush();
        }
    }
Example #3
0
            public override void Write(byte[] buf, int off, int len)
            {
                if (stream == null)
                {
                    throw new TTransportException(TTransportException.ExceptionType.NotOpen);
                }

#if !NET_CORE
                if (asyncMode)
                {
                    Exception eOuter = null;
                    var       evt    = new ManualResetEvent(false);

                    stream.BeginWrite(buf, off, len, asyncResult =>
                    {
                        try
                        {
                            if (stream != null)
                            {
                                stream.EndWrite(asyncResult);
                            }
                            else
                            {
                                eOuter = new TTransportException(TTransportException.ExceptionType.Interrupted);
                            }
                        }
                        catch (Exception e)
                        {
                            if (stream != null)
                            {
                                eOuter = e;
                            }
                            else
                            {
                                eOuter = new TTransportException(TTransportException.ExceptionType.Interrupted, e.Message);
                            }
                        }
                        evt.Set();
                    }, null);

                    evt.WaitOne();

                    if (eOuter != null)
                    {
                        throw eOuter; // rethrow exception
                    }
                }
                else
                {
                    stream.Write(buf, off, len);
                }
#else
                stream.Write(buf, off, len);
#endif
            }
Example #4
0
    private static IEnumerator <Int32> PipeServerAsyncEnumerator(AsyncEnumerator ae)
    {
        // Each server object performs asynchronous operations on this pipe
        using (var pipe = new NamedPipeServerStream(
                   "Echo", PipeDirection.InOut, -1, PipeTransmissionMode.Message,
                   PipeOptions.Asynchronous | PipeOptions.WriteThrough)) {
            // Asynchronously accept a client connection
            pipe.BeginWaitForConnection(ae.End(), null);
            yield return(1);

            // A client connected, let's accept another client
            var aeNewClient = new AsyncEnumerator();
            aeNewClient.BeginExecute(PipeServerAsyncEnumerator(aeNewClient), aeNewClient.EndExecute);

            // Accept the client connection
            pipe.EndWaitForConnection(ae.DequeueAsyncResult());

            // Asynchronously read a request from the client
            Byte[] data = new Byte[1000];
            pipe.BeginRead(data, 0, data.Length, ae.End(), null);
            yield return(1);

            // The client sent us a request, process it.
            Int32 bytesRead = pipe.EndRead(ae.DequeueAsyncResult());

            // Get the timestamp of this client's request
            DateTime now = DateTime.Now;

            // We want to save the timestamp of the most-recent client request. Since multiple
            // clients are running concurrently, this has to be done in a thread-safe way
            s_gate.BeginRegion(SyncGateMode.Exclusive, ae.End()); // Request exclusive access
            yield return(1);                                      // The iterator resumes when exclusive access is granted

            if (s_lastClientRequestTimestamp < now)
            {
                s_lastClientRequestTimestamp = now;
            }

            s_gate.EndRegion(ae.DequeueAsyncResult()); // Relinquish exclusive access

            // My sample server just changes all the characters to uppercase
            // But, you can replace this code with any compute-bound operation
            data = Encoding.UTF8.GetBytes(
                Encoding.UTF8.GetString(data, 0, bytesRead).ToUpper().ToCharArray());

            // Asynchronously send the response back to the client
            pipe.BeginWrite(data, 0, data.Length, ae.End(), null);
            yield return(1);

            // The response was sent to the client, close our side of the connection
            pipe.EndWrite(ae.DequeueAsyncResult());
        } // Close the pipe
    }
 private void OnWrite(IAsyncResult ar)
 {
     if (_server == null)
     {
         Debug.WriteLine("ignoring late write");
         return;
     }
     Debug.WriteLine("write completed");
     _server.EndWrite(ar);
     _server.Close();
     Debug.WriteLine("signaling report sent");
     _reportSent.Set();
 }
Example #6
0
        /// <summary>
        /// End an asynchronous write operation and flush the stream written to.
        /// </summary>
        /// <param name="iAsyncResult"></param>
        private void _endWrite(IAsyncResult iAsyncResult)
        {
            AsyncPipeStateWrapper wrapper    = iAsyncResult.AsyncState as AsyncPipeStateWrapper;
            NamedPipeServerStream connection = wrapper.NamedPipeServerStream;

            //Lock on the connection
            lock (connection)
            {
                //End the write operation and flush.
                connection.EndWrite(iAsyncResult);
                connection.Flush();
            }
        }
        private static void SendBytes(byte[] bytes)
        {
            if (_client != null)
            {
                var bytesSize   = BitConverter.GetBytes(bytes.Length);
                var bytesToSend = new byte[bytes.Length + sizeof(int)];

                Buffer.BlockCopy(bytesSize, 0, bytesToSend, 0, bytesSize.Length);
                Buffer.BlockCopy(bytes, 0, bytesToSend, sizeof(int), bytes.Length);

                _client.BeginWrite(bytesToSend, 0, bytesToSend.Length,
                                   delegate(IAsyncResult ar) { _client.EndWrite(ar); }, null);
            }
        }
Example #8
0
        bool Yaz(NamedPipeServerStream 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);
        }
Example #9
0
        private void OnWriteAsync(IAsyncResult ar)
        {
            if (_pipeServer == null)
            {
                ReleaseServerReference(false);
                return;
            }

            try
            {
                _pipeServer.EndWrite(ar);
            }
            catch (Exception ex)
            {
                ReleaseServerReference(false);
                MessageBox.Show(ex.Message, "RpcServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ReleaseServerReference(false);
        }
Example #10
0
            private void callbackWrite(IAsyncResult result)
            {
                _stream.EndWrite(result);
                _stream.Flush();

                if (_pos != -1)
                {
                    send();
                }
                else if (_msgQueue.Count > 0)
                {
                    _currentMsg = _msgQueue.Dequeue();
                    _pos        = 0;

                    send();
                }
                else
                {
                    _isWriting = false;
                }
            }
Example #11
0
 private void WriteDone(IAsyncResult result)
 {
     // The response was sent to the client, close our side of the connection
     m_pipe.EndWrite(result);
     m_pipe.Close();
 }
            public override void Write(byte[] buf, int off, int len)
            {
                if (stream == null)
                {
                    throw new TTransportException(TTransportException.ExceptionType.NotOpen);
                }

                // if necessary, send the data in chunks
                // there's a system limit around 0x10000 bytes that we hit otherwise
                // MSDN: "Pipe write operations across a network are limited to 65,535 bytes per write. For more information regarding pipes, see the Remarks section."
                var nBytes = Math.Min(len, 15 * 4096);  // 16 would exceed the limit

                while (nBytes > 0)
                {
                    if (asyncMode)
                    {
                        Exception eOuter = null;
                        var       evt    = new ManualResetEvent(false);

                        stream.BeginWrite(buf, off, nBytes, asyncResult =>
                        {
                            try
                            {
                                if (stream != null)
                                {
                                    stream.EndWrite(asyncResult);
                                }
                                else
                                {
                                    eOuter = new TTransportException(TTransportException.ExceptionType.Interrupted);
                                }
                            }
                            catch (Exception e)
                            {
                                if (stream != null)
                                {
                                    eOuter = e;
                                }
                                else
                                {
                                    eOuter = new TTransportException(TTransportException.ExceptionType.Interrupted, e.Message, e);
                                }
                            }
                            evt.Set();
                        }, null);

                        evt.WaitOne();

                        if (eOuter != null)
                        {
                            throw eOuter; // rethrow exception
                        }
                    }
                    else
                    {
                        stream.Write(buf, off, nBytes);
                    }

                    off   += nBytes;
                    len   -= nBytes;
                    nBytes = Math.Min(len, nBytes);
                }
            }
 private void WriteDone(IAsyncResult result)
 {
     m_Pipe.EndWrite(result);
     //m_Pipe.Close();
 }
Example #14
0
 void WriteDone(IAsyncResult result)
 {
     //响应已发送给了客户端
     m_pipe.EndWrite(result);
     m_pipe.Close();
 }
Example #15
0
 private void WriteDone(IAsyncResult result)
 {
     // Ответ клиенту отправлен, закрываем соединение со своей стороны
     _pipeServerStream.EndWrite(result);
     _pipeServerStream.Close();
 }
Example #16
0
 private void AsyncBeginWriteCallback(IAsyncResult result)
 {
     _pipe.EndWrite(result);
 }
Example #17
0
 private void _writeDone(IAsyncResult result)
 {
     //响应已发给了客户端,关闭我们的这段连接
     m_pipe.EndWrite(result);
     m_pipe.Close();
 }
Example #18
0
 private void OnWriteFinished(IAsyncResult result)
 {
     _pipe.EndWrite(result);
 }
Example #19
0
        private void BeginWriteCallback(IAsyncResult ar)
        {
            NamedPipeServerStream server = ar.AsyncState as NamedPipeServerStream;

            server.EndWrite(ar);
        }