Exemplo n.º 1
0
        internal bool Connect(IPAddress IP, int port, int readTimeout, int allowedFailedCount = 0)
        {
            _serverIP           = new IPEndPoint(IP, port);
            _readTimeout        = readTimeout;
            _allowedFailedCount = allowedFailedCount;
            _currentFailedCount = 0;

            try
            {
                _tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _tcpClient.Connect(_serverIP);
                _tcpClient.ReceiveTimeout = _readTimeout;

                _tcpStream = new NetworkStream(_tcpClient);
                Connected  = true;
                return(true);
            }
            catch
            {
                Action <TCPError> localEvent = TCPErrorEvent;
                if (localEvent != null)
                {
                    TCPError error = new TCPError();
                    error.Message = string.Format("Unable to connect to {0} on port {1}", _serverIP.Address, _serverIP.Port);
                    localEvent(error);
                }
            }
            return(false);
        }
Exemplo n.º 2
0
 internal void Write(string data)
 {
     try
     {
         if (_tcpStream.CanWrite && Connected)
         {
             byte[] message = System.Text.Encoding.UTF8.GetBytes(data + Environment.NewLine);
             _tcpStream.Write(message, 0, message.Length);
         }
     }
     catch (IOException)
     {
         /*
          * _currentFailedCount++;
          * Action<TCPError> localEvent = TCPErrorEvent;
          * if (localEvent != null && _tcpStream.CanRead)
          * {
          *  TCPError error = new TCPError();
          *  error.Message = string.Format("Read Timeout, No Response from Server in {0}ms", _readTimeout);
          *  localEvent(error);
          * }
          */
     }
     catch (Exception ex)
     {
         Action <TCPError> localEvent = TCPErrorEvent;
         if (localEvent != null)
         {
             TCPError error = new TCPError();
             error.Message = ex.Message;
             localEvent(error);
         }
     }
 }
Exemplo n.º 3
0
        internal string Read()
        {
            try
            {
                if (_tcpStream.CanRead && Connected)
                {
                    byte[] readBytes = new byte[100000];
                    _tcpStream.Read(readBytes, 0, readBytes.Length);
                    string result = Encoding.UTF8.GetString(readBytes, 0, readBytes.Length);
                    // Reset Failed Counter
                    _currentFailedCount = 0;
                    return(result.TrimEnd('\0'));
                }
            }
            catch (IOException)
            {
                /*
                 * _currentFailedCount++;
                 * Action<TCPError> localEvent = TCPErrorEvent;
                 * if (localEvent != null && _tcpStream.CanRead)
                 * {
                 *  TCPError error = new TCPError();
                 *  error.Message = string.Format("Read Timeout, No Response from Server in {0}ms", _readTimeout);
                 *  localEvent(error);
                 * }
                 */
            }
            catch (Exception ex)
            {
                _currentFailedCount++;
                Action <TCPError> localEvent = TCPErrorEvent;
                if (localEvent != null)
                {
                    TCPError error = new TCPError();
                    error.Message = ex.Message;
                    localEvent(error);
                }
            }

            if (_currentFailedCount > _allowedFailedCount)
            {
                Action <int> localEvent = TCPConnectionEvent;
                if (localEvent != null)
                {
                    localEvent(_currentFailedCount);
                }
                Disconnect();
                _currentFailedCount = 0;
            }
            return(null);
        }