public void UnRegistTest() { TcpHostTransportChannel hostChannel = new TcpHostTransportChannel(20002); Assert.IsTrue(hostChannel.Regist()); Assert.IsTrue(hostChannel.UnRegist()); }
/// <summary> /// 根据KAE网络资源来构建一个网络宿主信道 /// </summary> /// <param name="resource">KAE网络资源</param> /// <returns>返回构建后的宿主信道</returns> /// <exception cref="NotSupportedException">不支持的网络类型</exception> public static IHostTransportChannel BuildHostChannel(KAENetworkResource resource) { IHostTransportChannel channel; switch (resource.NetworkUri.NetworkType) { case NetworkTypes.TCP: channel = new TcpHostTransportChannel((((TcpUri)resource.NetworkUri).IsUseDynamicResource ? GetDynamicTCPPort() : ((TcpUri)resource.NetworkUri).Port)); break; default: throw new NotSupportedException("#Sadly, current network type wasn't supported yet! #Network Type: " + resource.NetworkUri.NetworkType); } return(channel); }
/// <summary> /// 根据KAE网络资源来构建一个网络宿主信道 /// </summary> /// <param name="network">网络通信类型</param> /// <param name="uri">返回的网络通信资源对象</param> /// <returns>返回构建后的宿主信道</returns> /// <exception cref="NotSupportedException">不支持的网络类型</exception> public static IHostTransportChannel BuildHostChannel(NetworkTypes network, out Uri uri) { IHostTransportChannel channel; switch (network) { case NetworkTypes.TCP: int port = GetDynamicTCPPort(); channel = new TcpHostTransportChannel(port); uri = new TcpUri(string.Format("tcp://{0}:{1}", GetCurrentMachineIP(), port)); break; default: throw new NotSupportedException("#Sadly, current network type wasn't supported yet! #Network Type: " + network); } return(channel); }
public void DisconnectTest() { TcpTransportChannel connectedChannel = null; TcpHostTransportChannel hostChannel = new TcpHostTransportChannel(20004); Assert.IsTrue(hostChannel.Regist()); hostChannel.ChannelCreated += delegate(object sender, LightSingleArgEventArgs <ITransportChannel> args) { connectedChannel = (TcpTransportChannel)args.Target; }; TcpTransportChannel transportChannel = new TcpTransportChannel("127.0.0.1", 20004); Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Unknown); transportChannel.Connect(); Assert.IsTrue(transportChannel.IsConnected); Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Opened); transportChannel.Disconnect(); Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Closed); Assert.IsTrue(hostChannel.UnRegist()); }
/// <summary> /// 初始化资源 /// </summary> /// <param name="businessPort">KAE Agent启动时需要注册的业务通信端口</param> /// <param name="systemPort">KAE Agent启动时需要注册的系统管理通信端口</param> /// <param name="localIP">当前本机IP</param> public static void Initialize(ushort businessPort, ushort systemPort, string localIP = null) { if (IsInitialized) { return; } IsInitialized = true; if (string.IsNullOrEmpty(localIP)) { localIP = NetworkHelper.GetCurrentMachineIP(); } /*Openning TCP port for business requests.*/ TcpUri uri = new TcpUri(string.Format("tcp://{0}:{1}", localIP, businessPort)); IHostTransportChannel hostChannel = new TcpHostTransportChannel(businessPort); hostChannel.ChannelCreated += ChannelCreated; if (!hostChannel.Regist()) { hostChannel.ChannelCreated -= ChannelCreated; throw new AllocResourceFailedException("#Sadly, We couldn't alloc current network resource. #Uri: " + uri); } hostChannel.Tag = new KAENetworkResource { NetworkUri = uri, Protocol = ProtocolTypes.Metadata }; _resources.Add(ProtocolTypes.Metadata, new Tuple <IHostTransportChannel, Uri>(hostChannel, uri)); _tracing.DebugInfo("\t\t#Initialized network resource.\t(P: {0},\tURL: {1})", ConsoleColor.Gray, ProtocolTypes.Metadata, uri); /*Openning TCP port for system management*/ uri = new TcpUri(string.Format("tcp://{0}:{1}", localIP, systemPort)); hostChannel = new TcpHostTransportChannel(systemPort); hostChannel.ChannelCreated += ChannelCreated; if (!hostChannel.Regist()) { hostChannel.ChannelCreated -= ChannelCreated; throw new AllocResourceFailedException("#Sadly, We couldn't alloc current network resource. #Uri: " + uri); } hostChannel.Tag = new KAENetworkResource { NetworkUri = uri, Protocol = ProtocolTypes.INTERNAL_SPECIAL_RESOURCE }; _resources.Add(ProtocolTypes.INTERNAL_SPECIAL_RESOURCE, new Tuple <IHostTransportChannel, Uri>(hostChannel, uri)); _tracing.DebugInfo("\t\t#Initialized network resource.\t(P: {0},\tURL: {1})", ConsoleColor.Gray, ProtocolTypes.INTERNAL_SPECIAL_RESOURCE, uri); }
private void InnerHightSpeedSendTest(int destinationInstanceCount, int destinationMsgCount, int port) { int msgRecvCount = 0, sendMsgCount = 0; int previousMsgRecvCount = 0, previousSendMsgCount = 0; AutoResetEvent msgEvent = new AutoResetEvent(false); Console.Write("#Begining prepare Host channel resource..."); TcpHostTransportChannel hostChannel = new TcpHostTransportChannel(port); Assert.IsTrue(hostChannel.Regist()); hostChannel.ChannelCreated += delegate(object sender, LightSingleArgEventArgs <ITransportChannel> args) { MessageTransportChannel <MetadataContainer> msgChannel = new MessageTransportChannel <MetadataContainer>((IRawTransportChannel)args.Target, new MetadataProtocolStack()); msgChannel.ReceivedMessage += delegate(object s, LightSingleArgEventArgs <List <MetadataContainer> > a) { Interlocked.Add(ref msgRecvCount, a.Target.Count); }; }; Console.WriteLine("Done"); Console.Write("#Begining prepare {0} numbers of TCP channel...", destinationInstanceCount); IList <TcpTransportChannel> clients = new List <TcpTransportChannel>(); for (int i = 0; i < destinationInstanceCount; i++) { TcpTransportChannel transportChannel = new TcpTransportChannel("127.0.0.1", port); Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Unknown); transportChannel.Connect(); Assert.IsTrue(transportChannel.IsConnected); Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Opened); clients.Add(transportChannel); } Console.WriteLine("Done"); MetadataContainer container = new MetadataContainer(); container.SetAttribute(0x00, new StringValueStored("Test-Name")); container.SetAttribute(0x01, new StringValueStored("Test-Value")); byte[] data = MetadataObjectEngine.ToBytes(container); Assert.IsNotNull(data); Console.WriteLine("#Sending message on those of transport channels..."); ThreadPool.QueueUserWorkItem(delegate { while (true) { previousMsgRecvCount = msgRecvCount; previousSendMsgCount = sendMsgCount; Thread.Sleep(1000); if (destinationMsgCount != previousSendMsgCount || destinationMsgCount != previousMsgRecvCount) { Console.WriteLine(); Console.WriteLine("Send Count: {0} <---{1} messages/per-second.", sendMsgCount, sendMsgCount - previousSendMsgCount); Console.WriteLine("Recv Count: {0} <---{1} messages/per-second.", msgRecvCount, msgRecvCount - previousMsgRecvCount); Console.WriteLine(); } } }); ThreadPool.QueueUserWorkItem(delegate { while (sendMsgCount != destinationMsgCount) { for (int i = 0; i < clients.Count; i++) { clients[i].Send(data); Interlocked.Increment(ref sendMsgCount); if (sendMsgCount == destinationMsgCount) { break; } } //Thread.Sleep(100); } msgEvent.Set(); }); msgEvent.WaitOne(); Thread.Sleep(2000); Assert.IsTrue(sendMsgCount == destinationMsgCount); Assert.IsTrue(sendMsgCount == msgRecvCount); foreach (TcpTransportChannel clientChannel in clients) { clientChannel.Disconnect(); } Thread.Sleep(2000); }
public void SendMessageTest() { ITransportChannel connectedChannel = null; AutoResetEvent resetEvent = new AutoResetEvent(false); AutoResetEvent msgEvent = new AutoResetEvent(false); AutoResetEvent channelEvent = new AutoResetEvent(false); TcpHostTransportChannel hostChannel = new TcpHostTransportChannel(20005); Assert.IsTrue(hostChannel.Regist()); hostChannel.ChannelCreated += delegate(object sender, LightSingleArgEventArgs <ITransportChannel> args) { connectedChannel = args.Target; channelEvent.Set(); }; TcpTransportChannel transportChannel = new TcpTransportChannel("127.0.0.1", 20005); Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Unknown); transportChannel.Connect(); if (!channelEvent.WaitOne(2000)) { throw new System.Exception("#Cannot passed channel be connect test."); } Assert.IsNotNull(connectedChannel); Assert.IsTrue(connectedChannel.IsConnected); Assert.IsTrue(transportChannel.IsConnected); Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Opened); MetadataContainer msg = null; IMessageTransportChannel <MetadataContainer> msgChannel = new MessageTransportChannel <MetadataContainer>((IRawTransportChannel)connectedChannel, new MetadataProtocolStack()); msgChannel.ReceivedMessage += delegate(object sender, LightSingleArgEventArgs <List <MetadataContainer> > args) { msg = args.Target[0]; msgEvent.Set(); }; MetadataContainer container = new MetadataContainer(); container.SetAttribute(0x00, new StringValueStored("Test-Name")); container.SetAttribute(0x01, new StringValueStored("Test-Value")); byte[] data = MetadataObjectEngine.ToBytes(container); Assert.IsNotNull(data); int sendCount = transportChannel.Send(data); Assert.IsTrue(sendCount == data.Length || sendCount == 1); if (!msgEvent.WaitOne(2000)) { throw new System.Exception("#Cannot passed message channel receive test."); } Assert.IsNotNull(msg); Console.WriteLine(msg); msgChannel.Disconnected += delegate { resetEvent.Set(); }; transportChannel.Disconnect(); if (!resetEvent.WaitOne(10000)) { throw new System.Exception("#Cannot passed transport channel disconnect test."); } Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Closed); Thread.Sleep(2000); Assert.IsTrue(!connectedChannel.IsConnected); Assert.IsTrue(hostChannel.UnRegist()); }