Inheritance: PendingOperationManager, IDisposable
コード例 #1
0
ファイル: IOTests.cs プロジェクト: sq/Libraries
        public override void SetUp()
        {
            base.SetUp();
            var adapter = new StreamDataAdapter(this.Stream);

            Reader = new AsyncTextReader(adapter, Encoding.ASCII);
        }
コード例 #2
0
ファイル: Web.cs プロジェクト: pakoito/Fracture
        public static IEnumerator<object> IssueRequest(HttpWebRequest request)
        {
            var fResponse = new Future<HttpWebResponse>();
            ThreadPool.QueueUserWorkItem(
                (__) => {
                    try {
                        request.BeginGetResponse(
                            (ar) => {
                                try {
                                    var _ = (HttpWebResponse)request.EndGetResponse(ar);
                                    fResponse.SetResult(_, null);
                                } catch (Exception ex) {
                                    fResponse.SetResult(null, ex);
                                }
                            }, null
                        );
                    } catch (Exception ex_) {
                        fResponse.SetResult(null, ex_);
                    }
                }
            );

            yield return fResponse;
            if (fResponse.Failed)
                throw new RequestFailedException(fResponse.Error);

            using (var response = fResponse.Result) {
                var fResponseStream = Future.RunInThread(
                    () => response.GetResponseStream()
                );
                yield return fResponseStream;

                Encoding encoding = AsyncTextReader.DefaultEncoding;
                if (!string.IsNullOrEmpty(response.CharacterSet))
                    encoding = Encoding.GetEncoding(response.CharacterSet);

                string responseText;
                using (var stream = fResponseStream.Result)
                using (var adapter = new AsyncTextReader(new StreamDataAdapter(stream, false), encoding)) {
                    var fText = adapter.ReadToEnd();

                    yield return fText;

                    responseText = fText.Result;
                }

                var cookies = new Cookie[response.Cookies.Count];
                response.Cookies.CopyTo(cookies, 0);

                yield return new Result(new Response {
                    Body = responseText,
                    ContentType = response.ContentType,
                    StatusCode = response.StatusCode,
                    StatusDescription = response.StatusDescription,
                    Cookies = cookies
                });
            }
        }
コード例 #3
0
        public void SetUp()
        {
            var dataPath = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + @"\..\..\data\");
            var testFile = dataPath + "test.py";

            Stream = System.IO.File.OpenRead(testFile);
            var encoding = Util.IO.DetectStreamEncoding(Stream);
            var adapter  = new StreamDataAdapter(Stream);

            Reader = new AsyncTextReader(adapter, encoding);
        }
コード例 #4
0
ファイル: TelnetServer.cs プロジェクト: mbahar94/fracture
 internal TelnetClient (TelnetServer server, TcpClient client) {
     Server = server;
     client.Client.NoDelay = true;
     client.Client.Blocking = false;
     Data = new SocketDataAdapter(client.Client, true);
     Data.ThrowOnDisconnect = false;
     Data.ThrowOnFullSendBuffer = false;
     Encoding encoding = Encoding.ASCII;
     Input = new AsyncTextReader(Data, encoding);
     Output = new AsyncTextWriter(Data, encoding);
     Output.AutoFlush = true;
     _SendFuture = server._Scheduler.Start(SendMessagesTask(), TaskExecutionPolicy.RunWhileFutureLives);
 }
コード例 #5
0
ファイル: Program.cs プロジェクト: mbahar94/fracture
        static IEnumerator<object> ReceiveTask (SocketDataAdapter adapter) {
            var input = new AsyncTextReader(adapter, Encoding.ASCII);
            int i = 0;
            string message = null;
            Reader = input;
            while (true) {                
                var f = input.ReadLine();
                yield return f;

                if (!f.GetResult(out message))
                    throw new DisconnectedException();

                if (message == null)
                    throw new DisconnectedException();
                else
                    i += 1;

                if ((i % 1000) == 0)
                    Console.WriteLine("Recieved: {0}", i);
            }
        }
コード例 #6
0
ファイル: Request.cs プロジェクト: sq/Fracture
        private IEnumerator<object> RequestTask(ListenerContext context, SocketDataAdapter adapter)
        {
            var startedWhen = DateTime.UtcNow;
            bool successful = false;

            try {
                const int headerBufferSize = 1024 * 32;
                const int bodyBufferSize = 1024 * 128;
                const double requestLineTimeout = 5;

                // RFC2616:
                // Words of *TEXT MAY contain characters from character sets other than ISO-8859-1 [22]
                //  only when encoded according to the rules of RFC 2047 [14].
                Encoding headerEncoding;
                try {
                    headerEncoding = Encoding.GetEncoding("ISO-8859-1");
                } catch {
                    headerEncoding = Encoding.ASCII;
                }

                Request request;
                RequestBody body;
                HeaderCollection headers;
                long bodyBytesRead = 0;
                long? expectedBodyLength = null;

                var reader = new AsyncTextReader(adapter, headerEncoding, headerBufferSize, false);
                string requestLineText;

                while (true) {
                    var fRequestLine = reader.ReadLine();
                    var fRequestOrTimeout = Scheduler.Start(new WaitWithTimeout(fRequestLine, requestLineTimeout));

                    yield return fRequestOrTimeout;

                    if (fRequestOrTimeout.Failed) {
                        if (!(fRequestOrTimeout.Error is TimeoutException))
                            OnRequestError(fRequestOrTimeout.Error);

                        yield break;
                    }

                    if (fRequestLine.Failed) {
                        if (!(fRequestLine.Error is SocketDisconnectedException))
                            OnRequestError(fRequestLine.Error);

                        yield break;
                    }

                    requestLineText = fRequestLine.Result;

                    // RFC2616:
                    // In the interest of robustness, servers SHOULD ignore any empty line(s) received where a
                    //  Request-Line is expected. In other words, if the server is reading the protocol stream
                    //   at the beginning of a message and receives a CRLF first, it should ignore the CRLF.
                    if ((requestLineText != null) && (requestLineText.Trim().Length == 0))
                        continue;

                    break;
                }

                var requestLineParsed = DateTime.UtcNow;

                headers = new HeaderCollection();
                while (true) {
                    var fHeaderLine = reader.ReadLine();
                    yield return fHeaderLine;

                    if (String.IsNullOrWhiteSpace(fHeaderLine.Result))
                        break;

                    headers.Add(new Header(fHeaderLine.Result));
                }

                var headersParsed = DateTime.UtcNow;

                var expectHeader = (headers.GetValue("Expect") ?? "").ToLowerInvariant();
                var expectsContinue = expectHeader.Contains("100-continue");

                string hostName;
                if (headers.Contains("Host")) {
                    hostName = String.Format("http://{0}", headers["Host"].Value);
                } else {
                    var lep = (IPEndPoint)adapter.Socket.LocalEndPoint;
                    hostName = String.Format("http://{0}:{1}", lep.Address, lep.Port);
                }

                var requestLine = new RequestLine(hostName, requestLineText);

                var remainingBytes = reader.DisposeAndGetRemainingBytes();
                bodyBytesRead += remainingBytes.Count;

                var connectionHeader = (headers.GetValue("Connection") ?? "").ToLowerInvariant();
                var shouldKeepAlive =
                    ((requestLine.Version == "1.1") || connectionHeader.Contains("keep-alive")) &&
                    !connectionHeader.Contains("close");

                if (headers.Contains("Content-Length"))
                    expectedBodyLength = long.Parse(headers["Content-Length"].Value);

                body = new RequestBody(remainingBytes, expectedBodyLength);

                if (expectsContinue)
                    yield return adapter.Write(Continue100, 0, Continue100.Length);

                request = new Request(
                    this, adapter, shouldKeepAlive,
                    requestLine, headers, body
                );

                IncomingRequests.Enqueue(request);

                var requestEnqueued = DateTime.UtcNow;
                DateTime? requestBodyRead = null;

                // FIXME: I think it's technically accepted to send a body without a content-length, but
                //  it seems to be impossible to make that work right.
                if (expectedBodyLength.HasValue) {
                    using (var bodyBuffer = BufferPool<byte>.Allocate(bodyBufferSize))
                    while (bodyBytesRead < expectedBodyLength.Value) {
                        long bytesToRead = Math.Min(expectedBodyLength.Value - bodyBytesRead, bodyBufferSize);

                        if (bytesToRead <= 0)
                            break;

                        var fBytesRead = adapter.Read(bodyBuffer.Data, 0, (int)bytesToRead);
                        yield return fBytesRead;

                        if (fBytesRead.Failed) {
                            if (fBytesRead.Error is SocketDisconnectedException)
                                break;

                            body.Failed(fBytesRead.Error);
                            OnRequestError(fBytesRead.Error);
                            yield break;
                        }

                        var bytesRead = fBytesRead.Result;

                        bodyBytesRead += bytesRead;
                        body.Append(bodyBuffer.Data, 0, bytesRead);
                    }

                    requestBodyRead = DateTime.UtcNow;
                }

                body.Finish();
                successful = true;

                request.Timing = new Request.TimingData {
                    Line = (requestLineParsed - startedWhen),
                    Headers = (headersParsed - requestLineParsed),
                    Queue = (requestEnqueued - headersParsed),
                    Body = (requestBodyRead - requestEnqueued)
                };
            } finally {
                if (!successful)
                    adapter.Dispose();
            }
        }
コード例 #7
0
ファイル: Web.cs プロジェクト: pakoito/Fracture
 public static AsyncTextReader GetRequestReader(this HttpListenerContext context)
 {
     var encoding = context.Request.ContentEncoding ?? Encoding.UTF8;
     var adapter = new StreamDataAdapter(context.Request.InputStream, true);
     var result = new AsyncTextReader(adapter, encoding);
     return result;
 }
コード例 #8
0
ファイル: Program.cs プロジェクト: kg/HeapProfiler
        public static IEnumerator<object> RunProcessWithResult(
            ProcessStartInfo psi, ProcessPriorityClass? priority = null,
            IEnumerable<KeyValuePair<string, string>> customEnvironment = null
        )
        {
            psi.UseShellExecute = false;
            psi.CreateNoWindow = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;

            if (customEnvironment != null)
                foreach (var kvp in customEnvironment)
                    psi.EnvironmentVariables[kvp.Key] = kvp.Value;

            var fProcess = StartProcess(psi);
            yield return fProcess;

            if (priority.HasValue)
            try {
                fProcess.Result.PriorityClass = priority.Value;
            } catch {
            }

            using (var process = fProcess.Result)
            using (var stdout = new AsyncTextReader(
                new StreamDataAdapter(process.StandardOutput.BaseStream, false), Encoding.ASCII, 1024 * 16
            ))
            using (var stderr = new AsyncTextReader(
                new StreamDataAdapter(process.StandardError.BaseStream, false), Encoding.ASCII, 1024 * 16
            ))
            try {

                var fStdOut = stdout.ReadToEnd();
                var fStdErr = stderr.ReadToEnd();

                yield return WaitForProcessExit(process);

                yield return fStdOut;
                yield return fStdErr;

                yield return new Result(new RunProcessResult {
                    StdOut = fStdOut.Result,
                    StdErr = fStdErr.Result,
                    ExitCode = process.ExitCode
                });
            } finally {
                try {
                    if (!process.HasExited)
                        process.Kill();
                } catch {
                }
            }
        }
コード例 #9
0
ファイル: IOTests.cs プロジェクト: sq/Fracture
 public override void SetUp()
 {
     base.SetUp();
     var adapter = new StreamDataAdapter(this.Stream);
     Reader = new AsyncTextReader(adapter, Encoding.ASCII);
 }
コード例 #10
0
ファイル: IOTests.cs プロジェクト: mbahar94/fracture
 public void SetUp () {
     var dataPath = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + @"\..\..\data\");
     var testFile = dataPath + "test.py";
     Stream = System.IO.File.OpenRead(testFile);
     var encoding = Util.IO.DetectStreamEncoding(Stream);
     var adapter = new StreamDataAdapter(Stream);
     Reader = new AsyncTextReader(adapter, encoding);
 }
コード例 #11
0
ファイル: Program.cs プロジェクト: mbahar94/fracture
        static IEnumerator<object> PeerTask (TcpClient client, Peer peer) {
            var adapter = new SocketDataAdapter(client.Client, true);
            var input = new AsyncTextReader(adapter, Encoding.ASCII);
            var output = new AsyncTextWriter(adapter, Encoding.ASCII);

            adapter.ThrowOnDisconnect = false;
            adapter.ThrowOnFullSendBuffer = false;
            output.AutoFlush = true;

            peer.Input = input;
            peer.Output = output;

            yield return output.WriteLine("Welcome! Please enter your name.");

            string name = null;
            yield return input.ReadLine().Bind(() => name);

            if (name == null) {
                PeerDisconnected(peer);
                yield break;
            }

            peer.Name = name;

            PeerConnected(peer);

            yield return output.Write(VT100.EraseScreen);

            string nextLine = null;

            while (peer.Connected) {
                var f = input.ReadLine();
                yield return f;

                if (!f.GetResult(out nextLine) || nextLine == null) {
                    PeerDisconnected(peer);
                    yield break;
                }

                if (nextLine.Length > 0)
                    DispatchNewMessage(peer, nextLine);
            }
        }
コード例 #12
0
        IEnumerator<object> SearchInFiles(SearchQuery search, BlockingQueue<string> filenames, IFuture completionFuture)
        {
            var searchedFiles = new List<string>();
            var buffer = new List<SearchResult>();
            var sb = new StringBuilder();

            int numFiles = 0;

            using (Finally.Do(() => {
                SetSearchResults(buffer);
                lblStatus.Text = String.Format("{0} result(s) found.", buffer.Count);
                pbProgress.Style = ProgressBarStyle.Continuous;
                pbProgress.Value = 0;
            }))
            while (filenames.Count > 0 || !completionFuture.Completed) {
                var f = filenames.Dequeue();
                yield return f;

                var filename = f.Result as string;

                if (filename == null)
                    continue;
                if (searchedFiles.Contains(filename))
                    continue;

                if (PendingSearchQuery != null)
                    break;

                searchedFiles.Add(filename);

                int lineNumber = 0;
                var lineBuffer = new LineEntry[3];

                var insertResult = (Action)(() => {
                    var item = new SearchResult();
                    item.Filename = filename;
                    item.LineNumber = lineBuffer[1].LineNumber;

                    sb.Remove(0, sb.Length);
                    for (int i = 0; i < 3; i++) {
                        if (lineBuffer[i].Text != null) {
                            var line = lineBuffer[i].Text;
                            if (line.Length > 512)
                                line = line.Substring(0, 512);
                            sb.Append(line);
                        }

                        if (i < 2)
                            sb.Append("\r\n");
                    }
                    item.Context = sb.ToString();

                    buffer.Add(item);

                    if ((buffer.Count % 250 == 0) || ((buffer.Count < 50) && (buffer.Count % 5 == 1)))
                        SetSearchResults(buffer);
                });

                var stepSearch = (Action)(() => {
                    string currentLine = lineBuffer[1].Text;

                    if ((currentLine != null) && search.Regex.IsMatch(currentLine))
                        insertResult();
                });

                var insertLine = (Action<LineEntry>)((line) => {
                    lineBuffer[0] = lineBuffer[1];
                    lineBuffer[1] = lineBuffer[2];
                    lineBuffer[2] = line;

                    stepSearch();
                });

                numFiles += 1;
                if (numFiles % 50 == 0) {
                    lblStatus.Text = String.Format("Scanning '{0}'...", filename);
                    if (completionFuture.Completed) {
                        int totalNumFiles = numFiles + filenames.Count;
                        int progress = (numFiles * 1000 / totalNumFiles);

                        if (pbProgress.Value != progress)
                            pbProgress.Value = progress;
                        if (pbProgress.Style != ProgressBarStyle.Continuous)
                            pbProgress.Style = ProgressBarStyle.Continuous;
                    }
                }

                FileDataAdapter adapter = null;
                try {
                    adapter = new FileDataAdapter(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                } catch {
                    if (adapter != null)
                        adapter.Dispose();
                    continue;
                }
                using (adapter) {
                    var fEncoding = Future.RunInThread(
                        () => DetectEncoding(adapter.BaseStream)
                    );
                    yield return fEncoding;

                    Future<string> thisLine = null, nextLine = null;

                    using (var reader = new AsyncTextReader(adapter, fEncoding.Result, SearchBufferSize))
                    while (true) {
                        thisLine = nextLine;

                        if (thisLine != null)
                            yield return thisLine;

                        nextLine = reader.ReadLine();

                        if (thisLine == null)
                            continue;

                        lineNumber += 1;
                        string line = thisLine.Result;
                        insertLine(new LineEntry { Text = line, LineNumber = lineNumber });

                        if (line == null)
                            break;
                        if (PendingSearchQuery != null)
                            break;

                        if (lineNumber % 10000 == 5000) {
                            var newStatus = String.Format("Scanning '{0}'... (line {1})", filename, lineNumber);
                            if (lblStatus.Text != newStatus)
                                lblStatus.Text = newStatus;
                        }
                    }
                }
            }
        }