Esempio n. 1
0
 public Action1005(ZyGames.Framework.Game.Contract.HttpGet httpGet)
     : base(ActionIDDefine.Cst_Action1005, httpGet)
 {
 }
Esempio n. 2
0
        /// <summary>
        /// 获取Action处理的输出字节流
        /// </summary>
        /// <returns></returns>
        public static byte[] GetActionResponse(int actionId, BaseUser baseUser, string parameters, out HttpGet httpGet)
        {
            int userId = baseUser != null?baseUser.GetUserId() : 0;

            GameSession session   = GameSession.Get(userId);
            string      sessionId = session != null ? session.SessionId : "";
            string      param     = string.Format("MsgId={0}&St={1}&Sid={2}&Uid={3}&ActionID={4}{5}",
                                                  0,
                                                  "st",
                                                  sessionId,
                                                  userId,
                                                  actionId,
                                                  parameters);

            httpGet = new HttpGet(param, session);
            BaseStruct         baseStruct = FindRoute(GameEnvironment.Setting.ActionTypeName, httpGet, actionId);
            SocketGameResponse response   = new SocketGameResponse();

            baseStruct.UserFactory = uid => { return(baseUser); };
            baseStruct.SetPush();
            baseStruct.DoInit();
            using (ILocking locking = baseStruct.RequestLock())
            {
                if (locking.IsLocked)
                {
                    if (!baseStruct.GetError() &&
                        baseStruct.ReadUrlElement() &&
                        baseStruct.DoAction() &&
                        !baseStruct.GetError())
                    {
                        baseStruct.BuildPacket();
                        baseStruct.WriteAction(response);
                    }
                    else
                    {
                        baseStruct.WriteErrorAction(response);
                    }
                }
            }
            return(response.ReadByte());
        }
Esempio n. 3
0
 /// <summary>
 /// 请求处理
 /// </summary>
 /// <param name="httpGet"></param>
 /// <param name="response"></param>
 /// <param name="userFactory"></param>
 public static void Request(HttpGet httpGet, IGameResponse response, Func <int, BaseUser> userFactory)
 {
     Request(GameEnvironment.Setting.ActionTypeName, httpGet, response, userFactory);
 }
Esempio n. 4
0
 protected abstract void OnRequested(HttpGet httpGet, IGameResponse response);
Esempio n. 5
0
        private void OnHttpRequest(IAsyncResult result)
        {
            try
            {
                HttpListener        listener = (HttpListener)result.AsyncState;
                HttpListenerContext context  = listener.EndGetContext(result);
                listener.BeginGetContext(OnHttpRequest, listener);
                HttpListenerRequest  request  = context.Request;
                HttpListenerResponse response = context.Response;
                string      sid = request.QueryString["sid"];
                GameSession session;
                if (string.IsNullOrEmpty(sid))
                {
                    session = GameSession.CreateNew(Guid.NewGuid(), request);
                }
                else
                {
                    session = GameSession.Get(sid) ?? GameSession.CreateNew(Guid.NewGuid(), request);
                }

                string data = "";
                if (Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    if (string.Compare(request.HttpMethod, "get", true) == 0)
                    {
                        data = request.RawUrl.Substring(8);
                        data = HttpUtility.UrlDecode(data);
                    }
                }
                else
                {
                    data = request.QueryString["d"];
                }

                if (string.IsNullOrEmpty(data))
                {
                    using (var reader = new StreamReader(request.InputStream, request.ContentEncoding))
                    {
                        data = reader.ReadToEnd();
                        data = HttpUtility.ParseQueryString(data)["d"];
                    }
                }

                int statuscode = 0;
                Dictionary <string, string> param = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);
                if (data != null)
                {
                    var nvc = HttpUtility.ParseQueryString(data);
                    foreach (var key in nvc.AllKeys)
                    {
                        param[key] = nvc[key];
                    }
                }
                statuscode = CheckHttpParam(param);

                if (statuscode != (int)HttpStatusCode.OK)
                {
                    response.StatusCode = statuscode;
                    response.Close();
                    return;
                }

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

                HttpGet httpGet = new HttpGet(new RequestPackage()
                {
                    UrlParam = data, Session = session
                });
                httpGet["UserHostAddress"] = session.EndAddress;
                httpGet["ssid"]            = session.KeyCode.ToString("N");
                httpGet["http"]            = "1";

                var clientConnection = new HttpClientConnection {
                    Context      = context,
                    Session      = session,
                    ActionGetter = httpGet,
                    GameResponse = httpresponse
                };
                clientConnection.TimeoutTimer = new Timer(OnHttpRequestTimeout, clientConnection, httpRequestTimeout, Timeout.Infinite);


                OnRequested(httpGet, httpresponse);
                byte[] respData = httpresponse.ReadByte();
                OnHttpResponse(clientConnection, respData, 0, respData.Length);
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("OnHttpRequest error:{0}", ex);
            }
        }
Esempio n. 6
0
 protected abstract void OnCallRemote(string route, HttpGet httpGet, MessageHead head, MessageStructure structure);
Esempio n. 7
0
        /// <summary>
        /// 获取Action处理的输出字节流
        /// </summary>
        /// <returns></returns>
        public static byte[] GetActionResponse(int actionId, BaseUser baseUser, string parameters, out HttpGet httpGet)
        {
            string serverHost = string.Format("{0}:{1}", ActionConfig.Current.IpAddress, ActionConfig.Current.Port);
            string param      = string.Format("MsgId={0}&St={1}&Sid={2}&Uid={3}&ActionID={4}{5}",
                                              0,
                                              "st",
                                              baseUser.GetSessionId(),
                                              baseUser.GetUserId(),
                                              actionId,
                                              parameters);

            httpGet = new HttpGet(param, baseUser.SocketSid, baseUser.RemoteAddress);
            BaseStruct         baseStruct = FindRoute(ActionConfig.Current.TypeName, httpGet, actionId);
            SocketGameResponse response   = new SocketGameResponse();

            baseStruct.UserFactory = uid => { return(baseUser); };
            baseStruct.SetPush();
            baseStruct.DoInit();
            using (baseStruct.RequestLock())
            {
                if (!baseStruct.GetError() &&
                    baseStruct.ReadUrlElement() &&
                    baseStruct.DoAction() &&
                    !baseStruct.GetError())
                {
                    baseStruct.BuildPacket();
                    baseStruct.WriteAction(response);
                }
                else
                {
                    baseStruct.WriteErrorAction(response);
                }
            }
            return(response.ReadByte());
        }
Esempio n. 8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="httpGet"></param>
 /// <param name="response"></param>
 /// <param name="userFactory"></param>
 public static void Request(HttpGet httpGet, IGameResponse response, Func <int, BaseUser> userFactory)
 {
     Request(ActionConfig.Current.TypeName, httpGet, response, userFactory);
 }
Esempio n. 9
0
 /// <summary>
 /// Checks the remote.
 /// </summary>
 /// <returns><c>true</c>, if remote was checked, <c>false</c> otherwise.</returns>
 /// <param name="route">Route.</param>
 /// <param name="httpGet">Http get.</param>
 protected virtual bool CheckRemote(string route, HttpGet httpGet)
 {
     return(true);
 }
Esempio n. 10
0
        private void ProcessPackage(object state)
        {
            var package = (RequestPackage)state;

            if (package == null)
            {
                return;
            }

            var param    = package.Param;
            var session  = package.Session;
            var actionid = package.ActionId;

            try
            {
                if (actionid == (int)ActionEnum.Heartbeat)
                {
                    // 客户端tcp心跳包
                    session.LastActivityTime = DateTime.Now;
                    OnHeartbeat(session);
                    session.ExitSession();
                    Interlocked.Decrement(ref runningNum);
                    return;
                }

                HttpGet httpGet = new HttpGet(param, session);
                byte[]  data    = new byte[0];
                if (!string.IsNullOrEmpty(package.Route))
                {
                    if (CheckRemote(package.Route, httpGet))
                    {
                        MessageStructure response = new MessageStructure();
                        OnCallRemote(package.Route, httpGet, response);
                        data = response.PopBuffer();
                    }
                }
                else
                {
                    SocketGameResponse response = new SocketGameResponse();
                    OnRequested(httpGet, response);
                    data = response.ReadByte();
                }
                try
                {
                    session.SendAsync(data, 0, data.Length);
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("PostSend异常{0}", ex);
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Task异常{0}", ex);
            }
            finally
            {
                session.ExitSession();
                Interlocked.Decrement(ref runningNum);
            }
        }