예제 #1
0
        private void BackgroundWorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            var receiveBuffer = new byte[4096];
            int receiveBytesCount;
            var node = (Node)doWorkEventArgs.Argument;

            using (var socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
            {
                try
                {
                    socket.Connect(node.Address, node.Port);
                }
                catch
                {
                    doWorkEventArgs.Result = CheckConnectionResult.CreateFail(node.Id);
                    return;
                }

                if (!socket.Connected)
                {
                    doWorkEventArgs.Result = CheckConnectionResult.CreateFail(node.Id);
                    return;
                }

                var messageToSend = Encoding.UTF8.GetBytes("{\"numberOfRecords\":200,\"version\":1,\"includeInfo\":true}");
                try
                {
                    socket.Send(messageToSend);
                }
                catch
                {
                    doWorkEventArgs.Result = CheckConnectionResult.CreateFail(node.Id);
                    return;
                }

                try
                {
                    receiveBytesCount = socket.Receive(receiveBuffer, 4096, SocketFlags.None);
                }
                catch
                {
                    doWorkEventArgs.Result = CheckConnectionResult.CreateFail(node.Id);
                    return;
                }
            }

            var receivedData      = Encoding.UTF8.GetString(receiveBuffer, 0, receiveBytesCount);
            var dataDecoderResult = _dataDecoder.Decode(receivedData);

            if (dataDecoderResult == null)
            {
                doWorkEventArgs.Result = CheckConnectionResult.CreateFail(node.Id);
                return;
            }

            doWorkEventArgs.Result = CheckConnectionResult.CreateSuccess(node.Id, dataDecoderResult);
        }
예제 #2
0
        private void OneDone(CheckConnectionResult result)
        {
            var nodes = _nodeRepository.GetAll();
            var node  = nodes.First(x => x.Id == result.Id);

            var now = DateTime.Now;

            if (result.IsOnline)
            {
                node.CurrentStatus         = NodeStatus.Available;
                node.UpSince               = result.DataDecoderResult.UpSince;
                node.LastDataAvailableTime = result.DataDecoderResult.LastDataAvailableTime;
                node.LastAccessed          = now;
            }
            else
            {
                node.ResetSessionVariables();
            }
            node.LastTry = now;
            _nodeRepository.Update(node);
            FillList();
        }