static public int RECV_BUFFER_SIZE = 10 * 1024 * 1024; //!< Size of the buffer for read operations // Use this for initialization public NetworkEyeConnection(string host, Int32 port) { m_state = SensoNetworkState.SENSO_DISCONNECTED; m_port = port; if (IPAddress.TryParse(host, out m_ip)) { doConnect(); } }
private void doConnect() { m_sock = new TcpClient(AddressFamily.InterNetwork); m_sock.NoDelay = true; if (m_state == SensoNetworkState.SENSO_DISCONNECTED) { var connectCB = new AsyncCallback(ProcessConnectResult); m_connectRes = m_sock.BeginConnect(m_ip, m_port, connectCB, null); m_state = SensoNetworkState.SENSO_CONNECTING; } }
public void Disconnect() { try { if (m_state == SensoNetworkState.SENSO_CONNECTING) { m_sock.EndConnect(m_connectRes); } else if (m_state == SensoNetworkState.SENSO_CONNECTED) { m_sock.Close(); } } catch (Exception ex) { Debug.Log("Disconnect error: " + ex.Message); } IsReceiving = false; m_stream = null; m_sock = null; m_state = SensoNetworkState.SENSO_DISCONNECTED; }
// The following method is called when each asynchronous operation completes. void ProcessConnectResult(IAsyncResult result) { try { m_sock.EndConnect(result); } catch (Exception ex) { // eat the exception } if (m_sock.Connected) { Debug.Log("Connected to Eye Server"); m_sock.SendTimeout = 100; m_stream = m_sock.GetStream(); m_state = SensoNetworkState.SENSO_CONNECTED; } else { Debug.Log("Unable to connect to Eye Server"); m_stream = null; m_state = SensoNetworkState.SENSO_DISCONNECTED; } }