/// <summary> /// Initiates a network connection to the message router at the /// specified network endpoint and then initiates the transmission /// of the message once the connection is established. /// </summary> /// <param name="ep">The remote router's endpoint.</param> /// <param name="msg">The message to be sent (or <c>null</c>).</param> public void Connect(IPEndPoint ep, Msg msg) { using (TimedLock.Lock(router.SyncRoot)) { Assertion.Test(sock == null); sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); localEP = new ChannelEP(Transport.Tcp, router.NormalizeEP(router.TcpEP)); sock.NoDelay = !router.TcpDelay; sock.SendBufferSize = router.TcpSockConfig.SendBufferSize; sock.ReceiveBufferSize = router.TcpSockConfig.ReceiveBufferSize; if (router.FragmentTcp) { sock.SendMax = 1; sock.ReceiveMax = 1; } // Queue the channel initialization message and the message passed Msg initMsg; initMsg = new TcpInitMsg(router.RouterEP, new MsgRouterInfo(router), isUplink, router.TcpEP.Port); initMsg._TTL = 1; Serialize(initMsg); Enqueue(initMsg); try { SetLastAccess(); remoteEP = new ChannelEP(Transport.Tcp, router.NormalizeEP(ep)); if (msg != null) { msg._SetToChannel(remoteEP); msg._SetFromChannel(localEP); msg._Trace(router, 2, "TCP: Queue", null); Serialize(msg); Enqueue(msg); } router.Trace(2, "TCP: Outbound", "LocalEP=" + localEP.NetEP.ToString() + " remoteEP=" + remoteEP.NetEP.ToString(), null); sock.BeginConnect(remoteEP.NetEP, new AsyncCallback(OnConnect), null); } catch (Exception e) { router.Trace(string.Format(null, "TCP: Connect Failed [{0}]", ep), e); router.OnTcpClose(this); Close(); } } }
public void Msg_Clone_TcpInitMsg() { TcpInitMsg msg; Msg.ClearTypes(); Msg.LoadTypes(Assembly.GetExecutingAssembly()); msg = new TcpInitMsg("physical://root/hub/leaf", new MsgRouterInfo(new Version(1000, 0)), true, 77); msg = (TcpInitMsg)msg.Clone(); Assert.AreEqual(new MsgEP("physical://root/hub/leaf"), msg.RouterEP); Assert.AreEqual(new Version(1000, 0), msg.RouterInfo.ProtocolVersion); Assert.IsTrue(msg.IsUplink); Assert.IsTrue(msg.RouterInfo.IsP2P); Assert.AreEqual(77, msg.ListenPort); TestBaseCloning(msg); }
public void Msg_Serialize_TcpInitMsg() { TcpInitMsg msg; EnhancedStream es = new EnhancedMemoryStream(); Msg.ClearTypes(); Msg.LoadTypes(Assembly.GetExecutingAssembly()); msg = new TcpInitMsg("physical://root/hub/leaf", new MsgRouterInfo(new Version(1000, 0)), true, 77); Msg.Save(es, msg); es.Seek(0, SeekOrigin.Begin); msg = (TcpInitMsg)Msg.Load(es); Assert.AreEqual(new MsgEP("physical://root/hub/leaf"), msg.RouterEP); Assert.AreEqual(new Version(1000, 0), msg.RouterInfo.ProtocolVersion); Assert.IsTrue(msg.IsUplink); Assert.IsTrue(msg.RouterInfo.IsP2P); Assert.AreEqual(77, msg.ListenPort); }
/// <summary> /// Associates the channel with the open socket passed and begins /// listening for messages received on the socket. /// </summary> /// <param name="sock">The open socket.</param> public void Open(EnhancedSocket sock) { using (TimedLock.Lock(router.SyncRoot)) { Assertion.Test(this.sock == null); this.sock = sock; this.connected = true; this.localEP = router.NormalizeEP(new ChannelEP(Transport.Tcp, router.TcpEP)); this.remoteEP = new ChannelEP(Transport.Tcp, new IPEndPoint(((IPEndPoint)sock.RemoteEndPoint).Address, 0)); this.sendQueue.Clear(); sock.SendBufferSize = router.TcpSockConfig.SendBufferSize; sock.ReceiveBufferSize = router.TcpSockConfig.ReceiveBufferSize; // Send the channel initialization message to the other endpoint. sock.NoDelay = !router.TcpDelay; if (router.FragmentTcp) { sock.SendMax = 1; sock.ReceiveMax = 1; } router.Trace(2, "TCP: Inbound", "LocalEP=" + localEP.NetEP.ToString() + " remoteEP=" + remoteEP.NetEP.ToString(), null); SetLastAccess(); BeginReceive(); TcpInitMsg msg; msg = new TcpInitMsg(router.RouterEP, new MsgRouterInfo(router), isUplink, localEP.NetEP.Port); msg._TTL = 1; Transmit(router.NormalizeEP(remoteEP), msg, false); } }