Beispiel #1
0
        private static void requestCallback(IAsyncResult asynchronousResult)
        {
            NetWebClient netWebClient = asynchronousResult.AsyncState as NetWebClient;
            Stream       stream       = null;

            try
            {
                HttpWebRequest httpWebRequest = netWebClient.request_;
                stream = httpWebRequest.EndGetRequestStream(asynchronousResult);
                stream.Write(netWebClient.bytes_, 0, netWebClient.bytes_.Length);
                netWebClient.request();
            }
            catch (WebException ex)
            {
                netWebClient.setError(ex.Status, ex.Message, 5, ex.Response as HttpWebResponse);
            }
            catch (Exception ex2)
            {
                netWebClient.setError(WebExceptionStatus.UnknownError, ex2.Message, 5, null);
            }
            finally
            {
                netWebClient.bytes_ = null;
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
Beispiel #2
0
        private static void timeoutCallback(object state, bool timedout)
        {
            if (!timedout)
            {
                return;
            }
            NetWebClient netWebClient = state as NetWebClient;

            if (netWebClient.request_ == null)
            {
                netWebClient.setError(WebExceptionStatus.UnknownError, string.Empty, 5, null);
                return;
            }
            netWebClient.setError(WebExceptionStatus.Timeout, string.Empty, 5, null);
        }
Beispiel #3
0
        private static void responseCallback(IAsyncResult asynchronousResult)
        {
            NetWebClient netWebClient = asynchronousResult.AsyncState as NetWebClient;

            try
            {
                HttpWebRequest httpWebRequest = netWebClient.request_;
                netWebClient.response_       = (httpWebRequest.EndGetResponse(asynchronousResult) as HttpWebResponse);
                netWebClient.responseStream_ = netWebClient.response_.GetResponseStream();
                NetWebClient.clearStream(NetWebClient.shared_.memoryStream_);
                netWebClient.responseStream_.BeginRead(NetWebClient.shared_.buffer_, 0, 1024, new AsyncCallback(NetWebClient.readCallback), netWebClient);
            }
            catch (WebException ex)
            {
                netWebClient.setError(ex.Status, ex.Message, 5, ex.Response as HttpWebResponse);
            }
            catch (Exception ex2)
            {
                netWebClient.setError(WebExceptionStatus.UnknownError, ex2.Message, 5, null);
            }
        }
Beispiel #4
0
        private static void readCallback(IAsyncResult asynchronousResult)
        {
            NetWebClient netWebClient = asynchronousResult.AsyncState as NetWebClient;

            try
            {
                Stream stream = netWebClient.responseStream_;
                int    num    = stream.EndRead(asynchronousResult);
                if (0 < num)
                {
                    NetWebClient.shared_.memoryStream_.Write(NetWebClient.shared_.buffer_, 0, num);
                    stream.BeginRead(NetWebClient.shared_.buffer_, 0, 1024, new AsyncCallback(NetWebClient.readCallback), netWebClient);
                }
                else if (asynchronousResult.IsCompleted)
                {
                    byte[] buffer_ = NetWebClient.shared_.buffer_;
                    NetWebClient.shared_.memoryStream_.Position = 0L;
                    if (0 < netWebClient.encryptVersion_)
                    {
                        if (NetWebClient.descryptTo(NetWebClient.shared_.compressedStream_, NetWebClient.shared_.memoryStream_) < 0)
                        {
                            throw new Exception("A message may be corrupted.");
                        }
                        NetWebClient.swap <MemoryStream>(ref NetWebClient.shared_.memoryStream_, ref NetWebClient.shared_.compressedStream_);
                    }
                    NetWebClient.Encoding encoding = netWebClient.encoding_;
                    if (encoding != NetWebClient.Encoding.Deflate)
                    {
                        if (encoding == NetWebClient.Encoding.GZip)
                        {
                            NetWebClient.swap <MemoryStream>(ref NetWebClient.shared_.memoryStream_, ref NetWebClient.shared_.compressedStream_);
                            using (GZipStream gzipStream = new GZipStream(NetWebClient.shared_.compressedStream_, CompressionMode.Decompress, true))
                            {
                                NetWebClient.copyTo(NetWebClient.shared_.memoryStream_, gzipStream, buffer_, 1024);
                            }
                        }
                    }
                    else
                    {
                        NetWebClient.swap <MemoryStream>(ref NetWebClient.shared_.memoryStream_, ref NetWebClient.shared_.compressedStream_);
                        MemoryStream memoryStream_     = NetWebClient.shared_.memoryStream_;
                        MemoryStream compressedStream_ = NetWebClient.shared_.compressedStream_;
                        compressedStream_.ReadByte();
                        compressedStream_.ReadByte();
                        using (DeflateStream deflateStream = new DeflateStream(compressedStream_, CompressionMode.Decompress, true))
                        {
                            NetWebClient.copyTo(memoryStream_, deflateStream, buffer_, 1024);
                        }
                        if (!NetWebClient.checkHash(compressedStream_, memoryStream_))
                        {
                            netWebClient.setError(WebExceptionStatus.UnknownError, "Invalid Hash", 5, null);
                            return;
                        }
                    }
                    netWebClient.setSuccess(4);
                }
            }
            catch (WebException ex)
            {
                netWebClient.setError(ex.Status, ex.Message, 5, ex.Response as HttpWebResponse);
            }
            catch (Exception ex2)
            {
                netWebClient.setError(WebExceptionStatus.UnknownError, ex2.Message, 5, null);
            }
        }