/// <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
                }
            }
Exemplo n.º 2
0
 private void _writeDone(IAsyncResult result)
 {
     //响应已发给了客户端,关闭我们的这段连接
     m_pipe.EndWrite(result);
     byte[] data = new byte[1000];
     m_pipe.BeginRead(data, 0, data.Length, _gotResponse, data);
 }
Exemplo n.º 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();
    }
Exemplo n.º 4
0
    private void Async_Write_Completed(IAsyncResult result)
    {
        clientStream.EndWrite(result);
        clientStream.Flush();

        Log("Written To Server => " + ASCIIEncoding.ASCII.GetString(write_buffer));
    }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 7
0
        //
        // End pipe write
        //
        private TaskResult EndWriteCallBack(IAsyncResult asyncResult)
        {
            pipeStream.EndWrite(asyncResult);
            pipeStream.Flush();

            return(new TaskResult {
                IsSuccess = true
            });
        }
Exemplo n.º 8
0
        void WriteDone(IAsyncResult result)
        {
            //数据已经发送给了服务器
            m_pipe.EndWrite(result);

            //异步的读取服务器的响应
            byte[] data = new Byte[1000];
            m_pipe.BeginRead(data, 0, data.Length, GotResponse, data);
        }
Exemplo n.º 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();
     }
 }
Exemplo n.º 10
0
        private static void PipeWriteCallback(IAsyncResult ar)
        {
            //var pipe = (NamedPipeClientStream)ar.AsyncState;

            pipeClient.EndWrite(ar);
            pipeClient.Flush();
            pipeClient.WaitForPipeDrain();
            pipeClient.Close();
        }
Exemplo n.º 11
0
        /********************************************************************************************************/
        // PRIVATE METHODS SECTION
        /********************************************************************************************************/
        #region -- private methods --

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

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

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

            _namedPipeClient.BeginRead(data, 0, data.Length, GotResponse, data);
        }
Exemplo n.º 14
0
    private void WritePixelsAsyncCallback(IAsyncResult result)
    {
        if (RenderingStream != null && RenderingStream.IsConnected)
        {
            RenderingStream.EndWrite(result);

            RenderingStream.Flush();

            IsWritingPixels = false;
        }
    }
Exemplo n.º 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");
     }
 }
Exemplo n.º 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;
     }
 }
Exemplo n.º 17
0
 private void AsyncSend(IAsyncResult iar)
 {
     try
     {
         NamedPipeClientStream namedPipeClientStream = (NamedPipeClientStream)iar.AsyncState;
         namedPipeClientStream.EndWrite(iar);
         namedPipeClientStream.Flush();
         namedPipeClientStream.Close();
         namedPipeClientStream.Dispose();
     }
     catch (Exception)
     {
     }
 }
Exemplo n.º 18
0
    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);
        }
    }
 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);
     }
 }
Exemplo n.º 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);
     }
 }
        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);
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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());
     }
 }
Exemplo n.º 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)
            {
            }
        }
Exemplo n.º 25
0
        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);
            }
        }
Exemplo n.º 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
            {
            }
        }
Exemplo n.º 27
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 (Exception oEX)
            {
                Console.WriteLine(oEX.Message);
            }
        }
Exemplo n.º 28
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();
                //PipeFinished.Invoke(this, new EventArgs());
            }
            catch (Exception oEX)
            {
                Debug.WriteLine(oEX.Message);
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
        }