Beispiel #1
0
 public HostService.HostInfo getAvailableHost()
 {
     //if (HostService.mDebugMode)
     //{
     //    return HostService.mDebugHost[this.mConnectType];
     //}
     if (this.mCurrentHost == null)
     {
         this.mCurrentHost = this.getFirstAvailableItem(this.mDnsList);
         this.mCurrentType = 0;
         if (this.mCurrentHost == null)
         {
             Log.i("Network", "dns hosts are not available all, try builtin host...");
             this.mCurrentHost = this.getFirstAvailableItem(this.mBuiltInList);
             this.mCurrentType = 1;
         }
         if (this.mCurrentHost == null)
         {
             Log.i("Network", "builtin hosts are not available all, try backup host...");
             this.mCurrentHost = this.getFirstAvailableItem(this.mBakList);
             this.mCurrentType = 2;
         }
         if (this.mCurrentHost == null)
         {
             Log.i("Network", "all hosts are not available , try dns again...");
             this.resetAvailableStatus(this.mDnsList, this.getRetryArray()[0]);
             this.resetAvailableStatus(this.mBuiltInList, this.getRetryArray()[1]);
             this.resetAvailableStatus(this.mBakList, this.getRetryArray()[2]);
             this.mCurrentHost = this.mDnsList[0];
             this.mCurrentType = 0;
         }
     }
     Log.d("Network", string.Concat(new object[] { "get available host = ", this.mCurrentHost.mHost, ", port = ", this.mCurrentHost.mPort, ", retryLeft = ", this.mCurrentHost.mRetryLeft }));
     return(this.mCurrentHost);
 }
Beispiel #2
0
        public static bool sendPack(SessionPack sessionPack)
        {
            HostService.HostInfo info = HostService.mShortConnHosts.getAvailableHost();
            string     cmdUri         = string.Concat(new object[] { "http://", info.mHost, ":", info.mPort, sessionPack.mCmdUri });
            string     mHost          = info.mHost;
            HttpClient client         = new HttpClient(cmdUri, sessionPack.mSeqID, mHost);

            sessionPack.mHttpClient = client;
            //FlowControl.onSend(sessionPack.mCmdID, sessionPack.mCmdUri, (uint) sessionPack.mCacheBodyBuffer.Length);

            //if (sessionPack.mCmdUri == "/cgi-bin/micromsg-bin/getchatroommemberdetail") {



            //    byte[] data = new byte[0];
            //    using (WebClient _client = new WebClient())
            //    {

            //        data = _client.UploadData(cmdUri, "POST", sessionPack.mCacheBodyBuffer);

            //        Log.e("Network", "  send body = " + Util.byteToHexStr(sessionPack.mCacheBodyBuffer) + " revce length \n" + data.Length+" data \n"+Util.byteToHexStr(data)+" \nKey "+ Util.byteToHexStr( SessionPackMgr.getSessionKeyEx()));
            //    }
            //    // GetChatroomMemberDetailResponse response = GetChatroomMemberDetailResponse.ParseFrom(data);


            //}
            bool flag = client.send(sessionPack.mCacheBodyBuffer, sessionPack.mCacheBodyBuffer.Length, null, sessionPack.mSeqID);

            sessionPack.timeInSent = Util.getNowMilliseconds();
            return(flag);
        }
Beispiel #3
0
        private static bool checkReadyEx()
        {
            if (isValid())
            {
                if ((mLastConnectedTimestamp != 0.0) && (SessionPack.getNow() > (mLastConnectedTimestamp + 30000.0)))
                {
                    EventCenter.postEvent(EventConst.ON_NET_LONGCONN_KEEPALIVE, null, null);
                    mLastConnectedTimestamp = 0.0;
                }
                return(true);
            }
            if (mReconnectLeft <= 0)
            {
                if (mReconnectLeft == 0)
                {
                    Log.e("Network", "no chance to try connect.");
                    mReconnectLeft = -1;
                }
                return(false);
            }
            double num  = SessionPack.getNow();
            double num2 = mLastConnTimestamp + getSleepTime(mReconnectLeft);

            if ((mReconnectLeft == 3) || (num >= num2))
            {
                mLastConnTimestamp = num;
                mReconnectLeft--;
                Connector.setConnectorStatus(ConntectorStatus.Connecting);
                mSocketClient = new SocketClient();
                HostService.HostInfo info  = HostService.mLongConnHosts.getAvailableHost();
                SocketError          error = mSocketClient.Connect(info.mHost, info.mPort);
                if (error == ((SocketError)((int)SocketError.Success)))
                {
                    Log.i("Network", "connect success.");
                    mLastConnectedTimestamp = num;
                    //NetworkDeviceWatcher.onConnected();
                    mReconnectLeft = 2;
                    mFirstKeepAlivePointAfterConnected = restartKeepAlivePoint(0xbb8);
                    mConnFailedCount = 0;
                    Connector.setConnectorStatus(ConntectorStatus.Connected);
                    return(true);
                }
                if (error == ((SocketError)((int)SocketError.HostNotFound)))
                {
                    HostService.setDNSFailed();
                }
                Log.e("Network", "connect error:" + error.ToString());
                close();
                mConnFailedCount++;
                if (mConnFailedCount >= 3)
                {
                    Log.i("Network", "longconnect is not available as connect failed(3/3).");
                    setAvailable(false);
                    mConnFailedCount = 0;
                }
                HostService.mLongConnHosts.onCurrentHostFailed();
                Connector.setConnectorStatus(ConntectorStatus.Disconnect);
            }
            return(false);
        }
Beispiel #4
0
 public void onCurrentHostFailed()
 {
     if ((this.mCurrentHost != null) && !HostService.mDebugMode)
     {
         this.mCurrentHost.mRetryLeft--;
         if (this.mCurrentHost.mRetryLeft <= 0)
         {
             Log.e("Network", string.Concat(new object[] { "set current host not available , host = ", this.mCurrentHost.mHost, ", port= ", this.mCurrentHost.mPort, ", type=", this.mCurrentType }));
             this.mCurrentHost.mRetryLeft = 0;
             this.mCurrentHost            = null;
         }
     }
 }
Beispiel #5
0
 private void addHost(string host, List <HostService.HostInfo> listHost, int retry, int type)
 {
     if (listHost.Count >= 0x12)
     {
         Log.e("Network", "add host too much , ignored host = " + host);
     }
     else if ((from item in listHost
               where item.mHost == host
               select item).Count <HostService.HostInfo>() != 0)
     {
         Log.e("Network", "host exist already , ignored host = " + host);
     }
     else
     {
         foreach (int num in this.getPortMap())
         {
             HostService.HostInfo info = new HostService.HostInfo(host, num, retry);
             listHost.Add(info);
         }
     }
 }
Beispiel #6
0
 public void cleanDnsHost()
 {
     this.mDnsList.Clear();
     this.mCurrentHost = null;
 }