Write() public méthode

public Write ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
Résultat void
    static void WriteBytes(PipeStream pipeStream, byte[] buffer)
    {
        Assert.True(pipeStream.IsConnected);
        Assert.True(buffer.Length > 0);

        pipeStream.WriteByte(buffer[0]);
        if (buffer.Length > 1)
        {
            pipeStream.Write(buffer, 1, buffer.Length - 1);
        }
    }
 public static void DoStreamOperations(PipeStream stream)
 {
     if (stream.CanWrite)
     {
         stream.Write(new byte[] { 123, 124 }, 0, 2);
     }
     if (stream.CanRead)
     {
         if (stream.ReadByte() != 123)
         {
             Console.WriteLine("First byte read != 123");
         }
         if (stream.ReadByte() != 124)
         {
             Console.WriteLine("Second byte read != 124");
         }
     }
     Console.WriteLine("*** Operations finished. ***");
 }
		public static void Write(PipeStream stream, byte[] buffer, int offset, int count, bool isAsync, TimeoutHelper timeoutHelper)
		{
			// 异步时,使用异步方式写入数据
			if (isAsync)
			{
				IAsyncResult asyncResult = stream.BeginWrite(buffer, offset, count, null, null);

				// 等待 timeoutHelper 计算后的剩余时间,如果在这段时间内没有读到数据,那么将直接返回,这时,由于没有读到数据,返回的数据长度为 0
				asyncResult.AsyncWaitHandle.WaitOne(timeoutHelper == null ? 60000 : (int)timeoutHelper.RemainingTime().TotalMilliseconds);

				if (asyncResult.GetType() == pipeStreamAsyncResultType)
				{
					pipeStreamAsyncResult_waitHandle.SetValue(asyncResult, null);
				}

				stream.EndWrite(asyncResult);

				return;
			}

			// 使用系统内置的方式进行同步阻塞式读取,该方法直到读取到数据才返回
			stream.Write(buffer, offset, count);
		}
Exemple #4
0
        /// <summary>
        /// Überträgt ein Objekt in eine Kommunikationseinheit.
        /// </summary>
        /// <param name="pipe">Die Kommunikationseinheit.</param>
        /// <param name="serializer">Die Serialisierungsinstanz.</param>
        /// <param name="instance">Das betroffene Objekt.</param>
        internal static void SendToPipe( PipeStream pipe, XmlSerializer serializer, object instance )
        {
            // Create helper
            using (var temp = new MemoryStream())
            {
                // Create writer configuration
                var settings = new XmlWriterSettings { CheckCharacters = false };

                // Create
                using (var writer = XmlWriter.Create( temp, settings ))
                    serializer.Serialize( writer, instance );

                // Read data and create buffer for length
                var len = new byte[sizeof( long )];
                var data = temp.ToArray();

                // Lock briefly
                var lenLock = GCHandle.Alloc( len, GCHandleType.Pinned );
                try
                {
                    // Fill
                    Marshal.WriteInt64( lenLock.AddrOfPinnedObject(), data.LongLength );
                }
                finally
                {
                    // Release
                    lenLock.Free();
                }

                // Send all
                pipe.Write( len, 0, len.Length );

                // Write in blocks
                for (int n = 0; n < data.Length; )
                {
                    // Block size
                    int block = Math.Min( BlockSize, data.Length - n );

                    // Send chunck
                    pipe.Write( data, n, block );
                    pipe.Flush();

                    // Advance
                    n += block;
                }

                // Flush
                pipe.Flush();
                pipe.WaitForPipeDrain();
            }
        }
 public static void DoStreamOperations(PipeStream stream)
 {
     if (stream.CanWrite)
     {
         stream.Write(new byte[] { 123, 124 }, 0, 2);
     }
     if (stream.CanRead)
     {
         Assert.Equal(123, stream.ReadByte());
         Assert.Equal(124, stream.ReadByte());
     }
 }
        static string StreamIntercept(PipeStream client, PipeStream server)
        {
            /*bytes.Clear();
            int count;
            do
            {
                count = client.Read(buffer, 0, 8192);
                AddBuffer(count);
            } while (SomethingToRead(client.SafePipeHandle));
            var array = bytes.ToArray();
            server.Write(array, 0, bytes.Count);
            return Encoding.ASCII.GetString(array);*/

            bytes.Clear();
            int integer;
            do
            {
                integer = client.ReadByte();
                if (integer >= 0)
                    bytes.Add((byte)integer);
            } while (integer >= 0 && integer != 10);
            var array = bytes.ToArray();
            server.Write(array, 0, bytes.Count);
            return Encoding.ASCII.GetString(array);
        }