Exemplo n.º 1
0
 /// Block on the stream forever, reading data as soon as we can.
 private void ReadStreamData()
 {
     using (var streamReader = new StreamReader(_stream))
     {
         try
         {
             while (!streamReader.EndOfStream)
             {
                 var line = streamReader.ReadLine();
                 lock (this)
                 {
                     EventHandler.Trigger(new DataReceivedEvent(line));
                 }
             }
         }
         catch (IOException)
         {
         }
     }
     lock (this)
     {
         _state  = TcpTransportState.Idle;
         _writer = null;
         _stream = null;
         EventHandler.Trigger(new ConnectionClosedEvent());
     }
 }
Exemplo n.º 2
0
        /// Open a connection to the remote host or dispatch an error event.
        private void Connect()
        {
            lock (this)
            {
                _client = new TcpClient();
                try
                {
                    _client.Connect(_host, _port);
                }
                catch (Exception error)
                {
                    EventHandler.Trigger(new ErrorEvent {
                        Exception = error
                    });
                    _state = TcpTransportState.Idle;
                    return;
                }

                _state = TcpTransportState.Connected;
                EventHandler.Trigger(new ConnectionOpenedEvent());
            }

            _stream = _client.GetStream();
            _writer = new StreamWriter(_stream);
            ReadStreamData();
        }
Exemplo n.º 3
0
        public void Connect(string host, int port)
        {
            lock (this)
            {
                if (_state != TcpTransportState.Idle)
                {
                    throw new TcpTransportException(TcpTransportErrors.ConnectionBusy, "A connection is already in progress");
                }

                _host   = host;
                _port   = port;
                _state  = TcpTransportState.Connecting;
                _thread = new Thread(Connect);
                _thread.Start();
            }
        }
Exemplo n.º 4
0
        public void Close()
        {
            lock (this)
            {
                if (_state != TcpTransportState.Connected)
                {
                    throw new TcpTransportException(TcpTransportErrors.NotConencted, "No active connection");
                }

                lock (this)
                {
                    _stream.Close();
                    _client.Close();
                    _client = null;
                    _state  = TcpTransportState.Idle;
                }
            }
        }