示例#1
0
        public void TestBindWithoutSsl()
        {
            ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); });

            ServerSockNetChannel server = SockNetServer.Create(GetLocalIpAddress(), 0, ServerSockNetChannel.DefaultBacklog, pool);

            try
            {
                server.Bind();

                server.Pipe.AddIncomingFirst <ChunkedBuffer>((ISockNetChannel channel, ref ChunkedBuffer data) =>
                {
                    channel.Send(data);
                });

                BlockingCollection <string> incomingData = new BlockingCollection <string>();

                ClientSockNetChannel client = SockNetClient.Create(GetLocalIpAddress(), server.LocalEndpoint.Port, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool);
                Assert.IsNotNull(client.Connect().WaitForValue(TimeSpan.FromSeconds(5)));

                client.Pipe.AddIncomingFirst((ISockNetChannel channel, ref ChunkedBuffer data) =>
                {
                    StreamReader reader = new StreamReader(data.Stream);

                    incomingData.Add(reader.ReadToEnd());
                });

                client.Send(Encoding.UTF8.GetBytes("a test!"));

                string incomingValue = null;

                Assert.IsTrue(incomingData.TryTake(out incomingValue, TimeSpan.FromSeconds(5)));
                Assert.AreEqual("a test!", incomingValue);
            }
            finally
            {
                server.Close();
            }
        }
        /// <summary>
        /// Creates a SockNet remote client with an endpoint and a receive buffer pool.
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="bufferPool"></param>
        internal RemoteSockNetChannel(ServerSockNetChannel parent, Socket socket, ObjectPool <byte[]> bufferPool, ICollection <ISockNetChannelModule> modules)
            : base(socket, bufferPool)
        {
            this.parent = parent;

            this.Pipe = parent.Pipe.Clone(this, false);

            foreach (ISockNetChannelModule module in modules)
            {
                this.AddModule(module);
            }

            this.State = RemoteSockNetChannelState.Disconnected;

            if (parent.IsSsl)
            {
                AttachAsSslServer(parent.CertificateValidationCallback, parent.ServerCertificate).OnFulfilled = OnAttached;
            }
            else
            {
                Attach().OnFulfilled = OnAttached;
            }
        }