コード例 #1
0
        public void ServerThreadMethod(object o)
        {
            var p = (ByteBufferBidirectionalStream)o;

            Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " Server Started");
            SslStream        serverStream = new SslStream(p.GetServerStream());
            String           password     = File.ReadAllText(@".\certpassword.txt");
            X509Certificate2 cert         = new X509Certificate2(@".\localcert.pfx", password);

            Console.WriteLine("Got cert");
            serverStream.AuthenticateAsServerAsync(cert).GetAwaiter().GetResult();
            Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " Done authenticating as server");

            byte[]           writeBlock = new byte[8 * 1024];
            RandomDataStream sourceData = new RandomDataStream(577 * 1024);
            int readSize;
            int i = 0;

            while ((readSize = sourceData.Read(writeBlock, 0, writeBlock.Length)) != 0)
            {
                serverStream.Write(writeBlock, 0, writeBlock.Length);
                Console.WriteLine("Pass " + i + " write done, size=" + readSize);
                i++;
                Interlocked.Add(ref _bytesInFlight, writeBlock.Length);
            }
            serverStream.Dispose();
            Console.WriteLine("All writes done.");
        }
コード例 #2
0
        public void ServerThreadMethod(object o)
        {
            var              p            = (ByteBufferBidirectionalStream)o;
            SslStream        serverStream = new SslStream(p.GetServerStream());
            String           password     = File.ReadAllText(@".\certpassword.txt");
            X509Certificate2 cert         = new X509Certificate2(@".\localcert.pfx", password);

            serverStream.AuthenticateAsServerAsync(cert).GetAwaiter().GetResult();

            byte[]           writeBlock = new byte[8 * 1024];
            RandomDataStream sourceData = new RandomDataStream(577 * 1024);
            int readSize;

            while ((readSize = sourceData.Read(writeBlock, 0, writeBlock.Length)) != 0)
            {
                serverStream.Write(writeBlock, 0, readSize);
                Interlocked.Add(ref _bytesInFlight, readSize);
            }
            serverStream.Dispose();
        }
コード例 #3
0
        private static void Run(object state)
        {
            var st = state as State;

            if (st.IsLocal)
            {
                using (var ostream = new StreamWriter(new FileStream(st.Path, FileMode.Create, FileAccess.Write), Encoding.UTF8))
                {
                    if (st.FileLength > 0)
                    {
                        long lengthToRead = st.FileLength;
                        using (Stream rndStream = new RandomDataStream(st.FileLength))
                        {
                            byte[] readBytes = new byte[BuffSize];
                            while (lengthToRead > 0)
                            {
                                int bytesRead = rndStream.Read(readBytes, 0, (int)Math.Min(BuffSize, lengthToRead));

                                // Break when the end of the file is reached.
                                if (bytesRead > 0)
                                {
                                    if (st.WriteInNewLines)
                                    {
                                        ostream.WriteLine(Encoding.UTF8.GetString(readBytes, 0, bytesRead));
                                    }
                                    else
                                    {
                                        ostream.Write(Encoding.UTF8.GetString(readBytes, 0, bytesRead));
                                    }
                                }
                                else
                                {
                                    break;
                                }
                                lengthToRead -= bytesRead;
                            }
                        }
                    }
                }
            }
            else
            {
                using (var ostream = st.Client.CreateFile(st.Path, IfExists.Overwrite))
                {
                    if (st.FileLength > 0)
                    {
                        long lengthToRead = st.FileLength;
                        using (Stream rndStream = new RandomDataStream(st.FileLength))
                        {
                            byte[] readBytes = new byte[BuffSize];
                            while (lengthToRead > 0)
                            {
                                int bytesRead = rndStream.Read(readBytes, 0, (int)Math.Min(BuffSize, lengthToRead));

                                // Break when the end of the file is reached.
                                if (bytesRead > 0)
                                {
                                    ostream.Write(readBytes, 0, bytesRead);
                                }
                                else
                                {
                                    break;
                                }

                                lengthToRead -= bytesRead;
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(st.NonRandomText))
                    {
                        var readBytes = Encoding.UTF8.GetBytes(st.NonRandomText);
                        ostream.Write(readBytes, 0, readBytes.Length);
                    }
                }
            }
        }