Ejemplo n.º 1
0
 private void Awake()
 {
     _state    = ORTCPClientState.Disconnected;
     _events   = new Queue <ORTCPEventType>();
     _messages = new Queue <string>();
     _packets  = new Queue <ORSocketPacket>();
 }
Ejemplo n.º 2
0
 private void SetTcpClient(TcpClient tcpClient)
 {
     _client = tcpClient;
     if (_client.Connected)
     {
         _stream = _client.GetStream();
         _reader = new StreamReader(_stream);
         _writer = new StreamWriter(_stream);
         _state  = ORTCPClientState.Connected;
         _events.Enqueue(ORTCPEventType.Connected);
         _readThread = new Thread(ReadData);
         _readThread.IsBackground = true;
         _readThread.Start();
     }
     else
     {
         _state = ORTCPClientState.Disconnected;
     }
 }
Ejemplo n.º 3
0
 public void Disconnect()
 {
     _state = ORTCPClientState.Disconnected;
     try { if (_reader != null)
           {
               _reader.Close();
           }
     } catch (Exception e) { e.ToString(); }
     try { if (_writer != null)
           {
               _writer.Close();
           }
     } catch (Exception e) { e.ToString(); }
     try { if (_client != null)
           {
               _client.Close();
           }
     } catch (Exception e) { e.ToString(); }
 }
Ejemplo n.º 4
0
    public void Connect(string hostname, int port)
    {
        StatusText.text = ("[TCPClient] trying to connect to " + hostname + " " + port);
        if (_state == ORTCPClientState.Connected)
        {
            return;
        }

        this.hostname = hostname;
        this.port     = port;
        _state        = ORTCPClientState.Connecting;
        _messages.Clear();
        _events.Clear();
        _client = new TcpClient();
        _client.BeginConnect(hostname,
                             port,
                             new AsyncCallback(ConnectCallback),
                             _client);
    }
Ejemplo n.º 5
0
    private void ReadData()
    {
        bool endOfStream = false;

        while (!endOfStream)
        {
            if (socketType == ORTCPSocketType.Text)
            {
                String response = null;
                try { response = _reader.ReadLine(); } catch (Exception e) { e.ToString(); }

                if (response != null)
                {
                    response = response.Replace(Environment.NewLine, "");
                    _events.Enqueue(ORTCPEventType.DataReceived);
                    _messages.Enqueue(response);
                }
                else
                {
                    endOfStream = true;
                }
            }
            else if (socketType == ORTCPSocketType.Binary)
            {
                byte[] bytes     = new byte[bufferSize];
                int    bytesRead = _stream.Read(bytes, 0, bufferSize);
                if (bytesRead == 0)
                {
                    endOfStream = true;
                }
                else
                {
                    _events.Enqueue(ORTCPEventType.DataReceived);
                    _packets.Enqueue(new ORSocketPacket(bytes, bytesRead));
                }
            }
        }

        _state = ORTCPClientState.Disconnected;
        _client.Close();
        _events.Enqueue(ORTCPEventType.Disconnected);
    }