public void Connect(RedFoxEndpoint endpoint, ISocketConfiguration socketConfiguration) { if (socketConfiguration == null) { throw new ArgumentNullException("socketConfiguration"); } if (_socket != null) { throw new InvalidOperationException("Subscriber already connected"); } _cts = new CancellationTokenSource(); _socket = SocketFactory.CreateAndConnectAsync(endpoint, NodeType.Subscriber, socketConfiguration); _socket.Disconnected += SocketDisconnected; NodeGreetingMessageVerifier.SendReceiveAndVerify(_socket, socketConfiguration.ConnectTimeout); if (!_cts.IsCancellationRequested) { _messageFrameWriter = MessageFrameWriterFactory.CreateWriterFromSocket(_socket); _messageReceiveLoop = new MessageReceiveLoop(_messageSerialization, _socket, OnMessageReceived, MessageReceiveLoopOnException); _messageReceiveLoop.Start(); } }
public void Connect(RedFoxEndpoint endpoint, TimeSpan connectTimeout) { var socketConfiguration = (SocketConfiguration)SocketConfiguration.Default.Clone(); socketConfiguration.ConnectTimeout = connectTimeout; Connect(endpoint, socketConfiguration); }
public void ServiceQueue_AddMessageFrame_connect_single_message_received(ServiceQueueRotationAlgorithm rotationAlgorithm) { var endpoint = new RedFoxEndpoint("/path"); using (var serviceQueue = new ServiceQueue(rotationAlgorithm)) using (var serviceQueueReader = new ServiceQueueReader()) { IMessage messageReceived = null; var received = new ManualResetEventSlim(); serviceQueueReader.MessageReceived += (s, m) => { messageReceived = m; received.Set(); }; var testMessage = new TestMessage(); var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage); serviceQueue.AddMessageFrame(testMessageFrame); serviceQueue.Bind(endpoint); serviceQueueReader.Connect(endpoint); Assert.IsTrue(received.Wait(TimeSpan.FromSeconds(1))); Assert.AreEqual(testMessage, messageReceived); } }
public void GetHashCode_same_when_all_fields_match() { var endpoint1 = new RedFoxEndpoint(RedFoxTransport.Tcp, "host", 1234, "/path"); var endpoint2 = new RedFoxEndpoint(RedFoxTransport.Tcp, "host", 1234, "/path"); Assert.AreEqual(endpoint1.GetHashCode(), endpoint2.GetHashCode()); }
public void Bind(RedFoxEndpoint endpoint, NodeType nodeType, ISocketConfiguration socketConfiguration, ClientConnectedDelegate onClientConnected = null, ClientDisconnectedDelegate onClientDisconnected = null) { if (_listener != null || !_stopped.IsSet) { throw new InvalidOperationException("Server already bound, please use Unbind first"); } var ipAddress = IpAddressFromHostTranslator.GetIpAddressForHost(endpoint.Host); _endpoint = endpoint; _listener = new TcpListener(ipAddress, endpoint.Port); if (onClientConnected != null) { ClientConnected += onClientConnected; } if (onClientDisconnected != null) { ClientDisconnected += onClientDisconnected; } _stopped.Reset(); _started.Reset(); _listener.Start(); _cts = new CancellationTokenSource(); StartAcceptLoop(socketConfiguration, _cts.Token, nodeType); }
public void TryParse_port_should_be_parsed() { RedFoxEndpoint endpoint; Assert.IsTrue(RedFoxEndpoint.TryParse("tcp://hostname:1234", out endpoint)); Assert.AreEqual(1234, endpoint.Port); }
public void TryParse_transport_should_be_parsed(string endpointUri, RedFoxTransport expectedTransport) { RedFoxEndpoint endpoint; Assert.IsTrue(RedFoxEndpoint.TryParse(endpointUri, out endpoint)); Assert.AreEqual(expectedTransport, endpoint.Transport); }
public void Equals_different_Transport_false() { var endpoint = new RedFoxEndpoint { Transport = (RedFoxTransport)255 }; Assert.False(endpoint.Equals(new RedFoxEndpoint())); Assert.False(endpoint.Equals((object)new RedFoxEndpoint())); }
public InProcSocket Connect(RedFoxEndpoint endpoint) { if (endpoint.Transport != RedFoxTransport.Inproc) { throw new ArgumentException("Only InProcess transport endpoints are allowed to be registered"); } BlockingCollection <InProcSocketPair> accepter; if (!_registeredAccepterPorts.TryGetValue(endpoint, out accepter)) { throw new InvalidOperationException("Endpoint not listening to InProcess clients"); } var clientStream = new BlockingConcurrentQueue <MessageFrame>(); var serverStream = new BlockingConcurrentQueue <MessageFrame>(); var clientSocket = new InProcSocket(endpoint, clientStream, serverStream); var serverSocket = new InProcSocket(endpoint, serverStream, clientStream); var socketPair = new InProcSocketPair(clientSocket, serverSocket); accepter.Add(socketPair); return(clientSocket); }
public void ServiceQueue_connect_Writer_send_multiple_message_received() { const int count = 1000; using (var serviceQueue = new ServiceQueue()) using (var writer = new ServiceQueueWriter()) { var endpoint = new RedFoxEndpoint("/path"); var messageFrames = new List <MessageFrame>(); var counterSignal = new CounterSignal(count, 0); serviceQueue.MessageFrameReceived += m => { messageFrames.Add(m); counterSignal.Increment(); }; serviceQueue.Bind(endpoint); writer.Connect(endpoint); for (var i = 0; i < count; i++) { writer.SendMessage(new TestMessage()); } Assert.IsTrue(counterSignal.Wait(TimeSpan.FromSeconds(30))); Assert.AreEqual(count, messageFrames.Count); } }
public void TryParse_path_should_be_parsed(string endpointString, string path) { RedFoxEndpoint endpoint; Assert.IsTrue(RedFoxEndpoint.TryParse(endpointString, out endpoint)); Assert.AreEqual(path, endpoint.Path); }
public void Equals(string endpointUri1, string endpointUri2) { var endpoint1 = RedFoxEndpoint.Parse(endpointUri1); var endpoint2 = RedFoxEndpoint.Parse(endpointUri2); Assert.AreEqual(endpoint2, endpoint1); }
public void Bind(RedFoxEndpoint endpoint, NodeType nodeType, ISocketConfiguration socketConfiguration, ClientConnectedDelegate onClientConnected = null, ClientDisconnectedDelegate onClientDisconnected = null) { if (_listener != null || !_stopped.IsSet) throw new InvalidOperationException("Server already bound, please use Unbind first"); var ipAddress = IpAddressFromHostTranslator.GetIpAddressForHost(endpoint.Host); _endpoint = endpoint; _listener = new TcpListener(ipAddress, endpoint.Port); if (onClientConnected != null) ClientConnected += onClientConnected; if (onClientDisconnected != null) ClientDisconnected += onClientDisconnected; _stopped.Reset(); _started.Reset(); _listener.Start(); _cts = new CancellationTokenSource(); StartAcceptLoop(socketConfiguration, _cts.Token, nodeType); }
public void ServiceQueue_connect_AddMessageFrame_single_message_received(ServiceQueueRotationAlgorithm rotationAlgorithm) { var endpoint = new RedFoxEndpoint("/path"); using (var serviceQueue = new ServiceQueue(rotationAlgorithm)) using (var serviceQueueReader = new ServiceQueueReader()) { IMessage messageReceived = null; var received = new ManualResetEventSlim(); serviceQueueReader.MessageReceived += (s, m) => { messageReceived = m; received.Set(); }; serviceQueue.Bind(endpoint); serviceQueueReader.Connect(endpoint); var testMessage = new TestMessage(); var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage); serviceQueue.AddMessageFrame(testMessageFrame); Assert.IsTrue(received.Wait(TimeSpan.FromSeconds(1))); Assert.AreEqual(testMessage, messageReceived); } }
public InProcSocket(RedFoxEndpoint endpoint, BlockingConcurrentQueue<MessageFrame> writeStream, BlockingConcurrentQueue<MessageFrame> readStream) { if (writeStream == null) throw new ArgumentNullException("writeStream"); if (readStream == null) throw new ArgumentNullException("readStream"); Endpoint = endpoint; _writeStream = writeStream; _readStream = readStream; }
public void GetHashCode_different_Host() { var endpoint = new RedFoxEndpoint { Host = "host" }; Assert.AreNotEqual(endpoint.GetHashCode(), new RedFoxEndpoint().GetHashCode()); }
public void GetHashCode_different_Path() { var endpoint = new RedFoxEndpoint { Path = "/path" }; Assert.AreNotEqual(endpoint.GetHashCode(), new RedFoxEndpoint().GetHashCode()); }
public void Equals_all_fields_match_true() { var endpoint1 = new RedFoxEndpoint(RedFoxTransport.Tcp, "host", 1234, "/path"); var endpoint2 = new RedFoxEndpoint(RedFoxTransport.Tcp, "host", 1234, "/path"); Assert.True(endpoint1.Equals(endpoint2)); Assert.True(endpoint1.Equals((object)endpoint2)); }
public void GetHashCode_different_Port() { var endpoint = new RedFoxEndpoint { Port = 1234 }; Assert.AreNotEqual(endpoint.GetHashCode(), new RedFoxEndpoint().GetHashCode()); }
public void GetHashCode_different_Transport() { var endpoint = new RedFoxEndpoint { Transport = (RedFoxTransport)255 }; Assert.AreNotEqual(endpoint.GetHashCode(), new RedFoxEndpoint().GetHashCode()); }
public void publishers_bind_same_endpoint_twice_fails() { using (var publisher = new Publisher()) { var endpoint = new RedFoxEndpoint("/path"); publisher.Bind(endpoint); Assert.Throws<InvalidOperationException>(() => publisher.Bind(endpoint)); } }
public void Equals_different_Host_false() { var endpoint = new RedFoxEndpoint { Host = "host" }; Assert.False(endpoint.Equals(new RedFoxEndpoint())); Assert.False(endpoint.Equals((object)new RedFoxEndpoint())); }
public void Constructor_Transport_Host_Port_Path_setting_fields() { var endpoint = new RedFoxEndpoint(RedFoxTransport.Tcp, "host", 1234, "/path"); Assert.AreEqual(RedFoxTransport.Tcp, endpoint.Transport); Assert.AreEqual("host", endpoint.Host); Assert.AreEqual(1234, endpoint.Port); Assert.AreEqual("/path", endpoint.Path); }
public void Constructor_default_values() { var endpoint = new RedFoxEndpoint(); Assert.AreEqual(RedFoxTransport.Inproc, endpoint.Transport); Assert.AreEqual(null, endpoint.Host); Assert.AreEqual(0, endpoint.Port); Assert.AreEqual(null, endpoint.Path); }
public void Equals_different_Port_false() { var endpoint = new RedFoxEndpoint { Port = 1234 }; Assert.False(endpoint.Equals(new RedFoxEndpoint())); Assert.False(endpoint.Equals((object)new RedFoxEndpoint())); }
public void Equals_different_Path_false() { var endpoint = new RedFoxEndpoint { Path = "/path" }; Assert.False(endpoint.Equals(new RedFoxEndpoint())); Assert.False(endpoint.Equals((object)new RedFoxEndpoint())); }
public void responders_bind_same_endpoint_twice_fails() { using (var responder = TestHelpers.CreateTestResponder()) { var endpoint = new RedFoxEndpoint("/path"); responder.Bind(endpoint); Assert.Throws<InvalidOperationException>(() => responder.Bind(endpoint)); } }
private void DisconnectSocketsForEndpoint(RedFoxEndpoint endpoint) { var socketsMatchingEndpoint = _clientSockets.Keys.Where(socket => socket.Endpoint.Equals(endpoint)); foreach (var socket in socketsMatchingEndpoint) { socket.Disconnect(); } }
public void responders_bind_same_endpoint_twice_fails() { using (var responder = TestHelpers.CreateTestResponder()) { var endpoint = new RedFoxEndpoint("/path"); responder.Bind(endpoint); Assert.Throws <InvalidOperationException>(() => responder.Bind(endpoint)); } }
public void ServiceQueues_bind_same_endpoint_twice_fails() { using (var serviceQueue = new ServiceQueue()) { var endpoint = new RedFoxEndpoint("/path"); serviceQueue.Bind(endpoint); Assert.Throws <InvalidOperationException>(() => serviceQueue.Bind(endpoint)); } }
public void ServiceQueues_bind_same_endpoint_twice_fails() { using (var serviceQueue = new ServiceQueue()) { var endpoint = new RedFoxEndpoint("/path"); serviceQueue.Bind(endpoint); Assert.Throws<InvalidOperationException>(() => serviceQueue.Bind(endpoint)); } }
public TcpSocket(RedFoxEndpoint endpoint, TcpClient tcpClient) { if (tcpClient == null) throw new ArgumentNullException("tcpClient"); TcpClient = tcpClient; _remoteEndPoint = tcpClient.Client.RemoteEndPoint; Endpoint = endpoint; Stream = tcpClient.GetStream(); }
public void publishers_bind_same_endpoint_twice_fails() { using (var publisher = new Publisher()) { var endpoint = new RedFoxEndpoint("/path"); publisher.Bind(endpoint); Assert.Throws <InvalidOperationException>(() => publisher.Bind(endpoint)); } }
public void two_ServiceQueues_same_endpoint_fails() { using (var serviceQueue1 = new ServiceQueue()) using (var serviceQueue2 = new ServiceQueue()) { var endpoint = new RedFoxEndpoint("/path"); serviceQueue1.Bind(endpoint); Assert.Throws <InvalidOperationException>(() => serviceQueue2.Bind(endpoint)); } }
public void two_publishers_same_endpoint_fails() { using (var publisher1 = new Publisher()) using (var publisher2 = new Publisher()) { var endpoint = new RedFoxEndpoint("/path"); publisher1.Bind(endpoint); Assert.Throws <InvalidOperationException>(() => publisher2.Bind(endpoint)); } }
public void two_publishers_same_endpoint_fails() { using (var publisher1 = new Publisher()) using (var publisher2 = new Publisher()) { var endpoint = new RedFoxEndpoint("/path"); publisher1.Bind(endpoint); Assert.Throws<InvalidOperationException>(() => publisher2.Bind(endpoint)); } }
public void two_ServiceQueues_same_endpoint_fails() { using (var serviceQueue1 = new ServiceQueue()) using (var serviceQueue2 = new ServiceQueue()) { var endpoint = new RedFoxEndpoint("/path"); serviceQueue1.Bind(endpoint); Assert.Throws<InvalidOperationException>(() => serviceQueue2.Bind(endpoint)); } }
public void GetHashCode_case_insensitive_Host_same() { var endpoint1 = new RedFoxEndpoint { Host = "HOST" }; var endpoint2 = new RedFoxEndpoint { Host = "host" }; Assert.AreEqual(endpoint1.GetHashCode(), endpoint2.GetHashCode()); }
public void GetHashCode_ignore_Path_when_using_Tcp() { var endpoint1 = new RedFoxEndpoint { Transport = RedFoxTransport.Tcp, Path = "ignore" }; var endpoint2 = new RedFoxEndpoint { Transport = RedFoxTransport.Tcp }; Assert.AreEqual(endpoint1.GetHashCode(), endpoint2.GetHashCode()); }
public ISocketAccepter CreateAndBind(RedFoxEndpoint endpoint, NodeType nodeType, ISocketConfiguration socketConfiguration, ClientConnectedDelegate onClientConnected = null, ClientDisconnectedDelegate onClientDisconnected = null) { if (socketConfiguration == null) throw new ArgumentNullException("socketConfiguration"); var server = CreateForTransport(endpoint.Transport); server.Bind(endpoint, nodeType, socketConfiguration, onClientConnected, onClientDisconnected); return server; }
public ISocket CreateAndConnectAsync(RedFoxEndpoint endpoint, NodeType nodeType, ISocketConfiguration socketConfiguration) { switch (endpoint.Transport) { case RedFoxTransport.Inproc: return CreateInProcSocket(endpoint); case RedFoxTransport.Tcp: return CreateTcpSocket(endpoint, nodeType, socketConfiguration); default: throw new NotSupportedException(String.Format("Transport {0} not supported", endpoint.Transport)); } }
public BlockingCollection<InProcSocketPair> RegisterAccepter(RedFoxEndpoint endpoint) { if (endpoint.Transport != RedFoxTransport.Inproc) throw new ArgumentException("Only InProcess transport endpoints are allowed to be registered"); var result = _registeredAccepterPorts.AddOrUpdate( endpoint, e => new BlockingCollection<InProcSocketPair>(), (e, v) => { throw new InvalidOperationException("Endpoint already listening to InProcess clients"); }); return result; }
public void Connect(RedFoxEndpoint endpoint, ISocketConfiguration socketConfiguration) { if (socketConfiguration == null) throw new ArgumentNullException("socketConfiguration"); if (_socket != null) throw new InvalidOperationException("Subscriber already connected"); _cts = new CancellationTokenSource(); _socket = SocketFactory.CreateAndConnectAsync(endpoint, NodeType.ServiceQueueWriter, socketConfiguration); _socket.Disconnected += SocketDisconnected; NodeGreetingMessageVerifier.SendReceiveAndVerify(_socket, socketConfiguration.ConnectTimeout); if (!_cts.IsCancellationRequested) { _messageFrameWriter = MessageFrameWriterFactory.CreateWriterFromSocket(_socket); } }
private static ISocket CreateTcpSocket(RedFoxEndpoint endpoint, NodeType nodeType, ISocketConfiguration socketConfiguration) { var hasReceiveTimeout = NodeTypeHasReceiveTimeout.HasReceiveTimeout(nodeType); var tcpClient = new TcpClient { ReceiveTimeout = hasReceiveTimeout ? socketConfiguration.ReceiveTimeout.ToMillisOrZero() : 0, SendTimeout = socketConfiguration.SendTimeout.ToMillisOrZero(), NoDelay = true, ReceiveBufferSize = socketConfiguration.ReceiveBufferSize, SendBufferSize = socketConfiguration.SendBufferSize }; ConnectTcpSocket(tcpClient, endpoint.Host, endpoint.Port, socketConfiguration.ConnectTimeout); return new TcpSocket(endpoint, tcpClient); }
public void Bind(RedFoxEndpoint endpoint, NodeType nodeType, ISocketConfiguration socketConfiguration, ClientConnectedDelegate onClientConnected = null, ClientDisconnectedDelegate onClientDisconnected = null) { if (_listener != null || !_stopped.IsSet) throw new InvalidOperationException("Server already bound, please use Unbind first"); _listener = InProcessEndpoints.Instance.RegisterAccepter(endpoint); _endpoint = endpoint; if (onClientConnected != null) ClientConnected += onClientConnected; if (onClientDisconnected != null) ClientDisconnected += onClientDisconnected; _started.Reset(); _cts = new CancellationTokenSource(); Task.Factory.StartNew(() => StartAcceptLoop(_cts.Token, socketConfiguration), TaskCreationOptions.LongRunning); _started.Wait(); }
public void ServiceQueue_connect_Writer_send_single_message_MessageFramesCount_is_one() { using (var serviceQueue = new ServiceQueue()) using (var writer = new ServiceQueueWriter()) { var endpoint = new RedFoxEndpoint("/path"); var received = new ManualResetEventSlim(); serviceQueue.MessageFrameReceived += m => received.Set(); serviceQueue.Bind(endpoint); writer.Connect(endpoint); Assert.AreEqual(0, serviceQueue.MessageFramesCount); writer.SendMessage(new TestMessage()); Assert.IsTrue(received.Wait(TimeSpan.FromSeconds(1))); Assert.AreEqual(1, serviceQueue.MessageFramesCount); } }
public InProcSocket Connect(RedFoxEndpoint endpoint) { if (endpoint.Transport != RedFoxTransport.Inproc) throw new ArgumentException("Only InProcess transport endpoints are allowed to be registered"); BlockingCollection<InProcSocketPair> accepter; if (!_registeredAccepterPorts.TryGetValue(endpoint, out accepter)) { throw new InvalidOperationException("Endpoint not listening to InProcess clients"); } var clientStream = new BlockingConcurrentQueue<MessageFrame>(); var serverStream = new BlockingConcurrentQueue<MessageFrame>(); var clientSocket = new InProcSocket(endpoint, clientStream, serverStream); var serverSocket = new InProcSocket(endpoint, serverStream, clientStream); var socketPair = new InProcSocketPair(clientSocket, serverSocket); accepter.Add(socketPair); return clientSocket; }
public void unbind_disconnects_client() { using (var responder = TestHelpers.CreateTestResponder()) using (var requester = new Requester()) { var endpoint = new RedFoxEndpoint("/path"); var connected = new ManualResetEventSlim(); var disconnected = new ManualResetEventSlim(); responder.ClientConnected += (s, c) => connected.Set(); responder.ClientDisconnected += s => disconnected.Set(); responder.Bind(endpoint); requester.Connect(endpoint); Assert.IsTrue(connected.Wait(TimeSpan.FromSeconds(1))); responder.Unbind(endpoint); Assert.IsTrue(disconnected.Wait(TimeSpan.FromSeconds(1))); } }
public void unbind_disconnects_client() { using (var publisher = new Publisher()) using (var subscriber = new Subscriber()) { var endpoint = new RedFoxEndpoint("/path"); var connected = new ManualResetEventSlim(); var disconnected = new ManualResetEventSlim(); publisher.ClientConnected += (s, c) => connected.Set(); publisher.ClientDisconnected += s => disconnected.Set(); publisher.Bind(endpoint); subscriber.Connect(endpoint); Assert.IsTrue(connected.Wait(TimeSpan.FromSeconds(1))); publisher.Unbind(endpoint); Assert.IsTrue(disconnected.Wait(TimeSpan.FromSeconds(1))); } }
public void ServiceQueue_connect_Writer_send_single_message_received_fires_MessageFrameReceived_event() { using (var serviceQueue = new ServiceQueue()) using (var writer = new ServiceQueueWriter()) { var endpoint = new RedFoxEndpoint("/path"); MessageFrame messageFrame = null; var received = new ManualResetEventSlim(); serviceQueue.MessageFrameReceived += m => { messageFrame = m; received.Set(); }; serviceQueue.Bind(endpoint); writer.Connect(endpoint); writer.SendMessage(new TestMessage()); Assert.IsTrue(received.Wait(TimeSpan.FromSeconds(1))); Assert.IsNotNull(messageFrame); } }
public void Bind(RedFoxEndpoint endpoint, ISocketConfiguration socketConfiguration) { var server = SocketAccepterFactory.CreateAndBind(endpoint, NodeType.Responder, socketConfiguration, OnClientConnected); _servers[endpoint] = server; }
public void Bind(RedFoxEndpoint endpoint) { Bind(endpoint, SocketConfiguration.Default); }
public bool Unbind(RedFoxEndpoint endpoint) { ISocketAccepter removedServer; var serverRemoved = _servers.TryRemove(endpoint, out removedServer); if (serverRemoved) { removedServer.Unbind(); DisconnectSocketsForEndpoint(endpoint); } return serverRemoved; }
public bool UnregisterAccepter(RedFoxEndpoint endpoint) { BlockingCollection<InProcSocketPair> oldValue; return _registeredAccepterPorts.TryRemove(endpoint, out oldValue); }