static public int get_desc(IntPtr l)
 {
     try {
         FSocketStateChanged self = (FSocketStateChanged)checkSelf(l);
         pushValue(l, true);
         pushValue(l, self.desc);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int get_currentState(IntPtr l)
 {
     try {
         FSocketStateChanged self = (FSocketStateChanged)checkSelf(l);
         pushValue(l, true);
         pushEnum(l, (int)self.currentState);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_desc(IntPtr l)
 {
     try {
         FSocketStateChanged self = (FSocketStateChanged)checkSelf(l);
         System.String       v;
         checkType(l, 2, out v);
         self.desc = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_currentState(IntPtr l)
 {
     try {
         FSocketStateChanged self = (FSocketStateChanged)checkSelf(l);
         ESocketState        v;
         checkEnum(l, 2, out v);
         self.currentState = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int constructor(IntPtr l)
 {
     try {
         FSocketStateChanged o;
         ESocketState        a1;
         checkEnum(l, 2, out a1);
         System.String a2;
         checkType(l, 3, out a2);
         o = new FSocketStateChanged(a1, a2);
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Example #6
0
 private void OnConnected(System.IAsyncResult ia)
 {
     try
     {
         m_socket.EndConnect(ia);
         m_socketState           = ESocketState.ESS_Connected;
         m_socket.ReceiveTimeout = 10;
         m_socket.SendTimeout    = 3000;
         m_socket.SendBufferSize = 8 * 1024;
         m_socket.NoDelay        = true;
         connNoti = new FSocketStateChanged(m_socketState, "connected");
     }
     catch (System.Exception e)
     {
         m_socketState = ESocketState.ESS_ConnecttingFailed;
     }
 }
Example #7
0
    /// <summary>
    /// connect to server
    /// </summary>
    /// <param name="szIp">ip adress of server</param>
    /// <param name="port">port number of server</param>
    /// <returns>true if connected to server, otherwise false</returns>
    public bool Connect(string szIp, int port /*, System.Action<int> fn = null, bool bAutoRetry = true, System.Action<int> fnDisconnect = null*/)
    {
        bool          bSuccess         = false;
        string        newServerIp      = "";
        AddressFamily newAddressFamily = AddressFamily.InterNetwork;

        //convert ip address
        MobileDevicesInterface.ConvertIPAddress(szIp, port.ToString(), out newServerIp, out newAddressFamily);
        if (!string.IsNullOrEmpty(newServerIp))
        {
            szIp = newServerIp;
        }
        m_socket = new Socket(newAddressFamily, SocketType.Stream, ProtocolType.Tcp);
        //Debug.Log("Converted Socket AddressFamily :" + newAddressFamily.ToString() + "ServerIp:" + szIp + " port=" + port);

        try
        {
            m_socket.BeginConnect(szIp, port, OnConnected, null);
            m_ConnectStartTime = Time.realtimeSinceStartup;
            m_socketState      = ESocketState.ESS_Connectting;
            bSuccess           = true;
        }
        catch (System.Exception e)
        {
            string tip = string.Format("Connect {0}:{1} Error, {2}", szIp, port.ToString(), e.ToString());
            m_socketState = ESocketState.ESS_ConnecttingFailed;

            FSocketStateChanged state = new FSocketStateChanged(m_socketState, tip);
            if (null != onSocketStateChanged)
            {
                onSocketStateChanged.Invoke(state);
            }
            bSuccess = false;
        }

        return(bSuccess);
    }
Example #8
0
    void Update()
    {
        switch (m_socketState)
        {
        case ESocketState.ESS_Connectting:
            if (Time.realtimeSinceStartup - m_ConnectStartTime >= m_ConnectInternal)
            {
                m_socketState = ESocketState.ESS_ConnecttingFailed;
            }
            break;

        case ESocketState.ESS_ConnecttingFailed:
            if (m_socket != null)
            {
                Disconnect();
                connNoti = new FSocketStateChanged(ESocketState.ESS_ConnecttingFailed, "connection out of date");
                _onSocketStateChanged.Invoke(connNoti);
                connNoti = null;
            }
            break;

        case ESocketState.ESS_Connected:
            if (connNoti != null)
            {
                _onSocketStateChanged.Invoke(connNoti);
                connNoti = null;
            }

            if (sendNoti != null)
            {
                _onFailedSendMsg.Invoke(sendNoti);
                sendNoti = null;
            }

            if (m_socket != null)
            {
                if (m_socket.Connected == false)
                {
                    m_socketState = ESocketState.ESS_Disconnected;
                }
                else if (m_socket.Poll(100, SelectMode.SelectRead) && m_socket.Available == 0)
                {
                    m_socketState = ESocketState.ESS_Disconnected;
                }
            }
            break;

        case ESocketState.ESS_Disconnected:
            if (m_socket != null)
            {
                Disconnect();
                connNoti = new FSocketStateChanged(ESocketState.ESS_Disconnected, "Disconnected");
                _onSocketStateChanged.Invoke(connNoti);
                connNoti = null;
            }
            break;

        case ESocketState.ESS_Quit:
            break;
        }
    }