public virtual void TestConnectFutureFailureTiming() { Int32 port = 12345; StringBuilder buf = new StringBuilder(); IoConnector connector = CreateConnector(); connector.SessionCreated += (s, e) => buf.Append("X"); connector.SessionOpened += (s, e) => buf.Append("Y"); connector.ExceptionCaught += (s, e) => buf.Append("Z"); try { IConnectFuture future = connector.Connect(CreateEndPoint(port)); future.Await(); buf.Append("1"); try { future.Session.Close(true); Assert.Fail(); } catch { // Signifies a successful test execution Assert.IsTrue(true); } Assert.AreEqual("1", buf.ToString()); } finally { connector.Dispose(); } }
public void Connect(IPEndPoint target) { Connector = new AsyncSocketConnector(); Connector.ConnectTimeoutInMillis = 10000; //Connector.FilterChain.AddLast("logging", new LoggingFilter()); Connector.Handler = this; //var ssl = new CustomSslFilter((X509Certificate)null); //ssl.SslProtocol = System.Security.Authentication.SslProtocols.Tls; //Connector.FilterChain.AddFirst("ssl", ssl); Connector.FilterChain.AddLast("protocol", new ProtocolCodecFilter(new AriesProtocol(Kernel))); var future = Connector.Connect(target, new Action <IoSession, IConnectFuture>(OnConnect)); Task.Run(() => { if (!future.Await(10000)) { SessionClosed(null); } if (future.Canceled || future.Exception != null) { SessionClosed(null); } }); }
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(); } }
public void TestUnbindDisconnectsClients() { Bind(true); IoConnector connector = NewConnector(); IoSession[] sessions = new IoSession[5]; for (int i = 0; i < sessions.Length; i++) { IConnectFuture future = connector.Connect(CreateEndPoint(port)); future.Await(); sessions[i] = future.Session; Assert.IsTrue(sessions[i].Connected); Assert.IsTrue(sessions[i].Write(IoBuffer.Allocate(1)).Await().Written); } // Wait for the server side sessions to be created. Thread.Sleep(500); ICollection <IoSession> managedSessions = acceptor.ManagedSessions.Values; Assert.AreEqual(5, managedSessions.Count); acceptor.Unbind(); // Wait for the client side sessions to close. Thread.Sleep(500); //Assert.AreEqual(0, managedSessions.Count); foreach (IoSession element in managedSessions) { Assert.IsFalse(element.Connected); } }
public void TestUnbindResume() { Bind(true); IoConnector connector = NewConnector(); IoSession session = null; IConnectFuture future = connector.Connect(CreateEndPoint(port)); future.Await(); session = future.Session; Assert.IsTrue(session.Connected); Assert.IsTrue(session.Write(IoBuffer.Allocate(1)).Await().Written); // Wait for the server side session to be created. Thread.Sleep(500); ICollection <IoSession> managedSession = acceptor.ManagedSessions.Values; Assert.AreEqual(1, managedSession.Count); acceptor.Unbind(); // Wait for the client side sessions to close. Thread.Sleep(500); //Assert.AreEqual(0, managedSession.Count); foreach (IoSession element in managedSession) { Assert.IsFalse(element.Connected); } // Rebind Bind(true); // Check again the connection future = connector.Connect(CreateEndPoint(port)); future.Await(); session = future.Session; Assert.IsTrue(session.Connected); Assert.IsTrue(session.Write(IoBuffer.Allocate(1)).Await().Written); // Wait for the server side session to be created. Thread.Sleep(500); managedSession = acceptor.ManagedSessions.Values; Assert.AreEqual(1, managedSession.Count); }
public void Connect(IPEndPoint target) { Connector = new AsyncSocketConnector(); Connector.ConnectTimeoutInMillis = 10000; Connector.Handler = this; Connector.FilterChain.AddLast("protocol", new ProtocolCodecFilter(new FSOSandboxProtocol())); Connector.Connect(target, new Action <IoSession, IConnectFuture>(OnConnect)); }
public void Connect(IPEndPoint target) { if (Connector != null) { //old connector might still be establishing connection... //we need to stop that Connector.Handler = new NullIOHandler(); //don't hmu //we can't cancel a mina.net connector, but we can sure as hell ~~avenge it~~ stop it from firing events. //if we tried to dispose it, we'd get random disposed object exceptions because mina doesn't expect you to cancel that early. Disconnect(); //if we have already established a connection, make sure it is closed. } Connector = new AsyncSocketConnector(); var connector = Connector; Connector.ConnectTimeoutInMillis = 10000; //Connector.FilterChain.AddLast("logging", new LoggingFilter()); Connector.Handler = this; //var ssl = new CustomSslFilter((X509Certificate)null); //ssl.SslProtocol = System.Security.Authentication.SslProtocols.Tls; //Connector.FilterChain.AddFirst("ssl", ssl); Connector.FilterChain.AddLast("protocol", new ProtocolCodecFilter(new AriesProtocol(Kernel))); var future = Connector.Connect(target, (IoSession session, IConnectFuture future2) => { if (future2.Canceled || future2.Exception != null) { if (connector.Handler != null) { SessionClosed(session); } } if (connector.Handler is NullIOHandler) { session.Close(true); } else { this.Session = session; } }); Task.Run(() => { if (!future.Await(10000)) { SessionClosed(null); } if (future.Canceled || future.Exception != null) { SessionClosed(null); } }); }
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(); } } }
public void Start() { connector = new AsyncDatagramConnector(); connector.FilterChain.AddLast("RTPS", new ProtocolCodecFilter(new MessageCodecFactory())); connector.ExceptionCaught += (s, e) => { Console.WriteLine(e.Exception); }; connector.MessageReceived += (s, e) => { Console.WriteLine("Session recv..."); }; connector.MessageSent += (s, e) => { Console.WriteLine("Session sent..."); }; connector.SessionCreated += (s, e) => { Console.WriteLine("Session created..."); }; connector.SessionOpened += (s, e) => { Console.WriteLine("Session opened..."); }; connector.SessionClosed += (s, e) => { Console.WriteLine("Session closed..."); }; connector.SessionIdle += (s, e) => { Console.WriteLine("Session idle..."); }; IConnectFuture connFuture = connector.Connect(new IPEndPoint(locator.SocketAddress, locator.Port)); connFuture.Await(); connFuture.Complete += (s, e) => { IConnectFuture f = (IConnectFuture)e.Future; if (f.Connected) { Console.WriteLine("...connected"); session = f.Session; } else { Console.WriteLine("Not connected...exiting"); } }; }
public void Connect(IPEndPoint target) { Connector = new AsyncSocketConnector(); Connector.ConnectTimeoutInMillis = 10000; //Connector.FilterChain.AddLast("logging", new LoggingFilter()); Connector.Handler = this; //var ssl = new CustomSslFilter((X509Certificate)null); //ssl.SslProtocol = System.Security.Authentication.SslProtocols.Tls; //Connector.FilterChain.AddFirst("ssl", ssl); Connector.FilterChain.AddLast("protocol", new ProtocolCodecFilter(new AriesProtocol(Kernel))); Connector.Connect(target, new Action <IoSession, IConnectFuture>(OnConnect)); }
private void buttonConnect_Click(object sender, EventArgs e) { String server = textBoxServer.Text; if (String.IsNullOrEmpty(server)) { return; } if (checkBoxSSL.Checked) { if (!connector.FilterChain.Contains("ssl")) { connector.FilterChain.AddFirst("ssl", new SslFilter("TempCert", null)); } } else if (connector.FilterChain.Contains("ssl")) { connector.FilterChain.Remove("ssl"); } IPEndPoint ep; String[] parts = server.Trim().Split(':'); if (parts.Length > 0) { ep = new IPEndPoint(IPAddress.Parse(parts[0]), Int32.Parse(parts[1])); } else { ep = new IPEndPoint(IPAddress.Loopback, Int32.Parse(parts[0])); } IConnectFuture future = connector.Connect(ep).Await(); if (future.Connected) { session = future.Session; session.Write("LOGIN " + textBoxUser.Text); } else { MessageBox.Show("Could not connect to " + server); } }
/// <summary> /// 상대방에 접속처리를 한다. /// </summary> private void Connect() { if ((this.connector == null) || (this.endPoint == null)) { return; } int tryCount = 0; IConnectFuture connectFuture; while (this.session == null) { try { tryCount++; connectFuture = connector.Connect(this.endPoint); connectFuture.Await(); if (connectFuture.Connected) { session = connectFuture.Session; } else { throw new Exception("상대방이 응답이 없습니다."); } } catch (Exception ex) { Console.WriteLine(ex); if (tryCount > MAX_TRY_COUNT) { break; // while() } Thread.Sleep(SEC); } } }
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(); } } }