Ejemplo n.º 1
0
        public void TestConnectFutureSuccessTiming()
        {
            Int32      port     = 12345;
            IoAcceptor acceptor = CreateAcceptor();

            acceptor.Bind(CreateEndPoint(port));

            StringBuilder buf = new StringBuilder();

            try
            {
                IoConnector connector = CreateConnector();
                connector.SessionCreated  += (s, e) => buf.Append("1");
                connector.SessionOpened   += (s, e) => buf.Append("2");
                connector.ExceptionCaught += (s, e) => buf.Append("X");

                IConnectFuture future = connector.Connect(CreateEndPoint(port));
                future.Await();
                buf.Append("3");
                future.Session.Close(true);
                // sessionCreated() will fire before the connect future completes
                // but sessionOpened() may not
                Assert.IsTrue(new Regex("12?32?").IsMatch(buf.ToString()));
            }
            finally
            {
                acceptor.Unbind();
                acceptor.Dispose();
            }
        }
Ejemplo n.º 2
0
        public void TestSessionCallbackInvocation()
        {
            Int32 callbackInvoked       = 0;
            Int32 sessionCreatedInvoked = 1;
            Int32 sessionCreatedInvokedBeforeCallback = 2;

            Boolean[]      assertions = { false, false, false };
            CountdownEvent countdown  = new CountdownEvent(2);

            IConnectFuture[] callbackFuture = new IConnectFuture[1];

            Int32 port = 12345;

            IoAcceptor  acceptor  = CreateAcceptor();
            IoConnector connector = CreateConnector();

            try
            {
                acceptor.Bind(CreateEndPoint(port));

                connector.SessionCreated += (s, e) =>
                {
                    assertions[sessionCreatedInvoked] = true;
                    assertions[sessionCreatedInvokedBeforeCallback] = !assertions[callbackInvoked];
                    countdown.Signal();
                };

                IConnectFuture future = connector.Connect(CreateEndPoint(port), (s, f) =>
                {
                    assertions[callbackInvoked] = true;
                    callbackFuture[0]           = f;
                    countdown.Signal();
                });

                Assert.IsTrue(countdown.Wait(TimeSpan.FromSeconds(5)), "Timed out waiting for callback and IoHandler.sessionCreated to be invoked");
                Assert.IsTrue(assertions[callbackInvoked], "Callback was not invoked");
                Assert.IsTrue(assertions[sessionCreatedInvoked], "IoHandler.sessionCreated was not invoked");
                Assert.IsFalse(assertions[sessionCreatedInvokedBeforeCallback], "IoHandler.sessionCreated was invoked before session callback");
                Assert.AreSame(future, callbackFuture[0], "Callback future should have been same future as returned by connect");
            }
            finally
            {
                try
                {
                    connector.Dispose();
                }
                finally
                {
                    acceptor.Dispose();
                }
            }
        }
Ejemplo n.º 3
0
        public override void Start()
        {
            Bootstrap();

            using (var db = DAFactory.Get)
            {
                db.Hosts.CreateHost(CreateHost());
            }

            Acceptor = new AsyncSocketAcceptor();

            try {
                if (Config.Certificate != null)
                {
                    var ssl = new SslFilter(new System.Security.Cryptography.X509Certificates.X509Certificate2(Config.Certificate))
                    {
                        SslProtocol = SslProtocols.Tls
                    };
                    Acceptor.FilterChain.AddLast("ssl", ssl);
                    if (Debugger != null)
                    {
                        Acceptor.FilterChain.AddLast("packetLogger", new AriesProtocolLogger(Debugger.GetPacketLogger(), Kernel.Get <ISerializationContext>()));
                        Debugger.AddSocketServer(this);
                    }
                    Acceptor.FilterChain.AddLast("protocol", new ProtocolCodecFilter(Kernel.Get <AriesProtocol>()));
                    Acceptor.Handler = this;

                    Acceptor.Bind(IPEndPointUtils.CreateIPEndPoint(Config.Binding));
                    LOG.Info("Listening on " + Acceptor.LocalEndPoint + " with TLS");
                }

                //Bind in the plain too as a workaround until we can get Mina.NET to work nice for TLS in the AriesClient
                PlainAcceptor = new AsyncSocketAcceptor();
                if (Debugger != null)
                {
                    PlainAcceptor.FilterChain.AddLast("packetLogger", new AriesProtocolLogger(Debugger.GetPacketLogger(), Kernel.Get <ISerializationContext>()));
                }

                PlainAcceptor.FilterChain.AddLast("protocol", new ProtocolCodecFilter(Kernel.Get <AriesProtocol>()));
                PlainAcceptor.Handler = this;
                PlainAcceptor.Bind(IPEndPointUtils.CreateIPEndPoint(Config.Binding.Replace("100", "101")));
                LOG.Info("Listening on " + PlainAcceptor.LocalEndPoint + " in the plain");
            }
            catch (Exception ex)
            {
                LOG.Error("Unknown error bootstrapping server: " + ex.ToString(), ex);
            }
        }
Ejemplo n.º 4
0
        void Run()
        {
            clients = new List <ClientObject>();

            acceptor = new AsyncSocketAcceptor();

            acceptor.FilterChain.AddLast("logger", new LoggingFilter());
            // acceptor.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));
            acceptor.FilterChain.AddLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
            acceptor.MessageReceived += MessageReceived;
            acceptor.MessageSent     += MessageSent;
            acceptor.SessionIdle     += SessionIdle;
            acceptor.SessionOpened   += SessionOpened;
            acceptor.SessionClosed   += SessionClosed;
            acceptor.ExceptionCaught += ExceptionCaught;
            acceptor.Bind(endPoint);
            isStarted = true;

            Debug.Log(String.Format("Server listen on {0} {1}", endPoint.Address, endPoint.Port));
        }
Ejemplo n.º 5
0
        public void TestSendLargeFile()
        {
            Assert.AreEqual(FILE_SIZE, file.Length, "Test file not as big as specified");

            CountdownEvent countdown = new CountdownEvent(1);

            Boolean[]   success   = { false };
            Exception[] exception = { null };

            Int32       port      = 12345;
            IoAcceptor  acceptor  = CreateAcceptor();
            IoConnector connector = CreateConnector();

            try
            {
                acceptor.ExceptionCaught += (s, e) =>
                {
                    exception[0] = e.Exception;
                    e.Session.Close(true);
                };

                Int32 index = 0;
                acceptor.MessageReceived += (s, e) =>
                {
                    IoBuffer buffer = (IoBuffer)e.Message;
                    while (buffer.HasRemaining)
                    {
                        int x = buffer.GetInt32();
                        if (x != index)
                        {
                            throw new Exception(String.Format("Integer at {0} was {1} but should have been {0}", index, x));
                        }
                        index++;
                    }
                    if (index > FILE_SIZE / 4)
                    {
                        throw new Exception("Read too much data");
                    }
                    if (index == FILE_SIZE / 4)
                    {
                        success[0] = true;
                        e.Session.Close(true);
                    }
                };

                acceptor.Bind(CreateEndPoint(port));

                connector.ExceptionCaught += (s, e) =>
                {
                    exception[0] = e.Exception;
                    e.Session.Close(true);
                };
                connector.SessionClosed += (s, e) => countdown.Signal();

                IConnectFuture future = connector.Connect(CreateEndPoint(port));
                future.Await();

                IoSession session = future.Session;
                session.Write(file);

                countdown.Wait();

                if (exception[0] != null)
                {
                    throw exception[0];
                }

                Assert.IsTrue(success[0], "Did not complete file transfer successfully");
                Assert.AreEqual(1, session.WrittenMessages, "Written messages should be 1 (we wrote one file)");
                Assert.AreEqual(FILE_SIZE, session.WrittenBytes, "Written bytes should match file size");
            }
            finally
            {
                try
                {
                    connector.Dispose();
                }
                finally
                {
                    acceptor.Dispose();
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// MinaListener
        /// </summary>
        void StartMinaListener()
        {
            if (Acceptor != null)
                throw new Exception("This should not happen!");

            Acceptor = new AsyncSocketAcceptor();
            Acceptor.FilterChain.AddLast("logger", new LoggingFilter());
            Acceptor.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));

            Acceptor.ExceptionCaught += HandleException;
            Acceptor.SessionOpened += HandeleSessionOpened;
            Acceptor.SessionClosed += HandeleSessionClosed;
            Acceptor.SessionIdle += HandleIdle;
            Acceptor.MessageReceived += HandleReceived;

            Acceptor.SessionConfig.ReadBufferSize = 10*1024;
            Acceptor.SessionConfig.SetIdleTime(IdleStatus.BothIdle, 10);

            Acceptor.Bind(new IPEndPoint(IPAddress.Any, _port));
        }