private async Task AcceptLoopAsync(ISocketConfiguration socketConfiguration, CancellationToken cancellationToken, NodeType nodeType) { _started.Set(); try { while (!cancellationToken.IsCancellationRequested) { var tcpClient = await _listener.AcceptTcpClientAsync(); SetupTcpClientParametersWithoutReceiveTimeout(tcpClient, socketConfiguration); tcpClient.ReceiveTimeout = NodeTypeHasReceiveTimeout.HasReceiveTimeout(nodeType) ? socketConfiguration.ReceiveTimeout.ToMillisOrZero() : 0; var socket = new TcpSocket(_endpoint, tcpClient); socket.Disconnected += () => ClientDisconnected(socket); TryFireClientConnectedEvent(socket, socketConfiguration); } } catch (ThreadAbortException) { } catch (OperationCanceledException) { } catch (ObjectDisposedException) { } finally { _stopped.Set(); } }
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); }
private void OnClientConnected(ISocket socket, ISocketConfiguration socketConfiguration) { if (socket == null) { throw new ArgumentNullException("socket"); } var remoteNodeType = NodeGreetingMessageVerifier.SendReceiveAndVerify(socket, socketConfiguration.ConnectTimeout); switch (remoteNodeType) { case NodeType.ServiceQueueReader: ReaderClientConnected(socket, socketConfiguration); break; case NodeType.ServiceQueueWriter: WriterClientConnected(socket, socketConfiguration); break; default: throw new RedFoxProtocolException(String.Format("Unsupported node type connected to ServiceQueue: {0}", remoteNodeType)); } if (socket.IsDisconnected) { // this is to fix the race condition if socket was disconnected meanwhile CheckIfSocketDisconnected(socket); } }
private void OnClientConnected(ISocket socket, ISocketConfiguration socketConfiguration) { if (socket == null) { throw new ArgumentNullException("socket"); } NodeGreetingMessageVerifier.SendReceiveAndVerify(socket, socketConfiguration.ConnectTimeout); var messageFrameWriter = MessageFrameWriterFactory.CreateWriterFromSocket(socket); var messageQueue = new MessageQueueBatch(socketConfiguration.SendBufferSize); var messageReceiveLoop = new MessageReceiveLoop(_messageSerialization, socket, OnMessageReceived, MessageReceiveLoopOnException); if (_broadcastSockets.TryAdd(socket, new MessageQueueReceiveLoop(messageQueue, messageReceiveLoop))) { _messageQueueBroadcaster.Register(messageQueue, messageFrameWriter); ClientConnected(socket, socketConfiguration); messageReceiveLoop.Start(); } if (socket.IsDisconnected) { // this is to fix the race condition if socket was disconnected meanwhile SocketDisconnected(socket); } }
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(); } }
private void OnClientConnected(ISocket socket, ISocketConfiguration socketConfiguration) { if (socket == null) { throw new ArgumentNullException("socket"); } NodeGreetingMessageVerifier.SendReceiveAndVerify(socket, socketConfiguration.ConnectTimeout); var messageFrameWriter = MessageFrameWriterFactory.CreateWriterFromSocket(socket); var messageFrameReceiver = new MessageFrameReceiver(socket); var senderReceiver = new SenderReceiver(messageFrameWriter, messageFrameReceiver); socket.Disconnected += () => SocketDisconnected(socket); if (_clientSockets.TryAdd(socket, senderReceiver)) { var task = ReceiveRequestMessage(senderReceiver); ClientConnected(socket, socketConfiguration); } if (socket.IsDisconnected) { // this is to fix the race condition if socket was disconnected meanwhile SocketDisconnected(socket); } }
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); }
private void ReaderClientConnected(ISocket socket, ISocketConfiguration socketConfiguration) { var messageFrameWriter = MessageFrameWriterFactory.CreateWriterFromSocket(socket); if (_readerClientSockets.TryAdd(socket, messageFrameWriter)) { _messageQueueDistributor.Register(messageFrameWriter); ClientConnected(socket, socketConfiguration); } }
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)); } }
private void WriterClientConnected(ISocket socket, ISocketConfiguration socketConfiguration) { var messageFrameReceiver = new MessageFrameReceiver(socket); messageFrameReceiver.Disconnected += () => WriterSocketDisconnected(socket); var task = ReceiveAsync(messageFrameReceiver, _disposedToken); if (_writerClientSockets.TryAdd(socket, messageFrameReceiver)) { ClientConnected(socket, socketConfiguration); } }
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; }
private bool TryFireClientConnectedEvent(ISocket socket, ISocketConfiguration socketConfiguration) { try { ClientConnected(socket, socketConfiguration); return(true); } catch { // TODO: log exception somewhere return(false); } }
private bool TryFireClientConnectedEvent(InProcSocket socket, ISocketConfiguration socketConfiguration) { try { socket.Disconnected += () => ClientDisconnected(socket); ClientConnected(socket, socketConfiguration); return true; } catch { return false; } }
private bool TryFireClientConnectedEvent(InProcSocket socket, ISocketConfiguration socketConfiguration) { try { socket.Disconnected += () => ClientDisconnected(socket); ClientConnected(socket, socketConfiguration); return(true); } catch { return(false); } }
public SocketBase(ISocketConfiguration configuration) { _configuration = configuration; if (configuration.Autoconnect) { CyclicExecutor.Instance.Add(_cycleId, _cycleId, 5000, () => { CyclicExecutor.Instance.Enabled(_cycleId, false); if (!IsConnected && !_shutdown) { Open(); } }); } }
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); } }
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); }
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(); }
private void StartAcceptLoop(CancellationToken cancellationToken, ISocketConfiguration socketConfiguration) { _stopped.Reset(); _started.Set(); try { while (!cancellationToken.IsCancellationRequested) { var socketPair = _listener.Take(cancellationToken); TryFireClientConnectedEvent(socketPair.ServerSocket, socketConfiguration); } } catch (OperationCanceledException) { } finally { _stopped.Set(); } }
private void OnClientConnected(ISocket socket, ISocketConfiguration socketConfiguration) { if (socket == null) throw new ArgumentNullException("socket"); var remoteNodeType = NodeGreetingMessageVerifier.SendReceiveAndVerify(socket, socketConfiguration.ConnectTimeout); switch (remoteNodeType) { case NodeType.ServiceQueueReader: ReaderClientConnected(socket, socketConfiguration); break; case NodeType.ServiceQueueWriter: WriterClientConnected(socket, socketConfiguration); break; default: throw new RedFoxProtocolException(String.Format("Unsupported node type connected to ServiceQueue: {0}", remoteNodeType)); } if (socket.IsDisconnected) { // this is to fix the race condition if socket was disconnected meanwhile CheckIfSocketDisconnected(socket); } }
private void OnClientConnected(ISocket socket, ISocketConfiguration socketConfiguration) { if (socket == null) throw new ArgumentNullException("socket"); NodeGreetingMessageVerifier.SendReceiveAndVerify(socket, socketConfiguration.ConnectTimeout); var messageFrameWriter = MessageFrameWriterFactory.CreateWriterFromSocket(socket); var messageQueue = new MessageQueueBatch(socketConfiguration.SendBufferSize); var messageReceiveLoop = new MessageReceiveLoop(_messageSerialization, socket, OnMessageReceived, MessageReceiveLoopOnException); if (_broadcastSockets.TryAdd(socket, new MessageQueueReceiveLoop(messageQueue, messageReceiveLoop))) { _messageQueueBroadcaster.Register(messageQueue, messageFrameWriter); ClientConnected(socket, socketConfiguration); messageReceiveLoop.Start(); } if (socket.IsDisconnected) { // this is to fix the race condition if socket was disconnected meanwhile SocketDisconnected(socket); } }
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(); }
private void OnClientConnected(ISocket socket, ISocketConfiguration socketConfiguration) { if (socket == null) throw new ArgumentNullException("socket"); NodeGreetingMessageVerifier.SendReceiveAndVerify(socket, socketConfiguration.ConnectTimeout); var messageFrameWriter = MessageFrameWriterFactory.CreateWriterFromSocket(socket); var messageFrameReceiver = new MessageFrameReceiver(socket); var senderReceiver = new SenderReceiver(messageFrameWriter, messageFrameReceiver); socket.Disconnected += () => SocketDisconnected(socket); if (_clientSockets.TryAdd(socket, senderReceiver)) { var task = ReceiveRequestMessage(senderReceiver); ClientConnected(socket, socketConfiguration); } if (socket.IsDisconnected) { // this is to fix the race condition if socket was disconnected meanwhile SocketDisconnected(socket); } }
public void Bind(RedFoxEndpoint endpoint, ISocketConfiguration socketConfiguration) { var server = SocketAccepterFactory.CreateAndBind(endpoint, NodeType.Responder, socketConfiguration, OnClientConnected); _servers[endpoint] = server; }
private static void SetupTcpClientParametersWithoutReceiveTimeout(TcpClient tcpClient, ISocketConfiguration socketConfiguration) { tcpClient.SendTimeout = socketConfiguration.SendTimeout.ToMillisOrZero(); tcpClient.NoDelay = true; tcpClient.ReceiveBufferSize = socketConfiguration.ReceiveBufferSize; tcpClient.SendBufferSize = socketConfiguration.SendBufferSize; }
private bool TryFireClientConnectedEvent(ISocket socket, ISocketConfiguration socketConfiguration) { try { ClientConnected(socket, socketConfiguration); return true; } catch { // TODO: log exception somewhere return false; } }
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)); } }
private void StartAcceptLoop(ISocketConfiguration socketConfiguration, CancellationToken cancellationToken, NodeType nodeType) { var task = AcceptLoopAsync(socketConfiguration, cancellationToken, nodeType); }
public void Bind(RedFoxEndpoint endpoint, ISocketConfiguration socketConfiguration) { var server = SocketAccepterFactory.CreateAndBind(endpoint, NodeType.ServiceQueue, socketConfiguration, OnClientConnected, CheckIfSocketDisconnected); _servers[endpoint] = server; }
public SocketBase(ISocketConfiguration configuration) { _configuration = configuration; }
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)); }