示例#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();
            }
        }