Esempio n. 1
0
 private void OnStatusUpdated(object sender, StatusEventArgs e)
 {
     if (e.Type == NetworkUtility.requestStatus) //Request status
     {
         if (e.Status == NetworkUtility.sucessfulStatus)
         {
             lstStatus.Items.Insert(0, NetworkUtility.GetStatusInfo(e.Type, e.Status,
                                                                    NetworkUtility.GetRequestInfo(operation, client.GetDataLength())));
             //request success and receive data now
             client.ReceiveData(Int32.Parse(txtTimeout.Text));
         }
         else
         {
             lstStatus.Items.Insert(0, NetworkUtility.GetStatusInfo(e.Type, e.Status, null));
             grid.IsEnabled = true;
         }
     }
     else if (e.Type == NetworkUtility.receiveStatus)    //Receive status
     {
         if (e.Status == 0)
         {
             if (operation == NetworkUtility.encodeOper)
             {
                 count++;
             }
             else if (operation == NetworkUtility.decodeOper)
             {
                 count--;
             }
         }
         //update UI about the response.
         lblEncodeCycleCount.Content = count + " (cycle count)";
         lstStatus.Items.Insert(0, NetworkUtility.GetStatusInfo(e.Type, e.Status, null));
         grid.IsEnabled = true;
     }
 }
Esempio n. 2
0
        private void Run()
        {
            while (receiveEvent.WaitOne() && !isShutDown)
            {
                byte            respStatus  = 0;
                StatusEventArgs statusEvent = new StatusEventArgs();
                try
                {
                    int    respLength = 0;
                    int    readSoFar  = 0;
                    byte[] respData   = null;
                    do
                    {
                        try
                        {
                            stream.ReadTimeout = timeMillis;
                            byte[] inStream = new byte[dataLength];
                            int    read     = stream.Read(inStream, 0, dataLength);
                            //reading data in first go
                            if (readSoFar == 0)
                            {
                                respStatus = inStream[respStatusPos];                                   //3rd byte
                                                                                                        //Get response length of 4 bytes.
                                byte[] respLengthData = new byte[respLengthSize];
                                Array.Copy(inStream, respLengthPos, respLengthData, 0, respLengthSize); // 4th byte to 4 bytes length
                                respLength = BitConverter.ToInt32(respLengthData, 0);
                                //reading response.
                                readSoFar = read <= respLength ? read : respLength;
                                respData  = new byte[respLength];
                                Array.Copy(inStream, 0, respData, 0, readSoFar);
                            }
                            else
                            {
                                // reading remaining data.
                                int nextRead = (read + readSoFar) < respLength ? read : (respLength - readSoFar);
                                Array.Copy(inStream, 0, respData, readSoFar, nextRead);
                                readSoFar = readSoFar + nextRead;
                            }
                            //exit if no data to read or responded with error code
                            if (read == 0 || respStatus != 0)
                            {
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            respStatus = (byte)NetworkUtility.exceptErr;
                            break;
                        }
                    } while (readSoFar < respLength);

                    if (respStatus == 0 && respLength <= maxRespLength)
                    {
                        //store encrypted data
                        statusEvent.Data = null;
                        statusEvent.Data = new byte[respLength - respSizeWithoutData];
                        //read response data from 8th byte to last but one.
                        Array.Copy(respData, respDataPos, statusEvent.Data, 0, statusEvent.Data.Length);
                    }
                    else if (respStatus == 0 && respLength > maxRespLength)
                    {
                        // response status assigned as server response status returning zero
                        // eventhough max response size is greater than 1048576
                        respStatus = (byte)(NetworkUtility.maxRespErr);
                    }
                }catch (Exception ex)
                {
                    respStatus = (byte)NetworkUtility.exceptErr;
                }
                finally
                {
                    stream.Close();
                }

                statusEvent.Type   = NetworkUtility.receiveStatus;
                statusEvent.Status = respStatus;
                DataReceived(this, statusEvent);
                receiveEvent.Reset();
            }
        }