示例#1
0
 public void PrintError(object sender, ServerErrorEventArgs e)
 {
     Console.WriteLine("\nInternal Server Error!");
     Console.WriteLine("Code: {0}",e.Code);
     Console.WriteLine("Data: {0}",e.Data);
     Console.WriteLine(e.Message);
 }
示例#2
0
        // Return error code
        public int Post(string url, JsonTextWriter json)
        {
            WebRequest request = WebRequest.Create (serverURL+url);

              request.Method = "POST";

            json.AutoComplete();

            string text = json.ToString();

            Log.Write("Request: ");
            Log.WriteLine (text.ToString()+"\n");

            json.Close();

              byte[] postdata = System.Text.Encoding.ASCII.GetBytes (text);

            request.ContentLength = postdata.Length;

            // Wait one second when 60th RPC call is made during minute
            if (Interlocked.Read(ref throttle) == 60)
            {
                if (onMessage != null && severity >= 6)
                    onMessage(this,"[{0}] Throttling...", DateTime.Now);
                while (Interlocked.Read(ref throttle) == 60)
                    Thread.Sleep(1000);
            }

            if (timer.Enabled) Interlocked.Increment(ref throttle);

            try {
                Stream stream = request.GetRequestStream ();

              	stream.Write (postdata, 0, postdata.Length);
              	stream.Close ();
            } catch (System.Net.WebException ex) {
                if (onMessage != null && severity >= 3) onMessage(this,ex.Message);
                return -1;
            }

            if (onMessage != null && severity >= 7)
                onMessage(this,"\n[{0}] Connecting...",DateTime.Now);

            try {
              	WebResponse response = request.GetResponse ();
                ProcessResponse(response);
                if (onServerStatus != null) onServerStatus(this,new ServerStatusEventArgs());
            } catch (System.Net.WebException ex) {
                if (Regex.IsMatch(ex.Response.ContentType,"json-rpc")) {
                  WebResponse response = ex.Response;
                    ProcessResponse(response);
                    ServerErrorEventArgs args =
                        new ServerErrorEventArgs(r.error.code,r.error.data,r.error.message);
                    if (onServerError != null) onServerError(this,args);
                    return r.error.code;
                } else {
                    if (onMessage != null && severity >= 3) onMessage(this,ex.Message);
                    return -1;
                }
            }
            return 0;
        }