Beispiel #1
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 #2
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 #3
0
        public bool handler()
        {
            //if (!NetworkDeviceWatcher.isNetworkAvailable())
            //{
            //    return false;
            //}
            SessionPack sessionPack = SessionPackMgr.getFirstNotSended();

            if (sessionPack == null)
            {
                return(false);
            }
            if (SessionPackMgr.isAuthing())
            {
                return(false);
            }
            if (!Connector.isValidforPack(sessionPack))
            {
                return(false);
            }
            if (!this.checkSessionKey(ref sessionPack))
            {
                return(false);
            }
            if (!this.preProcessPack(sessionPack))
            {
                Log.e("Network", "Sender: Failed to PreProcess pack.");
                return(false);
            }
            if (!this.doSendPack(sessionPack))
            {
                return(false);
            }
            return(true);
        }
Beispiel #4
0
        public static bool sendPack(SessionPack sessionPack)
        {
            if (mSocketClient == null)
            {
                return(false);
            }
            NetPack.Request request = new NetPack.Request(sessionPack.mSeqID, sessionPack.mCmdID, sessionPack.mCacheBodyBuffer);

            // NetPack.Request request = new NetPack.Request(1, 701, Util.HexStringToByte("bf8e7f1603181100000000000000000000000000000000000000bd05b007b00787010d0000012b0000024c000002004b8116d636cdcf7c1eac04e1427a78c784acc68ed332c2475ae778918ef43170db0d46c3c7f7bacf1c14f2385438cb8e066083dd24d355d93cea6bf159ea20a309be99a125f26f8e8726504e8616533924faebd1ef985bf05876ac59513b1eca83696d87b1f7519754176fbf12e9f51e2e913469884f48419003e747416c7985d12abfad19b15bc3048322aea2ad801a2f8390262aab19a73bcd34199e0993c1659bad1d4859cb137892c179d536823e4418a4e8a06835a143b0416dda924e14f91a90567db32c0731f94844515779080789092025cdfbe12565a306b546c3daa2996e1670aa65456292c09d29f541cd2fe2fb8d9ad155f8b1824fad1037e7d058a1d36a69a8236f0230400316e98e314916fb197571e349a058a1d0ba7f36285a5aae4b54d4af84cd4579bfe1ceaa10a0d55875be281a56f98cb8fb59a51d58ad39d20f8513a8a14728409e9113cca6a90be4074b2f35000b45df07dc5d13fdc53a10793feb45f1ac1cbb56c3b744fe8dc7f1d8f2decd8886d324ddee3d58e4e5510042e4b319b93deae768cdd8d7f39fef00656e1f665e80e281ffe4f424a8d466309824a8479edc67693f8e769e5c97d7778c63a95507d60a901c1890fa9a1c6f5d7cb4fda44f454e81b3fc60c7c7582e2f888cde6f41a8c657566f8dbcdee4e97ba6f8681448e3c93308837cd5c6597e20d03757f5a24a5ff6bc4032566c14c753cef9f0b17e70fb1f96e75423b325badf3fdfff4c9d554b4ed29ff21c7545150190aa44b2a60c74634081c17f9a3ce36d299b7b04dc0c09f85ca71b61be02baee13841992b284ca5cd8fe0e6608d0158fcdb3b60706cef28350fd5998fddd19b9b4ccb661c7f4f5a9c80906dee68cb303da3519b5a1b588a6bc47af60cdf6f38cf28bbb1d0572bd23d6250fc15a64c39aabf50463b2e9d9b3d3059bff1466f137ffb62ebee8d29fe85f38d923b17f0c259e0dbb75c1a61f57f3e9006d6a9ae8159398c1883ab421174ebe646cce7952a65d09c1285288bb6132efc997846b6879a47448785eddac3e7ad705b020325b5faa19daeab8544e0b1a149b657a5f533d6ba31164c3a0203d6414496b098e77011602368b585527c7badc5f27df367bcc043718d3e613b5a0cdbe90eccb271f3d7bfc2393575df718c40b2d30263558a18edda94d12e50c770920223fe0b54c7b43354f052104db5364d1d825035d5da36a015b86fdf26301d0d214d717d14c9ff499a26e4e31976ff5c0b28c841ea0a42762638f245c12774c0f59b8a659f0c1a425aa1fbecfeb0648afc020fbf078a268a50c894b9674f6de085cfc2e"));

            byte[] outBuf = null;
            request.serializeToBuffer(ref outBuf);
            //FlowControl.onSend(sessionPack.mCmdID, sessionPack.mCmdUri, (uint) outBuf.Length);
            SocketError error = mSocketClient.Send(outBuf);

            // string a = Util.byteToHexStr(outBuf);
            sessionPack.timeInSent = Util.getNowMilliseconds();
            if (error != ((SocketError)((int)SocketError.Success)))
            {
                Log.e("Network", "Socket Send error : " + error.ToString());
                if ((error == ((SocketError)((int)SocketError.ConnectionAborted))) || (error == ((SocketError)((int)SocketError.ConnectionReset))))
                {
                    close();
                }
                return(false);
            }
            restartKeepAlivePoint(0xea60);
            return(true);
        }
Beispiel #5
0
 public static void onError(SessionPack sessionPack, PackResult ret)
 {
     sessionPack.mCacheBodyBuffer = null;
     sessionPack.mResponseBuffer  = null;
     sessionPack.onError(ret);
     sessionPack.timeInNetCompleted = Util.getNowMilliseconds();
 }
Beispiel #6
0
 public static bool checkKeepAlive()
 {
     if (mNextHelloTimestamp == 0.0)
     {
         return(false);
     }
     if (SessionPack.getNow() < mNextHelloTimestamp)
     {
         return(false);
     }
     if (!SessionPackMgr.getAccount().isValid() || !SessionPackMgr.isValidSessionKey())
     {
         stopKeepAlivePoint();
         return(false);
     }
     if (mFirstKeepAlivePointAfterConnected == mNextHelloTimestamp)
     {
         EventCenter.postEvent(EventConst.ON_NET_MM_NET_NOOP, null, null);
     }
     else
     {
         EventCenter.postEvent(EventConst.ON_NET_MM_NET_NOOP, null, null);
     }
     restartKeepAlivePoint(0xea60);
     return(true);
 }
Beispiel #7
0
        public static object onParserNewRegPack(SessionPack sessionPack)
        {
            NewRegResponse response = NewRegResponse.ParseFrom(sessionPack.mResponseBuffer);

            if (response == null)
            {
                Log.e("Network", "new reg parse failed. ");
                return(null);
            }
            int      uin = (int)response.Uin;
            RetConst ret = (RetConst)response.BaseResponse.Ret;

            Log.d("Network", string.Concat(new object[] { "new reg parsed success. uin= ", uin, "ret =", ret }));
            NewRegRequest mRequestObject = sessionPack.mRequestObject as NewRegRequest;
            uint          regMode        = mRequestObject.RegMode;

            if ((uin != 0) && (ret == RetConst.MM_OK))
            {
                Log.i("Network", "new reg PASS");
                SessionPackMgr.setSessionKey(Util.StringToByteArray(response.SessionKey));
                SessionPackMgr.getAccount().setUin(uin);
                SessionPackMgr.setAuthStatus(2);
                return(response);
            }
            Log.e("Network", "new reg failed, ret = " + ret);
            SessionPackMgr.setSessionKey(null);
            SessionPackMgr.getAccount().reset();
            return(response);
        }
Beispiel #8
0
 public static int clearCompletedOrCancel()
 {
     lock (mSessionPackLock)
     {
         if (mSessionPackQueue.isEmpty())
         {
             return(0);
         }
         int      num   = 0;
         ListNode node  = mSessionPackQueue._head._next;
         ListNode node2 = mSessionPackQueue._head;
         while (node != null)
         {
             SessionPack sessionPack = node._obj as SessionPack;
             if ((sessionPack.mSendStatus >= 5) || sessionPack.mCanceled)
             {
                 if ((sessionPack.mSendStatus < 5) && sessionPack.mCanceled)
                 {
                     OnCallback.onError(sessionPack, PackResult.BEEN_CANCELLED);
                 }
                 node2._next = node._next;
                 mSessionPackQueue._size--;
                 num++;
             }
             else
             {
                 node2 = node;
             }
             node = node._next;
         }
         mSessionPackQueue._tail = node2;
         return(num);
     }
 }
Beispiel #9
0
        public static object onParserRegPack(SessionPack sessionPack)
        {
            RegResponse response = RegResponse.ParseFrom(sessionPack.mResponseBuffer);

            if (response == null)
            {
                Log.e("Network", "register parse failed. ");
                return(null);
            }
            Log.d("Network", "register parsed success. ");
            int      uin = (int)response.Uin;
            RetConst ret = (RetConst)response.BaseResponse.Ret;

            if ((uin != 0) && (ret == RetConst.MM_OK))
            {
                Log.i("Network", "register PASS");
                SessionPackMgr.setSessionKey(response.SessionKey.ToByteArray());
                SessionPackMgr.getAccount().setUin(uin);
                SessionPackMgr.setAuthStatus(2);
                return(response);
            }
            Log.e("Network", "register Failed, ret = " + ret);
            SessionPackMgr.setSessionKey(null);
            SessionPackMgr.getAccount().reset();
            return(response);
        }
Beispiel #10
0
        public static bool isValidforPack(SessionPack sessionPack)
        {
            if (!LongConnector.isAvailable())
            {
                sessionPack.mConnectMode     = 2;
                sessionPack.mCacheBodyBuffer = null;
            }
            int mConnectMode = sessionPack.mConnectMode;

            if (mConnectMode == 1)
            {
                return(LongConnector.isValid());
            }
            if (mConnectMode != 2)
            {
                return(false);
            }
            if (string.IsNullOrEmpty(sessionPack.mCmdUri))
            {
                Log.e("Network", "Cannot send pack via http without uri,  will been cancelled. seq= " + sessionPack.mSeqID);
                sessionPack.mCanceled = true;
                return(false);
            }
            return(ShortConnector.isValid());
        }
Beispiel #11
0
 public static void putToTail(SessionPack sessionPack)
 {
     lock (mSessionPackLock)
     {
         mSessionPackQueue.putToTail(sessionPack, sessionPack.mSeqID);
     }
 }
Beispiel #12
0
 public bool sendPack(SessionPack sessionPack)
 {
     if (sessionPack == null)
     {
         Log.e("Network", "CMD to Network: null pack");
         return(false);
     }
     Log.i("Network", string.Concat(new object[] { "CMD to Network: cmd=", (CmdConst)sessionPack.mCmdID, " seq=", sessionPack.mSeqID }));
     mRetryAuthLeft = 3;
     Connector.onPrepareSend(false);
     LongConnector.setEnable(true);
     sessionPack.timeInCmd = Util.getNowMilliseconds();
     if ((sessionPack.mCmdID == 1) && SessionPackMgr.isAuthing())
     {
         Log.e("Network", "isAuthing, don't send auth request");
         return(false);
     }
     if (sessionPack.mCmdID == 1 || sessionPack.mCmdID == 0xb2)
     {
         SessionPackMgr.putToHead(sessionPack);
     }
     else
     {
         SessionPackMgr.putToTail(sessionPack);
     }
     NetHandler.wakeUp();
     return(true);
 }
Beispiel #13
0
        private bool checkSessionKey(ref SessionPack sessionPack)
        {
            if (sessionPack.mCmdID == 1 || sessionPack.mCmdID == 0xb2)
            {
                AuthPack.updateAccountInfoFromAuth(sessionPack.mRequestObject);

                return(true);
            }

            if (sessionPack.mCmdID == 0x20)
            {
                AuthPack.updateAccountInfoFromNewReg(sessionPack.mRequestObject);
                return(true);
            }
            if (sessionPack.mCmdID == 0x1f)
            {
                AuthPack.updateAccountInfoFromReg(sessionPack.mRequestObject);
                return(true);
            }
            if (sessionPack.mCmdID == 232 || sessionPack.mCmdID == 233)
            {
                return(true);
            }
            if (!this.needSessionKey(sessionPack))
            {
                return(true);
            }
            if (SessionPackMgr.isValidSessionKey())
            {
                return(true);
            }
            this.checkTryAutoAuth();
            return(false);
        }
Beispiel #14
0
        public static void checkSendTimeout()
        {
            SessionPack sessionPack = checkSendTimeoutEx();

            if (sessionPack != null)
            {
                Sender.getInstance().onSendTimeout(sessionPack);
            }
        }
Beispiel #15
0
 public static List <RawPackBody> receiveBodyList()
 {
     mListRecvPack.Clear();
     LongConnector.receive(mListRecvPack);
     ShortConnector.receiveList(mListRecvPack);
     if (mListRecvPack.Count > 0)
     {
         mLastActiveTimestamp = SessionPack.getNow();
     }
     return(mListRecvPack);
 }
Beispiel #16
0
        public static void sendHelloPack()
        {
            Log.i("Network", "send hello pack... ");
            SessionPack sessionPack = new SessionPack {
                mCmdID           = 6,
                mCacheBodyBuffer = new byte[0],
                mRetryLeft       = 1
            };

            SessionPackMgr.putToTail(sessionPack);
        }
Beispiel #17
0
 public static void onAllError(PackResult ret)
 {
     Log.e("Network", "onAllError");
     SessionPackMgr.cleanAllTimeoutPoint(3);
     SessionPackMgr.markAllToNotSended(3);
     for (SessionPack pack = SessionPackMgr.getFirstNotSended(); pack != null; pack = SessionPackMgr.getFirstNotSended())
     {
         pack.mSendStatus = 6;
         onError(pack, ret);
     }
 }
Beispiel #18
0
 public void onSendPackFailed(bool needReconnect, SessionPack sessionPack)
 {
     Log.e("Network", string.Concat(new object[] { "sender:onSendPackFailed, seq =", sessionPack.mSeqID, ", cmd =", sessionPack.mCmdID }));
     SessionPackMgr.cleanAllTimeoutPoint(sessionPack.mConnectMode);
     SessionPackMgr.markAllToNotSended(sessionPack.mConnectMode);
     if (SessionPackMgr.getAuthStatus() == 1)
     {
         SessionPackMgr.setAuthStatus(0);
     }
     Connector.onSendFailed(needReconnect, sessionPack.mConnectMode);
 }
Beispiel #19
0
        public static bool sendPack(SessionPack sessionPack)
        {
            mLastActiveTimestamp = SessionPack.getNow();
            switch (sessionPack.mConnectMode)
            {
            case 1:
                return(LongConnector.sendPack(sessionPack));

            case 2:
                return(ShortConnector.sendPack(sessionPack));
            }
            return(true);
        }
Beispiel #20
0
 public static void cancelAllPacket()
 {
     lock (mSessionPackLock)
     {
         if (!mSessionPackQueue.isEmpty())
         {
             for (ListNode node = mSessionPackQueue._head._next; node != null; node = node._next)
             {
                 SessionPack pack = node._obj as SessionPack;
                 pack.mCanceled = true;
             }
         }
     }
 }
Beispiel #21
0
        public static int getIdleTime()
        {
            double num = SessionPack.getNow();
            double mLastActiveTimestamp = Connector.mLastActiveTimestamp;
            int    num3 = (int)((num - mLastActiveTimestamp) / 100.0);

            if (num3 > 0x3e8)
            {
                num3 = 0x3e8;
            }
            if (num3 < 10)
            {
                num3 = 10;
            }
            return(num3);
        }
Beispiel #22
0
 public static void cleanAllTimeoutPoint(int connMode)
 {
     lock (mSessionPackLock)
     {
         if (!mSessionPackQueue.isEmpty())
         {
             for (ListNode node = mSessionPackQueue._head._next; node != null; node = node._next)
             {
                 SessionPack pack = node._obj as SessionPack;
                 if ((pack.mConnectMode & connMode) != 0)
                 {
                     pack.cancelTimeoutPoint();
                 }
             }
         }
     }
 }
Beispiel #23
0
 public static void getPackWithHttpClient(Func <SessionPack, bool> predicate)
 {
     lock (mSessionPackLock)
     {
         if (!mSessionPackQueue.isEmpty())
         {
             for (ListNode node = mSessionPackQueue._head._next; node != null; node = node._next)
             {
                 SessionPack arg = node._obj as SessionPack;
                 if (arg.mHttpClient != null)
                 {
                     predicate(arg);
                 }
             }
         }
     }
 }
Beispiel #24
0
 public static void markAllToNotSended(int connMode)
 {
     lock (mSessionPackLock)
     {
         if (!mSessionPackQueue.isEmpty())
         {
             for (ListNode node = mSessionPackQueue._head._next; node != null; node = node._next)
             {
                 SessionPack pack = node._obj as SessionPack;
                 if ((pack.mSendStatus <= 3) && ((pack.mConnectMode & connMode) != 0))
                 {
                     pack.mSendStatus = 0;
                 }
             }
         }
     }
 }
Beispiel #25
0
        public static object onParserBindOpMobileForReg(SessionPack sessionPack)
        {
            BindOpMobileResponse response = BindOpMobileResponse.ParseFrom(sessionPack.mResponseBuffer);

            if (response == null)
            {
                Log.e("Network", "BindOpMobileResponse parse failed. ");
                return(null);
            }
            if (response.BaseResponse.Ret == -301)
            {
                Log.i("Network", "Need to redirect IDC for BindOpMobileForReg...");
                HostService.updateAuthIDCHost(response.NewHostList);
                HostService.updateAuthBuiltinIP(response.BuiltinIPList);
                Connector.close();
            }
            return(response);
        }
Beispiel #26
0
 public static void closeAllHttpClientInPacks()
 {
     lock (mSessionPackLock)
     {
         if (!mSessionPackQueue.isEmpty())
         {
             for (ListNode node = mSessionPackQueue._head._next; node != null; node = node._next)
             {
                 SessionPack pack = node._obj as SessionPack;
                 if (pack.mHttpClient != null)
                 {
                     pack.mHttpClient.close();
                     pack.mHttpClient = null;
                 }
             }
         }
     }
 }
Beispiel #27
0
 private static SessionPack checkSendTimeoutEx()
 {
     lock (mSessionPackLock)
     {
         if (!mSessionPackQueue.isEmpty())
         {
             for (ListNode node = mSessionPackQueue._head._next; node != null; node = node._next)
             {
                 SessionPack pack = node._obj as SessionPack;
                 if (pack.isSendTimeout())
                 {
                     return(pack);
                 }
             }
         }
         return(null);
     }
 }
Beispiel #28
0
 public static SessionPack getFirstNotSended()
 {
     lock (mSessionPackLock)
     {
         if (!mSessionPackQueue.isEmpty())
         {
             for (ListNode node = mSessionPackQueue._head._next; node != null; node = node._next)
             {
                 SessionPack pack = node._obj as SessionPack;
                 if ((pack.mSendStatus == 0) && !pack.mCanceled)
                 {
                     return(pack);
                 }
             }
         }
         return(null);
     }
 }
Beispiel #29
0
        public static SessionPack makeAutoAuthPack(int scene)
        {
            NewAuthRequest request = makeAuthRequest(scene);

            if (request == null)
            {
                return(null);
            }
            SessionPack pack = new SessionPack
            {
                mCmdID         = 0xb2,
                mRequestObject = request,
                mCmdUri        = "/cgi-bin/micromsg-bin/newauth",
                mNeedAutoAuth  = true
            };

            pack.mProcRequestToByteArray += new RequestToByteArrayDelegate(AuthPack.authRequestToByteArray);
            pack.mCompleted += new SessionPackCompletedDelegate(AuthPack.onAutoAuthCompleted);
            return(pack);
        }
Beispiel #30
0
 public static void checkPackTimeout()
 {
     lock (mSessionPackLock)
     {
         if (!mSessionPackQueue.isEmpty())
         {
             ListNode node = mSessionPackQueue._head._next;
             double   now  = Util.getNowMilliseconds();
             while (node != null)
             {
                 SessionPack pack = node._obj as SessionPack;
                 if ((pack != null) && (pack.mSendStatus <= 3))
                 {
                     pack.checkPackTimeout(now);
                 }
                 node = node._next;
             }
         }
     }
 }