예제 #1
0
        private static void ReturnHandler(string label, HttpListenerRequest request, HttpListenerResponse response)
        {
            var s = RequestBodyAsString(request);

            GotResult?.Invoke(label, s);

            Turtle commandTurtle;

            if (ClientServer.TryGetTurtle(label, out commandTurtle))
            {
                commandTurtle.SetResult(s);
                CloseResponse(response);
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        private void EndGetContext(IAsyncResult res)
        {
            server.BeginGetContext(EndGetContext, null);
            try
            {
                var    context   = server.EndGetContext(res);
                var    request   = context.Request;
                var    response  = context.Response;
                string localPath = request.Url.LocalPath.TrimEnd('/');
                if (localPath.StartsWith("/turtle/") && request.QueryString.AllKeys.Contains("label"))
                {
                    Console.Write(">");
                    var label = request.QueryString["label"];

                    if (localPath.StartsWith("/turtle/return"))
                    {
                        using (System.IO.Stream body = request.InputStream)
                        {
                            using (System.IO.StreamReader reader = new System.IO.StreamReader(body, request.ContentEncoding))
                            {
                                string s = reader.ReadToEnd();
                                if (GotResult != null)
                                {
                                    GotResult.Invoke(label, s);
                                }

                                if (commandPoolOderSo.ContainsKey(label))
                                {
                                    commandPoolOderSo.First(c => c.Key == label).Value.Value(label, s);
                                    commandPoolOderSo.Remove(label);
                                }

                                //foreach (var result in s.Split('|'))
                                //{
                                //    if (result != "nil")
                                //        Console.Write(result + " ");
                                //}
                            }
                        }
                    }

                    Turtle turtle;
                    if (localPath == "/turtle/hello")
                    {
                        using (System.IO.Stream body = request.InputStream)
                        {
                            using (System.IO.StreamReader reader = new System.IO.StreamReader(body, request.ContentEncoding))
                            {
                                string s = reader.ReadToEnd();
                                Handshake(label, response, s);
                            }
                        }
                    }
                    else if (turtles.TryGetValue(label, out turtle))
                    {
                        if (turtle == null)
                        {
                            byte[] buffer = System.Text.Encoding.UTF8.GetBytes("register");
                            response.ContentLength64 = buffer.Length;
                            response.OutputStream.Write(buffer, 0, buffer.Length);
                        }
                        else if (localPath == "/turtle/command")
                        {
                            if (request.QueryString.AllKeys.Contains("value"))
                            {
                                turtle.AddCommand(request.QueryString["value"], (test, result) => { });
                            }
                        }
                        else if (localPath == "/turtle/queryCommand")
                        {
                            turtle.QueryCommand(response);
                        }
                        else if (localPath == "/turtle/disconnect")
                        {
                            turtles.Remove(label);
                            Program.webserver.UpdateList();
                        }
                    }
                }
                else if (localPath == "/user")
                {
                    User(response);
                }
                response.Close();
            }
            catch (Exception)
            {
            }
        }