Ejemplo n.º 1
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);
     }
 }
Ejemplo n.º 2
0
        private void onReceivedResponsePack(byte[] body, int seq, int cmd, string cmdUri)
        {
            Log.i("Network", string.Concat(new object[] { "receive a packet , cmd = ", (CmdConst)cmd, ",seq = ", seq, " , len = ", body.Length }));
            SessionPack sessionPack = SessionPackMgr.getSessionPackBySeq(seq);

            if (sessionPack == null)
            {
                //FlowControl.onReceive(cmd, "", (uint) body.Length);
                this.onReceivedNotify(seq, cmd, body);
            }
            else
            {
                sessionPack.timeInRecvEnd = Util.getNowMilliseconds();
                sessionPack.cancelTimeoutPoint();
                sessionPack.mSendStatus = 4;
                if (sessionPack.mCmdID == 6)
                {
                    sessionPack.mSendStatus = 5;
                }
                else if (sessionPack.mCmdID == 0x19)
                {
                    sessionPack.mSendStatus     = 5;
                    sessionPack.mResponseBuffer = body;
                    OnCallback.onSuccess(sessionPack);
                }
                else
                {
                    if (sessionPack.mCmdID == 1 || sessionPack.mCmdID == 0xb2)
                    {
                        SessionPackMgr.setAuthStatus(0);
                    }
                    if (sessionPack.mCanceled)
                    {
                        Log.i("Network", "receive a packet been cancelled.");
                    }
                    else
                    {
                        byte[] key = sessionPack.getSessionKey(false);
                        if (key == null)
                        {
                            Log.e("Network", "recv a packet without valid key, resend later.");
                            sessionPack.mCacheBodyBuffer = null;
                            sessionPack.mSendStatus      = 0;
                        }
                        else
                        {
                            byte[] serverId      = null;
                            byte[] decryptedData = null;
                            int    ret           = 0;
                            if (!MMPack.DecodePack(ref decryptedData, body, key, ref serverId, ref ret))
                            {
                                Log.e("Network", string.Concat(new object[] { "unpack error! cmd=", (CmdConst)cmd, " seq=", seq }));
                                Connector.close();
                                if (ret == -13)
                                {
                                    Log.e("Network", "session timeout");
                                    SessionPackMgr.setSessionKey(null);
                                }
                                if (sessionPack.mRetryLeft > 0)
                                {
                                    sessionPack.mCacheBodyBuffer = null;
                                    sessionPack.cancelTimeoutPoint();
                                    sessionPack.mSendStatus = 0;
                                }
                                else
                                {
                                    sessionPack.mSendStatus = 6;
                                    OnCallback.onError(sessionPack, PackResult.UNPACK_ERROR);
                                }
                            }
                            else
                            {
                                sessionPack.mSendStatus     = 5;
                                sessionPack.mResponseBuffer = decryptedData;
                                // string a = Util.byteToHexStr(decryptedData);
                                object responseObject = null;
                                if (!AuthPack.preProcessAuthPack(sessionPack, ref responseObject))
                                {
                                    sessionPack.mSendStatus = 6;
                                    OnCallback.onAllError(PackResult.AUTH_ERROR);
                                    Connector.onSendFailed(false, 3);
                                }
                                else
                                {
                                    this.updateCookie(sessionPack.mCmdID, ret, serverId);
                                    if (responseObject != null)
                                    {
                                        sessionPack.mResponseObject = responseObject;
                                    }
                                    else
                                    {
                                        try
                                        {
                                            sessionPack.mResponseObject = sessionPack.onResponseParser();
                                        }
                                        catch (Exception exception)
                                        {
                                            Log.e("Network", "onResponseParser failed," + exception.ToString());
                                            OnCallback.onError(sessionPack, PackResult.PARSER_ERROR);
                                            return;
                                        }
                                        if (sessionPack.mResponseObject == null)
                                        {
                                            Log.e("Network", "onResponseParser failed");
                                            OnCallback.onError(sessionPack, PackResult.PARSER_ERROR);
                                            return;
                                        }
                                    }
                                    OnCallback.onSuccess(sessionPack);
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private bool preProcessPack(SessionPack sessionPack)
        {
            if (sessionPack.mRetryLeft <= 0)
            {
                Log.e("Network", "sender: retryLeft =0,cmd =:" + ((CmdConst)sessionPack.mCmdID));
                sessionPack.mSendStatus = 6;
                OnCallback.onError(sessionPack, PackResult.RETRY_LIMIT);
                return(false);
            }
            if (sessionPack.mCanceled)
            {
                Log.e("Network", "not send packet been cancelled.)");
                return(false);
            }
            if ((sessionPack.mCmdID != 6) && (sessionPack.mRetryLeft != 3))
            {
                int newSeq = SessionPack.getSeqID();
                Log.d("Network", string.Concat(new object[] { "resend pack, change seq ", sessionPack.mSeqID, " to new seq ", newSeq }));
                SessionPackMgr.changeSessionPackSeq(sessionPack.mSeqID, newSeq);
                sessionPack.mSeqID = newSeq;
            }
            sessionPack.mRetryLeft--;
            sessionPack.mSendStatus = 1;
            if (sessionPack.mCacheBodyBuffer == null)
            {
                //   if ((sessionPack.mCmdID == 8) || (sessionPack.mCmdID == 0x19) || (sessionPack.mCmdID == 232 || (sessionPack.mCmdID == 233)))
                //  {
                //sessionPack.mCacheBodyBuffer = sessionPack.requestToByteArray();


                ////Log.e("Network", "mCacheBodyBuffer:"+);
                //if (sessionPack.mCacheBodyBuffer == null)
                //{
                //    Log.e("Network", "newsync check got bytearray failed.");
                //    sessionPack.mSendStatus = 6;
                //    OnCallback.onError(sessionPack, PackResult.PACK_ERROR);
                //    return false;
                //}

                //if (sessionPack.mCmdID == 232 || sessionPack.mCmdID == 233)
                //{
                //    if (!MMPack.EncodePackMini(sessionPack))
                //    //if (!MMPack.EncodePack(sessionPack))
                //    {
                //        Log.e("Network", "sender.encodePack failed,cmd= " + ((CmdConst)sessionPack.mCmdID));
                //        sessionPack.mCacheBodyBuffer = null;
                //        sessionPack.mSendStatus = 6;
                //        OnCallback.onError(sessionPack, PackResult.PACK_ERROR);
                //        return false;
                //    }
                //}

                //return true;
                //  }

                if (!MMPack.EncodePackMini(sessionPack))
                // if (!MMPack.EncodePack(sessionPack))
                {
                    Log.e("Network", "sender.encodePack failed,cmd= " + ((CmdConst)sessionPack.mCmdID));
                    sessionPack.mCacheBodyBuffer = null;
                    sessionPack.mSendStatus      = 6;
                    OnCallback.onError(sessionPack, PackResult.PACK_ERROR);
                    return(false);
                }
            }
            return(true);
        }