Exemplo n.º 1
0
        private static GameSession GetSession(System.Web.HttpContext context, RequestPackage package)
        {
            GameSession session;

            if (package.ProxySid != Guid.Empty)
            {
                session          = GameSession.Get(package.ProxySid) ?? GameSession.CreateNew(package.ProxySid, context.Request);
                session.ProxySid = package.ProxySid;
            }
            else
            {
                session = (string.IsNullOrEmpty(package.SessionId)
                        ? GameSession.GetSessionByCookie(context.Request)
                        : GameSession.Get(package.SessionId))
                          ?? GameSession.CreateNew(Guid.NewGuid(), context.Request);
            }
            return(session);
        }
Exemplo n.º 2
0
        private void OnHttpRequest(IAsyncResult result)
        {
            try
            {
                HttpListener        listener = (HttpListener)result.AsyncState;
                HttpListenerContext context  = listener.EndGetContext(result);
                listener.BeginGetContext(OnHttpRequest, listener);
                string userHostAddress = _httpCdnAddress.GetUserHostAddress(context.Request.RemoteEndPoint,
                                                                            key => context.Request.Headers[key]);
                RequestPackage package;
                if (!ActionDispatcher.TryDecodePackage(context, out package))
                {
                    return;
                }

                GameSession session;
                if (package.ProxySid != Guid.Empty)
                {
                    session          = GameSession.Get(package.ProxySid) ?? GameSession.CreateNew(package.ProxySid, context.Request);
                    session.ProxySid = package.ProxySid;
                }
                else
                {
                    session = (string.IsNullOrEmpty(package.SessionId)
                            ? GameSession.GetSessionByCookie(context.Request)
                            : GameSession.Get(package.SessionId))
                              ?? GameSession.CreateNew(Guid.NewGuid(), context.Request);
                }
                session.RemoteAddress = userHostAddress;
                package.Bind(session);

                ActionGetter httpGet = ActionDispatcher.GetActionGetter(package, session);
                if (package.IsUrlParam)
                {
                    httpGet["UserHostAddress"] = session.RemoteAddress;
                    httpGet["ssid"]            = session.KeyCode.ToString("N");
                    httpGet["http"]            = "1";
                }
                //set cookie
                var cookie = context.Request.Cookies["sid"];
                if (cookie == null)
                {
                    cookie         = new Cookie("sid", session.SessionId);
                    cookie.Expires = DateTime.Now.AddMinutes(5);
                    context.Response.SetCookie(cookie);
                }


                var httpresponse = new SocketGameResponse();
                httpresponse.WriteErrorCallback += new ScutActionDispatcher().ResponseError;

                var clientConnection = new HttpClientConnection
                {
                    Context      = context,
                    Session      = session,
                    ActionGetter = httpGet,
                    GameResponse = httpresponse
                };
                var section = GetSection();
                clientConnection.TimeoutTimer = new Timer(OnHttpRequestTimeout, clientConnection, section.HttpRequestTimeout, Timeout.Infinite);
                byte[] respData = new byte[0];
                if (!string.IsNullOrEmpty(package.RouteName))
                {
                    if (CheckRemote(package.RouteName, httpGet))
                    {
                        MessageStructure response = new MessageStructure();
                        OnCallRemote(package.RouteName, httpGet, response);
                        respData = response.PopBuffer();
                    }
                }
                else
                {
                    DoAction(httpGet, httpresponse);
                    respData = httpresponse.ReadByte();
                }
                OnHttpResponse(clientConnection, respData, 0, respData.Length);
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("OnHttpRequest error:{0}", ex);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <IHttpResponseAction> Execute(IHttpRequestContext context)
        {
            int            statusCode;
            RequestPackage package;
            var            actionDispatcher = GameEnvironment.Setting.ActionDispatcher;

            if (!actionDispatcher.TryDecodePackage(context.Request, out package, out statusCode))
            {
                return(new ByteResponse(statusCode, statusCode == 200 ? "OK" : "FAIL", new byte[0]));
            }

            GameSession session;

            if (package.ProxySid != Guid.Empty)
            {
                session          = GameSession.Get(package.ProxySid) ?? GameSession.CreateNew(package.ProxySid, context.Request);
                session.ProxySid = package.ProxySid;
            }
            else
            {
                session = (string.IsNullOrEmpty(package.SessionId)
                        ? GameSession.GetSessionByCookie(context.Request)
                        : GameSession.Get(package.SessionId))
                          ?? GameSession.CreateNew(Guid.NewGuid(), context.Request);
            }
            package.Bind(session);

            ActionGetter httpGet = actionDispatcher.GetActionGetter(package, session);

            if (package.IsUrlParam)
            {
                httpGet["UserHostAddress"] = session.RemoteAddress;
                httpGet["ssid"]            = session.KeyCode.ToString("N");
                httpGet["http"]            = "1";
            }

            var result = await System.Threading.Tasks.Task.Run <byte[]>(() =>
            {
                try
                {
                    return(ScriptEngines.GetCurrentMainScript().ProcessRequest(package, httpGet));
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("Excute mainclass error:{0}", ex);
                    return(new byte[0]);
                }
            });

            string sessionId = session.SessionId;
            var    response  = new ByteResponse(statusCode, "OK", result);

            response.CookieHandle += ctx =>
            {
                var cookie = ctx.Request.Cookies["sid"];
                if (cookie == null)
                {
                    cookie         = new Cookie("sid", sessionId);
                    cookie.Expires = DateTime.Now.AddMinutes(5);
                    ctx.Response.SetCookie(cookie);
                }
            };
            return(response);
        }