示例#1
0
        internal void EnqueueRequest(ReverserRequest request)
        {
            lock (awaitingRequestLock)
                awaitingRequests.Enqueue(request);

            requestWait.Set();
        }
 public ReverserSocketStreamWrapper(ReverserRequest request)
 {
     this.originRequest = request;
     this.id = Guid.NewGuid();
     this.commandBuffer = new List<Command>();
     this.clientCommandBuffer = new List<Command>();
     this.lastAction = DateTime.Now;
     this.pinging = false;
     Debug.WriteLine("SocketWrapper created");
 }
示例#3
0
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            context.Response.AddHeader("X-Http-Handler", "Reverser");
            context.Response.CacheControl = "No-Cache";
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            context.Response.Expires = -1500;
            context.Response.AddHeader("Pragma", "No-Cache");
            context.Response.AddHeader("Pragma", "No-Store");
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.Cache.SetNoServerCaching();
            context.Response.Cache.SetNoStore();
            string path = context.Request.PhysicalPath.ToLower();
            Type handlerType = null;
            ConstructorInfo constructor = null;
            ReverserHandler handler = null;
            if (handlers.ContainsKey(path))
            {
                handler = handlers[path];
            }
            else if (!File.Exists(path))
            {
                context.Response.StatusCode = 404;
                context.Response.Status = "404 Not Found";
                context.Response.StatusDescription = "The file you are searching for cannot be found";
                context.Response.Write("<html><head><title>404 - File not found</title></head><body><h1>404 - File not found</h1><p>The file you are searching for cannot be found.</p></body></html>");
                context.Response.End();
                return null;
            }
            else
            {
                XDocument xdoc = XDocument.Load(path);
                string handlerFullName = xdoc.Element("reverser").Element("handler").Attribute("class").Value;
                xdoc = null;
                Assembly assembly = context.ApplicationInstance.GetType().BaseType.Assembly;
                handlerType = assembly.GetType(handlerFullName);

                if (handlerType == null)
                {
                    context.Response.StatusCode = 500;
                    context.Response.Status = "500 Internal WebSocketServer Error";
                    context.Response.Write("The class referenced could not be found.");
                    context.Response.End();
                    return null;
                }

                if (!handlerType.IsSubclassOf(typeof(ReverserHandler)))
                {
                    context.Response.StatusCode = 500;
                    context.Response.Status = "500 Internal WebSocketServer Error";
                    context.Response.Write("The class referenced was not a reverser handler.");
                    context.Response.End();
                    return null;
                }

                constructor = handlerType.GetConstructor(Type.EmptyTypes);

                if (constructor == null)
                {
                    context.Response.StatusCode = 500;
                    context.Response.Status = "500 Internal WebSocketServer Error";
                    context.Response.Write("No empty constructor found.");
                    context.Response.End();
                    return null;
                }

                handler = (ReverserHandler)constructor.Invoke(null);
                handlers[path] = handler;

            }

            DebugMsg("Request received: " + context.Request.HttpMethod + " " + context.Request.Path);
            ReverserRequest request = new ReverserRequest(cb, context, extraData);
            handler.HandleRequest(request);
            return request;
        }
示例#4
0
 internal void HandleRequest(ReverserRequest req)
 {
     if (req.Context.Request.QueryString.ToString() == "ws" && EnableSockets)
     {
         if (socketServer == null)
         {
             lock (this)
             {
                 if (socketServer == null)
                 {
                     socketServer = new WebSocketServer<Guid>(new WebSocketServer<Guid>.WebSocketConnectionRequest(socketServer_Request),
                         req.Context.Request.Url.Host, req.Context.Request.Url.PathAndQuery.Split('?')[0]);
                     socketServer.Messages += new WebSocketServer<Guid>.WebSocketMessages(socketServer_Messages);
                     socketServer.Close += new WebSocketServer<Guid>.WebSocketClose(socketServer_Close);
                     socketServer.Start();
                     webSockPort = socketServer.Port;
                 }
             }
         }
         var socketAddress = "";
         var wrapper = new ReverserSocketStreamWrapper(req);
         var ub = new UriBuilder(req.Context.Request.Url);
         ub.Scheme = "ws";
         ub.Port = webSockPort;
         ub.Query = "_requestId=" + wrapper.Id.ToString();
         socketAddress = ub.ToString();
         var client = GetClient(req.Context.Request).Setup(this);
         wrapper.Client = client;
         client.Add(wrapper);
         OnClientConnect(client);
         lock (requestStreams)
         {
             reverserStreams.Add(wrapper);
         }
         lock (wrapperStreams)
         {
             wrapperStreams.Add(wrapper.Id, wrapper);
         }
         req.End(new Command("$_ws", ub.ToString()));
         req.CompletedSynchronously = true;
         return;
     }
     bool requireIdSet;
     ReverserRequestStream stream = ReverserRequestStream.Get(out requireIdSet, req.Id);
     lock (requestStreams)
     {
         if (!requestStreams.Contains(stream))
         {
             requestStreams.Add(stream);
             reverserStreams.Add(stream);
             var client = GetClient(req.Context.Request).Setup(this);
             stream.Client = client;
             client.Add(stream);
             OnClientConnect(client);
         }
     }
     if (requireIdSet)
     {
         req.CompletedSynchronously = true;
         req.End(new Command("$_requestId", stream.Id));
     }
     else if (req.Context.Request.HttpMethod == "POST")
     {
         stream.HandleDataRequest(req);
         req.CompletedSynchronously = true;
         req.End();
         requestWait.Set();
     }
     else
     {
         stream.AddRequest(req);
         requestWait.Set();
     }
 }
示例#5
0
        internal void HandleDataRequest(ReverserRequest req)
        {
            Command cmd;
            try
            {
                byte[] jsonData = req.Context.Request.BinaryRead(req.Context.Request.ContentLength);
                String json = req.Context.Request.ContentEncoding.GetString(jsonData);
                cmd = Newtonsoft.Json.JsonConvert.DeserializeObject<Command>(json);
            }
            catch { cmd = null; }

            if (cmd != null)
                lock (clientCommandsBuffer)
                    clientCommandsBuffer.Add(cmd);
        }
示例#6
0
 internal void AddRequest(ReverserRequest req)
 {
     requests.Add(req);
     lock (messageBuffer)
     {
         if (messageBuffer.Count > 0)
         {
             req.End(messageBuffer.ToArray());
             messageBuffer.Clear();
         }
     }
 }