Ejemplo n.º 1
0
    public static void _PostHttp(string strConnectURL, string strContent, CompleteCallback callback = null, object UserState = null)
    {
        // 產生連線
        HttpWebRequest cWebReq = (HttpWebRequest)WebRequest.Create(strConnectURL);

        // 設定連線內容
        cWebReq.AllowAutoRedirect = true;
        cWebReq.ServicePoint.Expect100Continue = true;
        cWebReq.Method        = "POST";
        cWebReq.ContentLength = strContent.Length;
        cWebReq.ContentType   = "application/json; charset=utf-8";
        // 設定 Timeout時間
        cWebReq.Timeout = Const.HttpGlobalTimeout;
        // 寫入參數
        System.IO.StreamWriter sw = new System.IO.StreamWriter(cWebReq.GetRequestStream());
        sw.Write(strContent);
        sw.Close();
        // 記錄相關資料
        CReqState myRequestState = new CReqState();

        myRequestState.request    = cWebReq;
        myRequestState.ConnectURL = strConnectURL;
        myRequestState.jsonInput  = strContent;
        myRequestState.userState  = UserState;
        if (callback != null)
        {
            myRequestState.callback += callback;
        }
        // 丟進去 Queue 裏等
        IAsyncResult result = cWebReq.BeginGetResponse(new AsyncCallback(FinishWebRequest), myRequestState);

        // 做等待的動作
        ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myRequestState, Const.HttpReponseTimeout, true);
    }
Ejemplo n.º 2
0
    // 要統一做處理
    static void FinishWebRequest(IAsyncResult result)
    {
        CReqState       asyncState = (CReqState)result.AsyncState;
        HttpWebResponse cRsp       = (HttpWebResponse)asyncState.request.EndGetResponse(result);
        StreamReader    cStmRdr    = new StreamReader(cRsp.GetResponseStream());
        string          output     = string.Empty;

        output = cStmRdr.ReadToEnd();
        cStmRdr.Close();
        asyncState.finishcallback(0, output);
    }
Ejemplo n.º 3
0
    public static void ProcessCallback(IAsyncResult Source = null)
    {
        m_MutexCallback.WaitOne();
        while (m_listCallback.Count > 0 || Source != null)
        {
            DebugLog("[ProcessCallback]");
            IAsyncResult result = null;
            if (Source == null)
            {
                result = m_listCallback[0];
                m_listCallback.RemoveAt(0);
            }
            else
            {
                result = Source;
            }
            CReqState asyncState = null;
#if !SHOW_EXCEPTION
            try
            {
#endif
            asyncState = (CReqState)result.AsyncState;
            DebugLog("1");
            HttpWebResponse cRsp = (HttpWebResponse)asyncState.request.EndGetResponse(result);
            DebugLog("2");
            StreamReader cStmRdr = new StreamReader(cRsp.GetResponseStream());
            DebugLog("3");
            string output = string.Empty;
            DebugLog("4");
            output = cStmRdr.ReadToEnd();
            DebugLog("5");
            cStmRdr.Close();
            asyncState.finishcallback(ErrorType.Success, output);
#if !SHOW_EXCEPTION
        }
        // 取不到資料
        catch (WebException e)
        {
            LogMgr.ErrorLog("[FinishWebRequest][ProcessCallback] Message:{0}, Status:{1}", e.Message, e.Source);
            if (asyncState != null)
            {
                asyncState.finishcallback(ErrorType.Error);
            }
            m_MutexCallback.ReleaseMutex();
            throw e;
        }
#endif
        }
        m_MutexCallback.ReleaseMutex();
    }
Ejemplo n.º 4
0
    public static void _PostHttp(string strConnectURL, string strContent, CompleteCallback callback = null)
    {
        // 產生連線
        HttpWebRequest cWebReq = (HttpWebRequest)WebRequest.Create(strConnectURL);

        // 設定連線內容
        cWebReq.AllowAutoRedirect = true;
        cWebReq.ServicePoint.Expect100Continue = true;
        cWebReq.Method        = "POST";
        cWebReq.ContentLength = strContent.Length;
        cWebReq.ContentType   = "application/json; charset=utf-8";
        // 寫入參數
        System.IO.StreamWriter sw = new System.IO.StreamWriter(cWebReq.GetRequestStream());
        sw.Write(strContent);
        sw.Close();
        CReqState myRequestState = new CReqState();

        myRequestState.request = cWebReq;
        if (callback != null)
        {
            myRequestState.callback += callback;
        }
        // 丟進去 Queue 裏等
        IAsyncResult result = cWebReq.BeginGetResponse(new AsyncCallback(FinishWebRequest), myRequestState);

        // 做等待的動作
        ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myRequestState, 5000, true);
        //try
        //{
        //    // 寫入參數
        //    System.IO.StreamWriter sw = new System.IO.StreamWriter(cWebReq.GetRequestStream());
        //    sw.Write(strContent);
        //    sw.Close();
        //    CReqState myRequestState = new CReqState();
        //    myRequestState.request = cWebReq;
        //    if (callback != null)
        //        myRequestState.callback += callback;
        //    // 丟進去 Queue 裏等
        //    IAsyncResult result = cWebReq.BeginGetResponse(new AsyncCallback(FinishWebRequest), myRequestState);
        //    // 做等待的動作
        //    ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myRequestState, 1000, true);
        //}
        //catch (System.Net.WebException webexception)
        //{
        //    string strMsg = webexception.Status.ToString();
        //}
    }
Ejemplo n.º 5
0
 // 統一回來的處理
 public static void ProtocolCompleteCallback(ErrorType errorCode, object result, object userState, CReqState state)
 {
     LogMgr.DebugLog("[ClientService][ProtocolCompleteCallback] errorCode:{0}, result:{1}, userState:{2}, state:{3}", errorCode, result, userState, state);
     // 檢查是不是正確
     if (errorCode == ErrorType.Error)
     {
         LogMgr.ErrorLog("[ClientService][ProtocolCompleteCallback] Error!! errorCode:{0}, result:{1}, userState:{2}, state:{3}", errorCode, result, userState, state);
         return;
     }
     else if (errorCode == ErrorType.Timeout)
     {
         // 做重送的動作?!
         LogMgr.ErrorLog("[ClientService][ProtocolCompleteCallback] Timeout!! errorCode:{0}, result:{1}, userState:{2}, state:{3}", errorCode, result, userState, state);
         return;
     }
     else
     {
         // 取得參數 (From Network)
         Dictionary <string, object> dictResult = null;
         if (result is string)
         {
             dictResult = JsonConvert.DeserializeObject <Dictionary <string, object> > (result.ToString());
             dictResult = JsonConvert.DeserializeObject <Dictionary <string, object> > (dictResult["d"].ToString());
         }
         // From Client Only Server
         else
         {
             dictResult = JsonConvert.DeserializeObject <Dictionary <string, object> > (JsonConvert.SerializeObject(result));
         }
         //Debug.Log (JsonConvert.SerializeObject( dictResult));
         if (dictResult.ContainsKey("Result") == true)
         {
             ErrorID eid = (ErrorID)System.Convert.ToInt32(dictResult["Result"]);
             if (eid != ErrorID.Success)
             {
                 LogMgr.Log("[ClientService][ProtocolCompleteCallback] Parser Error, ErrorID = {0}, Msg={1}", eid, IDMap.GetEnumAttribute(eid));
                 //GameUtility.ShowMessageBox (IDMap.GetEnumAttribute (eid), "錯誤");
                 //[problem]
                 PushClientAction(ClientActionID.ShowMessage, IDMap.GetEnumAttribute(eid), dictResult, userState);
             }
         }
         // 取得 ClientAction
         if (dictResult.ContainsKey("ClientAction") == false)
         {
             return;
         }
         //string jsonClientAction = dictResult["ClientAction"].ToString();
         List <KeyValuePair <string, object> > listAction = null;
         listAction = JsonConvert.DeserializeObject <List <KeyValuePair <string, object> > > (dictResult["ClientAction"].ToString());
         foreach (var ChildAction in listAction)
         {
             try
             {
                 PushClientAction(ChildAction.Key, ChildAction.Value, dictResult, userState);
             }
             catch (Exception e)
             {
                 LogMgr.ErrorLog("[Error!!] {0}", e.ToString());
             }
         }
     }
 }