Esempio n. 1
0
    /// <summary>
    /// 主动关闭服务器连接
    /// </summary>
    /// <param name="clearPkgList"></param>
    public void DisConnect(bool clearPkgList = true)
    {
        Log.info("DisConnect");
        m_netState = NetState.Disconnected;

        if (null != m_TgcpHandle)
        {
            m_TgcpHandle.CloseConnection();
            m_TgcpHandle.Fini();
            m_TgcpHandle.Destroy();
            m_TgcpHandle = null;
        }

        if (null != m_timeoutOperation)
        {
            m_timeoutOperation.cancel();
            m_timeoutOperation = null;
        }

        if (clearPkgList)
        {
            Log.info("客户端主动断开,清除m_RecvQueue", "NetworkConnect");
            if (null != m_RecvQueue)
            {
                m_RecvQueue.Clear();
            }
            Log.info("客户端主动断开,清除未发送完的回包", "NetworkConnect");
            if (null != m_pkgList)
            {
                m_pkgList.Clear();
            }
        }
        //         else {
        //             Log.info("后台主动断开", "NetworkConnect");
        //         }
        //TODO 通知隐藏tip
        //EventManager.Send(EventType_Net.TGCP_DisconnectedByManual_HideTip);
    }
Esempio n. 2
0
    //连接
    private int ConnectInternal()
    {
        int    ret        = 0;
        int    iSerivceID = 123;
        int    iBuffLen   = 1024 * 1024;
        string appid      = "appId123456";

        eAccountType   AccountType = eAccountType.TGCP_ACCOUNT_TYPE_PC_OPENID;
        eEncryptMethod iEncMethod  = eEncryptMethod.TGCP_ENCRYPT_METHOD_AES;
        eKeyMaking     iKeyMode    = eKeyMaking.TGCP_KEY_MAKING_INSVR;

//        if (this.m_loginInfo == null)
//        {
//#if !UNITY_EDITOR
//            m_loginInfo = MSDK.Instance.getLoginRecord();
//            GlobalData.system.SaveLoginRetInfo(m_loginInfo);

//            appid = (PlatformType.Weixin == m_loginInfo.platform) ? NetConfig.wx_appid : NetConfig.qq_appid;
//#endif
//        }

        if (null != m_TgcpHandle)
        {
            Log.info("有旧的tcp handler", "NetConnector.connectInternal");
        }
        m_TgcpHandle = new TGCPAPI();

#if UNITY_STANDALONE_WIN
        m_TgcpHandle.WSAStartUp(2, 0);
#endif

        //switch (this.m_loginInfo.platform)
        //{
        //    case PlatformType.Weixin:
        //        AccountType = eAccountType.TGCP_ACCOUNT_TYPE_WX_OPENID;
        //        break;
        //    case PlatformType.QQ:
        //        AccountType = eAccountType.TGCP_ACCOUNT_TYPE_QQ_OPENID;
        //        break;
        //    case PlatformType.Guest:
        //        AccountType = eAccountType.TGCP_ACCOUNT_TYPE_IOS_GUEST;
        //        break;
        //    default:
        //        Log.info("AccountType 末赋值");
        //        break;
        //}

        Log.info("appid: " + appid);
        Log.info("openid: " + m_openid);
        Log.info("accesstoken: " + "");
        Log.info("AccountType: " + AccountType);
        Log.info("iEncMethod: " + iEncMethod);
        Log.info("iKeyMode: " + iKeyMode);
        Log.info("iSerivceID: " + iSerivceID);

        //if (string.IsNullOrEmpty(m_loginInfo.openid))
        //{
        //    Log.info("openid为空,须重新登录");
        //    //openid为空操作待补
        //    OnOpenIdIsEmptyHandler();
        //    return ret;
        //}
        // 发送更新鉴权信息
        //EventManager.Send(EventType_DataReport.QQ_REPORT_UPDATE_AUTH_INFO, new object[] { accesstoken, openid });

        byte[] byteid    = TDRTools.strToByte(appid);
        byte[] bytetoken = TDRTools.strToByte("access tokey");

        ret = m_TgcpHandle.create_and_init_string(iSerivceID, byteid, byteid.Length, iBuffLen, AccountType, m_openid, bytetoken, bytetoken.Length);
        if (0 != ret)
        {
            Log.info("m_TgcpHandle.create_and_init_string failed: " + ret);
            return(ret);
        }

        ret = m_TgcpHandle.set_security_info(iEncMethod, iKeyMode, "");
        if (0 != ret)
        {
            Log.info("m_TgcpHandle.set_security_info failed: " + ret);
            return(ret);
        }

        ret = m_TgcpHandle.set_refresh_token_expire(0);
        if (0 != ret)
        {
            Log.info("m_TgcpHandle.set_refresh_token_expire failed: " + ret);
            return(ret);
        }

        // #if UNITY_EDITOR
        //         ret = m_TgcpHandle.set_authtype(eAuthType.TGCP_AUTH_NONE);
        // #endif
        //暂时都关闭鉴权
        ret = m_TgcpHandle.set_authtype(eAuthType.TGCP_AUTH_NONE);

        Log.info("m_TgcpHandle init success");

        try
        {
            Log.info("ip: " + m_ipAddress);
            Log.info("port: " + GlobalData.system.serverData.port);
            ret = AsynConnectServer(m_ipAddress, m_port);
            if (0 != ret)
            {
                Log.infoWarning("Could not connect!");
                NotifyDisconnected((TGCP_ERROR)ret);
            }
            Log.info("Net_initConned");
        }
        catch (Exception ex)
        {
            Log.info(ex.ToString());
        }

        return(ret);
    }