Ejemplo n.º 1
0
        //Closes the Connection
        public void Close(bool trackConnectStatus)
        {
            if (trackConnectStatus)
            {
                FConnectStatus = TConnectStatus.Disconnected;
            }

            FReadData = false;
            base.Close();
        }
Ejemplo n.º 2
0
        //Connect Callback
        protected void ConnectCallback(IAsyncResult asyncResult)
        {
            TConnectStatus connectStatus = TConnectStatus.Connected;

            try
            {
                EndConnect(asyncResult);
            }
            catch (Exception e)
            {
                connectStatus = TConnectStatus.NeverConnected;
                FConnectFailSinceLastEvaluate = true;
                AddErrorMessage(e.Message);
            }

            FReadData      = false;
            FConnectStatus = connectStatus;
        }
Ejemplo n.º 3
0
 //starts an asynchronous attempt to connect to the specified server and port
 public void BeginConnectAndTrackStatus()
 {
     //make sure we have a server string and a valid port number
     if (FRemoteHost != null && FRemotePort >= 0 && FRemotePort <= 65535)
     {
         FConnectStatus = TConnectStatus.Connecting;
         try
         {
             base.BeginConnect(FRemoteHost, FRemotePort, new AsyncCallback(this.ConnectCallback), this);
         }
         catch (Exception e)
         {
             FConnectStatus = TConnectStatus.NeverConnected;
             FConnectFailSinceLastEvaluate = true;
             AddErrorMessage(e.Message);
         }
     }
 }
Ejemplo n.º 4
0
        // Test the Connection if the Server is still available.
        private void TestConnection(ref int nTestBytesReceived, ref byte[] ReadBuffer)
        {
            bool blockingState = Client.Blocking;
            bool ReadData      = true;

            try
            {
                //we do a read that is guaranteed not to block
                //Client.Blocking = false;
                nTestBytesReceived = Client.Receive(ReadBuffer);
            }
            catch (SocketException socketException)
            {
                //the non-blocking read failed
                ReadData = false;

                // 10035 == WSAEWOULDBLOCK
                //the read may have failed because it WOULD have blocked, ie the
                //connection is still there, but nothing has been sent
                if (!(socketException.NativeErrorCode.Equals(10035)))
                {
                    //however, if there was some other exception, we've lost the connection
                    FConnectStatus = TConnectStatus.ConnectionLost;
                }
            }
            finally
            {
                Client.Blocking = blockingState;
            }

            if (ReadData)
            {
                //if we received 0 bytes on a non-blocking read, we've lost the connection
                if (nTestBytesReceived == 0)
                {
                    FConnectStatus = TConnectStatus.ConnectionLost;
                }
            }
        }
Ejemplo n.º 5
0
        //Read the data in form the Socket in a seperate Thread
        private void ReadToSocket()
        {
            FReadData = true;
            int nBytesReceived = 0;

            byte[] ReadBuffer = new byte[ReceiveBufferSize];

            try
            {
                NetworkStream Stream = this.GetStream();
                Client.Blocking = true;

                //check if the stream can Read
                if (Stream.CanRead == true)
                {
                    //check if data available on the stream
                    if (Stream.DataAvailable)
                    {
                        do
                        {
                            //Write the data from the Stream to the Buffer and check how many bytes received
                            string ReceivedString = String.Empty;
                            nBytesReceived = Stream.Read(ReadBuffer, 0, ReadBuffer.Length);

                            // if we received some bytes we convert them to a string
                            if (nBytesReceived > 0)
                            {
                                FDataReceived += System.Text.Encoding.ASCII.GetString(ReadBuffer, 0, nBytesReceived);
                            }
                        } while (Stream.DataAvailable);
                    }
                    else
                    {
                        //If there is no data to read we chack if the Server is still availabel;
                        //Try to Read the Socket and Check if the Server is still available
                        TestConnection(ref nBytesReceived, ref ReadBuffer);

                        //If the Server is still availbaleand we received bytes we send them to vvvv
                        if (nBytesReceived > 0)
                        {
                            //If we received data save this and add it to the rest we will received.
                            FDataReceived += System.Text.Encoding.ASCII.GetString(ReadBuffer, 0, nBytesReceived);
                        }
                    }
                }
            }
            catch (ObjectDisposedException objectDisposedException)
            {
                //if the TcpClient or NetworkStream is closed, we're also not connected anymore
                FConnectStatus = TConnectStatus.ConnectionLost;
                AddErrorMessage(objectDisposedException.Message);
            }
            catch (InvalidOperationException invalidOperationException)
            {
                //if the socket is closed, we're not connected anymore
                FConnectStatus = TConnectStatus.ConnectionLost;
                AddErrorMessage(invalidOperationException.Message);
            }
            catch (IOException ioException)
            {
                //if a network error occured, we're not connected anymore
                FConnectStatus = TConnectStatus.ConnectionLost;
                AddErrorMessage(ioException.Message);
            }
            catch (Exception e)
            {
                AddErrorMessage(e.Message);
            }

            // Add the receive Data to the DataStorage List.
            Monitor.Enter(ReadLock);
            try
            {
                if (!String.IsNullOrEmpty(FDataReceived))
                {
                    FDataStorage.Add(FDataReceived);
                }
            }
            finally
            {
                Monitor.Exit(ReadLock);
            }

            // Deltet the received data for the next reading cycling
            FDataReceived = String.Empty;

            //set the reading status to false
            FReadData = false;
        }