コード例 #1
0
            /// <summary>
            /// Sends the specified packet to this node.
            /// </summary>
            /// <param name="packet">The packet to send.</param>
            public void SendData(INodePacket packet)
            {
                MemoryStream          writeStream     = new MemoryStream();
                INodePacketTranslator writeTranslator = NodePacketTranslator.GetWriteTranslator(writeStream);

                try
                {
                    writeStream.WriteByte((byte)packet.Type);

                    // Pad for the packet length
                    writeStream.Write(BitConverter.GetBytes((int)0), 0, 4);
                    packet.Translate(writeTranslator);

                    // Now plug in the real packet length
                    writeStream.Position = 1;
                    writeStream.Write(BitConverter.GetBytes((int)writeStream.Length - 5), 0, 4);

#if FALSE
                    if (trace) // Avoid method call
                    {
                        CommunicationsUtilities.Trace(nodeId, "Sending Packet of type {0} with length {1}", packet.Type.ToString(), writeStream.Length - 5);
                    }
#endif

                    for (int i = 0; i < writeStream.Length; i += MaxPacketWriteSize)
                    {
                        int lengthToWrite = Math.Min((int)writeStream.Length - i, MaxPacketWriteSize);
                        if ((int)writeStream.Length - i <= MaxPacketWriteSize)
                        {
                            // We are done, write the last bit asynchronously.  This is actually the general case for
                            // most packets in the build, and the asynchronous behavior here is desirable.
                            _nodePipe.BeginWrite(writeStream.GetBuffer(), i, lengthToWrite, PacketWriteComplete, null);
                            return;
                        }
                        else
                        {
                            // If this packet is longer that we can write in one go, then we need to break it up.  We can't
                            // return out of this function and let the rest of the system continue because another operation
                            // might want to send data immediately afterward, and that could result in overlapping writes
                            // to the pipe on different threads.
                            IAsyncResult result = _nodePipe.BeginWrite(writeStream.GetBuffer(), i, lengthToWrite, null, null);
                            _nodePipe.EndWrite(result);
                        }
                    }
                }
                catch (IOException e)
                {
                    // Do nothing here because any exception will be caught by the async read handler
                    CommunicationsUtilities.Trace(_nodeId, "EXCEPTION in SendData: {0}", e);
                }
                catch (ObjectDisposedException) // This happens if a child dies unexpectedly
                {
                    // Do nothing here because any exception will be caught by the async read handler
                }
            }
コード例 #2
0
ファイル: Program.cs プロジェクト: geniussheep/MyTest
 private void _writeDone(IAsyncResult result)
 {
     //响应已发给了客户端,关闭我们的这段连接
     m_pipe.EndWrite(result);
     byte[] data = new byte[1000];
     m_pipe.BeginRead(data, 0, data.Length, _gotResponse, data);
 }
コード例 #3
0
    private static IEnumerator <Int32> PipeClientAsyncEnumerator(AsyncEnumerator ae, String serverName, String message)
    {
        // Each client object performs asynchronous operations on this pipe
        using (var pipe = new NamedPipeClientStream(serverName, "Echo",
                                                    PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.WriteThrough)) {
            pipe.Connect(); // Must Connect before setting ReadMode
            pipe.ReadMode = PipeTransmissionMode.Message;

            // Asynchronously send data to the server
            Byte[] output = Encoding.UTF8.GetBytes(message);
            pipe.BeginWrite(output, 0, output.Length, ae.End(), null);
            yield return(1);

            // The data was sent to the server
            pipe.EndWrite(ae.DequeueAsyncResult());

            // Asynchronously read the server's response
            Byte[] data = new Byte[1000];
            pipe.BeginRead(data, 0, data.Length, ae.End(), data);
            yield return(1);

            // The server responded, display the response and close out connection
            Int32 bytesRead = pipe.EndRead(ae.DequeueAsyncResult());

            Console.WriteLine("Server response: " + Encoding.UTF8.GetString(data, 0, bytesRead));
        } // Close();
    }
コード例 #4
0
    private void Async_Write_Completed(IAsyncResult result)
    {
        clientStream.EndWrite(result);
        clientStream.Flush();

        Log("Written To Server => " + ASCIIEncoding.ASCII.GetString(write_buffer));
    }
コード例 #5
0
        private void WriteDone(IAsyncResult ar)
        {
            m_pipe.EndWrite(ar);

            var data = new byte[1000];

            m_pipe.BeginRead(data, 0, data.Length, GotResponse, data);
        }
コード例 #6
0
        private void WriteDone(IAsyncResult result)
        {
            // The data was sent to the server
            m_pipe.EndWrite(result);

            // Asynchronously read the server's response
            Byte[] data = new Byte[1000];
            m_pipe.BeginRead(data, 0, data.Length, GotResponse, data);
        }
コード例 #7
0
        //
        // End pipe write
        //
        private TaskResult EndWriteCallBack(IAsyncResult asyncResult)
        {
            pipeStream.EndWrite(asyncResult);
            pipeStream.Flush();

            return(new TaskResult {
                IsSuccess = true
            });
        }
コード例 #8
0
ファイル: PipeClient.cs プロジェクト: phonia/CodeStudyTwo
        void WriteDone(IAsyncResult result)
        {
            //数据已经发送给了服务器
            m_pipe.EndWrite(result);

            //异步的读取服务器的响应
            byte[] data = new Byte[1000];
            m_pipe.BeginRead(data, 0, data.Length, GotResponse, data);
        }
コード例 #9
0
 /// <summary>
 /// End the sending of a message asynchronously and flush the stream.
 /// </summary>
 /// <param name="iAsyncResult"></param>
 private void _endSendMessage(IAsyncResult iAsyncResult)
 {
     //Get the connection lock
     lock (_connectionLock)
     {
         _pipeClientStream.EndWrite(iAsyncResult);
         _pipeClientStream.Flush();
     }
 }
コード例 #10
0
ファイル: Client.cs プロジェクト: algz/ALGZClassLibrary
        private static void PipeWriteCallback(IAsyncResult ar)
        {
            //var pipe = (NamedPipeClientStream)ar.AsyncState;

            pipeClient.EndWrite(ar);
            pipeClient.Flush();
            pipeClient.WaitForPipeDrain();
            pipeClient.Close();
        }
コード例 #11
0
        /********************************************************************************************************/
        // PRIVATE METHODS SECTION
        /********************************************************************************************************/
        #region -- private methods --

        private TaskResult EndWriteCallBack(IAsyncResult asyncResult)
        {
            _pipeClient.EndWrite(asyncResult);
            _pipeClient.Flush();

            return(new TaskResult {
                IsSuccess = true
            });
        }
コード例 #12
0
 private void ClientWriteCallback(IAsyncResult ar)
 {
     //TODO
     try
     {
         clientWrite.EndWrite(ar);
     }
     catch (Exception) { }
 }
コード例 #13
0
        private void WriteDone(IAsyncResult result)
        {
            // Данные отправлены на сервер
            _namedPipeClient.EndWrite(result);

            // Асинхронное чтение ответа сервера
            var data = new byte[DefaultByteBufferLength];

            _namedPipeClient.BeginRead(data, 0, data.Length, GotResponse, data);
        }
コード例 #14
0
    private void WritePixelsAsyncCallback(IAsyncResult result)
    {
        if (RenderingStream != null && RenderingStream.IsConnected)
        {
            RenderingStream.EndWrite(result);

            RenderingStream.Flush();

            IsWritingPixels = false;
        }
    }
コード例 #15
0
 protected void _OnWrite(IAsyncResult result)
 {
     //NetworkStream stream = (NetworkStream)result.AsyncState;
     try {
         _stream.EndWrite(result);
     } catch (IOException) {
         _DebugLog("_OnWrite IOException");
     } catch (NullReferenceException) {
         _DebugLog("_OnWrite NullReferenceException");
     }
 }
コード例 #16
0
 private void WritePipe(IAsyncResult r)
 {
     pipeClient.EndWrite(r);
     if (pipeWrites.Count > 0)
     {
         byte[] buf = (byte[])pipeWrites[0];
         pipeWrites.RemoveAt(0);
         pipeClient.BeginWrite(buf, 0, buf.Length, new AsyncCallback(WritePipe), null);
     }
     else
     {
         pipeWritePending = false;
     }
 }
コード例 #17
0
 private void AsyncSend(IAsyncResult iar)
 {
     try
     {
         NamedPipeClientStream namedPipeClientStream = (NamedPipeClientStream)iar.AsyncState;
         namedPipeClientStream.EndWrite(iar);
         namedPipeClientStream.Flush();
         namedPipeClientStream.Close();
         namedPipeClientStream.Dispose();
     }
     catch (Exception)
     {
     }
 }
コード例 #18
0
ファイル: PipeClient.cs プロジェクト: zanzo420/sb-debugger
    private static void SendBytes(byte[] bytes)
    {
        if (_clientStream != null)
        {
            byte[] bytesSize   = BitConverter.GetBytes(bytes.Length);
            byte[] bytesToSend = new byte[bytes.Length + sizeof(int)];

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

            _clientStream.BeginWrite(bytesToSend, 0, bytesToSend.Length,
                                     delegate(IAsyncResult ar) { _clientStream.EndWrite(ar); }, null);
        }
    }
コード例 #19
0
 private void AsyncSend(IAsyncResult iar)
 {
     try
     {
         NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState; // Get the pipe
         pipeStream.EndWrite(iar);                                                 // End the write
         pipeStream.Flush();
         pipeStream.Close();
         pipeStream.Dispose();
     }
     catch (Exception oEX)
     {
         ServiceLog.WriteLog("[Service] Intercomms Client AsyncSend Error," + oEX.Message);
     }
 }
コード例 #20
0
 private void AsyncSend(IAsyncResult asyncResult)
 {
     try
     {
         NamedPipeClientStream pipeStream = (NamedPipeClientStream)asyncResult.AsyncState;
         pipeStream.EndWrite(asyncResult);
         pipeStream.Flush();
         pipeStream.Close();
         pipeStream.Dispose();
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
     }
 }
コード例 #21
0
        protected void OnWrite(IAsyncResult iarIn)
        {
            try {
                // Get the pipe.
                NamedPipeClientStream pipeClientLocal = (NamedPipeClientStream)iarIn.AsyncState;

                // End the write.
                pipeClientLocal.EndWrite(iarIn);
                pipeClientLocal.Flush();
                pipeClientLocal.Close();
                pipeClientLocal.Dispose();
            } catch (Exception ex) {
                Debug.WriteLine(ex.Message);
            }
        }
コード例 #22
0
        private void AsyncSend(IAsyncResult iar)
        {
            try
            {
                NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState;

                pipeStream.EndWrite(iar);
                pipeStream.Flush();
                pipeStream.Close();
                pipeStream.Dispose();
            }
            catch (Exception oEX)
            {
                Debug.WriteLine(oEX.Message);
            }
        }
コード例 #23
0
 public static void AsynSendCallBack(IAsyncResult iar)
 {
     try
     {
         Console.WriteLine("[Client] Non sono riuscito a connettermi...");
         NamedPipeClientStream pipeClient = (NamedPipeClientStream)iar.AsyncState;
         pipeClient.EndWrite(iar);
         pipeClient.Flush();
         pipeClient.Close();
         pipeClient.Dispose();
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
 }
コード例 #24
0
        private void AsyncSend(IAsyncResult iAsyncResult)
        {
            try
            {
                NamedPipeClientStream pipeStream = (NamedPipeClientStream)iAsyncResult.AsyncState;


                pipeStream.EndWrite(iAsyncResult);
                pipeStream.Flush();
                pipeStream.Close();
                pipeStream.Dispose();
            }
            catch (Exception exception)
            {
            }
        }
コード例 #25
0
ファイル: PipeIPC.cs プロジェクト: dragokas/Excelerator
        private void AsyncSend(IAsyncResult iar)
        {
            try
            {
                NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState;

                pipeStream.EndWrite(iar);
                pipeStream.Flush();
                pipeStream.Close();
                pipeStream.Dispose();
            }
            catch (Exception except)
            {
                throw new SystemException(except.Message);
            }
        }
コード例 #26
0
        private 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
            {
            }
        }
コード例 #27
0
ファイル: PipeClient.cs プロジェクト: yhware/antidote-client
        private 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)
            {
                Console.WriteLine(oEX.Message);
            }
        }
コード例 #28
0
ファイル: PipeClient.cs プロジェクト: gouming/BetterExplorer
        private 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();
                //PipeFinished.Invoke(this, new EventArgs());
            }
            catch (Exception oEX)
            {
                Debug.WriteLine(oEX.Message);
            }
        }
コード例 #29
0
        private void AsyncSend(IAsyncResult iar)
        {
            try
            {
                // Get the pipe
                NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState;

                // End the write
                pipeStream.EndWrite(iar);
                Debug.WriteLine("[Client] Pipe successfully send data.");
                pipeStream.Flush();
                pipeStream.Close();
                pipeStream.Dispose();
            }
            catch (Exception oEX)
            {
                Debug.WriteLine(oEX.Message);
            }
        }
コード例 #30
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);
        }