/// <summary> /// 创建一个新的连接代理器 /// </summary> /// <param name="iep">远程终结点地址</param> /// <param name="protocolStack">协议栈</param> /// <param name="transactionManager">事务管理器</param> /// <returns>如果无法连接到远程地址,则返回null.</returns> /// <exception cref="System.ArgumentNullException">非法参数</exception> public static IServerConnectionAgent <BaseMessage> Create(IPEndPoint iep, IProtocolStack protocolStack, MessageTransactionManager transactionManager) { if (iep == null) { throw new ArgumentNullException(nameof(iep)); } if (protocolStack == null) { throw new ArgumentNullException(nameof(protocolStack)); } if (transactionManager == null) { throw new ArgumentNullException(nameof(transactionManager)); } ITransportChannel transportChannel = new TcpTransportChannel(iep); transportChannel.Connect(); if (!transportChannel.IsConnected) { return(null); } IMessageTransportChannel <BaseMessage> msgChannel = new MessageTransportChannel <BaseMessage>((IRawTransportChannel)transportChannel, protocolStack); return(new IntellectObjectConnectionAgent(msgChannel, transactionManager)); }
/// <summary> /// 创建一个新的连接代理器 /// </summary> /// <param name="iep">远程终结点地址</param> /// <param name="protocolStack">协议栈</param> /// <param name="transactionManager">事务管理器</param> /// <returns>如果无法连接到远程地址,则返回null.</returns> public static IThriftConnectionAgent Create(IPEndPoint iep, IProtocolStack <ThriftMessage> protocolStack, ThriftMessageTransactionManager transactionManager) { if (iep == null) { throw new ArgumentNullException("iep"); } if (protocolStack == null) { throw new ArgumentNullException("protocolStack"); } if (transactionManager == null) { throw new ArgumentNullException("transactionManager"); } ITransportChannel transportChannel = new TcpTransportChannel(iep); transportChannel.Connect(); if (!transportChannel.IsConnected) { return(null); } IMessageTransportChannel <ThriftMessage> msgChannel = new MessageTransportChannel <ThriftMessage>((IRawTransportChannel)transportChannel, protocolStack, new ThriftProtocolSegmentDataParser((ThriftProtocolStack)protocolStack)); return(new ThriftConnectionAgent(msgChannel, transactionManager)); }
//channel created event. void ChannelCreated(object sender, LightSingleArgEventArgs <ITransportChannel> e) { IMessageTransportChannel <MetadataContainer> msgChannel = new MessageTransportChannel <MetadataContainer>((IRawTransportChannel)e.Target, _protocolStack); IServerConnectionAgent <MetadataContainer> agent = new MetadataConnectionAgent(msgChannel, _transactionManager); _scheduler.Regist(agent); }
internal static void ChannelCreated(object sender, LightSingleArgEventArgs <ITransportChannel> e) { IHostTransportChannel hostChannel = (IHostTransportChannel)sender; KAENetworkResource tag = (KAENetworkResource)hostChannel.Tag; if (tag.Protocol == ProtocolTypes.Metadata) { IMessageTransportChannel <MetadataContainer> msgChannel = new MessageTransportChannel <MetadataContainer>((IRawTransportChannel)e.Target, (IProtocolStack)NetworkHelper.GetProtocolStack(tag.Protocol)); MetadataConnectionAgent agent = new MetadataConnectionAgent(msgChannel, (MetadataTransactionManager)NetworkHelper.GetTransactionManager(tag.Protocol)); agent.Disconnected += AgentDisconnected; agent.NewTransaction += MetadataNewTransactionHandler; agent.Tag = tag; } else if (tag.Protocol == ProtocolTypes.Intellegence) { IMessageTransportChannel <BaseMessage> msgChannel = new MessageTransportChannel <BaseMessage>((IRawTransportChannel)e.Target, (IProtocolStack)NetworkHelper.GetProtocolStack(tag.Protocol)); IntellectObjectConnectionAgent agent = new IntellectObjectConnectionAgent(msgChannel, (MessageTransactionManager)NetworkHelper.GetTransactionManager(tag.Protocol)); agent.Disconnected += AgentDisconnected; agent.NewTransaction += IntellegenceNewTransactionHandler; agent.Tag = tag; } }
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()); }
/// <summary> /// ����һ���µ����Ӵ����� /// </summary> /// <param name="iep">Զ���ս���ַ</param> /// <param name="protocolStack">Э��ջ</param> /// <param name="transactionManager">���������</param> /// <returns>��������ӵ�Զ�̵�ַ����null.</returns> public static IThriftConnectionAgent Create(IPEndPoint iep, IProtocolStack<ThriftMessage> protocolStack, ThriftMessageTransactionManager transactionManager) { if (iep == null) throw new ArgumentNullException("iep"); if (protocolStack == null) throw new ArgumentNullException("protocolStack"); if (transactionManager == null) throw new ArgumentNullException("transactionManager"); ITransportChannel transportChannel = new TcpTransportChannel(iep); transportChannel.Connect(); if (!transportChannel.IsConnected) return null; IMessageTransportChannel<ThriftMessage> msgChannel = new MessageTransportChannel<ThriftMessage>((IRawTransportChannel)transportChannel, protocolStack, new ThriftProtocolSegmentDataParser((ThriftProtocolStack) protocolStack)); return new ThriftConnectionAgent(msgChannel, transactionManager); }