Exemplo n.º 1
0
    public IEnumerator ConnectCoroutine(string _sIp, ushort _usPort, CONNECTION_MODE _eConnectionMode = CONNECTION_MODE.LINEAR)
    {
        m_sIp    = _sIp;
        m_usPort = _usPort;
        try {
            m_bConnected      = false;
            m_bSyncConnected  = false;
            m_bAsyncConnected = false;
            IPAddress oIp;
            if (IPAddress.TryParse(_sIp, out oIp) == false)
            {
                IPHostEntry hst = Dns.GetHostEntry(_sIp);
                _sIp = hst.AddressList[0].ToString();
            }
            //----------------------------------------------------------------
            //Connect asynchronous client
            m_oAsyncTCPSocket = new Socket(IPAddress.Parse(_sIp).AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            m_oAsyncTCPSocket.ReceiveTimeout = m_usConnectTimeout;
            m_oAsyncTCPSocket.SendTimeout    = m_usConnectTimeout;
            switch (_eConnectionMode)
            {
            case CONNECTION_MODE.PARALLEL:
                m_oAsyncTCPSocket.BeginConnect(new IPEndPoint(IPAddress.Parse(_sIp), _usPort), new AsyncCallback(ConnectCoroutineCallback), null);
                break;

            case CONNECTION_MODE.LINEAR:
            default:
                m_oAsyncTCPSocket.Connect(new IPEndPoint(IPAddress.Parse(_sIp), _usPort));
                break;
            }
#if !UNITY_ANDROID
            m_oAsyncTCPSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, m_usTimeout);
            m_oAsyncTCPSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, m_usTimeout);
            m_oAsyncTCPSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
#endif
            //----------------------------------------------------------------
            //Connect synchronous client
            m_oSyncTCPSocket = new Socket(IPAddress.Parse(_sIp).AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            m_oSyncTCPSocket.ReceiveTimeout = m_usConnectTimeout;
            m_oSyncTCPSocket.SendTimeout    = m_usConnectTimeout;
            switch (_eConnectionMode)
            {
            case CONNECTION_MODE.PARALLEL:
                m_oSyncTCPSocket.BeginConnect(new IPEndPoint(IPAddress.Parse(_sIp), _usPort), new AsyncCallback(ConnectCoroutineCallback), null);
                break;

            case CONNECTION_MODE.LINEAR:
            default:
                m_oSyncTCPSocket.Connect(new IPEndPoint(IPAddress.Parse(_sIp), _usPort));
                break;
            }
#if !UNITY_ANDROID
            m_oSyncTCPSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, m_usTimeout);
            m_oSyncTCPSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, m_usTimeout);
            m_oSyncTCPSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
#endif
            if (_eConnectionMode == CONNECTION_MODE.LINEAR)
            {
                m_bConnected = true;
            }
        } catch {
            m_bConnected      = false;
            m_bSyncConnected  = false;
            m_bAsyncConnected = false;
            Debug.Log("Error connection on ConnectCoroutine");
        }
        yield break;
    }
Exemplo n.º 2
0
 //------------------------------------------------------------------------
 /// <summary>Start connection to slave.</summary>
 /// <param name="_sIp">IP adress of modbus slave.</param>
 /// <param name="_usPort">Port number of modbus slave. Usually port 502 is used.</param>
 public void ConnectWithCoroutine(string _sIp, ushort _usPort, CONNECTION_MODE _eConnectionMode = CONNECTION_MODE.LINEAR)
 {
     m_sIp    = _sIp;
     m_usPort = _usPort;
     StartCoroutine(ConnectCoroutine(_sIp, _usPort, _eConnectionMode));
 }