예제 #1
0
        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();
            }
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        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();
            }
        }
예제 #5
0
 public MessageQueueReceiveLoop(MessageQueueBatch messageQueue, MessageReceiveLoop messageReceiveLoop)
 {
     MessageQueue = messageQueue;
     MessageReceiveLoop = messageReceiveLoop;
 }
예제 #6
0
 public MessageQueueReceiveLoop(MessageQueueBatch messageQueue, MessageReceiveLoop messageReceiveLoop)
 {
     MessageQueue       = messageQueue;
     MessageReceiveLoop = messageReceiveLoop;
 }